From ded1044258ac05a76e76a5955febedb2ae2a7168 Mon Sep 17 00:00:00 2001 From: chen2016013 <111894720+chen2016013@users.noreply.github.com> Date: Wed, 30 Aug 2023 14:11:11 +0800 Subject: [PATCH] [IR] Rigister LegacyKernelOp into KernelDialect (#56680) * Register LegacyKernelDialect & Rigister LegacyKernelOp * fix code style * delete LegacyKernelDialect ,register LegacyKernelOp into PaddleKernelDialect * fix bug * change as reviewed comments * bug fix * bug fix * try to restart coverage CI * pass legacy op to kernel pass * fix code style * fix code style * fix code style --- paddle/cinn/hlir/dialect/.gitignore | 1 + .../new_executor/new_ir_interpreter.cc | 7 ++- .../ir/kernel_dialect.cc | 7 ++- .../paddle_kernel_dialect/ir/kernel_op.h | 2 +- .../ir/legacy_kernel_op.cc | 62 +++++++++++++++++++ .../ir/legacy_kernel_op.h | 38 ++++++++++++ .../ir/transforms/pd_op_to_kernel_pass.cc | 16 ++++- 7 files changed, 125 insertions(+), 8 deletions(-) create mode 100644 paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.cc create mode 100644 paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.h diff --git a/paddle/cinn/hlir/dialect/.gitignore b/paddle/cinn/hlir/dialect/.gitignore index a21ba08d95a..3e41ce4c678 100644 --- a/paddle/cinn/hlir/dialect/.gitignore +++ b/paddle/cinn/hlir/dialect/.gitignore @@ -1 +1,2 @@ generated/** +generated/* diff --git a/paddle/fluid/framework/new_executor/new_ir_interpreter.cc b/paddle/fluid/framework/new_executor/new_ir_interpreter.cc index 28752ef1f7e..df9f398770e 100644 --- a/paddle/fluid/framework/new_executor/new_ir_interpreter.cc +++ b/paddle/fluid/framework/new_executor/new_ir_interpreter.cc @@ -42,6 +42,11 @@ #include "paddle/fluid/framework/new_executor/instruction/legacy_kernel_instruction.h" #include "paddle/fluid/framework/new_executor/instruction/phi_kernel_instruction.h" #include "paddle/fluid/ir/dialect/paddle_dialect/utils/utils.h" +#include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_attribute.h" +#include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_dialect.h" +#include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_op.h" +#include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_type.h" +#include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.h" #include "paddle/fluid/ir/phi_kernel_adaptor/phi_kernel_util.h" #include "paddle/ir/core/builtin_attribute.h" @@ -517,7 +522,7 @@ void NewIRInterpreter::BuildInstruction() { } VLOG(6) << "process " << op_name; - if (dialect::IsLegacyOp(op_name)) { + if (op->name().compare(paddle::dialect::LegacyKernelOp::name()) == 0) { vec_instruction_base_.emplace_back( std::make_unique(op_idx++, place_, diff --git a/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_dialect.cc b/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_dialect.cc index bb3d52d7664..5269fe62892 100644 --- a/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_dialect.cc +++ b/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_dialect.cc @@ -16,6 +16,7 @@ #include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_attribute.h" #include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_op.h" #include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_type.h" +#include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.h" #include "paddle/fluid/platform/init_phi.h" #include "paddle/phi/common/place.h" #include "paddle/phi/core/ddim.h" @@ -32,9 +33,9 @@ PaddleKernelDialect::PaddleKernelDialect(ir::IrContext *context) void PaddleKernelDialect::initialize() { RegisterTypes(); - RegisterTypes(); - RegisterOps(); - + RegisterTypes(); + RegisterOps(); RegisterAttributes(); } diff --git a/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_op.h b/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_op.h index 5cbd4b0b434..91dae1cfd56 100644 --- a/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_op.h +++ b/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_op.h @@ -24,7 +24,7 @@ namespace dialect { class PhiKernelOp : public ir::Op { public: using Op::Op; - static const char *name() { return "phi.kernel"; } + static const char *name() { return "pd_kernel.phi_kernel"; } static constexpr uint32_t attributes_num = 3; static const char *attributes_name[attributes_num]; std::string op_name(); diff --git a/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.cc b/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.cc new file mode 100644 index 00000000000..ca15657e104 --- /dev/null +++ b/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.cc @@ -0,0 +1,62 @@ +// Copyright (c) 2023 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. + +#include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.h" +#include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_attribute.h" +#include "paddle/ir/core/builtin_attribute.h" +#include "paddle/phi/core/enforce.h" + +namespace paddle { +namespace dialect { + +const char* LegacyKernelOp::attributes_name[attributes_num] = { // NOLINT + "op_name", + "kernel_name", + "kernel_key"}; + +void LegacyKernelOp::Verify() { + VLOG(4) << "Verifying inputs, outputs and attributes for: LegacyKernelOp."; + + auto& attributes = this->attributes(); + + PADDLE_ENFORCE(attributes.count("op_name") > 0 && + attributes.at("op_name").isa(), + phi::errors::PreconditionNotMet( + "Type of attribute: op_name is not right.")); + + PADDLE_ENFORCE(attributes.count("kernel_name") > 0 && + attributes.at("kernel_name").isa(), + phi::errors::PreconditionNotMet( + "Type of attribute: kernel_name is not right.")); + + PADDLE_ENFORCE(attributes.count("kernel_key") > 0 && + attributes.at("kernel_key").isa(), + phi::errors::PreconditionNotMet( + "Type of attribute: kernel_key is not right.")); +} + +std::string LegacyKernelOp::op_name() { + return attributes().at("op_name").dyn_cast().AsString(); +} +std::string LegacyKernelOp::kernel_name() { + return attributes().at("kernel_name").dyn_cast().AsString(); +} +phi::KernelKey LegacyKernelOp::kernel_key() { + return attributes().at("kernel_key").dyn_cast().data(); +} + +} // namespace dialect +} // namespace paddle + +IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::LegacyKernelOp) diff --git a/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.h b/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.h new file mode 100644 index 00000000000..524505a4830 --- /dev/null +++ b/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.h @@ -0,0 +1,38 @@ +// Copyright (c) 2023 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 + +#include "paddle/ir/core/builder.h" +#include "paddle/ir/core/op_base.h" +#include "paddle/phi/core/kernel_factory.h" + +namespace paddle { +namespace dialect { +class LegacyKernelOp : public ir::Op { + public: + using Op::Op; + static const char *name() { return "pd_kernel.legacy_kernel"; } + static constexpr uint32_t attributes_num = 3; + static const char *attributes_name[attributes_num]; + std::string op_name(); + std::string kernel_name(); + phi::KernelKey kernel_key(); + void Verify(); +}; + +} // namespace dialect +} // namespace paddle + +IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::LegacyKernelOp) diff --git a/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc b/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc index 4198098f2bd..64d37c95c1a 100644 --- a/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc +++ b/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc @@ -27,6 +27,7 @@ #include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_dialect.h" #include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_op.h" #include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_type.h" +#include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.h" #include "paddle/fluid/platform/place.h" #include "paddle/phi/api/lib/data_transform.h" #include "paddle/phi/api/lib/kernel_dispatch.h" @@ -507,6 +508,10 @@ std::unique_ptr PdOpLowerToKernelPass(ir::Program* prog, std::string phi_kernel_op_name = paddle::dialect::PhiKernelOp::name(); ir::OpInfo phi_kernel_op_info = ctx->GetRegisteredOpInfo(phi_kernel_op_name); + std::string legacy_kernel_op_name = paddle::dialect::LegacyKernelOp::name(); + ir::OpInfo legacy_kernel_op_info = + ctx->GetRegisteredOpInfo(legacy_kernel_op_name); + auto skip_feed_names = GetSkipFeedNames(block); for (auto op_item : *block) { @@ -1008,7 +1013,6 @@ std::unique_ptr PdOpLowerToKernelPass(ir::Program* prog, {"op_name", ir::StrAttribute::get(ctx, op_item->name())}, {"kernel_name", ir::StrAttribute::get(ctx, kernel_fn_str)}, {"kernel_key", dialect::KernelAttribute::get(ctx, kernel_key)}}; - auto op_attr_map = op_item->attributes(); for (auto& map_item : op_attr_map) { @@ -1019,8 +1023,14 @@ std::unique_ptr PdOpLowerToKernelPass(ir::Program* prog, op_attribute.emplace("is_inplace", ir::BoolAttribute::get(ctx, true)); } - ir::Operation* op = ir::Operation::Create( - vec_inputs, op_attribute, op_output_types, phi_kernel_op_info); + ir::Operation* op; + if (dialect::IsLegacyOp(op_item->name())) { + op = ir::Operation::Create( + vec_inputs, op_attribute, op_output_types, legacy_kernel_op_info); + } else { + op = ir::Operation::Create( + vec_inputs, op_attribute, op_output_types, phi_kernel_op_info); + } map_op_pair[op_item] = op; -- GitLab