diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index 6b5cadc9d2fdddcacb5928c1a18d368bbb442ec3..39ea06c89e6ba4ceb9a9552ed910ad869cff6fa5 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -3654,6 +3654,10 @@ void SplitWithNumInferMeta(const MetaTensor& x, auto input_axis_dim = x.dims().at(axis_value); // step1: get formated sections std::vector sections_vec; + PADDLE_ENFORCE_NE( + num, + 0, + phi::errors::InvalidArgument("Attr(num_or_sections) should not be 0.")); PADDLE_ENFORCE_EQ(input_axis_dim % num, 0, phi::errors::InvalidArgument( diff --git a/python/paddle/fluid/tests/unittests/test_split_op.py b/python/paddle/fluid/tests/unittests/test_split_op.py index d250302165bcbdc936a6e87627586c72f4eae3f5..4153e3e655f675ebabe9ed2a9c8b130a240a54e0 100644 --- a/python/paddle/fluid/tests/unittests/test_split_op.py +++ b/python/paddle/fluid/tests/unittests/test_split_op.py @@ -358,6 +358,14 @@ class TestSplitOpError(unittest.TestCase): self.assertRaises(TypeError, test_axis_type_tensor) + with paddle.fluid.dygraph.guard(): + + def test_0_num_tensor(): + x = paddle.uniform([1, 1, 1], dtype='float32') + paddle.split(x, num_or_sections=0) + + self.assertRaises(ValueError, test_0_num_tensor) + class API_TestSplit(unittest.TestCase): def test_out(self):