From 466910bcfca40131b68213d07951a8d79deb2685 Mon Sep 17 00:00:00 2001 From: Kyotak <2023352046@email.szu.edu.cn> Date: Fri, 10 Jan 2025 21:36:42 +0800 Subject: [PATCH 1/5] fix --- paddle/phi/kernels/cpu/cholesky_kernel.cc | 5 +++++ paddle/phi/kernels/gpu/cholesky_kernel.cu | 5 +++++ .../kernels/impl/cholesky_grad_kernel_impl.h | 5 +++++ test/legacy_test/test_cholesky_op.py | 17 +++++++++++++---- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/paddle/phi/kernels/cpu/cholesky_kernel.cc b/paddle/phi/kernels/cpu/cholesky_kernel.cc index cf5098a1f2c605..aaa312995d2826 100644 --- a/paddle/phi/kernels/cpu/cholesky_kernel.cc +++ b/paddle/phi/kernels/cpu/cholesky_kernel.cc @@ -33,6 +33,11 @@ void CholeskyKernel(const Context& dev_ctx, using OutputMatrixMap = Eigen::Map; auto& dims = x.dims(); + if (x.numel() == 0) { + out->Resize(dims); + dev_ctx.template Alloc(out); + return; + } int batch_count = 1; for (int i = 0; i < dims.size() - 2; i++) { batch_count *= static_cast(dims[i]); diff --git a/paddle/phi/kernels/gpu/cholesky_kernel.cu b/paddle/phi/kernels/gpu/cholesky_kernel.cu index 16e854c8de4c6a..755adb96e21a44 100644 --- a/paddle/phi/kernels/gpu/cholesky_kernel.cu +++ b/paddle/phi/kernels/gpu/cholesky_kernel.cu @@ -115,6 +115,11 @@ void CholeskyKernel(const Context& dev_ctx, bool upper, DenseTensor* out) { auto& dims = x.dims(); + if (x.numel() == 0) { + out->Resize(dims); + dev_ctx.template Alloc(out); + return; + } int batch_count = 1; for (int i = 0; i < dims.size() - 2; i++) { batch_count *= dims[i]; diff --git a/paddle/phi/kernels/impl/cholesky_grad_kernel_impl.h b/paddle/phi/kernels/impl/cholesky_grad_kernel_impl.h index 8e5fab281e795b..8a2e1484302f0a 100644 --- a/paddle/phi/kernels/impl/cholesky_grad_kernel_impl.h +++ b/paddle/phi/kernels/impl/cholesky_grad_kernel_impl.h @@ -245,6 +245,11 @@ void CholeskyGradKernel(const Context& dev_ctx, auto* x_grad_data = dev_ctx.template Alloc(x_grad); auto& dims = out.dims(); + if (out.numel() == 0) { + x_grad->Resize(dims); + dev_ctx.template Alloc(x_grad); + return; + } int batch_count = 1; for (int i = 0; i < dims.size() - 2; i++) { batch_count *= dims[i]; diff --git a/test/legacy_test/test_cholesky_op.py b/test/legacy_test/test_cholesky_op.py index 461464bdfeef2f..d177a2a5541d60 100644 --- a/test/legacy_test/test_cholesky_op.py +++ b/test/legacy_test/test_cholesky_op.py @@ -161,6 +161,11 @@ def init_config(self): self._input_shape = (32, 32) +class TestCholeskyOp0Size(TestCholeskyOp): + def init_config(self): + self._input_shape = (0, 2) + + class TestDygraph(unittest.TestCase): def test_dygraph(self): if core.is_compiled_with_rocm(): @@ -187,16 +192,16 @@ def setUp(self): if core.is_compiled_with_cuda() and (not core.is_compiled_with_rocm()): self.places.append(base.CUDAPlace(0)) - def check_static_result(self, place, with_out=False): + def check_static_result(self, place, input_shape, with_out=False): with paddle.static.program_guard( paddle.static.Program(), paddle.static.Program() ): input = paddle.static.data( - name="input", shape=[4, 4], dtype="float64" + name="input", shape=input_shape, dtype="float64" ) result = paddle.cholesky(input) - input_np = np.zeros([4, 4]).astype("float64") + input_np = np.zeros(input_shape).astype("float64") exe = base.Executor(place) try: @@ -211,7 +216,8 @@ def check_static_result(self, place, with_out=False): def test_static(self): for place in self.places: - self.check_static_result(place=place) + self.check_static_result(place=place, input_shape=[4, 4]) + self.check_static_result(place=place, input_shape=[2, 0]) def test_dygraph(self): for place in self.places: @@ -222,9 +228,12 @@ def test_dygraph(self): [[10, 11, 12], [13, 14, 15], [16, 17, 18]], ] ).astype("float64") + input_np_zero = np.zeros((0, 3, 3), dtype="float64") input = paddle.to_tensor(input_np) + input_zero = paddle.to_tensor(input_np_zero) try: result = paddle.cholesky(input) + result_zero = paddle.cholesky(input_zero) except RuntimeError as ex: print("The mat is singular") except ValueError as ex: From 9004e2e3cac6a4498cf566667d42f5e14ab85299 Mon Sep 17 00:00:00 2001 From: Kyotak <2023352046@email.szu.edu.cn> Date: Fri, 10 Jan 2025 21:39:18 +0800 Subject: [PATCH 2/5] update --- test/legacy_test/test_cholesky_op.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/legacy_test/test_cholesky_op.py b/test/legacy_test/test_cholesky_op.py index d177a2a5541d60..99622b4bda581f 100644 --- a/test/legacy_test/test_cholesky_op.py +++ b/test/legacy_test/test_cholesky_op.py @@ -217,7 +217,7 @@ def check_static_result(self, place, input_shape, with_out=False): def test_static(self): for place in self.places: self.check_static_result(place=place, input_shape=[4, 4]) - self.check_static_result(place=place, input_shape=[2, 0]) + self.check_static_result(place=place, input_shape=[0, 0]) def test_dygraph(self): for place in self.places: From ccee69e6e7d012bd090743302d32b15ae94385bd Mon Sep 17 00:00:00 2001 From: Kyotak <2023352046@email.szu.edu.cn> Date: Fri, 10 Jan 2025 23:17:57 +0800 Subject: [PATCH 3/5] fix --- test/legacy_test/test_cholesky_op.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/legacy_test/test_cholesky_op.py b/test/legacy_test/test_cholesky_op.py index 99622b4bda581f..81b46ee1b48689 100644 --- a/test/legacy_test/test_cholesky_op.py +++ b/test/legacy_test/test_cholesky_op.py @@ -161,7 +161,7 @@ def init_config(self): self._input_shape = (32, 32) -class TestCholeskyOp0Size(TestCholeskyOp): +class TestCholeskyOpZeroSize(TestCholeskyOp): def init_config(self): self._input_shape = (0, 2) From d21a3d337647a4db390ca4b44681b2f61e294ae3 Mon Sep 17 00:00:00 2001 From: Kyotak <2023352046@email.szu.edu.cn> Date: Sat, 11 Jan 2025 13:55:17 +0800 Subject: [PATCH 4/5] fix --- test/legacy_test/test_cholesky_op.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/legacy_test/test_cholesky_op.py b/test/legacy_test/test_cholesky_op.py index 81b46ee1b48689..7fec7de7ceaa22 100644 --- a/test/legacy_test/test_cholesky_op.py +++ b/test/legacy_test/test_cholesky_op.py @@ -163,7 +163,7 @@ def init_config(self): class TestCholeskyOpZeroSize(TestCholeskyOp): def init_config(self): - self._input_shape = (0, 2) + self._input_shape = (0, 0) class TestDygraph(unittest.TestCase): @@ -218,6 +218,7 @@ def test_static(self): for place in self.places: self.check_static_result(place=place, input_shape=[4, 4]) self.check_static_result(place=place, input_shape=[0, 0]) + self.check_static_result(place=place, input_shape=[5, 0, 0]) def test_dygraph(self): for place in self.places: From 4bf46e7909355b9b8b5f9eb9d3fab21b09a95959 Mon Sep 17 00:00:00 2001 From: Kyotak <2023352046@email.szu.edu.cn> Date: Sat, 11 Jan 2025 17:57:43 +0800 Subject: [PATCH 5/5] fix --- test/legacy_test/test_cholesky_op.py | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/test/legacy_test/test_cholesky_op.py b/test/legacy_test/test_cholesky_op.py index 7fec7de7ceaa22..97a49d34c538e0 100644 --- a/test/legacy_test/test_cholesky_op.py +++ b/test/legacy_test/test_cholesky_op.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np @@ -64,14 +63,7 @@ def test_check_output(self): self.check_output(check_pir=True) def test_check_grad(self): - places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - or core.is_compiled_with_rocm() - ): - places.append(base.CPUPlace()) + places = [base.CPUPlace()] if core.is_compiled_with_cuda() and (not core.is_compiled_with_rocm()): places.append(base.CUDAPlace(0)) for p in places: @@ -181,14 +173,7 @@ def test_dygraph(self): class TestCholeskySingularAPI(unittest.TestCase): def setUp(self): - self.places = [] - if ( - os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() - in ['1', 'true', 'on'] - or not core.is_compiled_with_cuda() - or core.is_compiled_with_rocm() - ): - self.places.append(base.CPUPlace()) + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda() and (not core.is_compiled_with_rocm()): self.places.append(base.CUDAPlace(0))