-
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
[PHI Kernels] add pad op for xpu #53684
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
❌ The PR is not created using PR's template. You can refer to this Demo. |
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
#include "paddle/phi/kernels/pad_grad_kernel.h" | ||
|
||
#include "paddle/phi/backends/xpu/enforce_xpu.h" | ||
#include "paddle/phi/backends/xpu/xpu_context.h" |
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
pad_left, | ||
pad_right, | ||
value); | ||
PADDLE_ENFORCE_XDNN_SUCCESS(r, "pad"); |
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.
应该是pad_grad~
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.
请问这里不应该是和实际调用的XDNN API保持一致吗,pad_grad_kernel里面实际调用的也是xpu::pad
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.
好的~
class TestPadOpError(unittest.TestCase): | ||
def test_errors(self): | ||
with paddle.fluid.framework._static_guard(): | ||
with program_guard(Program(), Program()): | ||
input_data = np.random.random((2, 2)).astype("float32") | ||
|
||
def test_Variable(): | ||
paddle.nn.functional.pad(x=input_data, pad=[1, 1, 1, 1]) | ||
|
||
self.assertRaises(TypeError, test_Variable) | ||
|
||
data = paddle.static.data( | ||
name='data', shape=[4], dtype='float16' | ||
) | ||
paddle.nn.functional.pad(x=data, pad=[0, 1]) |
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.
对于xpu算子允许范围以外的数据类型,我不确定单测里需不需要测,需要看看@ykkk2333怎么说
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.
XPU实际是支持FP16的,已经加上了
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.
ok
fc = paddle.nn.Linear(4, 10) | ||
x = paddle.randn([2, 4]) | ||
x.stop_gradient = False | ||
feat = fc(x) # [2,3,10] | ||
|
||
out = self.call_func(feat) | ||
|
||
sgd = paddle.optimizer.SGD() | ||
sgd.minimize(paddle.mean(out)) | ||
self.assertTrue(self.var_prefix() in str(main_prog)) | ||
exe = paddle.static.Executor(paddle.XPUPlace(0)) | ||
exe.run(starup_prog) | ||
res = exe.run(fetch_list=[feat, 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.
这里为什么要实际跑一次训练?上面TestCase1-3测的和这里有什么不同?
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.
这个是我看CPU和GPU的单测里面有就加上了,看内容似乎是和框架里面的其他部分一起做的一个小的集成测试?
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
Others
PR changes
OPs
Description
为XPU添加pad_kernel及对应的反向算子
nn.functional.pad在
mode="constant"
和len(pad) == dim * 2
的情况下会调用pad op。该Op在CPU和GPU中复用了paddle/phi/kernels/func/padding.h中基于EigenTensor的实现,EigenTensor不支持XPU,需要调用xdnn的pad api实现。
单测部分的代码参考了https://github.com/PaddlePaddle/Paddle/blob/develop/python/paddle/fluid/tests/unittests/test_pad_op.py