diff --git a/paddle/cinn/hlir/dialect/.gitignore b/paddle/cinn/hlir/dialect/.gitignore index a21ba08d95acf381ab945cefc294161c6d4b3a8d..3e41ce4c67822f85ed473abe2b38cf4733e6cd3f 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 28752ef1f7eb1a643935e76afab0a7fc2e7c6d26..df9f398770e283db94fd7ba5dc7a076ba4411c86 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 bb3d52d7664052692d9a470ad9abef9d770dc418..5269fe62892a5375bfd5bcfd966d2cc9927fabbe 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 5cbd4b0b434b38acd83e02e5bf90bcb27cfb6bba..91dae1cfd560fef5bc9d9fdc7197dab8f4882b91 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 0000000000000000000000000000000000000000..ca15657e104856ecbbe3202e70311a6f5a7313e4 --- /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 0000000000000000000000000000000000000000..524505a48305fc05c3fd3ea5503d8dd411fcc062 --- /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 4198098f2bd4fbb65e00f452d043d56b80ae6f55..64d37c95c1aee69739ac55df255bc190ff875922 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;