Skip to content

Commit e0d75d2

Browse files
DanielSun11co63oc
authored andcommitted
[0-size Tensor No. 233、238、276、281 、324] Add 0-size Tensor support for zeropad2d、nonzero、__getitem__、__setitem__ API (PaddlePaddle#74057)
* fix nonzero dtype error * fix __geitem__ and zeropad2d * add zeropad2d unittest
1 parent 7fede19 commit e0d75d2

File tree

6 files changed

+19
-4
lines changed

6 files changed

+19
-4
lines changed

paddle/phi/kernels/cpu/nonzero_kernel.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void NonZeroKernel(const Context& dev_ctx,
5656
const int rank = dims.size();
5757

5858
if (numel == 0) {
59-
dev_ctx.template Alloc<T>(out);
59+
dev_ctx.template Alloc<int64_t>(out);
6060
return;
6161
}
6262

paddle/phi/kernels/gpu/gather_grad_kernel.cu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void GatherGradKernel(const Context& dev_ctx,
3131
const Scalar& axis,
3232
DenseTensor* x_grad) {
3333
// x [4, 2], index [2, 0], out [2, 0], x_grad [4, 2]
34-
if (out_grad.numel() == 0) {
34+
if (out_grad.numel() == 0 || (x_grad && x_grad->numel() == 0)) {
3535
if (x_grad) {
3636
phi::Full<T, Context>(
3737
dev_ctx, phi::IntArray(common::vectorize(x_grad->dims())), 0, x_grad);

paddle/phi/kernels/gpu/nonzero_kernel.cu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void NonZeroKernel(const Context &dev_ctx,
6666
const DenseTensor &condition,
6767
DenseTensor *out) {
6868
if (condition.numel() == 0) {
69-
dev_ctx.template Alloc<T>(out);
69+
dev_ctx.template Alloc<int64_t>(out);
7070
return;
7171
}
7272
DenseTensor in_data;

paddle/phi/kernels/gpu/pad3d_grad_kernel.cu

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,8 @@ PD_REGISTER_KERNEL(pad3d_grad,
690690
phi::Pad3dGradKernel,
691691
float,
692692
double,
693+
int,
694+
int64_t,
693695
phi::dtype::float16,
694696
phi::dtype::bfloat16,
695697
phi::dtype::complex<float>,

paddle/phi/kernels/xpu/nonzero_kernel.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void NonZeroKernel(const Context& dev_ctx,
3333
using XPUType = typename XPUTypeTrait<T>::Type;
3434

3535
if (numel == 0) {
36-
dev_ctx.template Alloc<T>(out);
36+
dev_ctx.template Alloc<int64_t>(out);
3737
return;
3838
}
3939

test/legacy_test/test_zeropad2d.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,19 @@ def test_support_pad4(self):
112112
ret_res = zeropad2d(x_tensor, pad_tensor).numpy()
113113
np.testing.assert_allclose(expect_res, ret_res, rtol=1e-05)
114114

115+
def test_support_pad5(self):
116+
"""
117+
test the zero size Tensor.
118+
"""
119+
pad = (1, 2, 3, 4)
120+
x = np.random.randint(-255, 255, size=[0, 2, 3])
121+
x_tensor = to_tensor(x, stop_gradient=False)
122+
ret_res = zeropad2d(x_tensor, pad)
123+
ret_res.backward()
124+
np.testing.assert_allclose(
125+
x_tensor.shape, x_tensor.grad.shape, rtol=1e-05
126+
)
127+
115128

116129
class TestZeroPad2DLayer(unittest.TestCase):
117130
"""

0 commit comments

Comments
 (0)