From 0dc9f209d9d8ac2a4fd99584b9f6074a73f2dc84 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Fri, 20 Jan 2023 21:49:22 +0800 Subject: [PATCH 1/2] [Divide by 0 Error] add svd check --- .../tests/unittests/test_linalg_svd_op.py | 35 +++++++++++++++++++ python/paddle/tensor/linalg.py | 4 +++ 2 files changed, 39 insertions(+) create mode 100644 python/paddle/fluid/tests/unittests/test_linalg_svd_op.py diff --git a/python/paddle/fluid/tests/unittests/test_linalg_svd_op.py b/python/paddle/fluid/tests/unittests/test_linalg_svd_op.py new file mode 100644 index 00000000000000..f9df083b74915a --- /dev/null +++ b/python/paddle/fluid/tests/unittests/test_linalg_svd_op.py @@ -0,0 +1,35 @@ +# 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 TestSparseEmbeddingAPIError(unittest.TestCase): + def test_errors(self): + with paddle.fluid.dygraph.guard(): + # The size of input in sparse_embedding 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.svd(x, full_matrices=False) + + 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 4cce1b01968a19..e508d4eec0a0bf 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -1921,6 +1921,10 @@ def svd(x, full_matrices=False, name=None): # U * UH == I # V * VH == I """ + + if x.size == 0: + raise ValueError("input size should not be 0") + if in_dygraph_mode(): return _C_ops.svd(x, full_matrices) else: From 8cdbceb0c6be0c2e4c88db99943f2dc2149b13a9 Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Wed, 1 Feb 2023 19:56:35 +0800 Subject: [PATCH 2/2] [Divide by 0 Error] svd check migrate to c++, move svd test_0_size --- .../tests/unittests/test_linalg_svd_op.py | 35 ------------------- .../fluid/tests/unittests/test_svd_op.py | 10 ++++++ python/paddle/tensor/linalg.py | 3 -- 3 files changed, 10 insertions(+), 38 deletions(-) delete mode 100644 python/paddle/fluid/tests/unittests/test_linalg_svd_op.py diff --git a/python/paddle/fluid/tests/unittests/test_linalg_svd_op.py b/python/paddle/fluid/tests/unittests/test_linalg_svd_op.py deleted file mode 100644 index f9df083b74915a..00000000000000 --- a/python/paddle/fluid/tests/unittests/test_linalg_svd_op.py +++ /dev/null @@ -1,35 +0,0 @@ -# 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 TestSparseEmbeddingAPIError(unittest.TestCase): - def test_errors(self): - with paddle.fluid.dygraph.guard(): - # The size of input in sparse_embedding 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.svd(x, full_matrices=False) - - self.assertRaises(ValueError, test_0_size) - - -if __name__ == '__main__': - unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_svd_op.py b/python/paddle/fluid/tests/unittests/test_svd_op.py index cf91162d9d8ca4..a760fef4ff2528 100644 --- a/python/paddle/fluid/tests/unittests/test_svd_op.py +++ b/python/paddle/fluid/tests/unittests/test_svd_op.py @@ -320,6 +320,16 @@ def test_static(self): ) np.testing.assert_allclose(fetches[0], gt_s, rtol=1e-05) + def test_errors(self): + with paddle.fluid.dygraph.guard(): + # The size of input in svd 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.svd(x, full_matrices=False) + + self.assertRaises(ValueError, test_0_size) + if __name__ == "__main__": paddle.enable_static() diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index 9efdc235e44c79..374afeb0e80ccd 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -1922,9 +1922,6 @@ def svd(x, full_matrices=False, name=None): # V * VH == I """ - if x.size == 0: - raise ValueError("input size should not be 0") - if in_dygraph_mode(): return _C_ops.svd(x, full_matrices) else: