From 15eb5357326db37a93ccb53c9003e5daed03a1a8 Mon Sep 17 00:00:00 2001 From: fangfangssj <1135470306@qq.com> Date: Tue, 10 Dec 2024 08:57:34 +0000 Subject: [PATCH 1/3] fix cross --- paddle/phi/kernels/cpu/cross_grad_kernel.cc | 7 +++ paddle/phi/kernels/cpu/cross_kernel.cc | 5 ++ paddle/phi/kernels/gpu/cross_grad_kernel.cu | 7 +++ paddle/phi/kernels/gpu/cross_kernel.cu | 5 ++ test/legacy_test/test_cross_op.py | 57 +++++++++++++++++++++ 5 files changed, 81 insertions(+) diff --git a/paddle/phi/kernels/cpu/cross_grad_kernel.cc b/paddle/phi/kernels/cpu/cross_grad_kernel.cc index 4c41107ba0199..b1bb210ee672f 100644 --- a/paddle/phi/kernels/cpu/cross_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/cross_grad_kernel.cc @@ -37,6 +37,13 @@ void CrossGradKernel(const Context &dev_ctx, auto *output_x_grad = x_grad; auto *output_y_grad = y_grad; int dim = axis; + if (input_x.numel() == 0 || input_y.numel() == 0) { + output_x_grad->Resize(input_x.dims()); + output_y_grad->Resize(input_y.dims()); + dev_ctx.template Alloc(output_x_grad); + dev_ctx.template Alloc(output_y_grad); + return; + } auto input_x_dims = input_x.dims(); if (dim != DDim::kMaxRank) { PADDLE_ENFORCE_EQ( diff --git a/paddle/phi/kernels/cpu/cross_kernel.cc b/paddle/phi/kernels/cpu/cross_kernel.cc index f7bbc4be6adae..d36f2abd58a22 100644 --- a/paddle/phi/kernels/cpu/cross_kernel.cc +++ b/paddle/phi/kernels/cpu/cross_kernel.cc @@ -32,6 +32,11 @@ void CrossKernel(const Context& dev_ctx, auto* output = out; int dim = axis; + if (input_x.numel() == 0 || input_y.numel() == 0) { + output->Resize(input_x.dims()); + dev_ctx.template Alloc(output); + return; + } auto input_x_dims = input_x.dims(); if (dim != DDim::kMaxRank) { diff --git a/paddle/phi/kernels/gpu/cross_grad_kernel.cu b/paddle/phi/kernels/gpu/cross_grad_kernel.cu index 33a3ce4e12f3e..5f7beac14344f 100644 --- a/paddle/phi/kernels/gpu/cross_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/cross_grad_kernel.cu @@ -84,6 +84,13 @@ void CrossGradKernel(const Context& dev_ctx, auto* output_y_grad = y_grad; int dim = axis; + if (input_x.numel() == 0 || input_y.numel() == 0) { + output_x_grad->Resize(input_x.dims()); + output_y_grad->Resize(input_y.dims()); + dev_ctx.template Alloc(output_x_grad); + dev_ctx.template Alloc(output_y_grad); + return; + } auto input_x_dims = input_x.dims(); if (dim != DDim::kMaxRank) { PADDLE_ENFORCE_EQ( diff --git a/paddle/phi/kernels/gpu/cross_kernel.cu b/paddle/phi/kernels/gpu/cross_kernel.cu index 0fa0a6f5da8c1..80f3922b7787a 100644 --- a/paddle/phi/kernels/gpu/cross_kernel.cu +++ b/paddle/phi/kernels/gpu/cross_kernel.cu @@ -63,6 +63,11 @@ void CrossKernel(const Context& dev_ctx, auto* output = out; int dim = axis; + if (input_x.numel() == 0 || input_y.numel() == 0) { + output->Resize(input_x.dims()); + dev_ctx.template Alloc(output); + return; + } auto input_x_dims = input_x.dims(); if (dim != DDim::kMaxRank) { PADDLE_ENFORCE_EQ( diff --git a/test/legacy_test/test_cross_op.py b/test/legacy_test/test_cross_op.py index b3936d101962a..71e6c9a29e603 100644 --- a/test/legacy_test/test_cross_op.py +++ b/test/legacy_test/test_cross_op.py @@ -170,6 +170,8 @@ def input_data(self): self.data_y = np.array( [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]] ).astype('float32') + self.data_x_zero = np.empty((3, 0)) + self.data_y_zero = np.empty((3, 0)) def test_cross_api(self): self.input_data() @@ -212,6 +214,41 @@ def test_cross_api(self): ) np.testing.assert_allclose(expect_out, np.array(res), rtol=1e-05) + main = paddle.static.Program() + startup = paddle.static.Program() + # case 3: + with paddle.static.program_guard(main, startup): + x = paddle.static.data(name='x', shape=[0, 3], dtype="float32") + y = paddle.static.data(name='y', shape=[0, 3], dtype="float32") + z = paddle.cross(x, y) + exe = base.Executor(base.CPUPlace()) + (res,) = exe.run( + main, + feed={'x': self.data_x_zero, 'y': self.data_y_zero}, + fetch_list=[z], + return_numpy=False, + ) + expect_out = np.empty((3, 0)) + np.testing.assert_allclose(expect_out, np.array(res), rtol=1e-05) + + main = paddle.static.Program() + startup = paddle.static.Program() + # case 4: + with paddle.static.program_guard(main, startup): + x = paddle.static.data(name='x', shape=[0, 3], dtype="float32") + y = paddle.static.data(name='y', shape=[0, 3], dtype="float32") + z = paddle.cross(x, y) + + exe = base.Executor(base.CUDAPlace(0)) + (res,) = exe.run( + main, + feed={'x': self.data_x_zero, 'y': self.data_y_zero}, + fetch_list=[z], + return_numpy=False, + ) + expect_out = np.empty((3, 0)) + np.testing.assert_allclose(expect_out, np.array(res), rtol=1e-05) + def test_cross_api1(self): with paddle.pir_utils.OldIrGuard(): self.input_data() @@ -227,6 +264,17 @@ def test_cross_api1(self): y_1 = paddle.cross(x, y, name='result') self.assertEqual(('result' in y_1.name), True) + main = paddle.static.Program() + startup = paddle.static.Program() + + # case 2: + with paddle.static.program_guard(main, startup): + x = paddle.static.data(name="x", shape=[0, 3], dtype="float32") + y = paddle.static.data(name='y', shape=[0, 3], dtype='float32') + + y_1 = paddle.cross(x, y, name='result') + self.assertEqual(('result' in y_1.name), True) + def test_dygraph_api(self): self.input_data() # case 1: @@ -250,6 +298,15 @@ def test_dygraph_api(self): ) np.testing.assert_allclose(expect_out, np_z, rtol=1e-05) + # case 3: + with base.dygraph.guard(): + x = paddle.to_tensor(self.data_x_zero) + y = paddle.to_tensor(self.data_y_zero) + z = paddle.cross(x, y, axis=1) + np_z = z.numpy() + expect_out = np.empty((3, 0)) + np.testing.assert_allclose(expect_out, np_z, rtol=1e-05) + if __name__ == '__main__': paddle.enable_static() From 0d791228372707ad116d912b799178db98812718 Mon Sep 17 00:00:00 2001 From: fangfangssj <1135470306@qq.com> Date: Tue, 10 Dec 2024 12:16:52 +0000 Subject: [PATCH 2/3] fix --- paddle/phi/kernels/cpu/cross_grad_kernel.cc | 7 ------- paddle/phi/kernels/cpu/cross_kernel.cc | 10 +++++----- paddle/phi/kernels/gpu/cross_grad_kernel.cu | 7 ------- paddle/phi/kernels/gpu/cross_kernel.cu | 10 +++++----- test/legacy_test/test_cross_op.py | 15 --------------- 5 files changed, 10 insertions(+), 39 deletions(-) diff --git a/paddle/phi/kernels/cpu/cross_grad_kernel.cc b/paddle/phi/kernels/cpu/cross_grad_kernel.cc index b1bb210ee672f..4c41107ba0199 100644 --- a/paddle/phi/kernels/cpu/cross_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/cross_grad_kernel.cc @@ -37,13 +37,6 @@ void CrossGradKernel(const Context &dev_ctx, auto *output_x_grad = x_grad; auto *output_y_grad = y_grad; int dim = axis; - if (input_x.numel() == 0 || input_y.numel() == 0) { - output_x_grad->Resize(input_x.dims()); - output_y_grad->Resize(input_y.dims()); - dev_ctx.template Alloc(output_x_grad); - dev_ctx.template Alloc(output_y_grad); - return; - } auto input_x_dims = input_x.dims(); if (dim != DDim::kMaxRank) { PADDLE_ENFORCE_EQ( diff --git a/paddle/phi/kernels/cpu/cross_kernel.cc b/paddle/phi/kernels/cpu/cross_kernel.cc index d36f2abd58a22..b641bd3c38924 100644 --- a/paddle/phi/kernels/cpu/cross_kernel.cc +++ b/paddle/phi/kernels/cpu/cross_kernel.cc @@ -32,11 +32,6 @@ void CrossKernel(const Context& dev_ctx, auto* output = out; int dim = axis; - if (input_x.numel() == 0 || input_y.numel() == 0) { - output->Resize(input_x.dims()); - dev_ctx.template Alloc(output); - return; - } auto input_x_dims = input_x.dims(); if (dim != DDim::kMaxRank) { @@ -75,6 +70,11 @@ void CrossKernel(const Context& dev_ctx, "But received: Input(X/Y).dims() == [%s].", input_x_dims)); } + if (input_x.numel() == 0 || input_y.numel() == 0) { + output->Resize(input_x.dims()); + dev_ctx.template Alloc(output); + return; + } auto outer_loops = 1; for (auto i = 0; i < dim; i++) { outer_loops *= static_cast(input_x_dims[i]); diff --git a/paddle/phi/kernels/gpu/cross_grad_kernel.cu b/paddle/phi/kernels/gpu/cross_grad_kernel.cu index 5f7beac14344f..33a3ce4e12f3e 100644 --- a/paddle/phi/kernels/gpu/cross_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/cross_grad_kernel.cu @@ -84,13 +84,6 @@ void CrossGradKernel(const Context& dev_ctx, auto* output_y_grad = y_grad; int dim = axis; - if (input_x.numel() == 0 || input_y.numel() == 0) { - output_x_grad->Resize(input_x.dims()); - output_y_grad->Resize(input_y.dims()); - dev_ctx.template Alloc(output_x_grad); - dev_ctx.template Alloc(output_y_grad); - return; - } auto input_x_dims = input_x.dims(); if (dim != DDim::kMaxRank) { PADDLE_ENFORCE_EQ( diff --git a/paddle/phi/kernels/gpu/cross_kernel.cu b/paddle/phi/kernels/gpu/cross_kernel.cu index 80f3922b7787a..624cbe0c8bc5a 100644 --- a/paddle/phi/kernels/gpu/cross_kernel.cu +++ b/paddle/phi/kernels/gpu/cross_kernel.cu @@ -63,11 +63,6 @@ void CrossKernel(const Context& dev_ctx, auto* output = out; int dim = axis; - if (input_x.numel() == 0 || input_y.numel() == 0) { - output->Resize(input_x.dims()); - dev_ctx.template Alloc(output); - return; - } auto input_x_dims = input_x.dims(); if (dim != DDim::kMaxRank) { PADDLE_ENFORCE_EQ( @@ -106,6 +101,11 @@ void CrossKernel(const Context& dev_ctx, input_x_dims)); } + if (input_x.numel() == 0 || input_y.numel() == 0) { + output->Resize(input_x.dims()); + dev_ctx.template Alloc(output); + return; + } std::vector cal_dims; std::vector left_strides; std::vector full_strides; diff --git a/test/legacy_test/test_cross_op.py b/test/legacy_test/test_cross_op.py index 71e6c9a29e603..304449fd0bc60 100644 --- a/test/legacy_test/test_cross_op.py +++ b/test/legacy_test/test_cross_op.py @@ -233,21 +233,6 @@ def test_cross_api(self): main = paddle.static.Program() startup = paddle.static.Program() - # case 4: - with paddle.static.program_guard(main, startup): - x = paddle.static.data(name='x', shape=[0, 3], dtype="float32") - y = paddle.static.data(name='y', shape=[0, 3], dtype="float32") - z = paddle.cross(x, y) - - exe = base.Executor(base.CUDAPlace(0)) - (res,) = exe.run( - main, - feed={'x': self.data_x_zero, 'y': self.data_y_zero}, - fetch_list=[z], - return_numpy=False, - ) - expect_out = np.empty((3, 0)) - np.testing.assert_allclose(expect_out, np.array(res), rtol=1e-05) def test_cross_api1(self): with paddle.pir_utils.OldIrGuard(): From eedea309bbe7e6aa1060a6d21dcfdaf3e05e70c4 Mon Sep 17 00:00:00 2001 From: fangfangssj Date: Wed, 11 Dec 2024 04:40:05 +0000 Subject: [PATCH 3/3] fix test --- test/legacy_test/test_cross_op.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/legacy_test/test_cross_op.py b/test/legacy_test/test_cross_op.py index 304449fd0bc60..9bc4b71464467 100644 --- a/test/legacy_test/test_cross_op.py +++ b/test/legacy_test/test_cross_op.py @@ -170,8 +170,8 @@ def input_data(self): self.data_y = np.array( [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]] ).astype('float32') - self.data_x_zero = np.empty((3, 0)) - self.data_y_zero = np.empty((3, 0)) + self.data_x_zero = np.array([]).reshape(0, 3).astype('float32') + self.data_y_zero = np.array([]).reshape(0, 3).astype('float32') def test_cross_api(self): self.input_data() @@ -220,7 +220,7 @@ def test_cross_api(self): with paddle.static.program_guard(main, startup): x = paddle.static.data(name='x', shape=[0, 3], dtype="float32") y = paddle.static.data(name='y', shape=[0, 3], dtype="float32") - z = paddle.cross(x, y) + z = paddle.cross(x, y, axis=1) exe = base.Executor(base.CPUPlace()) (res,) = exe.run( main, @@ -228,7 +228,7 @@ def test_cross_api(self): fetch_list=[z], return_numpy=False, ) - expect_out = np.empty((3, 0)) + expect_out = np.empty((0, 3)) np.testing.assert_allclose(expect_out, np.array(res), rtol=1e-05) main = paddle.static.Program() @@ -257,7 +257,7 @@ def test_cross_api1(self): x = paddle.static.data(name="x", shape=[0, 3], dtype="float32") y = paddle.static.data(name='y', shape=[0, 3], dtype='float32') - y_1 = paddle.cross(x, y, name='result') + y_1 = paddle.cross(x, y, axis=1, name='result') self.assertEqual(('result' in y_1.name), True) def test_dygraph_api(self): @@ -289,7 +289,7 @@ def test_dygraph_api(self): y = paddle.to_tensor(self.data_y_zero) z = paddle.cross(x, y, axis=1) np_z = z.numpy() - expect_out = np.empty((3, 0)) + expect_out = np.empty((0, 3)) np.testing.assert_allclose(expect_out, np_z, rtol=1e-05)