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

[PHI Kernels] add pad op for xpu #53684

Merged
merged 3 commits into from
May 11, 2023
Merged

Conversation

lj970926
Copy link
Contributor

PR types

Others

PR changes

OPs

Description

为XPU添加pad_kernel及对应的反向算子

nn.functional.padmode="constant"len(pad) == dim * 2的情况下会调用pad op。

该Op在CPUGPU中复用了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

@paddle-bot
Copy link

paddle-bot bot commented May 10, 2023

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

@paddle-bot
Copy link

paddle-bot bot commented May 10, 2023

❌ The PR is not created using PR's template. You can refer to this Demo.
Please use PR's template, it helps save our maintainers' time so that more developers get helped.

ykkk2333
ykkk2333 previously approved these changes May 11, 2023
Copy link
Contributor

@ykkk2333 ykkk2333 left a 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"
Copy link
Contributor

Choose a reason for hiding this comment

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

这个头文件可以不用

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

pad_left,
pad_right,
value);
PADDLE_ENFORCE_XDNN_SUCCESS(r, "pad");
Copy link
Contributor

Choose a reason for hiding this comment

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

应该是pad_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.

请问这里不应该是和实际调用的XDNN API保持一致吗,pad_grad_kernel里面实际调用的也是xpu::pad

Copy link
Contributor

Choose a reason for hiding this comment

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

好的~

Comment on lines +102 to +116
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])
Copy link
Contributor

Choose a reason for hiding this comment

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

对于xpu算子允许范围以外的数据类型,我不确定单测里需不需要测,需要看看@ykkk2333怎么说

Copy link
Contributor Author

Choose a reason for hiding this comment

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

XPU实际是支持FP16的,已经加上了

Copy link
Contributor

Choose a reason for hiding this comment

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

ok

Comment on lines +130 to +142
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])
Copy link
Contributor

Choose a reason for hiding this comment

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

这里为什么要实际跑一次训练?上面TestCase1-3测的和这里有什么不同?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这个是我看CPU和GPU的单测里面有就加上了,看内容似乎是和框架里面的其他部分一起做的一个小的集成测试?

RuohengMa
RuohengMa previously approved these changes May 11, 2023
Copy link
Contributor

@RuohengMa RuohengMa left a comment

Choose a reason for hiding this comment

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

LGTM

@lj970926 lj970926 dismissed stale reviews from RuohengMa and ykkk2333 via c2ae22a May 11, 2023 05:47
Copy link
Contributor

@RuohengMa RuohengMa left a comment

Choose a reason for hiding this comment

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

LGTM

@houj04 houj04 merged commit 6f28eb7 into PaddlePaddle:develop May 11, 2023
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