From c2c0b39fe1998857ad2427760177a802ebc1ab1b Mon Sep 17 00:00:00 2001 From: co63oc Date: Sun, 25 May 2025 09:47:27 +0800 Subject: [PATCH 1/4] Fix --- test/legacy_test/test_cartesian_prod.py | 66 +++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/test/legacy_test/test_cartesian_prod.py b/test/legacy_test/test_cartesian_prod.py index f23a2b13781f03..c3e5dc9faae46a 100644 --- a/test/legacy_test/test_cartesian_prod.py +++ b/test/legacy_test/test_cartesian_prod.py @@ -119,6 +119,72 @@ def test_dygraph(self): np.testing.assert_allclose(ref_res.flatten(), pd_res3) +class TestCartesianProd_ZeroSize(unittest.TestCase): + def setUp(self): + self.init_setting() + self.a_shape = [random.randint(1, 5)] + self.b_shape = [0] + self.a_np = np.random.random(self.a_shape).astype(self.dtype_np) + self.b_np = np.empty(0, self.dtype_np) + + self.place = [] + if ( + os.environ.get('FLAGS_CI_both_cpu_and_gpu', 'False').lower() + in ['1', 'true', 'on'] + or not paddle.is_compiled_with_cuda() + ): + self.place.append('cpu') + if paddle.is_compiled_with_cuda(): + self.place.append('gpu') + + def init_setting(self): + self.dtype_np = 'float32' + + def test_static_graph(self): + paddle.enable_static() + startup_program = paddle.static.Program() + main_program = paddle.static.Program() + for place in self.place: + with paddle.static.program_guard(main_program, startup_program): + a = paddle.static.data( + name="a", shape=self.a_shape, dtype=self.dtype_np + ) + b = paddle.static.data( + name="b", shape=self.b_shape, dtype=self.dtype_np + ) + out1 = paddle.cartesian_prod([a, b]) + exe = paddle.static.Executor(place=place) + feed_list = { + "a": self.a_np, + "b": self.b_np, + } + pd_res = exe.run( + main_program, + feed=feed_list, + fetch_list=[out1], + ) + + ref_res = np.array(list(product(self.a_np, self.b_np))).reshape( + [0, 2] + ) + print(pd_res) + print(ref_res.shape) + np.testing.assert_allclose(ref_res, pd_res[0]) + + def test_dygraph(self): + paddle.disable_static() + for place in self.place: + paddle.device.set_device(place) + a = paddle.to_tensor(self.a_np) + b = paddle.to_tensor(self.b_np) + + pd_res = paddle.cartesian_prod([a, b]) + ref_res = np.array(list(product(self.a_np, self.b_np))).reshape( + [0, 2] + ) + np.testing.assert_allclose(ref_res, pd_res) + + class TestCartesianProdErrors(unittest.TestCase): def test_errors(self): def test_input_not_1D(): From 67a767654298d0fd375d885899b4f12fb882e241 Mon Sep 17 00:00:00 2001 From: co63oc Date: Tue, 27 May 2025 15:48:39 +0800 Subject: [PATCH 2/4] Fix --- test/legacy_test/test_cartesian_prod.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/legacy_test/test_cartesian_prod.py b/test/legacy_test/test_cartesian_prod.py index c3e5dc9faae46a..c41965a07c111f 100644 --- a/test/legacy_test/test_cartesian_prod.py +++ b/test/legacy_test/test_cartesian_prod.py @@ -184,6 +184,19 @@ def test_dygraph(self): ) np.testing.assert_allclose(ref_res, pd_res) + def test_grad(self): + paddle.disable_static() + for place in self.place: + paddle.device.set_device(place) + a = paddle.to_tensor(self.a_np) + a.stop_gradient = False + b = paddle.to_tensor(self.b_np) + + out = paddle.cartesian_prod([a, b]) + loss = paddle.sum(out) + loss.backward() + np.testing.assert_allclose(a.grad.shape, a.shape) + class TestCartesianProdErrors(unittest.TestCase): def test_errors(self): From f9ba318a52030f69d494173ebb9d1c10e3885316 Mon Sep 17 00:00:00 2001 From: co63oc Date: Tue, 27 May 2025 15:51:04 +0800 Subject: [PATCH 3/4] Fix --- test/legacy_test/test_cartesian_prod.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/legacy_test/test_cartesian_prod.py b/test/legacy_test/test_cartesian_prod.py index c41965a07c111f..c902fca2125e8c 100644 --- a/test/legacy_test/test_cartesian_prod.py +++ b/test/legacy_test/test_cartesian_prod.py @@ -191,6 +191,7 @@ def test_grad(self): a = paddle.to_tensor(self.a_np) a.stop_gradient = False b = paddle.to_tensor(self.b_np) + b.stop_gradient = False out = paddle.cartesian_prod([a, b]) loss = paddle.sum(out) From a347f67658dd7694e28e6a1fa86e71b473e7f6f7 Mon Sep 17 00:00:00 2001 From: co63oc Date: Wed, 28 May 2025 13:15:48 +0800 Subject: [PATCH 4/4] Fix --- test/legacy_test/test_cartesian_prod.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/legacy_test/test_cartesian_prod.py b/test/legacy_test/test_cartesian_prod.py index c902fca2125e8c..20e426cae7562e 100644 --- a/test/legacy_test/test_cartesian_prod.py +++ b/test/legacy_test/test_cartesian_prod.py @@ -167,8 +167,6 @@ def test_static_graph(self): ref_res = np.array(list(product(self.a_np, self.b_np))).reshape( [0, 2] ) - print(pd_res) - print(ref_res.shape) np.testing.assert_allclose(ref_res, pd_res[0]) def test_dygraph(self):