From 888dcc170a31a1b7e48e0f7b05e1d9428ee2600f Mon Sep 17 00:00:00 2001 From: zoooo0820 Date: Fri, 16 Dec 2022 11:49:47 +0000 Subject: [PATCH 1/6] add 0-d support for paddle.kthvalue --- paddle/phi/infermeta/unary.cc | 26 +++++++++++-------- .../phi/kernels/cpu/kthvalue_grad_kernel.cc | 10 ++++++- paddle/phi/kernels/cpu/kthvalue_kernel.cc | 17 +++++++++++- .../phi/kernels/gpu/kthvalue_grad_kernel.cu | 10 ++++++- paddle/phi/kernels/gpu/kthvalue_kernel.cu | 13 ++++++++++ .../fluid/tests/unittests/test_kthvalue_op.py | 6 +++++ .../tests/unittests/test_zero_dim_tensor.py | 20 ++++++++++++++ 7 files changed, 88 insertions(+), 14 deletions(-) diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index c3b96b813b8c3..2e0adaf46397e 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -1751,14 +1751,18 @@ void KthvalueInferMeta(const MetaTensor& x, dim_size, dim_size, axis)); - PADDLE_ENFORCE_GE(axis, - -dim_size, - phi::errors::InvalidArgument( - "the axis must be [-%d, %d), but received %d .", - dim_size, - dim_size, - axis)); - if (axis < 0) axis += dim_size; + if (dim_size > 0) { + PADDLE_ENFORCE_GE(axis, + -dim_size, + phi::errors::InvalidArgument( + "the axis must be [-%d, %d), but received %d .", + dim_size, + dim_size, + axis)); + } + if (axis < 0) { + axis = (dim_size > 0) ? axis + dim_size : 0; + } PADDLE_ENFORCE_GE( k, 1, @@ -1766,8 +1770,8 @@ void KthvalueInferMeta(const MetaTensor& x, "the k in the kthvalue must >= 1, but received %d .", k)); PADDLE_ENFORCE_GE( input_dims.size(), - 1, - phi::errors::InvalidArgument("input of kthvalue must have >= 1d shape")); + 0, + phi::errors::InvalidArgument("input of kthvalue must have >= 0d shape")); if (config.is_runtime) { PADDLE_ENFORCE_GE( input_dims[axis], @@ -1781,7 +1785,7 @@ void KthvalueInferMeta(const MetaTensor& x, for (int64_t i = 0; i < axis; i++) { dimvec.emplace_back(input_dims[i]); } - if (keepdim) { + if (keepdim && dim_size > 0) { dimvec.emplace_back(static_cast(1)); } for (int64_t i = axis + 1; i < dim_size; i++) { diff --git a/paddle/phi/kernels/cpu/kthvalue_grad_kernel.cc b/paddle/phi/kernels/cpu/kthvalue_grad_kernel.cc index 386d41984b0ad..459c66fae09e4 100644 --- a/paddle/phi/kernels/cpu/kthvalue_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/kthvalue_grad_kernel.cc @@ -55,6 +55,14 @@ void KthvalueGradKernel(const Context& dev_ctx, DenseTensor* d_x) { auto in_dims = x.dims(); auto out_dims = indices.dims(); + T* x_grad_data = dev_ctx.template Alloc(d_x); + + // For 0D Tensor + if (in_dims.size() == 0) { + phi::funcs::set_constant(dev_ctx, d_x, 1.0); + return; + } + axis = (axis < 0) ? (in_dims.size() + axis) : axis; if (!keepdim) { std::vector tmp_out_shape; @@ -67,7 +75,7 @@ void KthvalueGradKernel(const Context& dev_ctx, } out_dims = phi::make_ddim(tmp_out_shape); } - T* x_grad_data = dev_ctx.template Alloc(d_x); + if (axis == in_dims.size() - 1) { const int64_t input_height = phi::product(phi::slice_ddim(in_dims, 0, in_dims.size() - 1)); diff --git a/paddle/phi/kernels/cpu/kthvalue_kernel.cc b/paddle/phi/kernels/cpu/kthvalue_kernel.cc index 5e436623cae7b..2a59325269569 100644 --- a/paddle/phi/kernels/cpu/kthvalue_kernel.cc +++ b/paddle/phi/kernels/cpu/kthvalue_kernel.cc @@ -81,9 +81,24 @@ void KthvalueKernel(const Context& dev_ctx, DenseTensor* output, DenseTensor* indices) { const auto& in_dims = x.dims(); - if (axis < 0) axis += in_dims.size(); + if (axis < 0) { + axis += in_dims.size(); + } T* output_data = dev_ctx.template Alloc(output); int64_t* indices_data = dev_ctx.template Alloc(indices); + // For 0D Tensor + if (in_dims.size() == 0) { + PADDLE_ENFORCE_EQ(k, + 1, + phi::errors::InvalidArgument( + "the k in the kthvalue must less equal than the " + "elemenents number of the input X, but received %d .", + k)); + + phi::Copy(dev_ctx, x, dev_ctx.GetPlace(), false, output); + phi::funcs::set_constant(dev_ctx, indices, 0); + return; + } auto out_dims = output->dims(); if (axis == in_dims.size() - 1) { const int64_t& input_height = diff --git a/paddle/phi/kernels/gpu/kthvalue_grad_kernel.cu b/paddle/phi/kernels/gpu/kthvalue_grad_kernel.cu index 044fe7c621ab1..69c65aa83957a 100644 --- a/paddle/phi/kernels/gpu/kthvalue_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/kthvalue_grad_kernel.cu @@ -16,6 +16,7 @@ #include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/funcs/math_function.h" #include "paddle/phi/kernels/funcs/top_k_function_cuda.h" namespace phi { @@ -43,8 +44,15 @@ void KthvalueGradKernel(const Context& dev_ctx, DenseTensor* d_x) { const auto& in_dims = x.dims(); auto out_dims = indices.dims(); - if (axis < 0) axis += in_dims.size(); T* x_grad_data = dev_ctx.template Alloc(d_x); + // For 0D Tensor + if (in_dims.size() == 0) { + phi::funcs::set_constant(dev_ctx, d_x, 1.0); + return; + } + + if (axis < 0) axis += in_dims.size(); + const T* out_grad_data = d_out.data(); const int64_t* indices_data = indices.data(); int pre, n, post; diff --git a/paddle/phi/kernels/gpu/kthvalue_kernel.cu b/paddle/phi/kernels/gpu/kthvalue_kernel.cu index b04cea2ceb55e..1340eb0d555ce 100644 --- a/paddle/phi/kernels/gpu/kthvalue_kernel.cu +++ b/paddle/phi/kernels/gpu/kthvalue_kernel.cu @@ -167,6 +167,19 @@ void KthvalueKernel(const Context& dev_ctx, T* output_data = dev_ctx.template Alloc(output); int64_t* indices_data = dev_ctx.template Alloc(indices); + // For 0D Tensor + if (in_dims.size() == 0) { + PADDLE_ENFORCE_EQ(k, + 1, + phi::errors::InvalidArgument( + "the k in the kthvalue must less equal than the " + "elemenents number of the input X, but received %d .", + k)); + + phi::Copy(dev_ctx, x, dev_ctx.GetPlace(), false, output); + phi::funcs::set_constant(dev_ctx, indices, 0); + return; + } if (axis == in_dims.size() - 1) { const int64_t& input_height = phi::product(phi::slice_ddim(in_dims, 0, in_dims.size() - 1)); diff --git a/python/paddle/fluid/tests/unittests/test_kthvalue_op.py b/python/paddle/fluid/tests/unittests/test_kthvalue_op.py index 276e3f4b8aa9b..93008fd773ac7 100644 --- a/python/paddle/fluid/tests/unittests/test_kthvalue_op.py +++ b/python/paddle/fluid/tests/unittests/test_kthvalue_op.py @@ -177,6 +177,12 @@ def test_dim_range_error(): self.assertRaises(ValueError, test_dim_range_error) + def test_k_error_0_dim_input(): + x_0d = paddle.full([], 1) + x_0d.kthvalue(k=8) + + self.assertRaises(ValueError, test_k_error_0_dim_input) + class TestModeOpInStatic(unittest.TestCase): def setUp(self): 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 9fb6017d446d0..db89a13f025cd 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -712,6 +712,15 @@ def test_scatter_nd(self): self.assertEqual(out.numpy()[3], 2) self.assertEqual(out.grad.shape, [5]) + def test_kthvalue(self): + x = paddle.randn(()) + x.stop_gradient = False + + out = paddle.kthvalue(x, 1) + out.backward() + self.assertEqual(out[0].shape, []) + self.assertEqual(out[1].shape, []) + class TestSundryAPIStatic(unittest.TestCase): def setUp(self): @@ -914,6 +923,17 @@ def test_scatter_nd(self): self.assertEqual(res[0].shape, (5,)) self.assertEqual(res[0][3], 2) + @prog_scope() + def test_kthvalue(self): + x = paddle.full([], 1, 'float32') + out = paddle.kthvalue(x, 1) + paddle.static.append_backward(out[0]) + + prog = paddle.static.default_main_program() + res = self.exe.run(prog, fetch_list=[out]) + self.assertEqual(len(res[0].shape), 0) + self.assertEqual(len(res[0].shape), 0) + # 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 075646315189f887b6605ffd7b68e5d7ccbdd10d Mon Sep 17 00:00:00 2001 From: zoooo0820 Date: Fri, 16 Dec 2022 12:43:20 +0000 Subject: [PATCH 2/6] add 0-d support for paddle.mode --- paddle/phi/infermeta/unary.cc | 37 ++++++++++--------- paddle/phi/kernels/cpu/kthvalue_kernel.cc | 5 +-- paddle/phi/kernels/cpu/mode_grad_kernel.cc | 10 ++++- paddle/phi/kernels/cpu/mode_kernel.cc | 8 ++++ paddle/phi/kernels/gpu/mode_grad_kernel.cu | 7 ++++ paddle/phi/kernels/gpu/mode_kernel.cu | 8 ++++ .../tests/unittests/test_zero_dim_tensor.py | 22 ++++++++++- 7 files changed, 75 insertions(+), 22 deletions(-) diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index 2e0adaf46397e..f7ec91fc04ed4 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -1760,9 +1760,7 @@ void KthvalueInferMeta(const MetaTensor& x, dim_size, axis)); } - if (axis < 0) { - axis = (dim_size > 0) ? axis + dim_size : 0; - } + if (axis < 0) axis += dim_size; PADDLE_ENFORCE_GE( k, 1, @@ -2034,33 +2032,38 @@ void ModeInferMeta(const MetaTensor& x, MetaTensor* indices) { auto input_dims = x.dims(); const int& dim_size = input_dims.size(); - PADDLE_ENFORCE_EQ( - (axis < dim_size) && (axis >= (-1 * dim_size)), - true, - errors::InvalidArgument( - "the axis of ModeOp must be [-%d, %d), but you set axis is %d", - dim_size, - dim_size, - axis)); + PADDLE_ENFORCE_LT(axis, + dim_size, + phi::errors::InvalidArgument( + "the axis must be [-%d, %d), but received %d .", + dim_size, + dim_size, + axis)); + if (dim_size > 0) { + PADDLE_ENFORCE_GE(axis, + -dim_size, + phi::errors::InvalidArgument( + "the axis must be [-%d, %d), but received %d .", + dim_size, + dim_size, + axis)); + } PADDLE_ENFORCE_GE( input_dims.size(), - 1, - errors::InvalidArgument("input of ModeOp must have >= 1d shape")); + 0, + errors::InvalidArgument("input of ModeOp must have >= 0d shape")); if (axis < 0) axis += dim_size; std::vector dimvec; for (int64_t i = 0; i < axis; i++) { dimvec.emplace_back(input_dims[i]); } - if (keepdim) { + if (keepdim && dim_size > 0) { dimvec.emplace_back(static_cast(1)); } for (int64_t i = axis + 1; i < dim_size; i++) { dimvec.emplace_back(input_dims[i]); } DDim dims = phi::make_ddim(dimvec); - PADDLE_ENFORCE_GE(input_dims.size(), - 1, - errors::InvalidArgument("input shape should >= 1d")); out->set_dims(dims); out->share_lod(x); out->set_dtype(x.dtype()); diff --git a/paddle/phi/kernels/cpu/kthvalue_kernel.cc b/paddle/phi/kernels/cpu/kthvalue_kernel.cc index 2a59325269569..a39fc59dcf419 100644 --- a/paddle/phi/kernels/cpu/kthvalue_kernel.cc +++ b/paddle/phi/kernels/cpu/kthvalue_kernel.cc @@ -81,9 +81,8 @@ void KthvalueKernel(const Context& dev_ctx, DenseTensor* output, DenseTensor* indices) { const auto& in_dims = x.dims(); - if (axis < 0) { - axis += in_dims.size(); - } + if (axis < 0) axis += in_dims.size(); + T* output_data = dev_ctx.template Alloc(output); int64_t* indices_data = dev_ctx.template Alloc(indices); // For 0D Tensor diff --git a/paddle/phi/kernels/cpu/mode_grad_kernel.cc b/paddle/phi/kernels/cpu/mode_grad_kernel.cc index 05675cf1ab411..2878e4f047f7a 100644 --- a/paddle/phi/kernels/cpu/mode_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/mode_grad_kernel.cc @@ -17,6 +17,7 @@ #include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/core/tensor_utils.h" +#include "paddle/phi/kernels/funcs/math_function.h" #include "paddle/phi/kernels/funcs/mode.h" namespace phi { @@ -32,9 +33,17 @@ void ModeGradKernel(const Context& dev_ctx, auto in_dims = x.dims(); auto out_dims = indices.dims(); + T* x_grad_data = dev_ctx.template Alloc(x_grad); + // axis < 0, get the real axis axis = (axis < 0) ? (in_dims.size() + axis) : axis; + // For 0D Tensor + if (in_dims.size() == 0) { + phi::funcs::set_constant(dev_ctx, x_grad, 1.0); + return; + } + if (!keepdim) { std::vector tmp_out_shape; for (int i = 0; i < axis; i++) { @@ -46,7 +55,6 @@ void ModeGradKernel(const Context& dev_ctx, } out_dims = phi::make_ddim(tmp_out_shape); } - T* x_grad_data = dev_ctx.template Alloc(x_grad); if (axis == in_dims.size() - 1) { // allocate the memory for the input_grad diff --git a/paddle/phi/kernels/cpu/mode_kernel.cc b/paddle/phi/kernels/cpu/mode_kernel.cc index 6535d1b89af42..00958ccd8647a 100644 --- a/paddle/phi/kernels/cpu/mode_kernel.cc +++ b/paddle/phi/kernels/cpu/mode_kernel.cc @@ -16,6 +16,7 @@ #include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/funcs/math_function.h" #include "paddle/phi/kernels/funcs/mode.h" namespace phi { @@ -34,6 +35,13 @@ void ModeKernel(const Context& dev_ctx, T* output_data = dev_ctx.template Alloc(out); int64_t* indices_data = dev_ctx.template Alloc(indices); + + if (in_dims.size() == 0) { + phi::Copy(dev_ctx, x, dev_ctx.GetPlace(), false, out); + phi::funcs::set_constant(dev_ctx, indices, 0); + return; + } + // if axis is not the last dim, transpose it to the last dim, do the // calculation, then tranpose it back to original axis. if (axis == in_dims.size() - 1) { diff --git a/paddle/phi/kernels/gpu/mode_grad_kernel.cu b/paddle/phi/kernels/gpu/mode_grad_kernel.cu index 43502621c2d3a..e297eb88d0ca0 100644 --- a/paddle/phi/kernels/gpu/mode_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/mode_grad_kernel.cu @@ -16,6 +16,7 @@ #include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/funcs/math_function.h" #include "paddle/phi/kernels/funcs/mode.h" namespace phi { @@ -61,6 +62,12 @@ void ModeGradKernel(const Context& dev_ctx, const T* out_grad_data = out_grad.data(); const int64_t* indices_data = indices.data(); + // For 0D Tensor + if (in_dims.size() == 0) { + phi::funcs::set_constant(dev_ctx, x_grad, 1.0); + return; + } + int pre, n, post; funcs::GetDims(in_dims, axis, &pre, &n, &post); diff --git a/paddle/phi/kernels/gpu/mode_kernel.cu b/paddle/phi/kernels/gpu/mode_kernel.cu index 629b9722cd6bc..dfef96688a0a9 100644 --- a/paddle/phi/kernels/gpu/mode_kernel.cu +++ b/paddle/phi/kernels/gpu/mode_kernel.cu @@ -16,6 +16,7 @@ #include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/funcs/math_function.h" #include "paddle/phi/kernels/funcs/mode.h" namespace phi { @@ -38,6 +39,13 @@ void ModeKernel(const Context& dev_ctx, T* output_data = dev_ctx.template Alloc(out); int64_t* indices_data = dev_ctx.template Alloc(indices); + // For 0D Tensor + if (in_dims.size() == 0) { + phi::Copy(dev_ctx, x, dev_ctx.GetPlace(), false, out); + phi::funcs::set_constant(dev_ctx, indices, 0); + return; + } + if (axis == in_dims.size() - 1) { const int64_t& input_height = phi::product(phi::slice_ddim(in_dims, 0, in_dims.size() - 1)); 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 db89a13f025cd..67e843e14d549 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -717,7 +717,16 @@ def test_kthvalue(self): x.stop_gradient = False out = paddle.kthvalue(x, 1) - out.backward() + out[0].backward() + self.assertEqual(out[0].shape, []) + self.assertEqual(out[1].shape, []) + + def test_mode(self): + x = paddle.randn(()) + x.stop_gradient = False + + out = paddle.mode(x) + out[0].backward() self.assertEqual(out[0].shape, []) self.assertEqual(out[1].shape, []) @@ -934,6 +943,17 @@ def test_kthvalue(self): self.assertEqual(len(res[0].shape), 0) self.assertEqual(len(res[0].shape), 0) + @prog_scope() + def test_mode(self): + x = paddle.full([], 1, 'float32') + out = paddle.mode(x) + paddle.static.append_backward(out[0]) + + prog = paddle.static.default_main_program() + res = self.exe.run(prog, fetch_list=[out]) + self.assertEqual(len(res[0].shape), 0) + self.assertEqual(len(res[0].shape), 0) + # 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 4acae8784ab237937c349e20e96b3286f508f229 Mon Sep 17 00:00:00 2001 From: zoooo0820 Date: Sat, 17 Dec 2022 02:10:43 +0000 Subject: [PATCH 3/6] fix coverage test for device --- .../tests/unittests/test_zero_dim_tensor.py | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 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 67e843e14d549..1d3072c36f4b5 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -713,22 +713,34 @@ def test_scatter_nd(self): self.assertEqual(out.grad.shape, [5]) def test_kthvalue(self): - x = paddle.randn(()) - x.stop_gradient = False + places = ['cpu'] + if paddle.is_compiled_with_cuda(): + places.append('gpu') + for place in places: + paddle.set_device(place) - out = paddle.kthvalue(x, 1) - out[0].backward() - self.assertEqual(out[0].shape, []) - self.assertEqual(out[1].shape, []) + x = paddle.randn(()) + x.stop_gradient = False + + out = paddle.kthvalue(x, 1) + out[0].backward() + self.assertEqual(out[0].shape, []) + self.assertEqual(out[1].shape, []) def test_mode(self): - x = paddle.randn(()) - x.stop_gradient = False + places = ['cpu'] + if paddle.is_compiled_with_cuda(): + places.append('gpu') + for place in places: + paddle.set_device(place) - out = paddle.mode(x) - out[0].backward() - self.assertEqual(out[0].shape, []) - self.assertEqual(out[1].shape, []) + x = paddle.randn(()) + x.stop_gradient = False + + out = paddle.mode(x) + out[0].backward() + self.assertEqual(out[0].shape, []) + self.assertEqual(out[1].shape, []) class TestSundryAPIStatic(unittest.TestCase): From 9c0a1e8833752c79e7d3a478e8cf28b4e968945a Mon Sep 17 00:00:00 2001 From: zoooo0820 Date: Mon, 26 Dec 2022 08:25:10 +0000 Subject: [PATCH 4/6] fix check-bug in windows --- paddle/phi/infermeta/unary.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index f7ec91fc04ed4..991b507ce4b98 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -1770,7 +1770,7 @@ void KthvalueInferMeta(const MetaTensor& x, input_dims.size(), 0, phi::errors::InvalidArgument("input of kthvalue must have >= 0d shape")); - if (config.is_runtime) { + if (dim_size > 0 && config.is_runtime) { PADDLE_ENFORCE_GE( input_dims[axis], k, From 0d7f3f945048a33b1f04c658deb9bea5fd78b3a7 Mon Sep 17 00:00:00 2001 From: zoooo0820 Date: Tue, 3 Jan 2023 11:50:23 +0000 Subject: [PATCH 5/6] change axis check from LT to LE --- paddle/phi/infermeta/unary.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index 991b507ce4b98..40ad4c523cb80 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -1744,7 +1744,7 @@ void KthvalueInferMeta(const MetaTensor& x, MetaConfig config) { auto input_dims = x.dims(); const int& dim_size = input_dims.size(); - PADDLE_ENFORCE_LT(axis, + PADDLE_ENFORCE_LE(axis, dim_size, phi::errors::InvalidArgument( "the axis must be [-%d, %d), but received %d .", @@ -2032,7 +2032,7 @@ void ModeInferMeta(const MetaTensor& x, MetaTensor* indices) { auto input_dims = x.dims(); const int& dim_size = input_dims.size(); - PADDLE_ENFORCE_LT(axis, + PADDLE_ENFORCE_LE(axis, dim_size, phi::errors::InvalidArgument( "the axis must be [-%d, %d), but received %d .", From 2d1f9e7226381f70e531c1647ca938421b0f8175 Mon Sep 17 00:00:00 2001 From: zoooo0820 Date: Fri, 6 Jan 2023 04:24:29 +0000 Subject: [PATCH 6/6] add shape & value check for grad when input is 0d tensor --- .../fluid/tests/unittests/test_zero_dim_tensor.py | 12 ++++++++++++ 1 file changed, 12 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 ffb7ad049e90a..4b77277cf1817 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -733,9 +733,15 @@ def test_kthvalue(self): out = paddle.kthvalue(x, 1) out[0].backward() + + # check shape of output value and indice self.assertEqual(out[0].shape, []) self.assertEqual(out[1].shape, []) + # check grad shape and value + self.assertEqual(x.grad.shape, []) + self.assertTrue(x.grad.numpy() == 1) + def test_mode(self): places = ['cpu'] if paddle.is_compiled_with_cuda(): @@ -748,9 +754,15 @@ def test_mode(self): out = paddle.mode(x) out[0].backward() + + # check shape of output value and indice self.assertEqual(out[0].shape, []) self.assertEqual(out[1].shape, []) + # check grad shape and value + self.assertEqual(x.grad.shape, []) + self.assertTrue(x.grad.numpy() == 1) + def test_scale(self): x = paddle.rand([]) x.stop_gradient = False