diff --git a/paddle/phi/kernels/reduce_all_kernel.cc b/paddle/phi/kernels/reduce_all_kernel.cc index e1651f12c1c84..9e799f0d219fc 100644 --- a/paddle/phi/kernels/reduce_all_kernel.cc +++ b/paddle/phi/kernels/reduce_all_kernel.cc @@ -25,6 +25,13 @@ void AllKernel(const Context& dev_ctx, const std::vector& dims, bool keep_dim, DenseTensor* out) { + auto x_dim = x.dims(); + for (int i = 0; i < x_dim.size(); i++) { + PADDLE_ENFORCE_LT(0, + x_dim[i], + errors::InvalidArgument( + "The dims of Input(X) should be greater than 0.")); + } bool reduce_all = recompute_reduce_all(x, dims); AllRawKernel(dev_ctx, x, dims, keep_dim, reduce_all, out); } diff --git a/python/paddle/fluid/tests/unittests/test_reduce_op.py b/python/paddle/fluid/tests/unittests/test_reduce_op.py index 67bae134dc16b..14c1fef545f87 100644 --- a/python/paddle/fluid/tests/unittests/test_reduce_op.py +++ b/python/paddle/fluid/tests/unittests/test_reduce_op.py @@ -1250,6 +1250,18 @@ def test_dygraph(self): paddle.enable_static() +class TestAllZeroError(unittest.TestCase): + def test_errors(self): + with paddle.fluid.dygraph.guard(): + + def test_0_size(): + array = np.array([], dtype=np.float32) + x = paddle.to_tensor(np.reshape(array, [0, 0, 0]), dtype='bool') + paddle.all(x, axis=1) + + self.assertRaises(ValueError, test_0_size) + + if __name__ == '__main__': paddle.enable_static() unittest.main()