From 7e08b67ce704e7ac279fa814be4a9f8926138024 Mon Sep 17 00:00:00 2001 From: ZHOU05030 <112939094+ZHOU05030@users.noreply.github.com> Date: Wed, 11 Dec 2024 14:22:44 +0800 Subject: [PATCH 01/12] add matrix_rank with 0 size tensor --- paddle/phi/core/dense_tensor_impl.cc | 5 +++ .../phi/kernels/cpu/matrix_rank_tol_kernel.cc | 6 +++ test/legacy_test/test_matrix_rank_op.py | 45 +++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/paddle/phi/core/dense_tensor_impl.cc b/paddle/phi/core/dense_tensor_impl.cc index 7a886f90b0376..9e6a1867d5681 100644 --- a/paddle/phi/core/dense_tensor_impl.cc +++ b/paddle/phi/core/dense_tensor_impl.cc @@ -40,6 +40,11 @@ size_t DenseTensor::memory_size() const { } void DenseTensor::check_memory_size() const { + if (numel() == 0) { + // For zero-sized tensors, typically no memory verification is needed, + // or consider special handling if necessary. + return; + } PADDLE_ENFORCE_NOT_NULL( holder_, common::errors::PreconditionNotMet("Tensor holds no memory. " diff --git a/paddle/phi/kernels/cpu/matrix_rank_tol_kernel.cc b/paddle/phi/kernels/cpu/matrix_rank_tol_kernel.cc index 608267ccb64a2..5aa70d283b7f9 100644 --- a/paddle/phi/kernels/cpu/matrix_rank_tol_kernel.cc +++ b/paddle/phi/kernels/cpu/matrix_rank_tol_kernel.cc @@ -86,6 +86,12 @@ void MatrixRankTolKernel(const Context& dev_ctx, bool use_default_tol, bool hermitian, DenseTensor* out) { + if (x.numel() == 0) { + out->Resize({1}); + auto* out_data = dev_ctx.template Alloc(out); + out_data[0] = 0; + return; + } dev_ctx.template Alloc(out); auto dim_x = x.dims(); auto dim_out = out->dims(); diff --git a/test/legacy_test/test_matrix_rank_op.py b/test/legacy_test/test_matrix_rank_op.py index 2422277af2811..52c42e0c1eb7c 100644 --- a/test/legacy_test/test_matrix_rank_op.py +++ b/test/legacy_test/test_matrix_rank_op.py @@ -17,6 +17,7 @@ import numpy as np from op_test import OpTest +from utils import dygraph_guard, static_guard import paddle from paddle import base, static @@ -230,5 +231,49 @@ def test_static(self): np.testing.assert_allclose(fetches[0], rank_np, rtol=1e-05) +class TestMatrixRankEmptyTensor(unittest.TestCase): + + def _get_places(self): + places = [] + if ( + os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() + in ['1', 'true', 'on'] + or not paddle.is_compiled_with_cuda() + ): + places.append(paddle.CPUPlace()) + if paddle.is_compiled_with_cuda(): + places.append(paddle.CUDAPlace(0)) + return places + + def _test_matrix_rank_static(self, place, shape, expected_rank): + with static_guard(): + with paddle.static.program_guard( + paddle.static.Program(), paddle.static.Program() + ): + x = paddle.static.data(name='x', shape=shape, dtype='float32') + y = paddle.linalg.matrix_rank(x) + exe = paddle.static.Executor(place) + res = exe.run( + feed={'x': np.zeros(shape, dtype='float32')}, fetch_list=[y] + ) + assert res[0] == expected_rank + + def _test_matrix_rank_dynamic(self, shape, expected_rank): + with dygraph_guard(): + x = paddle.full(shape, 1.0, dtype='float32') + y = paddle.linalg.matrix_rank(x) + assert y == expected_rank + + def test_matrix_rank_tensor(self): + test_cases = [([0, 0], 0), ([0, 6], 0), ([6, 0], 0), ([2, 3, 0, 0], 0)] + + for place in self._get_places(): + for shape, expected_rank in test_cases: + self._test_matrix_rank_static(place, shape, expected_rank) + + for shape, expected_rank in test_cases: + self._test_matrix_rank_dynamic(shape, expected_rank) + + if __name__ == '__main__': unittest.main() From 43d56b8d38875b4f2cc275c3d84dbfc6f452d2a0 Mon Sep 17 00:00:00 2001 From: ZHOU05030 <112939094+ZHOU05030@users.noreply.github.com> Date: Wed, 11 Dec 2024 17:21:32 +0800 Subject: [PATCH 02/12] fix --- paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu b/paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu index 9325731a5e11b..7e171deec7ca2 100644 --- a/paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu +++ b/paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu @@ -456,6 +456,12 @@ void MatrixRankAtolRtolKernel(const Context& dev_ctx, const paddle::optional& rtol, bool hermitian, DenseTensor* out) { + if (x.numel() == 0) { + out->Resize({1}); + auto* out_data = dev_ctx.template Alloc(out); + out_data[0] = 0; + return; + } auto* x_data = x.data(); dev_ctx.template Alloc(out); From f0c871f1d44314421a1caae936c1fc09c379654f Mon Sep 17 00:00:00 2001 From: ZHOU05030 <112939094+ZHOU05030@users.noreply.github.com> Date: Wed, 11 Dec 2024 21:33:47 +0800 Subject: [PATCH 03/12] fix --- paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu b/paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu index 7e171deec7ca2..4ca8b9ae0a79d 100644 --- a/paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu +++ b/paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu @@ -335,6 +335,12 @@ void MatrixRankTolKernel(const Context& dev_ctx, bool use_default_tol, bool hermitian, DenseTensor* out) { + if (x.numel() == 0) { + out->Resize({1}); + auto* out_data = dev_ctx.template Alloc(out); + out_data[0] = 0; + return; + } auto* x_data = x.data(); dev_ctx.template Alloc(out); From 502126b4f4302087a0d80d4ab408f7d4301a6395 Mon Sep 17 00:00:00 2001 From: ZHOU05030 <112939094+ZHOU05030@users.noreply.github.com> Date: Thu, 12 Dec 2024 15:38:20 +0800 Subject: [PATCH 04/12] fix --- paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu | 6 ------ 1 file changed, 6 deletions(-) diff --git a/paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu b/paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu index 4ca8b9ae0a79d..7e171deec7ca2 100644 --- a/paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu +++ b/paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu @@ -335,12 +335,6 @@ void MatrixRankTolKernel(const Context& dev_ctx, bool use_default_tol, bool hermitian, DenseTensor* out) { - if (x.numel() == 0) { - out->Resize({1}); - auto* out_data = dev_ctx.template Alloc(out); - out_data[0] = 0; - return; - } auto* x_data = x.data(); dev_ctx.template Alloc(out); From 68520b6dc1c4aa8fa7d9da9d2c301dadc697cac8 Mon Sep 17 00:00:00 2001 From: ZHOU05030 <112939094+ZHOU05030@users.noreply.github.com> Date: Thu, 12 Dec 2024 16:34:04 +0800 Subject: [PATCH 05/12] fix --- .../phi/kernels/cpu/matrix_rank_tol_kernel.cc | 40 +++++++++-- .../phi/kernels/gpu/matrix_rank_tol_kernel.cu | 10 +++ test/legacy_test/test_matrix_rank_op.py | 66 +++++++++++-------- 3 files changed, 84 insertions(+), 32 deletions(-) diff --git a/paddle/phi/kernels/cpu/matrix_rank_tol_kernel.cc b/paddle/phi/kernels/cpu/matrix_rank_tol_kernel.cc index 5aa70d283b7f9..bff8be100bb3c 100644 --- a/paddle/phi/kernels/cpu/matrix_rank_tol_kernel.cc +++ b/paddle/phi/kernels/cpu/matrix_rank_tol_kernel.cc @@ -86,17 +86,28 @@ void MatrixRankTolKernel(const Context& dev_ctx, bool use_default_tol, bool hermitian, DenseTensor* out) { - if (x.numel() == 0) { - out->Resize({1}); - auto* out_data = dev_ctx.template Alloc(out); - out_data[0] = 0; - return; - } dev_ctx.template Alloc(out); auto dim_x = x.dims(); auto dim_out = out->dims(); int rows = static_cast(dim_x[dim_x.size() - 2]); int cols = static_cast(dim_x[dim_x.size() - 1]); + PADDLE_ENFORCE_NE( + rows, + 0, + errors::InvalidArgument("The input Tensor x's shape[-2] should not " + "be 0, but shape is %s now.", + dim_x)); + PADDLE_ENFORCE_NE( + cols, + 0, + errors::InvalidArgument("The input Tensor x's shape[-1] should not " + "be 0, but shape is %s now.", + dim_x)); + if (x.numel() == 0) { + out->Resize({0}); + dev_ctx.template Alloc(out); + return; + } int k = std::min(rows, cols); int batches = static_cast(x.numel() / (rows * cols)); @@ -190,6 +201,23 @@ void MatrixRankAtolRtolKernel(const Context& dev_ctx, auto dim_out = out->dims(); int rows = static_cast(dim_x[dim_x.size() - 2]); int cols = static_cast(dim_x[dim_x.size() - 1]); + PADDLE_ENFORCE_NE( + rows, + 0, + errors::InvalidArgument("The input Tensor x's shape[-2] should not " + "be 0, but shape is %s now.", + dim_x)); + PADDLE_ENFORCE_NE( + cols, + 0, + errors::InvalidArgument("The input Tensor x's shape[-1] should not " + "be 0, but shape is %s now.", + dim_x)); + if (x.numel() == 0) { + out->Resize({0}); + dev_ctx.template Alloc(out); + return; + } int k = std::min(rows, cols); int batches = static_cast(x.numel() / (rows * cols)); diff --git a/paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu b/paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu index 7e171deec7ca2..0d4e69fe51c74 100644 --- a/paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu +++ b/paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu @@ -354,6 +354,11 @@ void MatrixRankTolKernel(const Context& dev_ctx, "The input Tensor x's shape[-1] should not " "be 0, but shape is %s now.", dim_x)); + if (x.numel() == 0) { + out->Resize({0}); + dev_ctx.template Alloc(out); + return; + } int k = std::min(rows, cols); auto numel = x.numel(); int batches = numel / (rows * cols); @@ -481,6 +486,11 @@ void MatrixRankAtolRtolKernel(const Context& dev_ctx, errors::InvalidArgument("The input Tensor x's shape[-1] should not " "be 0, but shape is %s now.", dim_x)); + if (x.numel() == 0) { + out->Resize({0}); + dev_ctx.template Alloc(out); + return; + } int k = std::min(rows, cols); auto numel = x.numel(); int batches = numel / (rows * cols); diff --git a/test/legacy_test/test_matrix_rank_op.py b/test/legacy_test/test_matrix_rank_op.py index 52c42e0c1eb7c..c524d97238bc6 100644 --- a/test/legacy_test/test_matrix_rank_op.py +++ b/test/legacy_test/test_matrix_rank_op.py @@ -17,7 +17,6 @@ import numpy as np from op_test import OpTest -from utils import dygraph_guard, static_guard import paddle from paddle import base, static @@ -245,34 +244,49 @@ def _get_places(self): places.append(paddle.CUDAPlace(0)) return places - def _test_matrix_rank_static(self, place, shape, expected_rank): - with static_guard(): - with paddle.static.program_guard( - paddle.static.Program(), paddle.static.Program() - ): - x = paddle.static.data(name='x', shape=shape, dtype='float32') - y = paddle.linalg.matrix_rank(x) - exe = paddle.static.Executor(place) - res = exe.run( - feed={'x': np.zeros(shape, dtype='float32')}, fetch_list=[y] - ) - assert res[0] == expected_rank - - def _test_matrix_rank_dynamic(self, shape, expected_rank): - with dygraph_guard(): - x = paddle.full(shape, 1.0, dtype='float32') - y = paddle.linalg.matrix_rank(x) - assert y == expected_rank + def _test_matrix_rank_static(self, place): + with paddle.static.program_guard( + paddle.static.Program(), paddle.static.Program() + ): + # Test static graph with empty tensors + x_valid = paddle.static.data( + name='x_valid', shape=[0, 6, 6], dtype='float32' + ) + x_invalid1 = paddle.static.data( + name='x_invalid1', shape=[0, 0], dtype='float32' + ) + x_invalid2 = paddle.static.data( + name='x_invalid2', shape=[2, 3, 0, 0], dtype='float32' + ) + + self.assertRaises(TypeError, paddle.linalg.matrix_rank, x_invalid1) + self.assertRaises(TypeError, paddle.linalg.matrix_rank, x_invalid2) + + y_valid = paddle.linalg.matrix_rank(x_valid) + + exe = paddle.static.Executor(place) + res_valid = exe.run( + feed={'x_valid': np.zeros((0, 6, 6), dtype='float32')}, + fetch_list=[y_valid], + ) + self.assertEqual(res_valid, 0) + + def _test_matrix_rank_dynamic(self): + with paddle.disable_static(): + x_valid = paddle.full((0, 6, 6), 1.0, dtype='float32') + x_invalid1 = paddle.full((0, 0), 1.0, dtype='float32') + x_invalid2 = paddle.full((2, 3, 0, 0), 1.0, dtype='float32') + + self.assertRaises(TypeError, paddle.linalg.matrix_rank, x_invalid1) + self.assertRaises(TypeError, paddle.linalg.matrix_rank, x_invalid2) + + y_valid = paddle.linalg.matrix_rank(x_valid) + self.assertEqual(y_valid, 0) def test_matrix_rank_tensor(self): - test_cases = [([0, 0], 0), ([0, 6], 0), ([6, 0], 0), ([2, 3, 0, 0], 0)] - for place in self._get_places(): - for shape, expected_rank in test_cases: - self._test_matrix_rank_static(place, shape, expected_rank) - - for shape, expected_rank in test_cases: - self._test_matrix_rank_dynamic(shape, expected_rank) + self._test_matrix_rank_static(place) + self._test_matrix_rank_dynamic() if __name__ == '__main__': From d29d928456b3ba5f8f17ee7f49037fccaebfc915 Mon Sep 17 00:00:00 2001 From: ZHOU05030 <112939094+ZHOU05030@users.noreply.github.com> Date: Thu, 12 Dec 2024 16:35:58 +0800 Subject: [PATCH 06/12] fix --- paddle/phi/core/dense_tensor_impl.cc | 5 ----- 1 file changed, 5 deletions(-) diff --git a/paddle/phi/core/dense_tensor_impl.cc b/paddle/phi/core/dense_tensor_impl.cc index 9e6a1867d5681..7a886f90b0376 100644 --- a/paddle/phi/core/dense_tensor_impl.cc +++ b/paddle/phi/core/dense_tensor_impl.cc @@ -40,11 +40,6 @@ size_t DenseTensor::memory_size() const { } void DenseTensor::check_memory_size() const { - if (numel() == 0) { - // For zero-sized tensors, typically no memory verification is needed, - // or consider special handling if necessary. - return; - } PADDLE_ENFORCE_NOT_NULL( holder_, common::errors::PreconditionNotMet("Tensor holds no memory. " From a35df75860641a065f0a13c9c6505edc1e1a5ce2 Mon Sep 17 00:00:00 2001 From: ZHOU05030 <112939094+ZHOU05030@users.noreply.github.com> Date: Thu, 12 Dec 2024 17:27:28 +0800 Subject: [PATCH 07/12] fix --- test/legacy_test/test_matrix_rank_op.py | 47 ++++++++++--------------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/test/legacy_test/test_matrix_rank_op.py b/test/legacy_test/test_matrix_rank_op.py index c524d97238bc6..f6a766fcb4d2c 100644 --- a/test/legacy_test/test_matrix_rank_op.py +++ b/test/legacy_test/test_matrix_rank_op.py @@ -17,6 +17,7 @@ import numpy as np from op_test import OpTest +from utils import dygraph_guard, static_guard import paddle from paddle import base, static @@ -245,43 +246,33 @@ def _get_places(self): return places def _test_matrix_rank_static(self, place): - with paddle.static.program_guard( - paddle.static.Program(), paddle.static.Program() - ): - # Test static graph with empty tensors - x_valid = paddle.static.data( - name='x_valid', shape=[0, 6, 6], dtype='float32' - ) - x_invalid1 = paddle.static.data( - name='x_invalid1', shape=[0, 0], dtype='float32' - ) - x_invalid2 = paddle.static.data( - name='x_invalid2', shape=[2, 3, 0, 0], dtype='float32' - ) - - self.assertRaises(TypeError, paddle.linalg.matrix_rank, x_invalid1) - self.assertRaises(TypeError, paddle.linalg.matrix_rank, x_invalid2) + with static_guard(): + with paddle.static.program_guard( + paddle.static.Program(), paddle.static.Program() + ): + x_valid = paddle.static.data( + name='x_valid', shape=[0, 6, 6], dtype='float32' + ) - y_valid = paddle.linalg.matrix_rank(x_valid) + y_valid = paddle.linalg.matrix_rank(x_valid) - exe = paddle.static.Executor(place) - res_valid = exe.run( - feed={'x_valid': np.zeros((0, 6, 6), dtype='float32')}, - fetch_list=[y_valid], - ) - self.assertEqual(res_valid, 0) + exe = paddle.static.Executor(place) + res_valid = exe.run( + feed={'x_valid': np.zeros((0, 6, 6), dtype='float32')}, + fetch_list=[y_valid], + ) + self.assertEqual(res_valid[0].shape, (0,)) def _test_matrix_rank_dynamic(self): - with paddle.disable_static(): + with dygraph_guard(): x_valid = paddle.full((0, 6, 6), 1.0, dtype='float32') x_invalid1 = paddle.full((0, 0), 1.0, dtype='float32') x_invalid2 = paddle.full((2, 3, 0, 0), 1.0, dtype='float32') - - self.assertRaises(TypeError, paddle.linalg.matrix_rank, x_invalid1) - self.assertRaises(TypeError, paddle.linalg.matrix_rank, x_invalid2) + self.assertRaises(ValueError, paddle.linalg.matrix_rank, x_invalid1) + self.assertRaises(ValueError, paddle.linalg.matrix_rank, x_invalid2) y_valid = paddle.linalg.matrix_rank(x_valid) - self.assertEqual(y_valid, 0) + self.assertEqual(y_valid.shape, [0]) def test_matrix_rank_tensor(self): for place in self._get_places(): From f2ef7a6d328fece0546b16e08726879e2fe1321d Mon Sep 17 00:00:00 2001 From: ZHOU05030 <112939094+ZHOU05030@users.noreply.github.com> Date: Fri, 13 Dec 2024 16:22:10 +0800 Subject: [PATCH 08/12] fix --- .../test_matrix_rank_atol_rtol_op.py | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/test/legacy_test/test_matrix_rank_atol_rtol_op.py b/test/legacy_test/test_matrix_rank_atol_rtol_op.py index 456b173a48ebc..62ea8e6bb6dc7 100644 --- a/test/legacy_test/test_matrix_rank_atol_rtol_op.py +++ b/test/legacy_test/test_matrix_rank_atol_rtol_op.py @@ -12,10 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. +import os import unittest import numpy as np from op_test import OpTest +from utils import dygraph_guard, static_guard import paddle from paddle import base, static @@ -474,5 +476,70 @@ def test_errors(self): paddle.linalg.matrix_rank(x, tol=0.2, hermitian=True, rtol=0.2) +class TestMatrixRankAtolRtolEmptyTensor(unittest.TestCase): + + def _get_places(self): + places = [] + if ( + os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() + in ['1', 'true', 'on'] + or not paddle.is_compiled_with_cuda() + ): + places.append(paddle.CPUPlace()) + if paddle.is_compiled_with_cuda(): + places.append(paddle.CUDAPlace(0)) + return places + + def _test_matrix_rank_static(self, place, atol, rtol): + with static_guard(): + with paddle.static.program_guard( + paddle.static.Program(), paddle.static.Program() + ): + x_valid = paddle.static.data( + name='x_valid', shape=[0, 6, 6], dtype='float32' + ) + + y_valid = paddle.linalg.matrix_rank( + x_valid, atol=atol, rtol=rtol + ) + + exe = paddle.static.Executor(place) + res_valid = exe.run( + feed={'x_valid': np.zeros((0, 6, 6), dtype='float32')}, + fetch_list=[y_valid], + ) + self.assertEqual(res_valid[0].shape, (0,)) + + def _test_matrix_rank_dynamic(self, atol, rtol): + with dygraph_guard(): + x_valid = paddle.full((0, 6, 6), 1.0, dtype='float32') + x_invalid1 = paddle.full((0, 0), 1.0, dtype='float32') + x_invalid2 = paddle.full((2, 3, 0, 0), 1.0, dtype='float32') + self.assertRaises( + ValueError, + paddle.linalg.matrix_rank, + x_invalid1, + atol=atol, + rtol=rtol, + ) + self.assertRaises( + ValueError, + paddle.linalg.matrix_rank, + x_invalid2, + atol=atol, + rtol=rtol, + ) + + y_valid = paddle.linalg.matrix_rank(x_valid, atol=atol, rtol=rtol) + self.assertEqual(y_valid.shape, [0]) + + def test_matrix_rank_tensor(self): + atol = 0.2 + rtol = 0.2 + for place in [paddle.CPUPlace()]: + self._test_matrix_rank_static(place, atol, rtol) + self._test_matrix_rank_dynamic(atol, rtol) + + if __name__ == '__main__': unittest.main() From 1ea98ed2b98eba9c6145b8ae3563c004f779db78 Mon Sep 17 00:00:00 2001 From: ZHOU05030 <112939094+ZHOU05030@users.noreply.github.com> Date: Sat, 14 Dec 2024 19:47:12 +0800 Subject: [PATCH 09/12] fix --- paddle/phi/kernels/cpu/matrix_rank_tol_kernel.cc | 2 +- paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu | 9 +-------- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/paddle/phi/kernels/cpu/matrix_rank_tol_kernel.cc b/paddle/phi/kernels/cpu/matrix_rank_tol_kernel.cc index bff8be100bb3c..1ed81b2d8da53 100644 --- a/paddle/phi/kernels/cpu/matrix_rank_tol_kernel.cc +++ b/paddle/phi/kernels/cpu/matrix_rank_tol_kernel.cc @@ -196,7 +196,6 @@ void MatrixRankAtolRtolKernel(const Context& dev_ctx, const paddle::optional& rtol, bool hermitian, DenseTensor* out) { - dev_ctx.template Alloc(out); auto dim_x = x.dims(); auto dim_out = out->dims(); int rows = static_cast(dim_x[dim_x.size() - 2]); @@ -218,6 +217,7 @@ void MatrixRankAtolRtolKernel(const Context& dev_ctx, dev_ctx.template Alloc(out); return; } + dev_ctx.template Alloc(out); int k = std::min(rows, cols); int batches = static_cast(x.numel() / (rows * cols)); diff --git a/paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu b/paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu index 0d4e69fe51c74..cdb62e62f6d42 100644 --- a/paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu +++ b/paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu @@ -461,15 +461,7 @@ void MatrixRankAtolRtolKernel(const Context& dev_ctx, const paddle::optional& rtol, bool hermitian, DenseTensor* out) { - if (x.numel() == 0) { - out->Resize({1}); - auto* out_data = dev_ctx.template Alloc(out); - out_data[0] = 0; - return; - } auto* x_data = x.data(); - dev_ctx.template Alloc(out); - auto dim_x = x.dims(); auto dim_out = out->dims(); int rows = dim_x[dim_x.size() - 2]; @@ -491,6 +483,7 @@ void MatrixRankAtolRtolKernel(const Context& dev_ctx, dev_ctx.template Alloc(out); return; } + dev_ctx.template Alloc(out); int k = std::min(rows, cols); auto numel = x.numel(); int batches = numel / (rows * cols); From 9c8adc6cd69b10299625b108e26e4b50e5675114 Mon Sep 17 00:00:00 2001 From: ZHOU05030 <112939094+ZHOU05030@users.noreply.github.com> Date: Sun, 15 Dec 2024 16:12:01 +0800 Subject: [PATCH 10/12] fix --- test/legacy_test/test_matrix_rank_op.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/test/legacy_test/test_matrix_rank_op.py b/test/legacy_test/test_matrix_rank_op.py index f6a766fcb4d2c..ac05bfc45ad82 100644 --- a/test/legacy_test/test_matrix_rank_op.py +++ b/test/legacy_test/test_matrix_rank_op.py @@ -263,8 +263,21 @@ def _test_matrix_rank_static(self, place): ) self.assertEqual(res_valid[0].shape, (0,)) - def _test_matrix_rank_dynamic(self): + def _test_matrix_rank_dynamic_cpu(self): with dygraph_guard(): + paddle.set_device("cpu") + x_valid = paddle.full((0, 6, 6), 1.0, dtype='float32') + x_invalid1 = paddle.full((0, 0), 1.0, dtype='float32') + x_invalid2 = paddle.full((2, 3, 0, 0), 1.0, dtype='float32') + self.assertRaises(ValueError, paddle.linalg.matrix_rank, x_invalid1) + self.assertRaises(ValueError, paddle.linalg.matrix_rank, x_invalid2) + + y_valid = paddle.linalg.matrix_rank(x_valid) + self.assertEqual(y_valid.shape, [0]) + + def _test_matrix_rank_dynamic_gpu(self): + with dygraph_guard(): + paddle.set_device("gpu") x_valid = paddle.full((0, 6, 6), 1.0, dtype='float32') x_invalid1 = paddle.full((0, 0), 1.0, dtype='float32') x_invalid2 = paddle.full((2, 3, 0, 0), 1.0, dtype='float32') @@ -277,7 +290,8 @@ def _test_matrix_rank_dynamic(self): def test_matrix_rank_tensor(self): for place in self._get_places(): self._test_matrix_rank_static(place) - self._test_matrix_rank_dynamic() + self._test_matrix_rank_dynamic_cpu() + self._test_matrix_rank_dynamic_gpu() if __name__ == '__main__': From 19874697b37ced6d7fbcd658ba4ee0d8cec3b67f Mon Sep 17 00:00:00 2001 From: ZHOU05030 <112939094+ZHOU05030@users.noreply.github.com> Date: Sun, 15 Dec 2024 18:02:12 +0800 Subject: [PATCH 11/12] fix --- test/legacy_test/test_matrix_rank_op.py | 1 - 1 file changed, 1 deletion(-) diff --git a/test/legacy_test/test_matrix_rank_op.py b/test/legacy_test/test_matrix_rank_op.py index ac05bfc45ad82..d8c7548378319 100644 --- a/test/legacy_test/test_matrix_rank_op.py +++ b/test/legacy_test/test_matrix_rank_op.py @@ -277,7 +277,6 @@ def _test_matrix_rank_dynamic_cpu(self): def _test_matrix_rank_dynamic_gpu(self): with dygraph_guard(): - paddle.set_device("gpu") x_valid = paddle.full((0, 6, 6), 1.0, dtype='float32') x_invalid1 = paddle.full((0, 0), 1.0, dtype='float32') x_invalid2 = paddle.full((2, 3, 0, 0), 1.0, dtype='float32') From dfae287877ed6fa4362aff6cb71d1dc2e3fc06df Mon Sep 17 00:00:00 2001 From: ZHOU05030 <112939094+ZHOU05030@users.noreply.github.com> Date: Mon, 16 Dec 2024 13:17:47 +0800 Subject: [PATCH 12/12] fix --- .../phi/kernels/cpu/matrix_rank_tol_kernel.cc | 20 +++++++++++++------ .../phi/kernels/gpu/matrix_rank_tol_kernel.cu | 20 +++++++++++++------ .../test_matrix_rank_atol_rtol_op.py | 12 +++++------ test/legacy_test/test_matrix_rank_op.py | 16 +++++++-------- 4 files changed, 42 insertions(+), 26 deletions(-) diff --git a/paddle/phi/kernels/cpu/matrix_rank_tol_kernel.cc b/paddle/phi/kernels/cpu/matrix_rank_tol_kernel.cc index 1ed81b2d8da53..b4d9fd2879549 100644 --- a/paddle/phi/kernels/cpu/matrix_rank_tol_kernel.cc +++ b/paddle/phi/kernels/cpu/matrix_rank_tol_kernel.cc @@ -95,16 +95,20 @@ void MatrixRankTolKernel(const Context& dev_ctx, rows, 0, errors::InvalidArgument("The input Tensor x's shape[-2] should not " - "be 0, but shape is %s now.", + "be 0, but received %s now.", dim_x)); PADDLE_ENFORCE_NE( cols, 0, errors::InvalidArgument("The input Tensor x's shape[-1] should not " - "be 0, but shape is %s now.", + "be 0, but received %s now.", dim_x)); if (x.numel() == 0) { - out->Resize({0}); + std::vector out_dims_vec(dim_x.size() - 2); + for (int i = 0; i < dim_x.size() - 2; ++i) { + out_dims_vec[i] = dim_x[i]; + } + out->Resize(phi::make_ddim(out_dims_vec)); dev_ctx.template Alloc(out); return; } @@ -204,16 +208,20 @@ void MatrixRankAtolRtolKernel(const Context& dev_ctx, rows, 0, errors::InvalidArgument("The input Tensor x's shape[-2] should not " - "be 0, but shape is %s now.", + "be 0, but received %s now.", dim_x)); PADDLE_ENFORCE_NE( cols, 0, errors::InvalidArgument("The input Tensor x's shape[-1] should not " - "be 0, but shape is %s now.", + "be 0, but received %s now.", dim_x)); if (x.numel() == 0) { - out->Resize({0}); + std::vector out_dims_vec(dim_x.size() - 2); + for (int i = 0; i < dim_x.size() - 2; ++i) { + out_dims_vec[i] = dim_x[i]; + } + out->Resize(phi::make_ddim(out_dims_vec)); dev_ctx.template Alloc(out); return; } diff --git a/paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu b/paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu index cdb62e62f6d42..7e387744238a9 100644 --- a/paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu +++ b/paddle/phi/kernels/gpu/matrix_rank_tol_kernel.cu @@ -346,16 +346,20 @@ void MatrixRankTolKernel(const Context& dev_ctx, 0, common::errors::InvalidArgument( "The input Tensor x's shape[-2] should not " - "be 0, but shape is %s now.", + "be 0, but received %s now.", dim_x)); PADDLE_ENFORCE_NE(cols, 0, common::errors::InvalidArgument( "The input Tensor x's shape[-1] should not " - "be 0, but shape is %s now.", + "be 0, but received %s now.", dim_x)); if (x.numel() == 0) { - out->Resize({0}); + std::vector out_dims_vec(dim_x.size() - 2); + for (int i = 0; i < dim_x.size() - 2; ++i) { + out_dims_vec[i] = dim_x[i]; + } + out->Resize(phi::make_ddim(out_dims_vec)); dev_ctx.template Alloc(out); return; } @@ -470,16 +474,20 @@ void MatrixRankAtolRtolKernel(const Context& dev_ctx, rows, 0, errors::InvalidArgument("The input Tensor x's shape[-2] should not " - "be 0, but shape is %s now.", + "be 0, but received %s now.", dim_x)); PADDLE_ENFORCE_NE( cols, 0, errors::InvalidArgument("The input Tensor x's shape[-1] should not " - "be 0, but shape is %s now.", + "be 0, but received %s now.", dim_x)); if (x.numel() == 0) { - out->Resize({0}); + std::vector out_dims_vec(dim_x.size() - 2); + for (int i = 0; i < dim_x.size() - 2; ++i) { + out_dims_vec[i] = dim_x[i]; + } + out->Resize(phi::make_ddim(out_dims_vec)); dev_ctx.template Alloc(out); return; } diff --git a/test/legacy_test/test_matrix_rank_atol_rtol_op.py b/test/legacy_test/test_matrix_rank_atol_rtol_op.py index 62ea8e6bb6dc7..cf30dafae859c 100644 --- a/test/legacy_test/test_matrix_rank_atol_rtol_op.py +++ b/test/legacy_test/test_matrix_rank_atol_rtol_op.py @@ -476,7 +476,7 @@ def test_errors(self): paddle.linalg.matrix_rank(x, tol=0.2, hermitian=True, rtol=0.2) -class TestMatrixRankAtolRtolEmptyTensor(unittest.TestCase): +class TestMatrixRankAtolRtolZeroSizeTensor(unittest.TestCase): def _get_places(self): places = [] @@ -496,7 +496,7 @@ def _test_matrix_rank_static(self, place, atol, rtol): paddle.static.Program(), paddle.static.Program() ): x_valid = paddle.static.data( - name='x_valid', shape=[0, 6, 6], dtype='float32' + name='x_valid', shape=[2, 0, 6, 6], dtype='float32' ) y_valid = paddle.linalg.matrix_rank( @@ -505,14 +505,14 @@ def _test_matrix_rank_static(self, place, atol, rtol): exe = paddle.static.Executor(place) res_valid = exe.run( - feed={'x_valid': np.zeros((0, 6, 6), dtype='float32')}, + feed={'x_valid': np.zeros((2, 0, 6, 6), dtype='float32')}, fetch_list=[y_valid], ) - self.assertEqual(res_valid[0].shape, (0,)) + self.assertEqual(res_valid[0].shape, tuple(x_valid.shape[:-2])) def _test_matrix_rank_dynamic(self, atol, rtol): with dygraph_guard(): - x_valid = paddle.full((0, 6, 6), 1.0, dtype='float32') + x_valid = paddle.full((2, 0, 6, 6), 1.0, dtype='float32') x_invalid1 = paddle.full((0, 0), 1.0, dtype='float32') x_invalid2 = paddle.full((2, 3, 0, 0), 1.0, dtype='float32') self.assertRaises( @@ -531,7 +531,7 @@ def _test_matrix_rank_dynamic(self, atol, rtol): ) y_valid = paddle.linalg.matrix_rank(x_valid, atol=atol, rtol=rtol) - self.assertEqual(y_valid.shape, [0]) + self.assertEqual(y_valid.shape, x_valid.shape[:-2]) def test_matrix_rank_tensor(self): atol = 0.2 diff --git a/test/legacy_test/test_matrix_rank_op.py b/test/legacy_test/test_matrix_rank_op.py index d8c7548378319..e529576d0304e 100644 --- a/test/legacy_test/test_matrix_rank_op.py +++ b/test/legacy_test/test_matrix_rank_op.py @@ -231,7 +231,7 @@ def test_static(self): np.testing.assert_allclose(fetches[0], rank_np, rtol=1e-05) -class TestMatrixRankEmptyTensor(unittest.TestCase): +class TestMatrixRankZeroSizeTensor(unittest.TestCase): def _get_places(self): places = [] @@ -251,40 +251,40 @@ def _test_matrix_rank_static(self, place): paddle.static.Program(), paddle.static.Program() ): x_valid = paddle.static.data( - name='x_valid', shape=[0, 6, 6], dtype='float32' + name='x_valid', shape=[2, 0, 6, 6], dtype='float32' ) y_valid = paddle.linalg.matrix_rank(x_valid) exe = paddle.static.Executor(place) res_valid = exe.run( - feed={'x_valid': np.zeros((0, 6, 6), dtype='float32')}, + feed={'x_valid': np.zeros((2, 0, 6, 6), dtype='float32')}, fetch_list=[y_valid], ) - self.assertEqual(res_valid[0].shape, (0,)) + self.assertEqual(res_valid[0].shape, tuple(x_valid.shape[:-2])) def _test_matrix_rank_dynamic_cpu(self): with dygraph_guard(): paddle.set_device("cpu") - x_valid = paddle.full((0, 6, 6), 1.0, dtype='float32') + x_valid = paddle.full((2, 0, 6, 6), 1.0, dtype='float32') x_invalid1 = paddle.full((0, 0), 1.0, dtype='float32') x_invalid2 = paddle.full((2, 3, 0, 0), 1.0, dtype='float32') self.assertRaises(ValueError, paddle.linalg.matrix_rank, x_invalid1) self.assertRaises(ValueError, paddle.linalg.matrix_rank, x_invalid2) y_valid = paddle.linalg.matrix_rank(x_valid) - self.assertEqual(y_valid.shape, [0]) + self.assertEqual(y_valid.shape, x_valid.shape[:-2]) def _test_matrix_rank_dynamic_gpu(self): with dygraph_guard(): - x_valid = paddle.full((0, 6, 6), 1.0, dtype='float32') + x_valid = paddle.full((2, 0, 6, 6), 1.0, dtype='float32') x_invalid1 = paddle.full((0, 0), 1.0, dtype='float32') x_invalid2 = paddle.full((2, 3, 0, 0), 1.0, dtype='float32') self.assertRaises(ValueError, paddle.linalg.matrix_rank, x_invalid1) self.assertRaises(ValueError, paddle.linalg.matrix_rank, x_invalid2) y_valid = paddle.linalg.matrix_rank(x_valid) - self.assertEqual(y_valid.shape, [0]) + self.assertEqual(y_valid.shape, x_valid.shape[:-2]) def test_matrix_rank_tensor(self): for place in self._get_places():