Skip to content

Commit

Permalink
[PTen] Add full kernel in pten (incomplete) (PaddlePaddle#36930)
Browse files Browse the repository at this point in the history
* initial tensor design & sign kernel demo

* add move constructor for meta & add lodtensor

* add dirs & sign xpu kernel

* add mean cpu&cuda kernel impl

* move sign & mean xpu & npu kernel

* add selected_rows basic impl

* refactor design, BaseTensor to DenseTensor, etc.

* add scale mkldnn kernel

* polish xpu & npu impl details

* fix mkldnn reuse compile failed

* change tensor operation lib name

* rename util filename

* add more comments

* change TensorImplInterface to TensorInterface

* add kernel key and factory

* remove MKLDNNTensorMeta, add MKLDNNDenseTensor

* change XXDeviceContext to XXContext

* add base kernel registrar utils & test on sign

* replace boost::any by paddle::any

* fix several ci failed

* fix npu compile error

* add ordered map util

* fix multiple ordered_map compile errors

* move dev into include dir

* support sign op in static op run

* fix static op run error

* fix new executor compile failed

* add dygraph branch & remove sign_op.h

* fix test_infer_no_need_buffer_slots

* fix rocm compile link error

* fix unitybuild error & clear glog

* fix npu compile failed

* skip quant trans test

* fix part windows compile problem

* fix xpu enforce error

* fix inference test failed

* remove ordered_map to solve quant failed

* fix part of rcom compile faild

* add more register kernels

* revert scale kernel temporarily

* fix code format error

* add new kernel registrar marco

* rename top to tcmpt

* revert xpu, npu, mkldnn impl & remove op def

* add kernel args parse functor to auto parse args

* revert some change & add scale kernels

* add op proto in dygraph kernelcontext building

* polish kernel dispatch logic & nameing rule

* fix scale kernel match error

* fix scale test failed

* add mean API and unittest

* test mean api success

* add branch to solve compiled error

* skip clang format error

* add mean skip rule in op_library

* add dot kernel, api and unittest (#6)

* remove old kernel and add symbol link

* fix dot compiled failed

* add merco for module declare

* fix npu and xpu compile error

* revert sign, mean, scale, dot kernel removing

* add comment for keeping old kernel impl

* fix mutable_data error

* fix bfloat16 conflit

* fix inference undef error

* adapt to msvc compile rules

* polish comment for template inst

* add cmake template instantiation for win

* fix backend to place device id bug

* fix ifdef error

* Op2functor (#7)

* add kernel args maker class

* make args maker non-const

* remove debug log

* modify codes by review options

* split constructPrKernelContext function

* fix output name bug

* fix test_mean_op test_sign_op failed

* fill_any_like kernel refactor (#10)

* fill_any_like kernel refactor

* remove useless code of full_like c++ api

* skip dtype for fill_any_like

* add attrs for kernel key constrcut

* add use_pt_kernel Flags to control whether to use pt kernel (#13)

* add use_pt_kernel Flags to control whether to use pt kernel

* change the default value to true for cheking pt kernels

* fix mutable_data cuda place error

* move high level apis into hapi

* remove selectedrows adapting temporarily

* Support Scalar in Tensor Compute Library (#14)

* fill_any_like kernel refactor

* remove useless code of full_like c++ api

* Support Scalar in Tensor Compute Library

* add scalar in dygraph and static graph mode

* keep the basic type for attr, instead of using scalar for all

* merge the code

* remove mkldnn tensor & polish details

* use flat_hash_map and small_vector in kernel factory

* Refactor flatten kernel (#12)

* refactor flatten kernel

* update infershape function

* fix compile bugs

* fix bugs when merge

* fix compiler bugs

* fix bugs when run test_flatten_api

* fix bugs when run test

* Revert "use flat_hash_map and small_vector in kernel factory"

This reverts commit 2309149.

* Move cpu, cuda and other device code into kernels (#15)

* fill_any_like kernel refactor

* remove useless code of full_like c++ api

* Support Scalar in Tensor Compute Library

* add scalar in dygraph and static graph mode

* keep the basic type for attr, instead of using scalar for all

* merge the code

* start refactor matmul

* move cpu, cuda and other device modules into kernels

* merge code

* polish code in operator.cc

* Perfect unitests (#16)

* perfect unittest

* update license

* replace with flat_hash_map, small_vector (#19)

* fix small_vector build error on windows platform

* replace with flat_hash_map, small_vector

* remove todo

* Perfect unitests (#20)

* perfect unittest

* update license

* fix bug when run tcmpt_utils_test

* refactor execution adapting impl

* fix insert conflit

* Fix CI bug of test_yolov3 (#21)

* fill_any_like kernel refactor

* remove useless code of full_like c++ api

* Support Scalar in Tensor Compute Library

* add scalar in dygraph and static graph mode

* keep the basic type for attr, instead of using scalar for all

* merge the code

* start refactor matmul

* move cpu, cuda and other device modules into kernels

* merge code

* polish code in operator.cc

* Fix CI bug of test_yolov3

* add the tensor base class, test=develop (#17)

* update the tensor base class, test=develop

* remove two funcs, test=develop

* update the error msg, test=develop

Co-authored-by: Chen Weihang <[email protected]>

* [no-verify] commit backend and tensor signature changes

* Rename tcmpt to pten (#23)

* rename tcmpt to pten

* update omitted files for rename to pten

* update omitted file for rename to pten

* remove k of all enum var

* remove kernel_instantiate (#26)

* remove symbols and spatial_tensor

* change common to functions

* readd share tensor impl methods

* add a candidate dense tensor class, test=develop (#28)

* change all Pt to Pten

* resolve conflit with xiaowei

* Op2functor opt1 (#27)

* replace to small vector and change to const &

* add std::move

Co-authored-by: Chen Weihang <[email protected]>

* polish kernel factory and kernel registry

* fix operator test error msg mismatch

* remove tensor signature and backend set member

* move scalar and polish enforce

* revert dtype layout change to fix error

* fix enum operator override error

* add several base unittests

* add pten utils tests

* polish some details

* Dev/op2func refactor 3 (PaddlePaddle#30)

* add a candidate dense tensor class, test=develop

* remove TensorBase::backend(), test=develop

* remove some ops, test=develop

* cherry-pick the pr of tensor meta, test=develop

* moves the dense tensor and some ops, test=develop

* update the linalg operator, test=develop

* update other operators, test=develop

* fix errors, test=develop

* fix bugs, test=develop

* try to resolve the problem of windows ci, test=develop

* updates codes, test=develop

* fix the tensor_utils.cc, test=develop

* modify the dense tensor, test=develop

* fix the data type, test=develop

Co-authored-by: shixiaowei02 <[email protected]>

* polish some details

* polish kernel signature details

* fix a bug about offsets of the tensor, test=develop (PaddlePaddle#31)

Co-authored-by: shixiaowei02 <[email protected]>

* polish some details

* add fill_constant kernel in pten

* fix bug of full api (c++)

* remove the support for SelectRows in new fill_constant kernel

* fix bug of setting fill_any_like kernel key

* merge code confilct

* modify fill_constant GetExpectedKernelType

* fix fill_constant KernelType bug

* polish code of build pten KernelContext

* refactor code of fill_constant in pten

Co-authored-by: Chen Weihang <[email protected]>
Co-authored-by: chentianyu03 <[email protected]>
Co-authored-by: YuanRisheng <[email protected]>
Co-authored-by: 石晓伟 <[email protected]>
  • Loading branch information
5 people authored Nov 8, 2021
1 parent a1ec1d5 commit 655f4e3
Show file tree
Hide file tree
Showing 18 changed files with 308 additions and 33 deletions.
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},
{"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} 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 @@ -16,4 +16,5 @@ limitations under the License. */

// See Note: [ How do we organize the kernel directory ]
#include "paddle/pten/infershape/binary.h"
#include "paddle/pten/infershape/nary.h"
#include "paddle/pten/infershape/unary.h"
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(nary SRCS nary.cc DEPS convert_utils)
cc_library(unary SRCS unary.cc DEPS convert_utils)
cc_library(binary SRCS binary.cc DEPS convert_utils)
27 changes: 27 additions & 0 deletions paddle/pten/infershape/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/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/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
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

0 comments on commit 655f4e3

Please sign in to comment.