diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index 417b6f358fcaabc0cacd432ec91a9057e2045f6f..fb5cb870cad77f78014a0086bd0dbf88396ce8b0 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -2272,7 +2272,19 @@ void ModeInferMeta(const MetaTensor& x, input_dims.size(), 0, errors::InvalidArgument("input of ModeOp must have >= 0d shape")); - if (axis < 0) axis += dim_size; + if (axis < 0) { + axis += dim_size; + if (dim_size != 0) { + PADDLE_ENFORCE_GE(axis, + 0, + phi::errors::InvalidArgument( + "the axis must be [-%d, %d), but received %d .", + dim_size, + dim_size, + axis - dim_size)); + } + } + std::vector dimvec; for (int64_t i = 0; i < axis; i++) { dimvec.emplace_back(input_dims[i]); diff --git a/test/legacy_test/test_zero_dim_tensor.py b/test/legacy_test/test_zero_dim_tensor.py index 7c814a839279a45d02d3d74e39cdb0a594085a42..2f02a00209f5625b75e274796dabf8e0cd1abe26 100644 --- a/test/legacy_test/test_zero_dim_tensor.py +++ b/test/legacy_test/test_zero_dim_tensor.py @@ -1245,21 +1245,6 @@ class TestSundryAPI(unittest.TestCase): self.assertEqual(x1.grad.shape, [5]) def test_mode(self): - # 1) x is 0D - x = paddle.randn([]) - x.stop_gradient = False - out, index = paddle.mode(x) - out.backward() - - self.assertEqual(out.shape, []) - self.assertEqual(out, x) - self.assertEqual(index.shape, []) - self.assertEqual(index, 0) - - self.assertEqual(x.grad.shape, []) - self.assertEqual(x.grad, 1.0) - - # 2) x is 1D x1 = paddle.randn([5]) x1.stop_gradient = False out1, index1 = paddle.mode(x1)