Skip to content

Commit

Permalink
[Divide by 0 Error] add eig check (PaddlePaddle#49971)
Browse files Browse the repository at this point in the history
* [Divide by 0 Error] add eig check

* [Divide by 0 Error] eig check migrate to c++

* [Divide by 0 Error] Fix class name error
  • Loading branch information
gouzil authored and pangengzheng committed Feb 2, 2023
1 parent 55bd773 commit 10e072d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions paddle/phi/kernels/cpu/eig_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ void EigKernel(const Context& dev_ctx,
int batch_count = BatchCount(x);
int order = x.dims()[x.dims().size() - 1];

PADDLE_ENFORCE_LT(0,
order,
errors::InvalidArgument(
"The order of Input(X) should be greater than 0."));

DenseTensor real_w;
DenseTensor real_v;

Expand Down
34 changes: 34 additions & 0 deletions python/paddle/fluid/tests/unittests/test_linalg_eig_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

import numpy as np

import paddle


class TestEigAPIError(unittest.TestCase):
def test_errors(self):
# The size of input in Eig should not be 0.
def test_0_size():
array = np.array([], dtype=np.float32)
x = paddle.to_tensor(np.reshape(array, [0, 0]), dtype='float32')
paddle.linalg.eig(x)

self.assertRaises(ValueError, test_0_size)


if __name__ == '__main__':
unittest.main()
1 change: 1 addition & 0 deletions python/paddle/tensor/linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2323,6 +2323,7 @@ def eig(x, name=None):
# [ (16.50471283351188+0j) , (-5.5034820550763515+0j) ,
# (-0.21026087843552282+0j)])
"""

if in_dygraph_mode():
return _C_ops.eig(x)
else:
Expand Down

0 comments on commit 10e072d

Please sign in to comment.