From fb74147c6aa3ae8a1256f8e84f46af3632190f44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=98=A5=E4=B9=94?= <83450930+Liyulingyue@users.noreply.github.com> Date: Tue, 31 Jan 2023 10:44:57 +0800 Subject: [PATCH] Fix the div 0 error of matrix_power (#49942) * add zero size check in matrix_power_kernel_impl.h * add zero size check in matrix_power_kernel_impl.h * add zero size check in unittest * bug_fix * bug_fix * bug_fix * bug_fix * bug_fix * bug fix * bug_fix * bug_fix * add static check * delete the dy codes --- paddle/phi/infermeta/unary.cc | 5 +++++ python/paddle/fluid/tests/unittests/test_matrix_power_op.py | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index e08f1769bef..8a3c33a4d6c 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 29f82b0350d..7f26a717019 100644 --- a/python/paddle/fluid/tests/unittests/test_matrix_power_op.py +++ b/python/paddle/fluid/tests/unittests/test_matrix_power_op.py @@ -312,6 +312,10 @@ class TestMatrixPowerAPIError(unittest.TestCase): 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 + input = fluid.data(name="input_4", shape=[1, 1, 0, 0], dtype="float32") + self.assertRaises(ValueError, paddle.linalg.matrix_power, input, 2) + class TestMatrixPowerSingularAPI(unittest.TestCase): def setUp(self): -- GitLab