From 917c1f533b75e8d4ec5cd484c63473345c6468ce Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Wed, 12 Apr 2023 09:05:08 +0000 Subject: [PATCH 01/12] support_0D_output_for_matrix_rank_multi_dot, test=allcase --- paddle/fluid/operators/matrix_rank_op.cc | 2 +- paddle/phi/infermeta/binary.cc | 2 +- paddle/phi/infermeta/multiary.cc | 2 +- paddle/phi/infermeta/unary.cc | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/paddle/fluid/operators/matrix_rank_op.cc b/paddle/fluid/operators/matrix_rank_op.cc index 16ca2cf09ec0b..3ac5c6e21434d 100644 --- a/paddle/fluid/operators/matrix_rank_op.cc +++ b/paddle/fluid/operators/matrix_rank_op.cc @@ -27,7 +27,7 @@ namespace detail { static DDim CheckAndGetOutputDim(const DDim& dim_x) { auto x_vec = phi::vectorize(dim_x); if (x_vec.size() == 2) { - return phi::make_ddim({1}); + return phi::make_ddim({}); } x_vec.erase(x_vec.end() - 2, x_vec.end()); return phi::make_ddim(x_vec); diff --git a/paddle/phi/infermeta/binary.cc b/paddle/phi/infermeta/binary.cc index 0a3e31054b0f5..fd6c96e686375 100644 --- a/paddle/phi/infermeta/binary.cc +++ b/paddle/phi/infermeta/binary.cc @@ -72,7 +72,7 @@ static void BinarySameInputDimsCheck(const MetaTensor& x, static DDim CheckAndGetOutputDim(const DDim& dim_x) { auto x_vec = phi::vectorize(dim_x); if (x_vec.size() == 2) { - return phi::make_ddim({1}); + return phi::make_ddim({}); } x_vec.erase(x_vec.end() - 2, x_vec.end()); return phi::make_ddim(x_vec); diff --git a/paddle/phi/infermeta/multiary.cc b/paddle/phi/infermeta/multiary.cc index 71fe149e7c0c0..89793cb1a0ba9 100644 --- a/paddle/phi/infermeta/multiary.cc +++ b/paddle/phi/infermeta/multiary.cc @@ -2335,7 +2335,7 @@ void MultiDotInferMeta(const std::vector& x, // If the last tensor is 1D of size n view it as a column vector (n, 1) if (last_dim.size() == 1) { last_dim = phi::make_ddim({static_cast(last_dim[0]), 1}); - out_dim = is_vector ? phi::make_ddim({1}) : phi::make_ddim({first_dim[0]}); + out_dim = is_vector ? phi::make_ddim({}) : phi::make_ddim({first_dim[0]}); } else { out_dim = is_vector ? phi::make_ddim({last_dim[1]}) : phi::make_ddim({first_dim[0], last_dim[1]}); diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index 8c87abf4fd86a..a47b856b998f9 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -38,7 +38,7 @@ namespace detail { static DDim CheckAndGetOutputDim(const DDim& dim_x) { auto x_vec = phi::vectorize(dim_x); if (x_vec.size() == 2) { - return phi::make_ddim({1}); + return phi::make_ddim({}); } x_vec.erase(x_vec.end() - 2, x_vec.end()); return phi::make_ddim(x_vec); From 93f9df2a5092ca2734e033bd7337c41c29ca6a1f Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Thu, 13 Apr 2023 03:45:32 +0000 Subject: [PATCH 02/12] add 0D output test for matrox_rank and mutli_dot test=allcase --- .../tests/unittests/test_zero_dim_tensor.py | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py index 985a62f931195..8f0cbfa10ceaa 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -2066,6 +2066,23 @@ def body(i, x): self.assertEqual(x.grad.shape, []) np.testing.assert_allclose(x.grad, np.array(1.0)) + def test_multi_dot(self): + a = paddle.randn([4]) + a.stop_gradient = False + b = paddle.randn([4, 5]) + b.stop_gradient = False + c = paddle.randn([5]) + c.stop_gradient = False + + out = paddle.linalg.multi_dot([a, b, c]) + out.retain_grads() + out.backward() + + self.assertTrue(out.shape, []) + self.assertTrue(a.grad.shape, [4]) + self.assertTrue(b.grad.shape, [4, 5]) + self.assertTrue(c.grad.shape, [5]) + class TestSundryAPIStatic(unittest.TestCase): def setUp(self): @@ -3609,6 +3626,19 @@ def test_broadcast_tensors(self): self.assertEqual(out1.shape, (2, 3)) self.assertEqual(out2.shape, (2, 3)) + def test_multi_dot(self): + a = paddle.randn([4]) + a.stop_gradient = False + b = paddle.randn([4, 5]) + b.stop_gradient = False + c = paddle.randn([5]) + c.stop_gradient = False + + out = paddle.linalg.multi_dot([a, b, c]) + prog = paddle.static.default_main_program() + res = self.exe.run(prog, fetch_list=[out]) + self.assertTrue(res[0].shape, []) + # Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest. class TestNoBackwardAPI(unittest.TestCase): @@ -4034,6 +4064,39 @@ def test_unique(self): self.assertEqual(res[2].shape, (1,)) self.assertEqual(res[3].shape, (1,)) + def test_matrix_rank(self): + x = paddle.eye(10) + x.stop_gradient = False + out = paddle.linalg.matrix_rank(x) + + self.assertTrue(out.shape, []) + np.testing.assert_equal(out, np.array(10)) + + c = paddle.ones(shape=[3, 4, 5]) + c.stop_gradient = False + out_c = paddle.linalg.matrix_rank(c) + self.assertTrue(out_c.shape, [3]) + np.testing.assert_equal(out_c, np.array([1, 1, 1])) + + def test_static_matrix_rank(self): + # 2D : OUTPUT 0D + x = paddle.eye(10) + x.stop_gradient = False + out = paddle.linalg.matrix_rank(x) + prog = paddle.static.default_main_program() + self.exe.run(paddle.static.default_startup_program()) + res = self.exe.run(prog, fetch_list=[out]) + self.assertTrue(res[0].shape, []) + + # 3D : OUTPUT 1D + c = paddle.ones(shape=[3, 4, 5]) + c.stop_gradient = False + out_c = paddle.linalg.matrix_rank(c) + prog = paddle.static.default_main_program() + self.exe.run(paddle.static.default_startup_program()) + res = self.exe.run(prog, fetch_list=[out_c]) + self.assertTrue(res[0].shape, [3]) + unary_apis_with_complex_input = [ paddle.real, From 76d065f051f757680ee90d4041aabb2c4f37b56e Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Thu, 13 Apr 2023 12:30:39 +0000 Subject: [PATCH 03/12] fix assert error ,test=allcase --- .../tests/unittests/test_zero_dim_tensor.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py index 8f0cbfa10ceaa..0d9bd29664bfa 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -2078,10 +2078,10 @@ def test_multi_dot(self): out.retain_grads() out.backward() - self.assertTrue(out.shape, []) - self.assertTrue(a.grad.shape, [4]) - self.assertTrue(b.grad.shape, [4, 5]) - self.assertTrue(c.grad.shape, [5]) + self.assertEqual(out.shape, []) + self.assertEqual(a.grad.shape, [4]) + self.assertEqual(b.grad.shape, [4, 5]) + self.assertEqual(c.grad.shape, [5]) class TestSundryAPIStatic(unittest.TestCase): @@ -3637,7 +3637,7 @@ def test_multi_dot(self): out = paddle.linalg.multi_dot([a, b, c]) prog = paddle.static.default_main_program() res = self.exe.run(prog, fetch_list=[out]) - self.assertTrue(res[0].shape, []) + self.assertEqual(res[0].shape, ()) # Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest. @@ -4069,13 +4069,13 @@ def test_matrix_rank(self): x.stop_gradient = False out = paddle.linalg.matrix_rank(x) - self.assertTrue(out.shape, []) + self.assertEqual(out.shape, ()) np.testing.assert_equal(out, np.array(10)) c = paddle.ones(shape=[3, 4, 5]) c.stop_gradient = False out_c = paddle.linalg.matrix_rank(c) - self.assertTrue(out_c.shape, [3]) + self.assertEqual(out_c.shape, (3,)) np.testing.assert_equal(out_c, np.array([1, 1, 1])) def test_static_matrix_rank(self): @@ -4086,7 +4086,7 @@ def test_static_matrix_rank(self): prog = paddle.static.default_main_program() self.exe.run(paddle.static.default_startup_program()) res = self.exe.run(prog, fetch_list=[out]) - self.assertTrue(res[0].shape, []) + self.assertEqual(res[0].shape, ()) # 3D : OUTPUT 1D c = paddle.ones(shape=[3, 4, 5]) From 29d57439c477ed7f22dedb1ee8a2fdb2b70f27b6 Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Fri, 14 Apr 2023 02:27:11 +0000 Subject: [PATCH 04/12] fix test error, test=allcase --- python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py index 0d9bd29664bfa..f9d84b46f3cc2 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -4095,7 +4095,7 @@ def test_static_matrix_rank(self): prog = paddle.static.default_main_program() self.exe.run(paddle.static.default_startup_program()) res = self.exe.run(prog, fetch_list=[out_c]) - self.assertTrue(res[0].shape, [3]) + self.assertEqual(res[0].shape, (3,)) unary_apis_with_complex_input = [ From 3754bcdcbde047dfac91e7c14ec8b910e099795a Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Fri, 14 Apr 2023 07:38:08 +0000 Subject: [PATCH 05/12] fix other test error, test=allcase --- paddle/phi/infermeta/multiary.cc | 6 +++++- paddle/phi/infermeta/unary.cc | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/paddle/phi/infermeta/multiary.cc b/paddle/phi/infermeta/multiary.cc index 89793cb1a0ba9..eb8c7f66f64ec 100644 --- a/paddle/phi/infermeta/multiary.cc +++ b/paddle/phi/infermeta/multiary.cc @@ -2335,7 +2335,11 @@ void MultiDotInferMeta(const std::vector& x, // If the last tensor is 1D of size n view it as a column vector (n, 1) if (last_dim.size() == 1) { last_dim = phi::make_ddim({static_cast(last_dim[0]), 1}); - out_dim = is_vector ? phi::make_ddim({}) : phi::make_ddim({first_dim[0]}); + out_dim = is_vector ? phi::make_ddim({1}) : phi::make_ddim({first_dim[0]}); + // if 1D * 2D * 1D, output dim should be 0D + if (inputs_dims.size() == 3 && is_vector && inputs_dims[1].size() == 2) { + out_dim = phi::make_ddim({}); + } } else { out_dim = is_vector ? phi::make_ddim({last_dim[1]}) : phi::make_ddim({first_dim[0], last_dim[1]}); diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index a47b856b998f9..c23e50a3fd752 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -2024,6 +2024,7 @@ void MatrixRankInferMeta(const MetaTensor& x, "if hermitian == true, matrix should be n*n")); } DDim dim_x_batch = detail::CheckAndGetOutputDim(dim_x); + if (dim_x.size() == 2 && hermitian) dim_x_batch = phi::make_ddim({1}); out->set_dims(dim_x_batch); out->share_lod(x); } From b440c4adb448f84f66d944d7cba591d94fd1da50 Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Fri, 14 Apr 2023 08:07:28 +0000 Subject: [PATCH 06/12] fix other test error, test=allcase --- paddle/phi/infermeta/multiary.cc | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/paddle/phi/infermeta/multiary.cc b/paddle/phi/infermeta/multiary.cc index eb8c7f66f64ec..89793cb1a0ba9 100644 --- a/paddle/phi/infermeta/multiary.cc +++ b/paddle/phi/infermeta/multiary.cc @@ -2335,11 +2335,7 @@ void MultiDotInferMeta(const std::vector& x, // If the last tensor is 1D of size n view it as a column vector (n, 1) if (last_dim.size() == 1) { last_dim = phi::make_ddim({static_cast(last_dim[0]), 1}); - out_dim = is_vector ? phi::make_ddim({1}) : phi::make_ddim({first_dim[0]}); - // if 1D * 2D * 1D, output dim should be 0D - if (inputs_dims.size() == 3 && is_vector && inputs_dims[1].size() == 2) { - out_dim = phi::make_ddim({}); - } + out_dim = is_vector ? phi::make_ddim({}) : phi::make_ddim({first_dim[0]}); } else { out_dim = is_vector ? phi::make_ddim({last_dim[1]}) : phi::make_ddim({first_dim[0], last_dim[1]}); From 650cad3f188ad9f56fc67bdafff2cd960441c723 Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Fri, 14 Apr 2023 08:49:26 +0000 Subject: [PATCH 07/12] fix test error, test=allcase --- paddle/phi/infermeta/unary.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index c23e50a3fd752..a47b856b998f9 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -2024,7 +2024,6 @@ void MatrixRankInferMeta(const MetaTensor& x, "if hermitian == true, matrix should be n*n")); } DDim dim_x_batch = detail::CheckAndGetOutputDim(dim_x); - if (dim_x.size() == 2 && hermitian) dim_x_batch = phi::make_ddim({1}); out->set_dims(dim_x_batch); out->share_lod(x); } From 35307511843c6f02ca335003fc960b1000754a4d Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Sun, 16 Apr 2023 02:34:43 +0000 Subject: [PATCH 08/12] fix matrix_rank and multi dot test err test=allcase --- .../tests/unittests/test_zero_dim_tensor.py | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py index f9d84b46f3cc2..525bcae8d6b44 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -3626,6 +3626,7 @@ def test_broadcast_tensors(self): self.assertEqual(out1.shape, (2, 3)) self.assertEqual(out2.shape, (2, 3)) + @prog_scope def test_multi_dot(self): a = paddle.randn([4]) a.stop_gradient = False @@ -3830,6 +3831,20 @@ def test_unique(self): self.assertEqual(inverse.shape, [1]) self.assertEqual(counts.shape, [1]) + def test_matrix_rank(self): + x = paddle.eye(10) + x.stop_gradient = False + out = paddle.linalg.matrix_rank(x) + + self.assertEqual(out.shape, ()) + np.testing.assert_equal(out, np.array(10)) + + c = paddle.ones(shape=[3, 4, 5]) + c.stop_gradient = False + out_c = paddle.linalg.matrix_rank(c) + self.assertEqual(out_c.shape, (3,)) + np.testing.assert_equal(out_c, np.array([1, 1, 1])) + class TestNoBackwardAPIStatic(unittest.TestCase): def setUp(self): @@ -4064,20 +4079,6 @@ def test_unique(self): self.assertEqual(res[2].shape, (1,)) self.assertEqual(res[3].shape, (1,)) - def test_matrix_rank(self): - x = paddle.eye(10) - x.stop_gradient = False - out = paddle.linalg.matrix_rank(x) - - self.assertEqual(out.shape, ()) - np.testing.assert_equal(out, np.array(10)) - - c = paddle.ones(shape=[3, 4, 5]) - c.stop_gradient = False - out_c = paddle.linalg.matrix_rank(c) - self.assertEqual(out_c.shape, (3,)) - np.testing.assert_equal(out_c, np.array([1, 1, 1])) - def test_static_matrix_rank(self): # 2D : OUTPUT 0D x = paddle.eye(10) From fcd901b0939dc94fee719cbab99d7693c8f86682 Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Sun, 16 Apr 2023 02:56:57 +0000 Subject: [PATCH 09/12] fix test error test=allcase --- python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py index 525bcae8d6b44..6c57bc0a5d852 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -3626,7 +3626,7 @@ def test_broadcast_tensors(self): self.assertEqual(out1.shape, (2, 3)) self.assertEqual(out2.shape, (2, 3)) - @prog_scope + @prog_scope() def test_multi_dot(self): a = paddle.randn([4]) a.stop_gradient = False From ffa9567f1f07d2cc646018df213eefb77c04355e Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Mon, 17 Apr 2023 02:51:51 +0000 Subject: [PATCH 10/12] fix test zero dim test, test=allcase --- python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py index 6c57bc0a5d852..b59a3337c47e1 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -3836,13 +3836,13 @@ def test_matrix_rank(self): x.stop_gradient = False out = paddle.linalg.matrix_rank(x) - self.assertEqual(out.shape, ()) + self.assertEqual(out.shape, []) np.testing.assert_equal(out, np.array(10)) c = paddle.ones(shape=[3, 4, 5]) c.stop_gradient = False out_c = paddle.linalg.matrix_rank(c) - self.assertEqual(out_c.shape, (3,)) + self.assertEqual(out_c.shape, [3]) np.testing.assert_equal(out_c, np.array([1, 1, 1])) From ea2c2517a7ee164706469e374154f2a9e46d360d Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Tue, 18 Apr 2023 06:05:55 +0000 Subject: [PATCH 11/12] add static backward test for multi_dot, test=allcase --- .../paddle/fluid/tests/unittests/test_zero_dim_tensor.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py index c3b5098ecdd4e..1c46a64f4648b 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -3681,9 +3681,15 @@ def test_multi_dot(self): c.stop_gradient = False out = paddle.linalg.multi_dot([a, b, c]) + paddle.static.append_backward(out.sum()) prog = paddle.static.default_main_program() - res = self.exe.run(prog, fetch_list=[out]) + res = self.exe.run( + prog, fetch_list=[out, a.grad_name, b.grad_name, c.grad_name] + ) self.assertEqual(res[0].shape, ()) + self.assertEqual(res[1].shape, (4,)) + self.assertEqual(res[2].shape, (4, 5)) + self.assertEqual(res[3].shape, (5,)) # Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest. From ee3865d4963ef47e739d0177975175d9057ba138 Mon Sep 17 00:00:00 2001 From: GGBond8488 <857631483@qq.com> Date: Wed, 19 Apr 2023 11:33:49 +0000 Subject: [PATCH 12/12] add tol 2d broadcast test case, test=allcase --- .../tests/unittests/test_zero_dim_tensor.py | 46 ++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py index 1c46a64f4648b..6c8dc45c50f91 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -3896,6 +3896,24 @@ def test_matrix_rank(self): self.assertEqual(out_c.shape, [3]) np.testing.assert_equal(out_c, np.array([1, 1, 1])) + # 2D, tol->float : OUTPUT 0D + x_tol = paddle.eye(10) + x_tol.stop_gradient = False + out_tol = paddle.linalg.matrix_rank(x_tol, tol=0.1) + self.assertEqual(out_tol.shape, []) + + # 3D, tol->float : OUTPUT 1D + c_tol = paddle.ones(shape=[3, 4, 5]) + c_tol.stop_gradient = False + out_c_tol = paddle.linalg.matrix_rank(c_tol, tol=0.1) + self.assertEqual(out_c_tol.shape, [3]) + + tol_2 = paddle.randn([2]) + # 2D, tol->Tensor[1,2] : OUTPUT 1D + d = paddle.eye(10) + out_d = paddle.linalg.matrix_rank(d, tol=tol_2) + self.assertEqual(out_d.shape, [2]) + class TestNoBackwardAPIStatic(unittest.TestCase): def setUp(self): @@ -4130,13 +4148,13 @@ def test_unique(self): self.assertEqual(res[2].shape, (1,)) self.assertEqual(res[3].shape, (1,)) + @prog_scope() def test_static_matrix_rank(self): # 2D : OUTPUT 0D x = paddle.eye(10) x.stop_gradient = False out = paddle.linalg.matrix_rank(x) prog = paddle.static.default_main_program() - self.exe.run(paddle.static.default_startup_program()) res = self.exe.run(prog, fetch_list=[out]) self.assertEqual(res[0].shape, ()) @@ -4149,6 +4167,32 @@ def test_static_matrix_rank(self): res = self.exe.run(prog, fetch_list=[out_c]) self.assertEqual(res[0].shape, (3,)) + # 2D, tol->float : OUTPUT 0D + x_tol = paddle.eye(10) + x_tol.stop_gradient = False + out_tol = paddle.linalg.matrix_rank(x_tol, tol=0.1) + prog = paddle.static.default_main_program() + res = self.exe.run(prog, fetch_list=[out_tol]) + self.assertEqual(res[0].shape, ()) + + # 3D, tol->float : OUTPUT 1D + c_tol = paddle.ones(shape=[3, 4, 5]) + c_tol.stop_gradient = False + out_c_tol = paddle.linalg.matrix_rank(c_tol, tol=0.1) + prog = paddle.static.default_main_program() + self.exe.run(paddle.static.default_startup_program()) + res = self.exe.run(prog, fetch_list=[out_c_tol]) + self.assertEqual(res[0].shape, (3,)) + + tol_2 = paddle.randn([2]) + # 2D, tol->Tensor[1,2] : OUTPUT 1D + d = paddle.eye(10) + out_d = paddle.linalg.matrix_rank(d, tol=tol_2) + prog = paddle.static.default_main_program() + self.exe.run(paddle.static.default_startup_program()) + res = self.exe.run(prog, fetch_list=[out_d]) + self.assertEqual(res[0].shape, (2,)) + unary_apis_with_complex_input = [ paddle.real,