From 896fdf6121133c35454b97e8ea726c933f3ebc36 Mon Sep 17 00:00:00 2001 From: Courtesy-Xs Date: Mon, 5 Dec 2022 09:39:37 +0000 Subject: [PATCH 1/7] [Zero-Dim] Support 0D for paddle.diagflat --- paddle/phi/infermeta/unary.cc | 6 ++++-- paddle/phi/kernels/cpu/diag_grad_kernel.cc | 6 +++--- paddle/phi/kernels/cpu/diag_kernel.cc | 6 +++--- paddle/phi/kernels/gpu/diag_grad_kernel.cu | 6 +++--- paddle/phi/kernels/gpu/diag_kernel.cu | 6 +++--- .../tests/unittests/test_zero_dim_tensor.py | 20 +++++++++++++++++++ .../unittests/xpu/test_zero_dim_tensor_xpu.py | 10 ++++++++++ python/paddle/tensor/creation.py | 6 +++--- 8 files changed, 49 insertions(+), 17 deletions(-) diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index f2c3873d81e5c..d9245ef00b001 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -563,8 +563,10 @@ void DiagInferMeta(const MetaTensor& x, MetaTensor* out) { auto x_dims = x.dims(); - if (x_dims.size() == 1UL) { - int64_t size_ = x_dims[0] + std::abs(offset); + if (x_dims.size() <= 1) { + VLOG(0) << "x_dims.size is " << x_dims.size(); + int64_t size_ = (x_dims.size() == 1UL ?: x_dims[0], 1) + std::abs(offset); + VLOG(0) << "altered size is " << size_; out->set_dims({size_, size_}); out->set_dtype(x.dtype()); } else if (x_dims.size() == 2UL) { diff --git a/paddle/phi/kernels/cpu/diag_grad_kernel.cc b/paddle/phi/kernels/cpu/diag_grad_kernel.cc index 616ea753ef1ba..4fabb0ec2b002 100644 --- a/paddle/phi/kernels/cpu/diag_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/diag_grad_kernel.cc @@ -32,9 +32,9 @@ void DiagGradKernel(const Context& dev_ctx, auto dx_dims = x_grad->dims(); auto dout_dims = out_grad.dims(); - if (dx_dims.size() == 1) { - auto dx_length = dx_dims[0]; - int dx_stride = phi::funcs::ComputeStride(0, dx_dims); + if (dx_dims.size() <= 1) { + auto dx_length = (dx_dims.size() == 1 ?: dx_dims[0], int64_t(1)); + int dx_stride = 1; auto dout_stride_0 = phi::funcs::ComputeStride(0, dout_dims); auto dout_stride_1 = phi::funcs::ComputeStride(1, dout_dims); diff --git a/paddle/phi/kernels/cpu/diag_kernel.cc b/paddle/phi/kernels/cpu/diag_kernel.cc index 4b060f0372a5b..fc5d454a46447 100644 --- a/paddle/phi/kernels/cpu/diag_kernel.cc +++ b/paddle/phi/kernels/cpu/diag_kernel.cc @@ -33,12 +33,12 @@ void DiagKernel(const Context& dev_ctx, auto out_dims = out->dims(); int64_t i; - if (x_dims.size() == 1) { + if (x_dims.size() <= 1) { phi::funcs::SetConstant set_padding_value; set_padding_value(dev_ctx, out, static_cast(padding_value)); - auto x_length = x_dims[0]; - const int& x_stride = phi::funcs::ComputeStride(0, x_dims); + auto x_length = (x_dims.size() == 1UL ?: x_dims[0], int64_t(1)); + const int& x_stride = 1; auto out_stride_0 = phi::funcs::ComputeStride(0, out_dims); auto out_stride_1 = phi::funcs::ComputeStride(1, out_dims); diff --git a/paddle/phi/kernels/gpu/diag_grad_kernel.cu b/paddle/phi/kernels/gpu/diag_grad_kernel.cu index 5a579ecc27b7f..eeab730c402c6 100644 --- a/paddle/phi/kernels/gpu/diag_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/diag_grad_kernel.cu @@ -73,10 +73,10 @@ void DiagGradKernel(const Context& dev_ctx, return std::tuple{block_size, grid_size}; }; - if (dx_dims.size() == 1) { - auto dx_length = dx_dims[0]; + if (dx_dims.size() <= 1) { + auto dx_length = (dx_dims.size() == 1 ?: dx_dims[0], int64_t(1)); auto size = (offset > 0) ? dx_length + offset : dx_length - offset; - int dx_stride = phi::funcs::ComputeStride(0, dx_dims); + int dx_stride = 1; if (size > 0) { auto dout_stride_0 = phi::funcs::ComputeStride(0, dout_dims); auto dout_stride_1 = phi::funcs::ComputeStride(1, dout_dims); diff --git a/paddle/phi/kernels/gpu/diag_kernel.cu b/paddle/phi/kernels/gpu/diag_kernel.cu index 95d3d3365d91b..4f58711ffccf2 100644 --- a/paddle/phi/kernels/gpu/diag_kernel.cu +++ b/paddle/phi/kernels/gpu/diag_kernel.cu @@ -77,13 +77,13 @@ void DiagKernel(const Context& dev_ctx, return std::tuple{block_size, grid_size}; }; - if (x_dims.size() == 1) { + if (x_dims.size() <= 1) { phi::funcs::SetConstant set_padding_value; set_padding_value(dev_ctx, out, static_cast(padding_value)); - auto x_length = x_dims[0]; + auto x_length = (x_dims.size() == 1UL ?: x_dims[0], int64_t(1)); auto size = (offset > 0) ? x_length + offset : x_length - offset; - const int& x_stride = phi::funcs::ComputeStride(0, x_dims); + const int& x_stride = 1; if (size > 0) { const auto& out_stride_0 = phi::funcs::ComputeStride(0, out_dims); const auto& out_stride_1 = phi::funcs::ComputeStride(1, out_dims); diff --git a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py index 5911b496a32d2..d991620e6d090 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -596,6 +596,16 @@ def test_searchsorted(self): self.assertEqual(out.shape, []) self.assertEqual(out.numpy(), 0) + def test_diagflat(self): + x = paddle.rand([]) + x.stop_gradient = False + out = paddle.diagflat(x, 1) + out.backward() + + self.assertEqual(out.shape, [2, 2]) + self.assertEqual(out.grad.shape, [2, 2]) + self.assertEqual(x.grad.shape, []) + class TestSundryAPIStatic(unittest.TestCase): def setUp(self): @@ -677,6 +687,16 @@ def test_searchsorted(self): self.assertEqual(res[0].shape, ()) self.assertEqual(res[0], 0) + @prog_scope() + def test_diagflat(self): + x = paddle.rand([]) + out = paddle.diagflat(x, 1) + paddle.static.append_backward(out) + + prog = paddle.static.default_main_program() + res = self.exe.run(prog, fetch_list=[out]) + self.assertEqual(res[0].shape, (2, 2)) + # Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest. class TestNoBackwardAPI(unittest.TestCase): diff --git a/python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py b/python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py index 3be5c315f3bcc..9728d769cddd0 100644 --- a/python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py +++ b/python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py @@ -424,6 +424,16 @@ def test_searchsorted(self): self.assertEqual(out.shape, []) self.assertEqual(out.numpy(), 0) + def test_diagflat(self): + x = paddle.rand([]) + x.stop_gradient = False + out = paddle.diagflat(x, 1) + out.backward() + + self.assertEqual(out.shape, [2, 2]) + self.assertEqual(out.grad.shape, [2, 2]) + self.assertEqual(x.grad.shape, []) + # Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest. class TestNoBackwardAPI(unittest.TestCase): diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index ddde2a68988b9..1004288ebb955 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -1347,14 +1347,14 @@ def diagflat(x, offset=0, name=None): """ padding_value = 0 if in_dygraph_mode(): - if len(x.shape) == 1: + if len(x.shape) <= 1: return _C_ops.diag(x, offset, padding_value) else: y = _C_ops.flatten(x, 0, -1) return _C_ops.diag(y, offset, padding_value) if _in_legacy_dygraph(): - if len(x.shape) == 1: + if len(x.shape) <= 1: return _legacy_C_ops.diag_v2( x, "offset", offset, "padding_value", padding_value ) @@ -1377,7 +1377,7 @@ def diagflat(x, offset=0, name=None): out1_shape = helper.create_variable_for_type_inference(x.dtype) out2 = helper.create_variable_for_type_inference(dtype=x.dtype) - if len(x.shape) == 1: + if len(x.shape) <= 1: helper.append_op( type='diag_v2', inputs={'X': x}, From 144bc162070afacc7c87cedc502fd348f848e763 Mon Sep 17 00:00:00 2001 From: Courtesy-Xs Date: Mon, 5 Dec 2022 09:43:14 +0000 Subject: [PATCH 2/7] delete some extra vlog for debug --- paddle/phi/infermeta/unary.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index d9245ef00b001..e53c56e1aa5e7 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -564,9 +564,7 @@ void DiagInferMeta(const MetaTensor& x, auto x_dims = x.dims(); if (x_dims.size() <= 1) { - VLOG(0) << "x_dims.size is " << x_dims.size(); int64_t size_ = (x_dims.size() == 1UL ?: x_dims[0], 1) + std::abs(offset); - VLOG(0) << "altered size is " << size_; out->set_dims({size_, size_}); out->set_dtype(x.dtype()); } else if (x_dims.size() == 2UL) { From 06183174d84c6ba2792006fab0e454611e00f5ef Mon Sep 17 00:00:00 2001 From: Courtesy-Xs Date: Mon, 5 Dec 2022 10:04:49 +0000 Subject: [PATCH 3/7] delete test case for fix conflict --- .../tests/unittests/test_zero_dim_tensor.py | 20 ------------------- .../unittests/xpu/test_zero_dim_tensor_xpu.py | 10 ---------- 2 files changed, 30 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py index d991620e6d090..5911b496a32d2 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -596,16 +596,6 @@ def test_searchsorted(self): self.assertEqual(out.shape, []) self.assertEqual(out.numpy(), 0) - def test_diagflat(self): - x = paddle.rand([]) - x.stop_gradient = False - out = paddle.diagflat(x, 1) - out.backward() - - self.assertEqual(out.shape, [2, 2]) - self.assertEqual(out.grad.shape, [2, 2]) - self.assertEqual(x.grad.shape, []) - class TestSundryAPIStatic(unittest.TestCase): def setUp(self): @@ -687,16 +677,6 @@ def test_searchsorted(self): self.assertEqual(res[0].shape, ()) self.assertEqual(res[0], 0) - @prog_scope() - def test_diagflat(self): - x = paddle.rand([]) - out = paddle.diagflat(x, 1) - paddle.static.append_backward(out) - - prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out]) - self.assertEqual(res[0].shape, (2, 2)) - # Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest. class TestNoBackwardAPI(unittest.TestCase): diff --git a/python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py b/python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py index 9728d769cddd0..3be5c315f3bcc 100644 --- a/python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py +++ b/python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py @@ -424,16 +424,6 @@ def test_searchsorted(self): self.assertEqual(out.shape, []) self.assertEqual(out.numpy(), 0) - def test_diagflat(self): - x = paddle.rand([]) - x.stop_gradient = False - out = paddle.diagflat(x, 1) - out.backward() - - self.assertEqual(out.shape, [2, 2]) - self.assertEqual(out.grad.shape, [2, 2]) - self.assertEqual(x.grad.shape, []) - # Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest. class TestNoBackwardAPI(unittest.TestCase): From 8515ff067fb66521ad7ca609543836eb4e0e4289 Mon Sep 17 00:00:00 2001 From: Courtesy-Xs Date: Mon, 5 Dec 2022 10:09:12 +0000 Subject: [PATCH 4/7] add 0D Tensor input test case for diagflat --- .../tests/unittests/test_zero_dim_tensor.py | 20 +++++++++++++++++++ .../unittests/xpu/test_zero_dim_tensor_xpu.py | 10 ++++++++++ 2 files changed, 30 insertions(+) diff --git a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py index e7381350624b9..0fc1ba6e0863d 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -653,6 +653,16 @@ def test_scatter_XD(self): self.assertEqual(out.numpy()[1][i], updates.numpy()[i]) self.assertEqual(out.grad.shape, [2, 3]) + def test_diagflat(self): + x = paddle.rand([]) + x.stop_gradient = False + out = paddle.diagflat(x, 1) + out.backward() + + self.assertEqual(out.shape, [2, 2]) + self.assertEqual(out.grad.shape, [2, 2]) + self.assertEqual(x.grad.shape, []) + class TestSundryAPIStatic(unittest.TestCase): def setUp(self): @@ -796,6 +806,16 @@ def test_scatter_XD(self): for i in range(3): self.assertEqual(res[0][1][i], 4) + @prog_scope() + def test_diagflat(self): + x = paddle.rand([]) + out = paddle.diagflat(x, 1) + paddle.static.append_backward(out) + + prog = paddle.static.default_main_program() + res = self.exe.run(prog, fetch_list=[out]) + self.assertEqual(res[0].shape, (2, 2)) + # Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest. class TestNoBackwardAPI(unittest.TestCase): diff --git a/python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py b/python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py index b07043689f7fe..a455ca256177d 100644 --- a/python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py +++ b/python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py @@ -475,6 +475,16 @@ def test_scatter_XD(self): for i in range(3): self.assertEqual(out.numpy()[1][i], updates.numpy()[i]) + def test_diagflat(self): + x = paddle.rand([]) + x.stop_gradient = False + out = paddle.diagflat(x, 1) + out.backward() + + self.assertEqual(out.shape, [2, 2]) + self.assertEqual(out.grad.shape, [2, 2]) + self.assertEqual(x.grad.shape, []) + # Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest. class TestNoBackwardAPI(unittest.TestCase): From c918623fac9e5a488171f58631b236cdcbfee205 Mon Sep 17 00:00:00 2001 From: Courtesy-Xs Date: Tue, 6 Dec 2022 02:09:06 +0000 Subject: [PATCH 5/7] fix syntax error --- paddle/phi/infermeta/unary.cc | 2 +- paddle/phi/kernels/cpu/diag_grad_kernel.cc | 2 +- paddle/phi/kernels/cpu/diag_kernel.cc | 2 +- paddle/phi/kernels/gpu/diag_grad_kernel.cu | 2 +- paddle/phi/kernels/gpu/diag_kernel.cu | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index 6a7b31317af84..c3b96b813b8c3 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -564,7 +564,7 @@ void DiagInferMeta(const MetaTensor& x, auto x_dims = x.dims(); if (x_dims.size() <= 1) { - int64_t size_ = (x_dims.size() == 1UL ?: x_dims[0], 1) + std::abs(offset); + int64_t size_ = (x_dims.size() == 1UL ? x_dims[0] : 1) + std::abs(offset); out->set_dims({size_, size_}); out->set_dtype(x.dtype()); } else if (x_dims.size() == 2UL) { diff --git a/paddle/phi/kernels/cpu/diag_grad_kernel.cc b/paddle/phi/kernels/cpu/diag_grad_kernel.cc index 4fabb0ec2b002..13d3d679ff006 100644 --- a/paddle/phi/kernels/cpu/diag_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/diag_grad_kernel.cc @@ -33,7 +33,7 @@ void DiagGradKernel(const Context& dev_ctx, auto dout_dims = out_grad.dims(); if (dx_dims.size() <= 1) { - auto dx_length = (dx_dims.size() == 1 ?: dx_dims[0], int64_t(1)); + auto dx_length = (dx_dims.size() == 1 ? dx_dims[0] : int64_t(1)); int dx_stride = 1; auto dout_stride_0 = phi::funcs::ComputeStride(0, dout_dims); diff --git a/paddle/phi/kernels/cpu/diag_kernel.cc b/paddle/phi/kernels/cpu/diag_kernel.cc index fc5d454a46447..1576d80b15206 100644 --- a/paddle/phi/kernels/cpu/diag_kernel.cc +++ b/paddle/phi/kernels/cpu/diag_kernel.cc @@ -37,7 +37,7 @@ void DiagKernel(const Context& dev_ctx, phi::funcs::SetConstant set_padding_value; set_padding_value(dev_ctx, out, static_cast(padding_value)); - auto x_length = (x_dims.size() == 1UL ?: x_dims[0], int64_t(1)); + auto x_length = (x_dims.size() == 1UL ? x_dims[0] : int64_t(1)); const int& x_stride = 1; auto out_stride_0 = phi::funcs::ComputeStride(0, out_dims); diff --git a/paddle/phi/kernels/gpu/diag_grad_kernel.cu b/paddle/phi/kernels/gpu/diag_grad_kernel.cu index eeab730c402c6..39ac78dae0216 100644 --- a/paddle/phi/kernels/gpu/diag_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/diag_grad_kernel.cu @@ -74,7 +74,7 @@ void DiagGradKernel(const Context& dev_ctx, }; if (dx_dims.size() <= 1) { - auto dx_length = (dx_dims.size() == 1 ?: dx_dims[0], int64_t(1)); + auto dx_length = (dx_dims.size() == 1 ? dx_dims[0] : int64_t(1)); auto size = (offset > 0) ? dx_length + offset : dx_length - offset; int dx_stride = 1; if (size > 0) { diff --git a/paddle/phi/kernels/gpu/diag_kernel.cu b/paddle/phi/kernels/gpu/diag_kernel.cu index 4f58711ffccf2..588bb17b79a0d 100644 --- a/paddle/phi/kernels/gpu/diag_kernel.cu +++ b/paddle/phi/kernels/gpu/diag_kernel.cu @@ -81,7 +81,7 @@ void DiagKernel(const Context& dev_ctx, phi::funcs::SetConstant set_padding_value; set_padding_value(dev_ctx, out, static_cast(padding_value)); - auto x_length = (x_dims.size() == 1UL ?: x_dims[0], int64_t(1)); + auto x_length = (x_dims.size() == 1UL ? x_dims[0] : int64_t(1)); auto size = (offset > 0) ? x_length + offset : x_length - offset; const int& x_stride = 1; if (size > 0) { From ad6f08d30da311dc2be82d20d55df8dbb4a2913e Mon Sep 17 00:00:00 2001 From: Courtesy-Xs Date: Tue, 6 Dec 2022 11:57:13 +0000 Subject: [PATCH 6/7] fix test case --- python/paddle/tensor/creation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 52305eb024be1..d597ff6a1317f 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -1486,7 +1486,7 @@ def diagflat(x, offset=0, name=None): return _C_ops.diag(y, offset, padding_value) if _in_legacy_dygraph(): - if len(x.shape) <= 1: + if len(x.shape) == 1: return _legacy_C_ops.diag_v2( x, "offset", offset, "padding_value", padding_value ) From 7ad1099262c5a6ae989281b71d378ce8fbfb2abb Mon Sep 17 00:00:00 2001 From: Courtesy-Xs Date: Tue, 6 Dec 2022 12:36:29 +0000 Subject: [PATCH 7/7] add diagflat test case with different offset --- .../tests/unittests/test_zero_dim_tensor.py | 53 ++++++++++++++----- .../unittests/xpu/test_zero_dim_tensor_xpu.py | 33 +++++++++--- 2 files changed, 67 insertions(+), 19 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py index 0fc1ba6e0863d..eae5528fba244 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -654,14 +654,33 @@ def test_scatter_XD(self): self.assertEqual(out.grad.shape, [2, 3]) def test_diagflat(self): - x = paddle.rand([]) - x.stop_gradient = False - out = paddle.diagflat(x, 1) - out.backward() + x1 = paddle.rand([]) + x2 = paddle.rand([]) + x3 = paddle.rand([]) - self.assertEqual(out.shape, [2, 2]) - self.assertEqual(out.grad.shape, [2, 2]) - self.assertEqual(x.grad.shape, []) + x1.stop_gradient = False + x2.stop_gradient = False + x3.stop_gradient = False + + out1 = paddle.diagflat(x1, 1) + out2 = paddle.diagflat(x2, -1) + out3 = paddle.diagflat(x3, 0) + + out1.backward() + out2.backward() + out3.backward() + + self.assertEqual(out1.shape, [2, 2]) + self.assertEqual(out2.shape, [2, 2]) + self.assertEqual(out3.shape, [1, 1]) + + self.assertEqual(out1.grad.shape, [2, 2]) + self.assertEqual(out2.grad.shape, [2, 2]) + self.assertEqual(out3.grad.shape, [1, 1]) + + self.assertEqual(x1.grad.shape, []) + self.assertEqual(x2.grad.shape, []) + self.assertEqual(x3.grad.shape, []) class TestSundryAPIStatic(unittest.TestCase): @@ -808,13 +827,23 @@ def test_scatter_XD(self): @prog_scope() def test_diagflat(self): - x = paddle.rand([]) - out = paddle.diagflat(x, 1) - paddle.static.append_backward(out) + x1 = paddle.rand([]) + out1 = paddle.diagflat(x1, 1) + paddle.static.append_backward(out1) + + x2 = paddle.rand([]) + out2 = paddle.diagflat(x2, -1) + paddle.static.append_backward(out2) + + x3 = paddle.rand([]) + out3 = paddle.diagflat(x3) + paddle.static.append_backward(out3) prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out]) - self.assertEqual(res[0].shape, (2, 2)) + res1, res2, res3 = self.exe.run(prog, fetch_list=[out1, out2, out3]) + self.assertEqual(res1.shape, (2, 2)) + self.assertEqual(res2.shape, (2, 2)) + self.assertEqual(res3.shape, (1, 1)) # Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest. diff --git a/python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py b/python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py index a455ca256177d..a6f91e5df4c66 100644 --- a/python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py +++ b/python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py @@ -476,14 +476,33 @@ def test_scatter_XD(self): self.assertEqual(out.numpy()[1][i], updates.numpy()[i]) def test_diagflat(self): - x = paddle.rand([]) - x.stop_gradient = False - out = paddle.diagflat(x, 1) - out.backward() + x1 = paddle.rand([]) + x2 = paddle.rand([]) + x3 = paddle.rand([]) - self.assertEqual(out.shape, [2, 2]) - self.assertEqual(out.grad.shape, [2, 2]) - self.assertEqual(x.grad.shape, []) + x1.stop_gradient = False + x2.stop_gradient = False + x3.stop_gradient = False + + out1 = paddle.diagflat(x1, 1) + out2 = paddle.diagflat(x2, -1) + out3 = paddle.diagflat(x3, 0) + + out1.backward() + out2.backward() + out3.backward() + + self.assertEqual(out1.shape, [2, 2]) + self.assertEqual(out2.shape, [2, 2]) + self.assertEqual(out3.shape, [1, 1]) + + self.assertEqual(out1.grad.shape, [2, 2]) + self.assertEqual(out2.grad.shape, [2, 2]) + self.assertEqual(out3.grad.shape, [1, 1]) + + self.assertEqual(x1.grad.shape, []) + self.assertEqual(x2.grad.shape, []) + self.assertEqual(x3.grad.shape, []) # Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest.