From c51a08bb3ea78a89b3207f5ed64ffbd1f4e27dc6 Mon Sep 17 00:00:00 2001 From: longranger2 <836253168@qq.com> Date: Sun, 5 Nov 2023 15:42:41 +0800 Subject: [PATCH 1/8] migrate empty_like, eye into pir --- python/paddle/tensor/creation.py | 4 ++-- test/legacy_test/test_eye_op.py | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 06932f5c9b5672..cabc8f8e88e562 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -1199,7 +1199,7 @@ def _check_attr(attr, message): else: num_columns = num_rows - if in_dynamic_mode(): + if in_dynamic_or_pir_mode(): out = _C_ops.eye( num_rows, num_columns, dtype, _current_expected_place() ) @@ -2177,7 +2177,7 @@ def empty_like(x, dtype=None, name=None): dtype = x.dtype dtype = convert_dtype(dtype) - if in_dynamic_mode(): + if in_dynamic_or_pir_mode(): out = _C_ops.empty( x.shape, convert_np_dtype_to_dtype_(dtype), diff --git a/test/legacy_test/test_eye_op.py b/test/legacy_test/test_eye_op.py index bd1b42183364c6..501450ebebedb8 100644 --- a/test/legacy_test/test_eye_op.py +++ b/test/legacy_test/test_eye_op.py @@ -23,6 +23,7 @@ from paddle import base from paddle.base import core, framework from paddle.base.framework import Program, program_guard +from paddle.pir_utils import test_with_pir_api class TestEyeOp(OpTest): @@ -46,7 +47,7 @@ def setUp(self): } def test_check_output(self): - self.check_output() + self.check_output(check_pir=True) def init_dtype(self): self.dtype = np.int32 @@ -69,7 +70,7 @@ def setUp(self): self.outputs = {'Out': np.eye(50, dtype=float)} def test_check_output(self): - self.check_output() + self.check_output(check_pir=True) class TestEyeOp2(OpTest): @@ -85,7 +86,7 @@ def setUp(self): self.outputs = {'Out': np.eye(99, 1, dtype=float)} def test_check_output(self): - self.check_output() + self.check_output(check_pir=True) class API_TestTensorEye(unittest.TestCase): @@ -144,6 +145,7 @@ def init_info(self): self.shapes = [[2, 3, 4]] self.save_path = os.path.join(self.temp_dir.name, self.path_prefix()) + @test_with_pir_api def test_static(self): main_prog = Program() starup_prog = Program() @@ -215,7 +217,7 @@ def setUp(self): def test_check_output(self): place = core.CUDAPlace(0) - self.check_output_with_place(place) + self.check_output_with_place(place, check_pir=True) if __name__ == "__main__": From 1978cf5e3c04fb58685ea18c03d1aeca7880301a Mon Sep 17 00:00:00 2001 From: longranger2 <836253168@qq.com> Date: Sun, 5 Nov 2023 15:49:16 +0800 Subject: [PATCH 2/8] fix bug --- test/legacy_test/test_empty_like_op.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/legacy_test/test_empty_like_op.py b/test/legacy_test/test_empty_like_op.py index f464388ae47204..6d74358f399d23 100644 --- a/test/legacy_test/test_empty_like_op.py +++ b/test/legacy_test/test_empty_like_op.py @@ -20,6 +20,7 @@ import paddle from paddle.base import core from paddle.base.data_feeder import convert_dtype +from paddle.pir_utils import test_with_pir_api from paddle.static import Program, program_guard @@ -163,6 +164,7 @@ class TestEmptyLikeAPI_Static(TestEmptyLikeAPICommon): def setUp(self): self.init_config() + @test_with_pir_api def test_static_graph(self): paddle.enable_static() train_program = Program() @@ -212,6 +214,7 @@ def init_config(self): self.data_x_shape = [200, 3] self.dtype = 'float16' + @test_with_pir_api def test_static_graph(self): paddle.enable_static() if paddle.base.core.is_compiled_with_cuda(): @@ -245,6 +248,7 @@ def init_config(self): self.data_x_shape = [200, 3] self.dtype = 'uint16' + @test_with_pir_api def test_static_graph(self): paddle.enable_static() if paddle.base.core.is_compiled_with_cuda(): From f1fb0eea9342df43e19cc315f46de336c9ac5990 Mon Sep 17 00:00:00 2001 From: LoneRanger <836253168@qq.com> Date: Tue, 7 Nov 2023 09:44:57 +0800 Subject: [PATCH 3/8] Update test_empty_like_op.py --- test/legacy_test/test_empty_like_op.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/test/legacy_test/test_empty_like_op.py b/test/legacy_test/test_empty_like_op.py index 6d74358f399d23..59de3096d979b6 100644 --- a/test/legacy_test/test_empty_like_op.py +++ b/test/legacy_test/test_empty_like_op.py @@ -178,19 +178,19 @@ def test_static_graph(self): out = paddle.empty_like(data_x) - place = ( - paddle.CUDAPlace(0) - if core.is_compiled_with_cuda() - else paddle.CPUPlace() - ) - exe = paddle.static.Executor(place) - res = exe.run(train_program, feed={'x': x}, fetch_list=[out]) - - self.dst_dtype = self.dtype - self.dst_shape = x.shape - self.__check_out__(res[0]) - - paddle.disable_static() + place = ( + paddle.CUDAPlace(0) + if core.is_compiled_with_cuda() + else paddle.CPUPlace() + ) + exe = paddle.static.Executor(place) + res = exe.run(train_program, feed={'x': x}, fetch_list=[out]) + + self.dst_dtype = self.dtype + self.dst_shape = x.shape + self.__check_out__(res[0]) + + paddle.disable_static() def init_config(self): self.x_shape = (200, 3) From 773d8c2e8ede542585e644a805fe10184388c812 Mon Sep 17 00:00:00 2001 From: longranger2 <836253168@qq.com> Date: Wed, 8 Nov 2023 23:53:51 +0800 Subject: [PATCH 4/8] fix bug --- test/legacy_test/test_empty_like_op.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/test/legacy_test/test_empty_like_op.py b/test/legacy_test/test_empty_like_op.py index 59de3096d979b6..83643b1e7a6d76 100644 --- a/test/legacy_test/test_empty_like_op.py +++ b/test/legacy_test/test_empty_like_op.py @@ -21,7 +21,6 @@ from paddle.base import core from paddle.base.data_feeder import convert_dtype from paddle.pir_utils import test_with_pir_api -from paddle.static import Program, program_guard class TestEmptyLikeAPICommon(unittest.TestCase): @@ -167,10 +166,10 @@ def setUp(self): @test_with_pir_api def test_static_graph(self): paddle.enable_static() - train_program = Program() - startup_program = Program() + train_program = paddle.static.Program() + startup_program = paddle.static.Program() - with program_guard(train_program, startup_program): + with paddle.static.program_guard(train_program, startup_program): x = np.random.random(self.x_shape).astype(self.dtype) data_x = paddle.static.data( 'x', shape=self.data_x_shape, dtype=self.dtype @@ -185,11 +184,11 @@ def test_static_graph(self): ) exe = paddle.static.Executor(place) res = exe.run(train_program, feed={'x': x}, fetch_list=[out]) - + self.dst_dtype = self.dtype self.dst_shape = x.shape self.__check_out__(res[0]) - + paddle.disable_static() def init_config(self): From ba8dc2bec803d06d5b6692dad485d796b9c37d28 Mon Sep 17 00:00:00 2001 From: longranger2 <836253168@qq.com> Date: Tue, 14 Nov 2023 23:05:36 +0800 Subject: [PATCH 5/8] fix bug --- python/paddle/tensor/creation.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index cabc8f8e88e562..621f7374a98de7 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -1183,7 +1183,9 @@ def eye(num_rows, num_columns=None, dtype=None, name=None): """ def _check_attr(attr, message): - if isinstance(attr, ((Variable, core.eager.Tensor))): + if isinstance( + attr, ((Variable, core.eager.Tensor, paddle.pir.OpResult)) + ): assert len(attr.shape) == 1 and attr.shape[0] in [1, -1] elif not isinstance(attr, int) or attr < 0: raise TypeError(f"{message} should be a non-negative int.") From f5ee5286d623acc7c5c8d511d8732df9885d2848 Mon Sep 17 00:00:00 2001 From: longranger2 <836253168@qq.com> Date: Thu, 23 Nov 2023 22:28:54 +0800 Subject: [PATCH 6/8] fix bug --- python/paddle/tensor/creation.py | 11 ++++++++++- test/legacy_test/test_eye_op.py | 2 -- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 621f7374a98de7..1587c94c972e78 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -2179,7 +2179,7 @@ def empty_like(x, dtype=None, name=None): dtype = x.dtype dtype = convert_dtype(dtype) - if in_dynamic_or_pir_mode(): + if in_dynamic_mode(): out = _C_ops.empty( x.shape, convert_np_dtype_to_dtype_(dtype), @@ -2187,6 +2187,15 @@ def empty_like(x, dtype=None, name=None): ) out.stop_gradient = True return out + elif in_pir_mode(): + shape = paddle.shape(x) + out = _C_ops.empty( + shape, + convert_np_dtype_to_dtype_(dtype), + _current_expected_place(), + ) + out.stop_gradient = True + return out else: helper = LayerHelper("empty_like", **locals()) check_variable_and_dtype( diff --git a/test/legacy_test/test_eye_op.py b/test/legacy_test/test_eye_op.py index 501450ebebedb8..c67b18f1d47d97 100644 --- a/test/legacy_test/test_eye_op.py +++ b/test/legacy_test/test_eye_op.py @@ -23,7 +23,6 @@ from paddle import base from paddle.base import core, framework from paddle.base.framework import Program, program_guard -from paddle.pir_utils import test_with_pir_api class TestEyeOp(OpTest): @@ -145,7 +144,6 @@ def init_info(self): self.shapes = [[2, 3, 4]] self.save_path = os.path.join(self.temp_dir.name, self.path_prefix()) - @test_with_pir_api def test_static(self): main_prog = Program() starup_prog = Program() From ebb025b82a8558737da248d02b4f84328695cc31 Mon Sep 17 00:00:00 2001 From: longranger2 <836253168@qq.com> Date: Sat, 25 Nov 2023 23:23:39 +0800 Subject: [PATCH 7/8] fix bug --- test/legacy_test/test_eye_op.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/legacy_test/test_eye_op.py b/test/legacy_test/test_eye_op.py index c67b18f1d47d97..14476040f51827 100644 --- a/test/legacy_test/test_eye_op.py +++ b/test/legacy_test/test_eye_op.py @@ -23,6 +23,7 @@ from paddle import base from paddle.base import core, framework from paddle.base.framework import Program, program_guard +from paddle.pir_utils import test_with_pir_api class TestEyeOp(OpTest): @@ -89,7 +90,8 @@ def test_check_output(self): class API_TestTensorEye(unittest.TestCase): - def test_out(self): + @test_with_pir_api + def test_static_out(self): with paddle.static.program_guard(paddle.static.Program()): data = paddle.eye(10) place = base.CPUPlace() @@ -114,10 +116,9 @@ def test_out(self): expected_result = np.eye(10, dtype="int64") self.assertEqual((result == expected_result).all(), True) - paddle.disable_static() + def test_dynamic_out(self): out = paddle.eye(10, dtype="int64") expected_result = np.eye(10, dtype="int64") - paddle.enable_static() self.assertEqual((out.numpy() == expected_result).all(), True) def test_errors(self): From 77ed49e56d6dac50832d4b18ae8f500037404c3b Mon Sep 17 00:00:00 2001 From: longranger2 <836253168@qq.com> Date: Sun, 26 Nov 2023 17:14:26 +0800 Subject: [PATCH 8/8] fix bug --- test/legacy_test/test_eye_op.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/legacy_test/test_eye_op.py b/test/legacy_test/test_eye_op.py index 14476040f51827..08fada8d0494bb 100644 --- a/test/legacy_test/test_eye_op.py +++ b/test/legacy_test/test_eye_op.py @@ -117,8 +117,10 @@ def test_static_out(self): self.assertEqual((result == expected_result).all(), True) def test_dynamic_out(self): + paddle.disable_static() out = paddle.eye(10, dtype="int64") expected_result = np.eye(10, dtype="int64") + paddle.enable_static() self.assertEqual((out.numpy() == expected_result).all(), True) def test_errors(self):