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

[Zero-Dim] Add Expand/Expand_as/Top_k for XPU to support Zero Dim Input. #50947

Merged
merged 29 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
541bdcc
Add unitest from shilong
yunyaoXYY Feb 15, 2023
592b3a0
Add kernel code from shilong
yunyaoXYY Feb 15, 2023
c5f19b9
fix codestyle
yunyaoXYY Feb 15, 2023
2e22a3e
add broadcast_shape test
yunyaoXYY Feb 15, 2023
42108bf
fix unitest
yunyaoXYY Feb 16, 2023
77d523a
fix unitests
yunyaoXYY Feb 16, 2023
48ac230
fix unitest
yunyaoXYY Feb 16, 2023
83b9eb3
add 0D grad support
yunyaoXYY Feb 17, 2023
7b77105
add 0D grad support
yunyaoXYY Feb 17, 2023
bbfe4f2
add 0D grad support
yunyaoXYY Feb 17, 2023
adaa85c
fix 0D tensor
yunyaoXYY Feb 17, 2023
0595069
fix 0D
yunyaoXYY Feb 19, 2023
d2bfc23
fix xpu 0D
yunyaoXYY Feb 19, 2023
26118b4
fix expand kernel
yunyaoXYY Feb 21, 2023
6624842
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yunyaoXYY Feb 21, 2023
91ab26c
fix xpu expand
yunyaoXYY Feb 21, 2023
1ab5d39
Fix 0D kernel
yunyaoXYY Feb 22, 2023
2e749e2
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yunyaoXYY Feb 22, 2023
716221e
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yunyaoXYY Feb 23, 2023
27ea99f
fix 0D
yunyaoXYY Feb 23, 2023
29f368f
fix 0D
yunyaoXYY Feb 23, 2023
46fe2c0
fix 0D
yunyaoXYY Feb 23, 2023
8a1a6f4
fix 0D
yunyaoXYY Feb 23, 2023
7d8a630
fix XPU top_k
yunyaoXYY Feb 24, 2023
3d376f8
cancel the modify of xpu
yunyaoXYY Feb 24, 2023
5624dc9
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yunyaoXYY Feb 27, 2023
da37b67
add XPU 0D tensor
yunyaoXYY Feb 27, 2023
027423e
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
yunyaoXYY Feb 28, 2023
32508f3
fix 0D
yunyaoXYY Feb 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions paddle/phi/kernels/xpu/expand_as_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ void ExpandAs(const Context& context,
auto vec_in_dims = phi::vectorize<int>(in_dims);
auto diff = target_shape.size() - vec_in_dims.size();
vec_in_dims.insert(vec_in_dims.begin(), diff, 1);

for (size_t i = 0; i < vec_in_dims.size(); ++i) {
PADDLE_ENFORCE_NE(target_shape[i],
0,
Expand All @@ -49,6 +48,19 @@ void ExpandAs(const Context& context,
target_shape[i]));
}
}
if (target_shape.size() == 0) {
phi::DDim out_dims = phi::make_ddim(target_shape);
out->Resize(out_dims);
context.template Alloc<T>(out);

int r = xpu::copy<XPUType>(context.x_context(),
reinterpret_cast<const XPUType*>(x.data<T>()),
reinterpret_cast<XPUType*>(out->data<T>()),
x.numel());
PADDLE_ENFORCE_XDNN_SUCCESS(r, "copy");
return;
}

phi::DDim out_dims = phi::make_ddim(target_shape);
out->Resize(out_dims);
context.template Alloc<T>(out);
Expand Down Expand Up @@ -95,7 +107,7 @@ void ExpandAsKernel(const Context& ctx,
rank));
PADDLE_ENFORCE_GE(
rank,
1,
0,
phi::errors::InvalidArgument("The rank (%d) of the input 'x' for "
"expand_as_v2 op must be positive.",
rank));
Expand Down
5 changes: 5 additions & 0 deletions paddle/phi/kernels/xpu/expand_grad_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ void ExpandGradKernel(const Context& ctx,
in_grad_dims.insert(
in_grad_dims.begin(), out_grad.dims().size() - in_grad->dims().size(), 1);

// Two zero
if (out_grad_dims.size() == 0 && in_grad_dims.size() == 0) {
return;
}

int r = xpu::expand_grad<XPUType>(
ctx.x_context(),
reinterpret_cast<const XPUType*>(out_grad.data<T>()),
Expand Down
14 changes: 13 additions & 1 deletion paddle/phi/kernels/xpu/expand_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void ExpandKernel(const Context& ctx,
auto rank = x.dims().size();
PADDLE_ENFORCE_GE(
rank,
1,
0,
phi::errors::InvalidArgument(
"The rank of the input 'X' for expand_v2_npu op must be positive, "
"but the value received is %d.",
Expand All @@ -94,6 +94,18 @@ void ExpandKernel(const Context& ctx,
shape_size,
rank));

if (shape_size == 0) {
phi::DDim out_dims = phi::make_ddim(final_expand_shape);
out->Resize(out_dims);
ctx.template Alloc<T>(out);

int r = xpu::copy<XPUType>(ctx.x_context(),
reinterpret_cast<const XPUType*>(x.data<T>()),
reinterpret_cast<XPUType*>(out->data<T>()),
x.numel());
PADDLE_ENFORCE_XDNN_SUCCESS(r, "copy");
return;
}
DDim out_dims = phi::make_ddim(final_expand_shape);
out->Resize(out_dims);
ctx.template Alloc<T>(out);
Expand Down
12 changes: 11 additions & 1 deletion paddle/phi/kernels/xpu/top_k_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include "paddle/phi/backends/xpu/enforce_xpu.h"
#include "paddle/phi/core/kernel_registry.h"

#include "paddle/phi/kernels/funcs/math_function.h"
namespace phi {

template <typename T, typename Context>
Expand Down Expand Up @@ -49,7 +49,17 @@ void TopkKernel(const Context& dev_ctx,
errors::External(
"XPU API does not support smallest topk operation currently."
" Operator will be supported in future update."));
if (in_dims.size() == 0) {
int r = xpu::copy<XPUType>(dev_ctx.x_context(),
reinterpret_cast<const XPUType*>(x.data<T>()),
reinterpret_cast<XPUType*>(out->data<T>()),
x.numel());
PADDLE_ENFORCE_XDNN_SUCCESS(r, "copy");

phi::funcs::set_constant(dev_ctx, indices, 0);

return;
}
if (axis < 0) axis += in_dims.size();

size_t k = k_scalar.to<int>();
Expand Down
132 changes: 132 additions & 0 deletions python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,138 @@ def setUp(self):
paddle.disable_static()
self.x = paddle.rand([])

def test_expand(self):
# case1
x = paddle.full([], 1, 'float32')
x.stop_gradient = False
out = paddle.expand(x, shape=[1])
out.retain_grads()
out.backward()

self.assertEqual(out.shape, [1])
np.testing.assert_allclose(out, 1.0)
self.assertEqual(x.grad.shape, [])
np.testing.assert_allclose(x.grad, 1.0)
self.assertEqual(out.grad.shape, [1])
np.testing.assert_allclose(out.grad, 1.0)

# case2
x1 = paddle.full([], 1, 'float32')
x1.stop_gradient = False
out1 = paddle.expand(x1, shape=[])
out1.retain_grads()
out1.backward()

self.assertEqual(out1.shape, [])
np.testing.assert_allclose(out1, 1.0)
self.assertEqual(x1.grad.shape, [])
np.testing.assert_allclose(x1.grad, 1.0)
self.assertEqual(out1.grad.shape, [])
np.testing.assert_allclose(out1.grad, 1.0)

# case3
x2 = paddle.full([], 1, 'float32')
x2.stop_gradient = False
out2 = paddle.expand(x2, shape=[1, 1])
out2.retain_grads()
out2.backward()

self.assertEqual(out2.shape, [1, 1])
np.testing.assert_allclose(out2, 1.0)
self.assertEqual(x2.grad.shape, [])
np.testing.assert_allclose(x2.grad, 1.0)
self.assertEqual(out2.grad.shape, [1, 1])
np.testing.assert_allclose(out2.grad, 1.0)

# case4
x3 = paddle.full([], 1, 'float32')
x3.stop_gradient = False
out3 = paddle.expand(x3, shape=[3, 3])
out3.retain_grads()
out3.backward()

self.assertEqual(out3.shape, [3, 3])
np.testing.assert_allclose(out3, 1.0)
self.assertEqual(x3.grad.shape, [])
np.testing.assert_allclose(x3.grad, 9.0)
self.assertEqual(out3.grad.shape, [3, 3])
np.testing.assert_allclose(out3.grad, 1.0)

def test_expand_as(self):
x = paddle.full([], 1, 'float32')
x.stop_gradient = False
y = paddle.full([], 1, 'float32')
y.stop_gradient = False
out = paddle.expand_as(x, y)
out.backward()
self.assertEqual(x.shape, [])
self.assertEqual(x.item(), 1.0)
self.assertEqual(x.grad.shape, [])
self.assertEqual(x.grad.item(), 1.0)
self.assertEqual(out.shape, [])
self.assertEqual(out.item(), 1.0)
self.assertEqual(out.grad, None)

x1 = paddle.full([], 1, 'float32')
x1.stop_gradient = False
y1 = paddle.full([1], 1, 'float32')
out1 = paddle.expand_as(x1, y1)
out1.backward()
self.assertEqual(x1.shape, [])
self.assertEqual(x1.item(), 1.0)
self.assertEqual(x1.grad.shape, [])
self.assertEqual(x1.grad.item(0), 1.0)
self.assertEqual(out1.shape, [1])
self.assertEqual(out1.item(0), 1.0)
self.assertEqual(out1.grad, None)

x2 = paddle.full([], 1, 'float32')
x2.stop_gradient = False
y2 = paddle.full([3, 3], 1, 'float32')
out2 = paddle.expand_as(x2, y2)
out2.backward()
self.assertEqual(x2.shape, [])
self.assertEqual(x2.item(), 1.0)
self.assertEqual(x2.grad.shape, [])
self.assertEqual(x2.grad.item(0), 9.0)
self.assertEqual(out2.shape, [3, 3])
self.assertEqual(out2.item(0), 1.0)
self.assertEqual(out2.grad, None)

def test_top_k(self):
x = paddle.full([], 1, 'float32')
x.stop_gradient = False
out, indices = paddle.topk(x, k=1, axis=0)
out.retain_grads()
out.backward()
self.assertEqual(indices.shape, [])
self.assertEqual(indices.item(), 0)
self.assertEqual(x.shape, [])
self.assertEqual(x.item(), 1.0)
self.assertEqual(x.grad.shape, [])
self.assertEqual(x.grad.item(0), 1.0)
self.assertEqual(out.shape, [])
self.assertEqual(out.item(), 1.0)
self.assertEqual(out.grad, 1.0)

x1 = paddle.full([], 1, 'float32')
x1.stop_gradient = False
out1, indices1 = paddle.topk(x1, k=1, axis=-1)
out1.retain_grads()
out1.backward()
self.assertEqual(indices1.shape, [])
self.assertEqual(indices1.item(), 0)
self.assertEqual(x1.shape, [])
self.assertEqual(x1.item(), 1.0)
self.assertEqual(x.grad.shape, [])
self.assertEqual(x.grad.item(0), 1.0)
self.assertEqual(out1.shape, [])
self.assertEqual(out1.item(), 1.0)
self.assertEqual(out1.grad, 1.0)

with self.assertRaises(ValueError):
tmp = paddle.topk(x1, k=1, axis=2)

def test_argmin(self):
x = paddle.rand([])
out1 = paddle.argmin(x, 0)
Expand Down