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

Support setting infershape function for custom grad op #38776

Merged

Conversation

chenwhql
Copy link
Contributor

@chenwhql chenwhql commented Jan 6, 2022

PR types

New features

PR changes

Others

Describe

Support setting infershap function for custom grad op

  1. 统一各执行体系中infershape的调用方式,因为我们正在注册op时,会默认将op.InferShape注册到OpInfo的成员函数指针中,所以调用op.Info().infer_shape_是安全的,或者说需要让它是安全的;如果直接调用op.InferShape,由于自定义算子的InferShape需要用户设定,会导致自定义算子的infershape在执行体系中无法被调用到,从而导致隐性bug
  • 但目前有几个op的infershape实现在MKLDNN场景下,由于在infershape函数中使用了非InferShapeContext的外部信息作为判断条件,导致直接调用op.Info().infer_shape_会无法使用这部分信息,从而导致出错(这严格来说并不是正确的使用方式,InferShape所使用的外部信息,应该通过InferShapeContext传入,而不应该从外部环境拿变量使用),该PR合入后追加PR修复
  1. 开放自定义反向op注册InferShapeFn的特性,以支持反向op输入不强绑定前向op输入,增强使用灵活性
PD_BUILD_OP(custom_relu_no_x_in_backward)
    .Inputs({"X"})
    .Outputs({"Out"})
    .SetKernelFn(PD_KERNEL(ReluForward));

PD_BUILD_GRAD_OP(custom_relu_no_x_in_backward)
    .Inputs({"Out", paddle::Grad("Out")})
    .Outputs({paddle::Grad("X")})
    .SetKernelFn(PD_KERNEL(ReluBackwardWithoutX))
    .SetInferShapeFn(PD_INFER_SHAPE(ReluBackwardWithoutXInferShape));

@chenwhql chenwhql changed the title Support setting infershap function for custom grad op Support setting infershape function for custom grad op Jan 7, 2022
Copy link
Collaborator

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

@zhiqiu zhiqiu left a comment

Choose a reason for hiding this comment

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

LGTM

@chenwhql chenwhql merged commit 046553c into PaddlePaddle:develop Jan 10, 2022
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.

3 participants