Skip to content

Commit

Permalink
Fix the div 0 error of matrix_power (PaddlePaddle#49942)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Liyulingyue authored and pangengzheng committed Feb 2, 2023
1 parent 832eb52 commit 1211722
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions paddle/phi/infermeta/unary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions python/paddle/fluid/tests/unittests/test_matrix_power_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ 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
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):
Expand Down

0 comments on commit 1211722

Please sign in to comment.