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

[code-gen] Support code-gen for opmaker of sparse op #46993

Merged
merged 14 commits into from
Oct 18, 2022

Conversation

zyfncg
Copy link
Contributor

@zyfncg zyfncg commented Oct 13, 2022

PR types

Others

PR changes

Others

Describe

支持通过Yaml配置生成Sparse类算子OpMaker以及相关静态图代码。

生成的代码包括paddle/fluid/operators/generated_sparse_op.cc中的算子定义和注册相关代码以及paddle/phi/ops/compat/generated_sparse_sig.cc中的kernel函数签名映射代码。

sparse_abs算子为例,paddle/fluid/operators/generated_sparse_op.cc中生成的代码如下:

class SparseAbsOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("x", "(Tensor), input 0 of sparse_abs op.");
    AddOutput("out", "(Tensor), output 0 of sparse_abs op.");
    AddComment(R"DOC(
TODO: Documentation of sparse_abs op.
)DOC");
  }
};

template <typename T>
class SparseAbsGradOpMaker : public framework::SingleGradOpMaker<T> {
 public:
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;

 protected:
  void Apply(GradOpPtr<T> grad_op) const override {
    grad_op->SetType("sparse_abs_grad");

    grad_op->SetInput("x", this->Input("x"));
    grad_op->SetInput(GradVarName("out"), this->OutputGrad("out"));

    grad_op->SetOutput(GradVarName("x"), this->InputGrad("x"));

    grad_op->SetAttrMap(this->Attrs());
  }
};

class SparseAbsOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;
};

DECLARE_INFER_SHAPE_FUNCTOR(sparse_abs, SparseAbsInferShapeFunctor,
                            PD_INFER_META(phi::UnchangedInferMeta));

REGISTER_OPERATOR(sparse_abs, ops::SparseAbsOp,
                  ops::SparseAbsOpMaker,
                  ops::SparseAbsGradOpMaker<paddle::framework::OpDesc>,
                  ops::SparseAbsGradOpMaker<paddle::imperative::OpBase>,
                  ops::SparseAbsInferShapeFunctor);

paddle/phi/ops/compat/generated_sparse_sig.cc中生成的代码如下:

KernelSignature SparseAbsOpArgumentMapping(const ArgumentMappingContext& ctx) {
  paddle::small_vector<const char*> inputs {"x"};
  paddle::small_vector<const char*> attrs;
  paddle::small_vector<const char*> outputs {"out"};
  const char* kernel_name = "unregistered";

  if (ctx.IsSparseCooTensorInput("x")) {
    kernel_name = "abs_coo";
  }
  if (ctx.IsSparseCsrTensorInput("x")) {
    kernel_name = "abs_csr";
  }
  KernelSignature sig (kernel_name, std::move(inputs), std::move(attrs), std::move(outputs));
  return sig;
}

/*
******************************************************************
NOTE: The following codes are for 'get_compat_kernel_signature.py'
All possible KernelSignatures returned by SparseAbsOpArgumentMapping:

return KernelSignature("abs_coo", {"x"}, {}, {"out"});
return KernelSignature("abs_csr", {"x"}, {}, {"out"});
******************************************************************
*/

PD_REGISTER_ARG_MAPPING_FN(sparse_abs, phi::SparseAbsOpArgumentMapping);
PD_REGISTER_ARG_MAPPING_FN(sparse_abs_grad, phi::SparseAbsGradOpArgumentMapping);

@paddle-bot
Copy link

paddle-bot bot commented Oct 13, 2022

你的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.

Comment on lines +420 to +421
if (op_type.compare(0, 7, "sparse_") == 0 && op_type != "sparse_momentum" &&
op_type != "sparse_attention") {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

[TODO] 这里Sparse类算子的过滤条件用硬编码的方式不够鲁棒,如果中间态代码短期内无法去掉,这里的逻辑需要再优化下

Copy link
Contributor

@zkh2016 zkh2016 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

@XiaoguangHu01 XiaoguangHu01 left a comment

Choose a reason for hiding this comment

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

LGTM

@zyfncg zyfncg merged commit bdd3dde into PaddlePaddle:develop Oct 18, 2022
@zyfncg zyfncg deleted the gene_sparse_opmaker branch October 18, 2022 02:05
zyfncg added a commit to zyfncg/Paddle that referenced this pull request Oct 27, 2022
)

* support generating code of opmaker for backward op invoke forward op

* gsupport code-gen of opmaker for sparse op

* refind logic of choose phi kernrel

* fix complie budg

* fix code_gen bug

* fix bug

* fix kernel signature code-gen

* fix complie bug of VarType

* fix complie bug of VarType

* fix test_sparse_conv_op

* fix test_sparse_norm_op
phlrain pushed a commit that referenced this pull request Nov 1, 2022
…6993) (#47417)

* support generating code of opmaker for backward op invoke forward op (#46912)

* [code-gen] Support code-gen for opmaker of sparse op (#46993)

* support generating code of opmaker for backward op invoke forward op

* gsupport code-gen of opmaker for sparse op

* refind logic of choose phi kernrel

* fix complie budg

* fix code_gen bug

* fix bug

* fix kernel signature code-gen

* fix complie bug of VarType

* fix complie bug of VarType

* fix test_sparse_conv_op

* fix test_sparse_norm_op

* [Phi] Refactor logic of judging whether having a phi kernrel (#46920)

* refind logic of choose phi kernrel

* fix complie budg

* update cmake
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.

5 participants