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] Support static build function for op builder #54197

Merged

Conversation

zhangbo9674
Copy link
Contributor

@zhangbo9674 zhangbo9674 commented May 30, 2023

PR types

New features

PR changes

Others

Description

本 pr 是 op builder 机制完善 pr:

在 op 定义的自动代码生成逻辑中添加 build 函数的自动生成逻辑,每个 op class 都将包含一个静态的 build 成员方法:

// example of FullOp::build
void FullOp::build(ir::Builder &builder, ir::OperationArgument &argument, std::vector<int64_t> shape, float value, phi::DataType dtype, phi::Place place) {

  ir::Attribute attr_shape = paddle::dialect::IntArrayAttribute::get(ir::IrContext::Instance(), phi::IntArray(shape));
  argument.addAttribute("shape", attr_shape);
  ir::Attribute attr_value = paddle::dialect::ScalarAttribute::get(ir::IrContext::Instance(), phi::Scalar(value));
  argument.addAttribute("value", attr_value);
  ir::Attribute attr_dtype = paddle::dialect::DataTypeAttribute::get(ir::IrContext::Instance(), dtype);
  argument.addAttribute("dtype", attr_dtype);
  ir::Attribute attr_place = paddle::dialect::PlaceAttribute::get(ir::IrContext::Instance(), place);
  argument.addAttribute("place", attr_place);

  phi::DenseTensor dense_out;
  phi::MetaTensor meta_out(&dense_out);

  phi::CreateInferMeta(shape, dtype, &meta_out);

  std::vector<ir::Type> argument_outputs;
  ir::Type out_dense_tensor_type = paddle::dialect::DenseTensorType::get(ir::IrContext::Instance(), TransToIrDataType(dense_out.dtype()), dense_out.dims(), dense_out.layout(), dense_out.lod(), dense_out.offset());
  argument_outputs.push_back(out_dense_tensor_type);
  argument.addTypes(argument_outputs.begin(), argument_outputs.end());
}

在 op builder 将借助该方法实现 operation 的构造,具体的使用示例如下:

ir::IrContext *ctx = ir::IrContext::Instance();
ctx->GetOrRegisterDialect<ir::BuiltinDialect>();
ctx->GetOrRegisterDialect<paddle::dialect::PaddleDialect>();
ir::Program program;
ir::Builder builder = ir::Builder::AtBlockEnd(ctx, program.block());
// create FullOp by Builder::create template method
paddle::dialect::FullOp full_op = builder.create<paddle::dialect::FullOp, std::vector<int64_t>, float, phi::DataType, phi::Place>({2,2}, 1.5, phi::DataType::FLOAT32, phi::CPUPlace());
ir::Type full_op_output = full_op.operation()->GetResultByIndex(0).type();

Todo

  • build 支持可变 attribute 机制

Other

Pcard-67164

@paddle-bot
Copy link

paddle-bot bot commented May 30, 2023

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot
Copy link

paddle-bot bot commented May 30, 2023

✅ This PR's description meets the template requirements!
Please wait for other CI results.

Copy link
Contributor

@winter-wang winter-wang left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@heavyrain-lzy heavyrain-lzy left a comment

Choose a reason for hiding this comment

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

LGTM

@zhangbo9674 zhangbo9674 merged commit 4bd5b69 into PaddlePaddle:develop Jun 1, 2023
24 of 25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants