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

[IR] Rigister LegacyKernelOp into KernelDialect #56680

Merged
merged 20 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions paddle/cinn/hlir/dialect/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
generated/**
generated/*
7 changes: 6 additions & 1 deletion paddle/fluid/framework/new_executor/new_ir_interpreter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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<LegacyKernelInstruction>(op_idx++,
place_,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -32,9 +33,9 @@ PaddleKernelDialect::PaddleKernelDialect(ir::IrContext *context)

void PaddleKernelDialect::initialize() {
RegisterTypes<paddle::dialect::AllocatedDenseTensorType>();
RegisterTypes<paddle::dialect::AllocatedSelectedRowsType>();
RegisterOps<dialect::PhiKernelOp>();

RegisterTypes<paddle::dialect::AllocatedDenseTensorType,
paddle::dialect::AllocatedSelectedRowsType>();
RegisterOps<dialect::PhiKernelOp, dialect::LegacyKernelOp>();
RegisterAttributes<paddle::dialect::KernelAttribute>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace dialect {
class PhiKernelOp : public ir::Op<PhiKernelOp> {
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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ir::StrAttribute>(),
phi::errors::PreconditionNotMet(
"Type of attribute: op_name is not right."));

PADDLE_ENFORCE(attributes.count("kernel_name") > 0 &&
attributes.at("kernel_name").isa<ir::StrAttribute>(),
phi::errors::PreconditionNotMet(
"Type of attribute: kernel_name is not right."));

PADDLE_ENFORCE(attributes.count("kernel_key") > 0 &&
attributes.at("kernel_key").isa<KernelAttribute>(),
phi::errors::PreconditionNotMet(
"Type of attribute: kernel_key is not right."));
}

std::string LegacyKernelOp::op_name() {
return attributes().at("op_name").dyn_cast<ir::StrAttribute>().AsString();
}
std::string LegacyKernelOp::kernel_name() {
return attributes().at("kernel_name").dyn_cast<ir::StrAttribute>().AsString();
}
phi::KernelKey LegacyKernelOp::kernel_key() {
return attributes().at("kernel_key").dyn_cast<KernelAttribute>().data();
}

} // namespace dialect
} // namespace paddle

IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::LegacyKernelOp)
Original file line number Diff line number Diff line change
@@ -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<LegacyKernelOp> {
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)
16 changes: 13 additions & 3 deletions paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -507,6 +508,10 @@ std::unique_ptr<ir::Program> 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) {
Expand Down Expand Up @@ -1008,7 +1013,6 @@ std::unique_ptr<ir::Program> 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) {
Expand All @@ -1019,8 +1023,14 @@ std::unique_ptr<ir::Program> 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;

Expand Down