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 alias kernel name #37881

Merged
merged 3 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
3 changes: 2 additions & 1 deletion paddle/fluid/framework/operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1760,7 +1760,8 @@ OpKernelType OperatorWithKernel::GetKernelTypeForVar(

KernelSignature OperatorWithKernel::GetExpectedPtenKernelArgs(
const ExecutionContext& ctx) const {
return KernelSignatureMap::Instance().Get(Type());
return KernelSignatureMap::Instance().Get(
pten::TransToPtenKernelName(Type()));
}

void OperatorWithKernel::BuildPtenKernelContext(
Expand Down
8 changes: 4 additions & 4 deletions paddle/fluid/framework/pten_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ KernelSignatureMap& KernelSignatureMap::Instance() {
if (pten::KernelFactory::Instance().HasCompatiblePtenKernel(op_type)) {
KernelArgsNameMakerByOpProto maker(op_proto);
VLOG(10) << "Register kernel signature for " << op_type;
Copy link
Contributor

Choose a reason for hiding this comment

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

这里的op_type 是不是也要pten::TransToPtenKernelName(op_type)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

不需要

auto success =
kernel_signature_map_->map_
.emplace(op_type, std::move(maker.GetKernelSignature()))
.second;
auto success = kernel_signature_map_->map_
.emplace(pten::TransToPtenKernelName(op_type),
std::move(maker.GetKernelSignature()))
.second;
PADDLE_ENFORCE_EQ(
success, true,
platform::errors::PermissionDenied(
Expand Down
15 changes: 7 additions & 8 deletions paddle/fluid/operators/elementwise/elementwise_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,26 +144,25 @@ class ElementwiseOp : public framework::OperatorWithKernel {
const framework::ExecutionContext &ctx) const override {
if (Type() == "elementwise_add") {
if (ctx.InputVar("X")->IsType<framework::LoDTensor>()) {
return framework::KernelSignature("elementwise_add", {"X", "Y"},
{"axis"}, {"Out"});
return framework::KernelSignature("add", {"X", "Y"}, {"axis"}, {"Out"});
}
}
if (Type() == "elementwise_sub") {
if (ctx.InputVar("X")->IsType<framework::LoDTensor>()) {
return framework::KernelSignature("elementwise_sub", {"X", "Y"},
{"axis"}, {"Out"});
return framework::KernelSignature("subtract", {"X", "Y"}, {"axis"},
{"Out"});
}
}
if (Type() == "elementwise_div") {
if (ctx.InputVar("X")->IsType<framework::LoDTensor>()) {
return framework::KernelSignature("elementwise_div", {"X", "Y"},
{"axis"}, {"Out"});
return framework::KernelSignature("divide", {"X", "Y"}, {"axis"},
{"Out"});
}
}
if (Type() == "elementwise_mul") {
if (ctx.InputVar("X")->IsType<framework::LoDTensor>()) {
return framework::KernelSignature("elementwise_mul", {"X", "Y"},
{"axis"}, {"Out"});
return framework::KernelSignature("multiply", {"X", "Y"}, {"axis"},
{"Out"});
}
}
return framework::KernelSignature("None", {"X"}, {}, {"Out"});
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/operators/fill_any_like_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class FillAnyLikeOp : public framework::OperatorWithKernel {

framework::KernelSignature GetExpectedPtenKernelArgs(
const framework::ExecutionContext &ctx) const override {
return framework::KernelSignature("fill_any_like", {}, {"value"}, {"Out"});
return framework::KernelSignature("full_like", {}, {"value"}, {"Out"});
}
};

Expand Down
3 changes: 1 addition & 2 deletions paddle/fluid/operators/fill_constant_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ class FillConstantOp : public framework::OperatorWithKernel {
value = str_value.empty() ? "value" : "str_value";
}
if (!ctx.OutputVar("Out")->IsType<framework::SelectedRows>()) {
return framework::KernelSignature("fill_constant", {}, {shape, value},
{"Out"});
return framework::KernelSignature("full", {}, {shape, value}, {"Out"});
}
return framework::KernelSignature("fill_constant.unregistered", {}, {}, {});
}
Expand Down
7 changes: 3 additions & 4 deletions paddle/fluid/operators/flatten_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,10 @@ class FlattenContiguousRangeOp : public framework::OperatorWithKernel {
framework::KernelSignature GetExpectedPtenKernelArgs(
const framework::ExecutionContext &ctx) const override {
if (ctx.HasOutput("XShape")) {
return framework::KernelSignature("flatten_contiguous_range.mid", {"X"},
{"start_axis", "stop_axis"},
{"Out", "XShape"});
return framework::KernelSignature(
"flatten.mid", {"X"}, {"start_axis", "stop_axis"}, {"Out", "XShape"});
} else {
return framework::KernelSignature("flatten_contiguous_range", {"X"},
return framework::KernelSignature("flatten", {"X"},
{"start_axis", "stop_axis"}, {"Out"});
}
}
Expand Down
8 changes: 4 additions & 4 deletions paddle/fluid/operators/reshape_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -555,13 +555,13 @@ class Reshape2Op : public ReshapeOp {
const framework::ExecutionContext &ctx) const override {
auto multi_inputs = ctx.MultiInput<framework::Tensor>("ShapeTensor");
if (multi_inputs.size() > 0) {
return framework::KernelSignature("reshape2.mulhost",
{"X", "ShapeTensor"}, {}, {"Out"});
return framework::KernelSignature("reshape.mulhost", {"X", "ShapeTensor"},
{}, {"Out"});
} else if (ctx.HasInput("Shape")) {
return framework::KernelSignature("reshape2.host", {"X", "Shape"}, {},
return framework::KernelSignature("reshape.host", {"X", "Shape"}, {},
{"Out"});
} else {
return framework::KernelSignature("reshape2", {"X"}, {"shape"}, {"Out"});
return framework::KernelSignature("reshape", {"X"}, {"shape"}, {"Out"});
}
}
};
Expand Down
28 changes: 28 additions & 0 deletions paddle/pten/core/convert_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ limitations under the License. */

namespace pten {

// the map between kernel name in fluid and pten
Copy link
Contributor

Choose a reason for hiding this comment

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

这里再加些注释,说明一下key,value 是什么含义吧

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

const std::unordered_map<std::string, std::string> kernel_alias_name_map = {
{"reshape2", "reshape"},
Copy link
Contributor

Choose a reason for hiding this comment

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

这个会导致咱们让大家迁移的时候,还需要额外关注这个文件的更新和映射,能否在注册时一起声明下这个name

Copy link
Contributor

Choose a reason for hiding this comment

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

如果用这种的话,建议这个map单独放到一个文件中管理

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

{"fill_any_like", "full_like"},
{"fill_constant", "full"},
{"matmul_v2", "matmul"},
{"flatten_contiguous_range", "flatten"},
{"reduce_mean", "mean"},
{"reduce_sum", "sum"},
{"elementwise_add", "add"},
{"elementwise_sub", "subtract"},
{"elementwise_mul", "muliply"},
{"elementwise_div", "divide"},
Copy link
Contributor

Choose a reason for hiding this comment

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

这里是不可以按照一个统一的规则排下序,后面迁移的kernel多了找起来也方便

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

// fluid kernel "mean/reshape/matmul/flatten/sum" should be deprecated
{"mean", "deprecated"},
{"reshape", "deprecated"},
{"matmul", "deprecated"},
{"flatten", "deprecated"},
{"sum", "deprecated"}};

// TODO(chenweihang): Add other place trans cases later
Backend TransToPtenBackend(const paddle::platform::Place& place) {
if (paddle::platform::is_cpu_place(place)) {
Expand Down Expand Up @@ -304,4 +324,12 @@ int TensorDtype2NumpyDtype(pten::DataType dtype) {
}
}

std::string TransToPtenKernelName(const std::string& fluid_op_name) {
Copy link
Contributor

Choose a reason for hiding this comment

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

可以返回const std::string&

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

if (kernel_alias_name_map.find(fluid_op_name) !=
kernel_alias_name_map.end()) {
return kernel_alias_name_map.at(fluid_op_name);
}
return fluid_op_name;
}

} // namespace pten
2 changes: 2 additions & 0 deletions paddle/pten/core/convert_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ namespace pten {
using DataType = paddle::experimental::DataType;
using DataLayout = paddle::experimental::DataLayout;

std::string TransToPtenKernelName(const std::string& fluid_op_name);
Copy link
Contributor

Choose a reason for hiding this comment

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

同上


Backend TransToPtenBackend(const paddle::platform::Place& place);
DataType TransToPtenDataType(
const paddle::framework::proto::VarType::Type& dtype);
Expand Down
3 changes: 2 additions & 1 deletion paddle/pten/core/kernel_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "paddle/pten/common/backend.h"
#include "paddle/pten/common/data_type.h"
#include "paddle/pten/common/layout.h"
#include "paddle/pten/core/convert_utils.h"
#include "paddle/pten/core/kernel_def.h"

// See Note [ Why still include the fluid headers? ]
Expand Down Expand Up @@ -269,7 +270,7 @@ class KernelFactory {
}

bool HasCompatiblePtenKernel(const std::string& op_type) const {
return compatible_op_types_.count(op_type) > 0;
return compatible_op_types_.count(TransToPtenKernelName(op_type)) > 0;
}

const Kernel& SelectKernelOrThrowError(const KernelName& kernel_name,
Expand Down
4 changes: 2 additions & 2 deletions paddle/pten/kernels/cpu/creation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void FillConstant(const CPUContext& dev_ctx,

PT_REGISTER_MODULE(CreationCPU);

PT_REGISTER_KERNEL("fill_any_like",
PT_REGISTER_KERNEL("full_like",
Copy link
Contributor

Choose a reason for hiding this comment

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

这样直接改会导致pten kernel在运行时都没有被选到吧,兼容态是根据这个名字来搜索是否执行pten kernel的

Copy link
Contributor Author

Choose a reason for hiding this comment

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

会选到

CPU,
ANY,
pten::FillAnyLike,
Expand All @@ -74,7 +74,7 @@ PT_REGISTER_KERNEL("fill_any_like",
bool,
paddle::platform::float16) {}

PT_REGISTER_KERNEL("fill_constant",
PT_REGISTER_KERNEL("full",
CPU,
ANY,
pten::FillConstant,
Expand Down
3 changes: 1 addition & 2 deletions paddle/pten/kernels/cpu/linalg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,4 @@ PT_REGISTER_KERNEL("dot",
complex128) {}

PT_REGISTER_KERNEL(
"matmul_v2", CPU, ANY, pten::Matmul, float, double, complex64, complex128) {
}
"matmul", CPU, ANY, pten::Matmul, float, double, complex64, complex128) {}
19 changes: 8 additions & 11 deletions paddle/pten/kernels/cpu/manipulation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ PT_REGISTER_MODULE(ManipulationCPU);

// TODO(yuanrisheng): "flatten_contiguous_range" is compatible with old kernel
// architecture, kernel_name should be "flatten".
PT_REGISTER_KERNEL("flatten_contiguous_range",
PT_REGISTER_KERNEL("flatten",
CPU,
ANY,
pten::Flatten,
Expand All @@ -146,7 +146,7 @@ PT_REGISTER_KERNEL("flatten_contiguous_range",
int,
int64_t) {}

PT_REGISTER_KERNEL("flatten_contiguous_range.mid",
PT_REGISTER_KERNEL("flatten.mid",
CPU,
ANY,
pten::FlattenWithXShape,
Expand Down Expand Up @@ -176,40 +176,37 @@ PT_REGISTER_KERNEL("cast",

// TODO(yuanrisheng): "reshape2" is compatible with old kernel
// architecture, kernel_name should be "reshape".
PT_REGISTER_KERNEL_WITH_NO_TYPE("reshape2",
PT_REGISTER_KERNEL_WITH_NO_TYPE("reshape",
CPU,
ANY,
pten::ReshapeFromVectorVal) {}

PT_REGISTER_KERNEL_WITH_NO_TYPE("reshape2.mid",
PT_REGISTER_KERNEL_WITH_NO_TYPE("reshape.mid",
CPU,
ANY,
pten::ReshapeFromVectorValWithXShape) {}

PT_REGISTER_KERNEL_WITH_NO_TYPE("reshape2.host",
CPU,
ANY,
pten::ReshapeFromDT) {
PT_REGISTER_KERNEL_WITH_NO_TYPE("reshape.host", CPU, ANY, pten::ReshapeFromDT) {
kernel->InputAt(1).SetBackend(pten::Backend::CPU);
kernel->InputAt(1).SetDataType(paddle::experimental::DataType::INT32);
}

PT_REGISTER_KERNEL_WITH_NO_TYPE("reshape2.host.mid",
PT_REGISTER_KERNEL_WITH_NO_TYPE("reshape.host.mid",
CPU,
ANY,
pten::ReshapeFromDTWithXShape) {
kernel->InputAt(1).SetBackend(pten::Backend::CPU);
kernel->InputAt(1).SetDataType(paddle::experimental::DataType::INT32);
}
PT_REGISTER_KERNEL_WITH_NO_TYPE("reshape2.mulhost",
PT_REGISTER_KERNEL_WITH_NO_TYPE("reshape.mulhost",
CPU,
ANY,
pten::ReshapeFromVectorDT) {
kernel->InputAt(1).SetBackend(pten::Backend::CPU);
kernel->InputAt(1).SetDataType(paddle::experimental::DataType::INT32);
}

PT_REGISTER_KERNEL_WITH_NO_TYPE("reshape2.mulhost.mid",
PT_REGISTER_KERNEL_WITH_NO_TYPE("reshape.mulhost.mid",
CPU,
ANY,
pten::ReshapeFromVectorDTWithXShape) {
Expand Down
12 changes: 6 additions & 6 deletions paddle/pten/kernels/cpu/math.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ using complex128 = ::paddle::platform::complex<double>;
// using bfloat16 = ::paddle::platform::bfloat16;

PT_REGISTER_KERNEL("sign", CPU, ANY, pten::Sign, float, double) {}
PT_REGISTER_KERNEL("reduce_mean", CPU, ANY, pten::Mean, float, double, bool) {}
PT_REGISTER_KERNEL("mean", CPU, ANY, pten::Mean, float, double, bool) {}
PT_REGISTER_KERNEL("scale",
CPU,
ANY,
Expand All @@ -130,7 +130,7 @@ PT_REGISTER_KERNEL("scale",
int,
int64_t) {}

PT_REGISTER_KERNEL("elementwise_add",
PT_REGISTER_KERNEL("add",
CPU,
ANY,
pten::ElementwiseAdd,
Expand All @@ -140,7 +140,7 @@ PT_REGISTER_KERNEL("elementwise_add",
int64_t,
complex64,
complex128) {}
PT_REGISTER_KERNEL("elementwise_sub",
PT_REGISTER_KERNEL("subtract",
CPU,
ANY,
pten::ElementwiseSub,
Expand All @@ -150,7 +150,7 @@ PT_REGISTER_KERNEL("elementwise_sub",
int64_t,
complex64,
complex128) {}
PT_REGISTER_KERNEL("elementwise_div",
PT_REGISTER_KERNEL("divide",
CPU,
ANY,
pten::ElementwiseDiv,
Expand All @@ -160,7 +160,7 @@ PT_REGISTER_KERNEL("elementwise_div",
int64_t,
complex64,
complex128) {}
PT_REGISTER_KERNEL("elementwise_mul",
PT_REGISTER_KERNEL("multiply",
CPU,
ANY,
pten::ElementwiseMul,
Expand All @@ -172,7 +172,7 @@ PT_REGISTER_KERNEL("elementwise_mul",
complex64,
complex128) {}

PT_REGISTER_KERNEL("reduce_sum",
PT_REGISTER_KERNEL("sum",
CPU,
ANY,
pten::Sum,
Expand Down
4 changes: 2 additions & 2 deletions paddle/pten/kernels/cuda/creation.cu
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void FillConstant(const CUDAContext& dev_ctx,

PT_REGISTER_MODULE(CreationCUDA);

PT_REGISTER_KERNEL("fill_any_like",
PT_REGISTER_KERNEL("full_like",
CUDA,
ANY,
pten::FillAnyLike,
Expand All @@ -75,7 +75,7 @@ PT_REGISTER_KERNEL("fill_any_like",
bool,
paddle::platform::float16) {}

PT_REGISTER_KERNEL("fill_constant",
PT_REGISTER_KERNEL("full",
CUDA,
ANY,
pten::FillConstant,
Expand Down
2 changes: 1 addition & 1 deletion paddle/pten/kernels/cuda/linalg.cu
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ PT_REGISTER_KERNEL("dot",
complex64,
complex128) {}

PT_REGISTER_KERNEL("matmul_v2",
PT_REGISTER_KERNEL("matmul",
CUDA,
ANY,
pten::Matmul,
Expand Down
Loading