-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
[NPU] Support npu op reciprocal and reciprocal grad #34531
Conversation
Thanks for your contribution! |
import paddle | ||
import paddle.fluid as fluid | ||
|
||
paddle.enable_static() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 如果不是用API组网,静态图执行的方式写单测,这个enable_static应该是没有必要的
- 上面的import,看看没有必要的也可以删掉了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 此处参考了npu pow op的单测写法,使用的是静态图的写法.
- 无用的import已remove.
return | ||
self.check_grad_with_place( | ||
self.place, ['X'], 'Out', max_relative_error=0.01) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FP16类型不用check_grad吗?这个NPU算子开发要求里有没有明确提到
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
有明确提到:参见NPU开发算子文档:check_grad_with_place检查反向梯度时,不支持对float16数据类型的检查,不用写相关单测。
self.dtype = np.float32 | ||
|
||
def init_kernel_type(self): | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
init_kernel_type这个方法在哪里用到了吗?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove.
ctx.template device_context<paddle::platform::NPUDeviceContext>() | ||
.stream(); | ||
|
||
const auto& runner = NpuOpRunner("Reciprocal", {*x}, {*out}, {}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
上面每一行之间的空行建议删除,下同
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
def set_npu(self): | ||
self.__class__.use_npu = True | ||
self.place = paddle.NPUPlace(0) | ||
self.__class__.no_need_check_grad = True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果fp16类型确定不需要梯度检查,需要使用skip_check_grad_ci装饰器,并描述原因(可以在源码里搜索下参考下别的单测)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
ops::ReciprocalGradNPUKernel<paddle::platform::NPUDeviceContext, int>, | ||
ops::ReciprocalGradNPUKernel<paddle::platform::NPUDeviceContext, int64_t>, | ||
ops::ReciprocalGradNPUKernel<paddle::platform::NPUDeviceContext, | ||
paddle::platform::float16>); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
看PR描述里提到 complex64, complex12是可以支持的,但这里没有注册,需要确认下开发要求
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已确定开发需求(@彭宇琪):npu上支持的数据类型与cpu上保持一致即可。故最终调整支持的数据类型为float, double和float16.
873f47b
to
758f37c
Compare
758f37c
to
541df3d
Compare
def init_dtype(self): | ||
self.dtype = np.float16 | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
建议增加对double数据类型的单测,C++算子支持的数据类型要尽量在单测中覆盖
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
测试fp64类型时,报了如下错误:
ExternalError: ACL error, the error code is : 500002. (at /workspace/limin-workspace/npu-paddle/Paddle/paddle/fluid/operators/npu_op_runner.cc:380) 1226: [operator < mean > error]。
原因:reciprocal的单测(反向)会自动添加mean op,但是mean的前向ReduceMeanD不支持double和int,注册mean npu kernel时却支持了fp64和int类型。
解决:将mean_op_npu.cc中对double和int类型的支持去掉。
541df3d
to
17912cf
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
PR types
New features
PR changes
OPs
Describe
[NPU] Support npu op reciprocal and reciprocal grad
说明:
NPU端支持的数据类型与CPU端的保持一致:float, float16, double。
测试fp64类型时,报了如下错误:
ExternalError: ACL error, the error code is : 500002. (at /workspace/limin-workspace/npu-paddle/Paddle/paddle/fluid/operators/npu_op_runner.cc:380) 1226: [operator < mean > error]。
原因:reciprocal的单测(反向)会自动添加mean op,但是mean的前向ReduceMeanD不支持double和int,注册mean kernel时却支持了fp64和int类型。
解决:将mean_op_npu.cc中对double和int类型的支持去掉。
运行结果
单测运行结果
调用 reciprocal npu kernel 和 reciprocal grad npu kernel
fp32:
fp64:
fp16: