From f2ec69b4af29931e596eb4d55bb27a5dc1db2099 Mon Sep 17 00:00:00 2001 From: gouzil <66515297+gouzil@users.noreply.github.com> Date: Fri, 3 Feb 2023 15:28:30 +0800 Subject: [PATCH] [Divide by 0 Error] add svd check (#49968) * [Divide by 0 Error] add svd check * [Divide by 0 Error] svd check migrate to c++, move svd test_0_size --- python/paddle/fluid/tests/unittests/test_svd_op.py | 10 ++++++++++ python/paddle/tensor/linalg.py | 1 + 2 files changed, 11 insertions(+) diff --git a/python/paddle/fluid/tests/unittests/test_svd_op.py b/python/paddle/fluid/tests/unittests/test_svd_op.py index cf91162d9d..a760fef4ff 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 @@ class TestSvdAPI(unittest.TestCase): ) 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 c59202977f..de8374d4ce 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -1921,6 +1921,7 @@ def svd(x, full_matrices=False, name=None): # U * UH == I # V * VH == I """ + if in_dygraph_mode(): return _C_ops.svd(x, full_matrices) else: -- GitLab