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

Backward C++ API Code-Generation #39057

Merged
merged 8 commits into from
Jan 24, 2022
Merged

Conversation

zyfncg
Copy link
Contributor

@zyfncg zyfncg commented Jan 19, 2022

PR types

New features

PR changes

APIs

Describe

反向C++ API接口自动生成,在#37668的基础上增加了反向C++ API接口。

反向C++ API的自动生成同样采用yaml文件进行配置,配置格式如下(以matmul_grad和scale_grad为例):

- backward_api : matmul_grad
  forward : matmul (const Tensor& x, const Tensor& y, bool transpose_x, bool transpose_y) -> Tensor(out)
  args : (const Tensor& x, const Tensor& y, const Tensor& out_grad, bool transpose_x=false, bool transpose_y=false)
  output : Tensor(x_grad), Tensor(y_grad)
  infer_meta :
    func : MatmulGradInferMeta
  kernel : 
    func : matmul_grad

- backward_api : scale_grad
  forward : scale (const Tensor& x, const Scalar& scale, float bias, bool bias_after_scale) -> Tensor(out)
  args : (const Tensor& out_grad, const Scalar& scale, float bias=0.0, bool bias_after_scale=true)
  output : Tensor(x_grad)
  invoke : scale(out_grad, scale, bias, bias_after_scale)

其中各项配置参数的含义:
backward_api : 反向C++ API的函数名
forward :对应的前向API(包括API名称,输入以及输出参数)
args : 反向API的输入参数,参数名需要与前向API对应
output : 返回参数类型,支持返回单个或多个Tensor与vector<Tensor>
infer_meta : 生成返回Tensor的Meta信息
    func : 调用的InferMeta函数
kernel :  kernel配置
    func : 调用的kernel函数(使用kernel注册名)
invoke : 复用前向C++ API,传入对应参数进行调用

注意:当前yaml配置格式非最终形态,后续仍会迭代调整。

@paddle-bot-old
Copy link

Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

# backward api file
set(bw_api_gen_file ${CMAKE_SOURCE_DIR}/python/paddle/utils/code_gen/backward_api_gen.py)
set(bw_api_yaml_file ${CMAKE_SOURCE_DIR}/python/paddle/utils/code_gen/backward.yaml)
set(bw_api_header_file ${CMAKE_SOURCE_DIR}/paddle/pten/api/include/backward_api.h)
Copy link
Contributor

Choose a reason for hiding this comment

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

反向API是不是暂时不要一并暴露给用户

Copy link
Contributor

Choose a reason for hiding this comment

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

建议不要一并放到api/include目录下,可以新创建一个子目录,比如api/backward

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -0,0 +1,74 @@
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
Copy link
Contributor

Choose a reason for hiding this comment

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

2022

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

See the License for the specific language governing permissions and
limitations under the License. */

#include "paddle/pten/infermeta/grad_infermeta.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

命名重复了,可以简短一些,grad.h或者backward.h,inferMeta有目录就可以了

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -0,0 +1,34 @@
- grad_api : matmul_grad
Copy link
Contributor

Choose a reason for hiding this comment

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

是不是上下命名一致好一些,如果文件或目录是backward api的话,里面建议也是backward_api,或者就文件和目录也用grad

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, 目前修改为backward_api

@@ -4,7 +4,8 @@ paddle/fluid/API_DEV.spec
paddle/fluid/API_PR.spec
paddle/fluid/op_use_default_grad_maker_DEV.spec
paddle/fluid/op_use_default_grad_maker_PR.spec
paddle/pten/api/*/api*
paddle/pten/api/*/api.*
paddle/pten/api/*/backward*
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's put specific filenames (api.yaml, backward.yaml) in .gitignore.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! This will be done in another PR

@zyfncg zyfncg merged commit f83d1c0 into PaddlePaddle:develop Jan 24, 2022
@DannyIsFunny
Copy link
Contributor

DannyIsFunny commented Jan 25, 2022

这个PR似乎导致Paddle 编译失败
编译命令:

cd Paddle && mkdir build && cd build
cmake .. -DWITH_PYTHON=ON -DWITH_GPU=OFF -DPYTHON_EXECUTABLE=`which python3`
make -j8

错误信息:
947d13df18a91a40dde3afc124515a7c

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.

4 participants