From 10e072d43150b70a47682407f278ecedde9a9e91 Mon Sep 17 00:00:00 2001 From: gouzil <66515297+gouzil@users.noreply.github.com> Date: Wed, 1 Feb 2023 14:46:34 +0800 Subject: [PATCH] [Divide by 0 Error] add eig check (#49971) * [Divide by 0 Error] add eig check * [Divide by 0 Error] eig check migrate to c++ * [Divide by 0 Error] Fix class name error --- paddle/phi/kernels/cpu/eig_kernel.cc | 5 +++ .../tests/unittests/test_linalg_eig_op.py | 34 +++++++++++++++++++ python/paddle/tensor/linalg.py | 1 + 3 files changed, 40 insertions(+) create mode 100644 python/paddle/fluid/tests/unittests/test_linalg_eig_op.py diff --git a/paddle/phi/kernels/cpu/eig_kernel.cc b/paddle/phi/kernels/cpu/eig_kernel.cc index 42a843391872f..c9bdf8af11682 100644 --- a/paddle/phi/kernels/cpu/eig_kernel.cc +++ b/paddle/phi/kernels/cpu/eig_kernel.cc @@ -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; diff --git a/python/paddle/fluid/tests/unittests/test_linalg_eig_op.py b/python/paddle/fluid/tests/unittests/test_linalg_eig_op.py new file mode 100644 index 0000000000000..18d95a4f383d9 --- /dev/null +++ b/python/paddle/fluid/tests/unittests/test_linalg_eig_op.py @@ -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() diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index 46f11130c0354..10c8c24a78724 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -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: