Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PTen] Add full kernel in pten (incomplete) #36930

Merged
merged 163 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from 161 commits
Commits
Show all changes
163 commits
Select commit Hold shift + click to select a range
3f545c4
initial tensor design & sign kernel demo
chenwhql Jul 9, 2021
1f4ea40
add move constructor for meta & add lodtensor
chenwhql Jul 12, 2021
44bf926
add dirs & sign xpu kernel
chenwhql Jul 12, 2021
b20689d
add mean cpu&cuda kernel impl
chenwhql Jul 15, 2021
79d2a1a
move sign & mean xpu & npu kernel
chenwhql Jul 15, 2021
434136f
add selected_rows basic impl
chenwhql Jul 16, 2021
6c6ee22
refactor design, BaseTensor to DenseTensor, etc.
chenwhql Jul 27, 2021
013c3fb
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
chenwhql Jul 27, 2021
33bba06
add scale mkldnn kernel
chenwhql Jul 28, 2021
d895a11
polish xpu & npu impl details
chenwhql Jul 28, 2021
62ebf01
fix mkldnn reuse compile failed
chenwhql Jul 29, 2021
7c09726
change tensor operation lib name
chenwhql Jul 29, 2021
7ae7f2f
resolve conflit with develop
chenwhql Jul 29, 2021
288efc2
rename util filename
chenwhql Jul 29, 2021
be3ddd5
add more comments
chenwhql Jul 29, 2021
3386c49
change TensorImplInterface to TensorInterface
chenwhql Jul 30, 2021
4ef6be5
add kernel key and factory
chenwhql Aug 4, 2021
b69066e
remove MKLDNNTensorMeta, add MKLDNNDenseTensor
chenwhql Aug 4, 2021
1d4f90e
resolve conflict with develop
chenwhql Aug 4, 2021
c732d57
change XXDeviceContext to XXContext
chenwhql Aug 5, 2021
374345f
add base kernel registrar utils & test on sign
chenwhql Aug 16, 2021
bbb6473
resolve conflict with develop
chenwhql Aug 16, 2021
0e18ff4
replace boost::any by paddle::any
chenwhql Aug 16, 2021
805896b
fix several ci failed
chenwhql Aug 17, 2021
fc4442b
fix npu compile error
chenwhql Aug 17, 2021
cefe30a
add ordered map util
chenwhql Aug 17, 2021
aa3e79b
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
chenwhql Aug 17, 2021
a1753a0
fix multiple ordered_map compile errors
chenwhql Aug 17, 2021
05a82e7
move dev into include dir
chenwhql Aug 18, 2021
90e9090
support sign op in static op run
chenwhql Aug 19, 2021
a94eefd
fix static op run error
chenwhql Aug 23, 2021
19da152
resolve confilt with develop
chenwhql Aug 23, 2021
021a505
fix new executor compile failed
chenwhql Aug 23, 2021
f24e45e
add dygraph branch & remove sign_op.h
chenwhql Aug 25, 2021
44acc84
fix test_infer_no_need_buffer_slots
chenwhql Aug 26, 2021
2b66ab4
fix rocm compile link error
chenwhql Aug 26, 2021
2a5ce9b
fix unitybuild error & clear glog
chenwhql Aug 26, 2021
39b7d06
fix npu compile failed
chenwhql Aug 26, 2021
d4dec61
skip quant trans test
chenwhql Aug 26, 2021
461f146
fix part windows compile problem
chenwhql Aug 26, 2021
35aee9a
Merge branch 'develop' into op2func_refactor
chenwhql Aug 26, 2021
ddfbbdd
fix xpu enforce error
chenwhql Aug 26, 2021
57bcd67
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Aug 26, 2021
7d82352
fix inference test failed
chenwhql Aug 27, 2021
d55bb4b
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Aug 27, 2021
d9476dd
Merge branch 'develop' into op2func_refactor
chenwhql Aug 27, 2021
193ee9d
remove ordered_map to solve quant failed
chenwhql Aug 30, 2021
f2db581
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
chenwhql Aug 30, 2021
db6ff09
fix part of rcom compile faild
chenwhql Aug 30, 2021
80bf6b8
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Aug 30, 2021
9031ab3
add more register kernels
chenwhql Aug 31, 2021
f7bbaca
revert scale kernel temporarily
chenwhql Sep 3, 2021
cea19d0
Merge branch 'develop' into op2func_refactor
chenwhql Sep 3, 2021
568bebd
fix code format error
chenwhql Sep 6, 2021
0eedc92
add new kernel registrar marco
chenwhql Sep 7, 2021
509d13e
rename top to tcmpt
chenwhql Sep 7, 2021
7146f92
revert xpu, npu, mkldnn impl & remove op def
chenwhql Sep 7, 2021
321b141
add kernel args parse functor to auto parse args
chenwhql Sep 8, 2021
57a14c6
resolve confilt with develop
chenwhql Sep 8, 2021
c3ebfea
revert some change & add scale kernels
chenwhql Sep 9, 2021
b67de9c
add op proto in dygraph kernelcontext building
chenwhql Sep 9, 2021
13c02aa
polish kernel dispatch logic & nameing rule
chenwhql Sep 10, 2021
1987ce9
fix scale kernel match error
chenwhql Sep 10, 2021
33a4c41
fix scale test failed
chenwhql Sep 10, 2021
c32fde9
add mean API and unittest
chenwhql Sep 13, 2021
a4e53ef
test mean api success
chenwhql Sep 17, 2021
1d9f33f
add branch to solve compiled error
chenwhql Sep 18, 2021
b0cf02c
skip clang format error
chenwhql Sep 18, 2021
95a612e
add mean skip rule in op_library
chenwhql Sep 18, 2021
83d6f77
add dot kernel, api and unittest (#6)
MingMingShangTian Sep 18, 2021
dad5e61
remove old kernel and add symbol link
chenwhql Sep 18, 2021
027f0b2
resolve conflit with tianyu
chenwhql Sep 18, 2021
8add5e4
fix dot compiled failed
chenwhql Sep 18, 2021
01b5ded
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
chenwhql Sep 18, 2021
71a3403
add merco for module declare
chenwhql Sep 22, 2021
4663033
fix npu and xpu compile error
chenwhql Sep 22, 2021
be15b02
revert sign, mean, scale, dot kernel removing
chenwhql Sep 23, 2021
8371096
add comment for keeping old kernel impl
chenwhql Sep 23, 2021
f1f6c8e
fix mutable_data error
chenwhql Sep 23, 2021
5547b44
fix bfloat16 conflit
chenwhql Sep 24, 2021
dd3323d
fix inference undef error
chenwhql Sep 24, 2021
65e68c6
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Sep 24, 2021
caaed19
adapt to msvc compile rules
chenwhql Sep 26, 2021
c9a3f38
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Sep 26, 2021
46b7762
polish comment for template inst
chenwhql Sep 26, 2021
4253f49
add cmake template instantiation for win
chenwhql Sep 27, 2021
4e871ea
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
chenwhql Sep 28, 2021
395a50f
Merge branch 'develop' into op2func_refactor
chenwhql Sep 29, 2021
817f052
fix backend to place device id bug
chenwhql Sep 29, 2021
7f640a6
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Sep 29, 2021
bf0f99b
fix ifdef error
chenwhql Sep 29, 2021
73de891
Op2functor (#7)
MingMingShangTian Sep 30, 2021
e9b219d
fill_any_like kernel refactor (#10)
zyfncg Sep 30, 2021
9789890
skip dtype for fill_any_like
chenwhql Oct 11, 2021
9b33270
add attrs for kernel key constrcut
chenwhql Oct 11, 2021
aa6ed57
add use_pt_kernel Flags to control whether to use pt kernel (#13)
MingMingShangTian Oct 12, 2021
9db8e4a
fix mutable_data cuda place error
chenwhql Oct 12, 2021
12c1178
Merge branch 'develop' into op2func_refactor
chenwhql Oct 12, 2021
c882b5c
move high level apis into hapi
chenwhql Oct 13, 2021
e30ca2a
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Oct 13, 2021
46ba70c
remove selectedrows adapting temporarily
chenwhql Oct 14, 2021
073aef3
Support Scalar in Tensor Compute Library (#14)
zyfncg Oct 14, 2021
06789ba
resolve conflit with yunfei
chenwhql Oct 14, 2021
3f5f789
remove mkldnn tensor & polish details
chenwhql Oct 14, 2021
2309149
use flat_hash_map and small_vector in kernel factory
chenwhql Oct 15, 2021
6ce92e5
Refactor flatten kernel (#12)
YuanRisheng Oct 15, 2021
e0322d5
Revert "use flat_hash_map and small_vector in kernel factory"
chenwhql Oct 15, 2021
d3ab655
Move cpu, cuda and other device code into kernels (#15)
zyfncg Oct 15, 2021
ddc7de8
Perfect unitests (#16)
YuanRisheng Oct 18, 2021
37791f7
replace with flat_hash_map, small_vector (#19)
MingMingShangTian Oct 18, 2021
28a6374
Perfect unitests (#20)
YuanRisheng Oct 18, 2021
e3e2b50
refactor execution adapting impl
chenwhql Oct 18, 2021
e0710fd
resolve conflits
chenwhql Oct 18, 2021
ff19bd0
fix insert conflit
chenwhql Oct 19, 2021
1dd0145
Fix CI bug of test_yolov3 (#21)
zyfncg Oct 19, 2021
b77d1ee
add the tensor base class, test=develop (#17)
Shixiaowei02 Oct 19, 2021
320b5f1
[no-verify] commit backend and tensor signature changes
chenwhql Oct 19, 2021
5b2999f
resolve conflit with xiaowei
chenwhql Oct 19, 2021
466ce03
Rename tcmpt to pten (#23)
zyfncg Oct 20, 2021
beec280
remove k of all enum var
chenwhql Oct 20, 2021
a49fd44
resolve conflit with yunfei
chenwhql Oct 20, 2021
373f9c1
Merge branch 'develop' into op2func_refactor
chenwhql Oct 20, 2021
ce210b4
remove kernel_instantiate (#26)
MingMingShangTian Oct 20, 2021
4e71d15
remove symbols and spatial_tensor
chenwhql Oct 20, 2021
04cf058
change common to functions
chenwhql Oct 20, 2021
ab8db2d
readd share tensor impl methods
chenwhql Oct 20, 2021
f1c9661
add a candidate dense tensor class, test=develop (#28)
Shixiaowei02 Oct 20, 2021
d3674e9
change all Pt to Pten
chenwhql Oct 20, 2021
4e2c0dd
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Oct 20, 2021
bbe59bc
resolve conflit with xiaowei
chenwhql Oct 21, 2021
76a588e
Op2functor opt1 (#27)
MingMingShangTian Oct 21, 2021
fb224ab
polish kernel factory and kernel registry
chenwhql Oct 21, 2021
252fb79
fix operator test error msg mismatch
chenwhql Oct 22, 2021
19b1095
remove tensor signature and backend set member
chenwhql Oct 22, 2021
24ef6c5
move scalar and polish enforce
chenwhql Oct 22, 2021
1685b67
revert dtype layout change to fix error
chenwhql Oct 22, 2021
7b7e988
fix enum operator override error
chenwhql Oct 22, 2021
52fead0
add several base unittests
chenwhql Oct 22, 2021
2ff2721
add pten utils tests
chenwhql Oct 23, 2021
e3ed2c6
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
chenwhql Oct 23, 2021
b5c77e5
polish some details
chenwhql Oct 24, 2021
5240ac0
Dev/op2func refactor 3 (#30)
chenwhql Oct 26, 2021
5fb285c
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Oct 26, 2021
3276897
Merge branch 'develop' into op2func_refactor
chenwhql Oct 26, 2021
558a848
polish some details
chenwhql Oct 26, 2021
72910fa
Merge branch 'op2func_refactor' of https://github.com/chenwhql/Paddle…
chenwhql Oct 26, 2021
8f100da
polish kernel signature details
chenwhql Oct 26, 2021
be9df70
fix a bug about offsets of the tensor, test=develop (#31)
chenwhql Oct 27, 2021
a83e9c7
polish some details
chenwhql Oct 27, 2021
9584c40
add fill_constant kernel in pten
zyfncg Nov 2, 2021
4d77b09
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg Nov 2, 2021
dfb7f37
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg Nov 2, 2021
7058f22
fix bug of full api (c++)
zyfncg Nov 2, 2021
2163b8f
remove the support for SelectRows in new fill_constant kernel
zyfncg Nov 2, 2021
f0c9c0c
fix bug of setting fill_any_like kernel key
zyfncg Nov 3, 2021
7166d62
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg Nov 3, 2021
fdd0ff8
merge code confilct
zyfncg Nov 3, 2021
b2d74cb
modify fill_constant GetExpectedKernelType
zyfncg Nov 5, 2021
8579d63
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg Nov 5, 2021
b657296
fix fill_constant KernelType bug
zyfncg Nov 5, 2021
8bd9c10
polish code of build pten KernelContext
zyfncg Nov 5, 2021
e278bb3
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg Nov 5, 2021
5d8a3f6
refactor code of fill_constant in pten
zyfncg Nov 8, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions paddle/fluid/framework/operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1838,6 +1838,10 @@ pten::KernelContext OperatorWithKernel::BuildPtenKernelContext(
if (std::type_index(attr.type()) == std::type_index(typeid(float))) {
op_kernel_ctx.EmplaceBackAttr(
std::move(pten::Scalar(BOOST_GET_CONST(float, attr))));
} else if (std::type_index(attr.type()) ==
std::type_index(typeid(std::string))) {
op_kernel_ctx.EmplaceBackAttr(
std::move(pten::Scalar(BOOST_GET_CONST(std::string, attr))));
} else {
PADDLE_THROW(platform::errors::Unimplemented(
"unsupported cast op attribute `%s` to Scalar when construct "
Expand Down
4 changes: 4 additions & 0 deletions paddle/fluid/imperative/prepared_operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ static pten::KernelContext BuildDygraphPtenKernelContext(
if (std::type_index(attr.type()) == std::type_index(typeid(float))) {
op_kernel_ctx.EmplaceBackAttr(
std::move(pten::Scalar(BOOST_GET_CONST(float, attr))));
} else if (std::type_index(attr.type()) ==
std::type_index(typeid(std::string))) {
op_kernel_ctx.EmplaceBackAttr(
std::move(pten::Scalar(BOOST_GET_CONST(std::string, attr))));
} else {
PADDLE_THROW(platform::errors::Unimplemented(
"unsupported cast op attribute `%s` to Scalar when construct "
Expand Down
44 changes: 43 additions & 1 deletion paddle/fluid/operators/fill_constant_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,51 @@ class FillConstantOp : public framework::OperatorWithKernel {

framework::OpKernelType GetExpectedKernelType(
const framework::ExecutionContext& ctx) const override {
return framework::OpKernelType(
framework::OpKernelType kt = framework::OpKernelType(
framework::proto::VarType::Type(ctx.Attr<int>("dtype")),
ctx.GetPlace());
// TODO(zyfncg) The force_cpu and place_type are conflicted, it's a issue
// lefted before, and we may merge them in the future.
// In order to invoke new fill_constant kernel, the place of OpKernelType
// will be setted by force_cpu and place_type here.
if (ctx.Attr<bool>("force_cpu")) {
kt.place_ = platform::CPUPlace();
}
auto place_type = ctx.Attr<int>("place_type");
if (place_type != -1) {
switch (place_type) {
case 0:
kt.place_ = platform::CPUPlace();
break;
case 1:
case 2:
kt.place_ = platform::CUDAPlace();
break;
case 3:
kt.place_ = platform::XPUPlace();
break;
default:
PADDLE_THROW(platform::errors::Unimplemented(
"Could NOT determine the place of variable, place_type = %d .",
place_type));
}
}

return kt;
}

framework::KernelSignature GetExpectedPtenKernelArgs(
const framework::ExecutionContext& ctx) const override {
if (!ctx.HasInput("ShapeTensor") &&
ctx.MultiInput<framework::Tensor>("ShapeTensorList").empty() &&
!ctx.HasInput("ValueTensor") &&
!ctx.OutputVar("Out")->IsType<framework::SelectedRows>()) {
const auto& str_value = ctx.Attr<std::string>("str_value");
std::string value = str_value.empty() ? "value" : "str_value";
return framework::KernelSignature("fill_constant.Scalar", {}, {value},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我们统一使用小写的overloaded name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

{"Out"});
}
return framework::KernelSignature("fill_constant.Unregistered", {}, {}, {});
}
};

Expand Down
2 changes: 1 addition & 1 deletion paddle/pten/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ add_subdirectory(tests)
# make an unity target for compile deps
set(PTEN_DEPS convert_utils dense_tensor kernel_factory kernel_context)
set(PTEN_DEPS ${PTEN_DEPS} math_cpu linalg_cpu creation_cpu manipulation_cpu)
set(PTEN_DEPS ${PTEN_DEPS} unary binary)
set(PTEN_DEPS ${PTEN_DEPS} 0_nary unary binary)
if(WITH_GPU OR WITH_ROCM)
set(PTEN_DEPS ${PTEN_DEPS} math_cuda linalg_cuda creation_cuda manipulation_cuda)
endif()
Expand Down
6 changes: 6 additions & 0 deletions paddle/pten/api/include/creation.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
namespace paddle {
namespace experimental {

Tensor full(const std::vector<int64_t>& shape,
const Scalar& value,
DataType dtype = DataType::FLOAT32,
Backend backend = Backend::CPU,
DataLayout layout = DataLayout::NCHW);

Tensor full_like(const Tensor& x,
const Scalar& value,
DataType dtype = DataType::UNDEFINED);
Expand Down
40 changes: 39 additions & 1 deletion paddle/pten/api/lib/creation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,52 @@ limitations under the License. */
namespace paddle {
namespace experimental {

Tensor full(const std::vector<int64_t>& shape,
const Scalar& value,
DataType dtype,
Backend backend,
DataLayout layout) {
// 1. Get kernel signature and kernel
pten::KernelKey kernel_key{backend, layout, dtype};
auto kernel = pten::KernelFactory::Instance().SelectKernelOrThrowError(
"fill_constant.Scalar", kernel_key);

// 2. Get Device Context
auto* dev_ctx = GetDeviceContextByBackend(kernel_key.backend());
auto kernel_context = pten::KernelContext(*dev_ctx);

// 3. Auto data transform
kernel_context.EmplaceBackAttr(value);

// 4. InferShape
auto out_meta = pten::FullInferShape(shape, dtype, layout);

// 5. Prepare outputs
const auto allocator =
std::make_shared<paddle::experimental::DefaultAllocator>(
pten::TransToFluidPlace(kernel_key.backend()));
auto dense_out = std::make_shared<pten::DenseTensor>(allocator, out_meta);
kernel_context.EmplaceBackOutput(dense_out);
Tensor out;
out.set_impl(dense_out);

// 6. Call kernel
kernel(&kernel_context);

return out;
}

Tensor full_like(const Tensor& x,
const Scalar& value,
paddle::experimental::DataType dtype) {
// 1. Get kernel signature and kernel
auto kernel_key_set = ParseKernelKeyByInputArgs(x);
auto kernel_key = kernel_key_set.GetHigestPriorityKernelKey();
auto kernel = pten::KernelFactory::Instance().SelectKernelOrThrowError(
"fill_any_like", kernel_key);
"fill_any_like",
{kernel_key.backend(),
kernel_key.layout(),
dtype == DataType::UNDEFINED ? kernel_key.dtype() : dtype});

// 2. Get Device Context
auto* dev_ctx = GetDeviceContextByBackend(kernel_key.backend());
Expand Down
12 changes: 12 additions & 0 deletions paddle/pten/common/scalar.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ class Scalar {

Scalar(bool val) : tag(Tag::HAS_B) { data_.b = val; } // NOLINT

Scalar(const std::string& str_value) : tag(Tag::HAS_D) { // NOLINT
if (str_value == "inf") {
data_.d = std::numeric_limits<double>::infinity();
} else if (str_value == "-inf") {
data_.d = -std::numeric_limits<double>::infinity();
} else if (str_value == "nan") {
data_.d = std::numeric_limits<double>::quiet_NaN();
} else {
data_.d = std::stod(str_value);
}
}

template <typename T>
inline T to() const {
switch (tag) {
Expand Down
1 change: 1 addition & 0 deletions paddle/pten/core/kernel_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ struct KernelImpl<Return (*)(Args...), kernel_fn> {
PT_SPECIALIZE_KernelCallHelper_FOR_ATTRIBUTE(int64_t);
PT_SPECIALIZE_KernelCallHelper_FOR_ATTRIBUTE(paddle::platform::float16);
PT_SPECIALIZE_KernelCallHelper_FOR_ATTRIBUTE(const Scalar&);
PT_SPECIALIZE_KernelCallHelper_FOR_ATTRIBUTE(const std::vector<int64_t>&);

/* Output Helpers */

Expand Down
1 change: 1 addition & 0 deletions paddle/pten/include/infershape.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ limitations under the License. */
#pragma once

// See Note: [ How do we organize the kernel directory ]
#include "paddle/pten/infershape/0_nary.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不建议使用数字命名文件,使用小写字母和下划线,这里是不是直接叫nary.h就可以

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, 已修改为nary

#include "paddle/pten/infershape/binary.h"
#include "paddle/pten/infershape/unary.h"
27 changes: 27 additions & 0 deletions paddle/pten/infershape/0_nary.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

// See Note [ Why still include the fluid headers? ]
#include "paddle/pten/infershape/0_nary.h"

namespace pten {

DenseTensorMeta FullInferShape(const std::vector<int64_t>& shape,
DataType dtype,
DataLayout layout) {
const auto& out_dims = paddle::framework::make_ddim(shape);
return {dtype, out_dims, layout};
}

} // namespace pten
34 changes: 34 additions & 0 deletions paddle/pten/infershape/0_nary.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#pragma once

// See Note [ Why still include the fluid headers? ]
#include "paddle/pten/core/tensor_meta.h"

namespace pten {

// Common InferShape Functions for 0-nary operators(no input tensor), The format
// like:
//
// 1. DenseTensorMeta [OpName]InferShape( ...)
// NOTE: The name "InferShape" may be not appropriate. "InferMeta" may be good.
// Because functions in this file
// not only can infer shape, but alse need infer lod or other useful data.

DenseTensorMeta FullInferShape(const std::vector<int64_t>& shape,
DataType dtype,
DataLayout layout);

} // namespace pten
1 change: 1 addition & 0 deletions paddle/pten/infershape/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
cc_library(0_nary SRCS 0_nary.cc DEPS convert_utils)
cc_library(unary SRCS unary.cc DEPS convert_utils)
cc_library(binary SRCS binary.cc DEPS convert_utils)
49 changes: 48 additions & 1 deletion paddle/pten/kernels/cpu/creation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,38 @@ void FillAnyLike(const CPUContext& dev_ctx,
const DenseTensor& x,
const Scalar& val,
DenseTensor* out) {
eigen::fill<CPUContext, T>(dev_ctx, out, val.to<float>());
auto value = val.to<float>();
using CommonType = typename std::common_type<
float,
typename std::conditional<
std::is_same<T, paddle::platform::float16>::value,
float,
T>::type>::type;

auto common_type_value = static_cast<CommonType>(value);

PADDLE_ENFORCE_EQ(
(common_type_value >=
static_cast<CommonType>(std::numeric_limits<T>::lowest())) &&
(common_type_value <=
static_cast<CommonType>(std::numeric_limits<T>::max())),
true,
paddle::platform::errors::InvalidArgument(
"The filled value is out of range for target type, "
"current kernel type is %s, the range should between %f "
"and %f, but now value is %f.",
typeid(T).name(),
static_cast<CommonType>(std::numeric_limits<T>::lowest()),
static_cast<CommonType>(std::numeric_limits<T>::max()),
static_cast<float>(value)));
eigen::fill<CPUContext, T>(dev_ctx, out, value);
}

template <typename T>
void FillConstant(const CPUContext& dev_ctx,
const Scalar& val,
DenseTensor* out) {
eigen::fill<CPUContext, T>(dev_ctx, out, val.to<T>());
}

} // namespace pten
Expand All @@ -41,3 +72,19 @@ PT_REGISTER_KERNEL("fill_any_like",
int64_t,
bool,
paddle::platform::float16) {}

PT_REGISTER_KERNEL("fill_constant.Scalar",
CPU,
ANY,
pten::FillConstant,
float,
double,
uint8_t,
int16_t,
int,
int64_t,
bool,
paddle::platform::float16,
paddle::platform::bfloat16,
paddle::platform::complex<float>,
paddle::platform::complex<double>) {}
5 changes: 5 additions & 0 deletions paddle/pten/kernels/cpu/creation.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ void FillAnyLike(const CPUContext& dev_ctx,
const Scalar& val,
DenseTensor* out);

template <typename T>
void FillConstant(const CPUContext& dev_ctx,
const Scalar& val,
DenseTensor* out);

} // namespace pten
47 changes: 47 additions & 0 deletions paddle/pten/kernels/cuda/creation.cu
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,41 @@ void FillAnyLike(const CUDAContext& dev_ctx,
const DenseTensor& x,
const Scalar& val,
DenseTensor* out) {
auto value = val.to<float>();
using CommonType = typename std::common_type<
float,
typename std::conditional<
std::is_same<T, paddle::platform::float16>::value,
float,
T>::type>::type;

auto common_type_value = static_cast<CommonType>(value);

PADDLE_ENFORCE_EQ(
(common_type_value >=
static_cast<CommonType>(std::numeric_limits<T>::lowest())) &&
(common_type_value <=
static_cast<CommonType>(std::numeric_limits<T>::max())),
true,
paddle::platform::errors::InvalidArgument(
"The filled value is out of range for target type, "
"current kernel type is %s, the range should between %f "
"and %f, but now value is %f.",
typeid(T).name(),
static_cast<CommonType>(std::numeric_limits<T>::lowest()),
static_cast<CommonType>(std::numeric_limits<T>::max()),
static_cast<float>(value)));

eigen::fill<CUDAContext, T>(dev_ctx, out, val.to<float>());
}

template <typename T>
void FillConstant(const CUDAContext& dev_ctx,
const Scalar& val,
DenseTensor* out) {
eigen::fill<CUDAContext, T>(dev_ctx, out, val.to<T>());
}

} // namespace pten

PT_REGISTER_MODULE(CreationCUDA);
Expand All @@ -41,3 +73,18 @@ PT_REGISTER_KERNEL("fill_any_like",
int64_t,
bool,
paddle::platform::float16) {}

PT_REGISTER_KERNEL("fill_constant.Scalar",
CUDA,
ANY,
pten::FillConstant,
float,
double,
uint8_t,
int16_t,
int,
int64_t,
bool,
paddle::platform::float16,
paddle::platform::complex<float>,
paddle::platform::complex<double>) {}
5 changes: 5 additions & 0 deletions paddle/pten/kernels/cuda/creation.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ void FillAnyLike(const CUDAContext& dev_ctx,
const Scalar& val,
DenseTensor* out);

template <typename T>
void FillConstant(const CUDAContext& dev_ctx,
const Scalar& val,
DenseTensor* out);

} // namespace pten

#endif
Loading