From 09926af166b060c9a9845c309110d3baa82921fd Mon Sep 17 00:00:00 2001 From: liyulingyue <852433440@qq.com> Date: Fri, 20 Jan 2023 19:11:42 +0800 Subject: [PATCH] add static check --- paddle/phi/infermeta/unary.cc | 5 +++++ python/paddle/fluid/tests/unittests/test_matrix_power_op.py | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index 5a7b2cf16a1f8..ef5d3c7c2eee2 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -1889,6 +1889,11 @@ void MatrixPowerInferMeta(const MetaTensor& x, int n, MetaTensor* out) { "The Input(X) should have at least 2 dimensions. But " "received a %d dimension tensor.", n_dim)); + for (int i = 0; i < n_dim; ++i) + PADDLE_ENFORCE_NE( + dims[i], + 0, + phi::errors::InvalidArgument("The size of Input(X) should not be 0.")); PADDLE_ENFORCE_EQ(dims[n_dim - 2], dims[n_dim - 1], phi::errors::InvalidArgument( diff --git a/python/paddle/fluid/tests/unittests/test_matrix_power_op.py b/python/paddle/fluid/tests/unittests/test_matrix_power_op.py index 7a3d81d16aafd..850e30d194921 100644 --- a/python/paddle/fluid/tests/unittests/test_matrix_power_op.py +++ b/python/paddle/fluid/tests/unittests/test_matrix_power_op.py @@ -312,7 +312,11 @@ def test_errors(self): input = fluid.data(name="input_3", shape=[4, 5], dtype="float32") self.assertRaises(ValueError, paddle.linalg.matrix_power, input, 2) - # The size of input should not be 0 + # The size of input should not be 0 (static graph) + input = fluid.data(name="input_4", shape=[1, 1, 0, 0], dtype="float32") + self.assertRaises(ValueError, paddle.linalg.matrix_power, input, 2) + + # The size of input should not be 0 (dynamic graph) with paddle.fluid.dygraph.guard(): input = paddle.to_tensor( paddle.uniform([1, 1, 0, 0]).astype('float32')