From 2e7257c1fadf60ba58f890c6fbd0b4458b899a49 Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <2742392377@qq.com> Date: Fri, 20 Oct 2023 09:54:47 +0000 Subject: [PATCH 01/20] expand --- python/paddle/tensor/attribute.py | 10 +++------- python/paddle/tensor/manipulation.py | 6 +++--- test/legacy_test/test_expand_as_v2_op.py | 10 +++++----- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/python/paddle/tensor/attribute.py b/python/paddle/tensor/attribute.py index f3dcaf06cd9bf..8bc7cff200b34 100644 --- a/python/paddle/tensor/attribute.py +++ b/python/paddle/tensor/attribute.py @@ -20,11 +20,7 @@ from paddle import _C_ops from ..base.data_feeder import check_type, check_variable_and_dtype -from ..base.framework import ( - in_dygraph_mode, - in_dynamic_or_pir_mode, - in_pir_mode, -) +from ..base.framework import in_dynamic_or_pir_mode, in_pir_mode from ..common_ops_import import Variable from ..framework import LayerHelper, core from .creation import _complex_to_real_dtype, assign @@ -300,7 +296,7 @@ def real(x, name=None): [[1., 2., 3.], [4., 5., 6.]]) """ - if in_dygraph_mode(): + if in_dynamic_or_pir_mode(): return _C_ops.real(x) else: check_variable_and_dtype(x, 'x', ['complex64', 'complex128'], 'real') @@ -348,7 +344,7 @@ def imag(x, name=None): [[6., 5., 4.], [3., 2., 1.]]) """ - if in_dygraph_mode(): + if in_dynamic_or_pir_mode(): return _C_ops.imag(x) else: check_variable_and_dtype(x, 'x', ['complex64', 'complex128'], 'imag') diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index ae61880c997be..5fbf4fef0fde8 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -1413,7 +1413,7 @@ def flip(x, axis, name=None): if isinstance(axis, int): axis = [axis] - if in_dynamic_mode(): + if in_dynamic_or_pir_mode(): return _C_ops.flip(x, axis) else: helper = LayerHelper("flip", **locals()) @@ -3430,7 +3430,7 @@ def expand_as(x, y, name=None): [[1, 2, 3], [1, 2, 3]]) """ - if in_dynamic_mode(): + if in_dynamic_or_pir_mode(): return _C_ops.expand_as(x, None, y.shape) else: check_variable_and_dtype( @@ -3499,7 +3499,7 @@ def broadcast_to(x, shape, name=None): [[1, 2, 3], [1, 2, 3]]) """ - if in_dynamic_mode(): + if in_dynamic_or_pir_mode(): return _C_ops.expand(x, shape) else: if isinstance(shape, Variable): diff --git a/test/legacy_test/test_expand_as_v2_op.py b/test/legacy_test/test_expand_as_v2_op.py index 13aa6863b9bd6..feee0ff8d453f 100755 --- a/test/legacy_test/test_expand_as_v2_op.py +++ b/test/legacy_test/test_expand_as_v2_op.py @@ -48,10 +48,10 @@ def if_enable_cinn(self): pass def test_check_output(self): - self.check_output(check_prim=True) + self.check_output(check_prim=True, check_pir=True) def test_check_grad(self): - self.check_grad(['X'], 'Out', check_prim=True) + self.check_grad(['X'], 'Out', check_prim=True, check_pir=True) class TestExpandAs_ZeroDim1(TestExpandAsBasic): @@ -104,11 +104,11 @@ def if_enable_cinn(self): self.enable_cinn = False def test_check_output(self): - self.check_output_with_place(place=paddle.CUDAPlace(0)) + self.check_output_with_place(place=paddle.CUDAPlace(0), check_pir=True) def test_check_grad(self): self.check_grad_with_place( - paddle.CUDAPlace(0), ['X'], 'Out', check_prim=True + paddle.CUDAPlace(0), ['X'], 'Out', check_prim=True, check_pir=True ) @@ -242,7 +242,7 @@ def setUp(self): self.outputs = {'Out': convert_float_to_uint16(output)} def test_check_output(self): - self.check_output_with_place(place=paddle.CUDAPlace(0)) + self.check_output_with_place(place=paddle.CUDAPlace(0), check_pir=True) def test_check_grad(self): pass From 08832d6ff3fb280d18d354e0fae4257eb5c35ea8 Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <2742392377@qq.com> Date: Mon, 23 Oct 2023 04:27:08 +0000 Subject: [PATCH 02/20] add pir --- python/paddle/nn/initializer/constant.py | 3 +-- python/paddle/tensor/logic.py | 4 ++-- python/paddle/tensor/math.py | 2 +- python/paddle/tensor/search.py | 2 +- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/python/paddle/nn/initializer/constant.py b/python/paddle/nn/initializer/constant.py index 5391142d503c3..d58cd56c1358c 100644 --- a/python/paddle/nn/initializer/constant.py +++ b/python/paddle/nn/initializer/constant.py @@ -72,10 +72,9 @@ def forward(self, var, block=None): if self._force_cpu: place = core.CPUPlace() if in_dygraph_mode(): - _C_ops.full_( + return _C_ops.full_( var, var.shape, float(self._value), var.dtype, place ) - return None else: return _C_ops.full( var.shape, float(self._value), var.dtype, place diff --git a/python/paddle/tensor/logic.py b/python/paddle/tensor/logic.py index 9b50993b89166..0c9bafb8b564c 100755 --- a/python/paddle/tensor/logic.py +++ b/python/paddle/tensor/logic.py @@ -139,7 +139,7 @@ def logical_and(x, y, out=None, name=None): [True , False, True , False]) """ - if in_dynamic_mode(): + if in_dynamic_or_pir_mode(): return _C_ops.logical_and(x, y) return _logical_op( @@ -413,7 +413,7 @@ def equal_all(x, y, name=None): Tensor(shape=[], dtype=bool, place=Place(cpu), stop_gradient=True, False) """ - if in_dynamic_mode(): + if in_dynamic_or_pir_mode(): return _C_ops.equal_all(x, y) else: helper = LayerHelper("equal_all", **locals()) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index f6ef1fa06ecfa..5cc61c3842f47 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -4437,7 +4437,7 @@ def isnan(x, name=None): Tensor(shape=[7], dtype=bool, place=Place(cpu), stop_gradient=True, [False, False, False, False, False, True , True ]) """ - if in_dynamic_mode(): + if in_dynamic_or_pir_mode(): return _C_ops.isnan(x) else: helper = LayerHelper("isnan_v2", **locals()) diff --git a/python/paddle/tensor/search.py b/python/paddle/tensor/search.py index 8fd2473231f93..c1392b5ccc121 100755 --- a/python/paddle/tensor/search.py +++ b/python/paddle/tensor/search.py @@ -459,7 +459,7 @@ def nonzero(x, as_tuple=False): shape = x.shape rank = len(shape) - if in_dynamic_mode(): + if in_dynamic_or_pir_mode(): outs = _C_ops.nonzero(x) else: check_variable_and_dtype( From b0fbc706e807c753da777eae28f8f0af66eabdb7 Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <2742392377@qq.com> Date: Mon, 23 Oct 2023 06:02:55 +0000 Subject: [PATCH 03/20] add test --- python/paddle/tensor/math.py | 2 +- test/legacy_test/test_expand_op.py | 3 +++ test/legacy_test/test_flip.py | 16 ++++++++++------ test/legacy_test/test_logit_op.py | 2 +- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 5cc61c3842f47..63fbda9e2d7df 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -5209,7 +5209,7 @@ def logit(x, eps=None, name=None): """ if eps is None: eps = 0.0 - if in_dynamic_mode(): + if in_dynamic_or_pir_mode(): return _C_ops.logit(x, eps) else: check_variable_and_dtype( diff --git a/test/legacy_test/test_expand_op.py b/test/legacy_test/test_expand_op.py index f86af46ba7e5d..2d47c6966415b 100644 --- a/test/legacy_test/test_expand_op.py +++ b/test/legacy_test/test_expand_op.py @@ -17,6 +17,7 @@ import numpy as np from op_test import OpTest +import paddle from paddle import base @@ -24,6 +25,8 @@ class TestExpandOpRank1(OpTest): def setUp(self): self.op_type = "expand" + self.python_api = paddle.expand + self.public_python_api = paddle.expand self.init_data() self.dtype = ( "float32" if base.core.is_compiled_with_rocm() else "float64" diff --git a/test/legacy_test/test_flip.py b/test/legacy_test/test_flip.py index 4e5cc58ad3312..e4f729ded8234 100644 --- a/test/legacy_test/test_flip.py +++ b/test/legacy_test/test_flip.py @@ -100,10 +100,10 @@ def init_attrs(self): self.attrs = {"axis": self.axis} def test_check_output(self): - self.check_output(check_cinn=True) + self.check_output(check_cinn=True, check_pir=True) def test_check_grad(self): - self.check_grad(["X"], "Out", check_cinn=True) + self.check_grad(["X"], "Out", check_cinn=True, check_pir=True) def init_test_case(self): self.in_shape = (6, 4, 2, 3) @@ -167,12 +167,16 @@ def test_check_output(self): if core.is_compiled_with_cuda(): place = core.CUDAPlace(0) if core.is_float16_supported(place): - self.check_output_with_place(place, check_cinn=True) + self.check_output_with_place( + place, check_cinn=True, check_pir=True + ) def test_check_grad(self): place = core.CUDAPlace(0) if core.is_float16_supported(place): - self.check_grad_with_place(place, ["X"], "Out", check_cinn=True) + self.check_grad_with_place( + place, ["X"], "Out", check_cinn=True, check_pir=True + ) cls_name = "{}_{}".format(parent.__name__, "FP16OP") TestFlipFP16.__name__ = cls_name @@ -202,12 +206,12 @@ def init_dtype(self): def test_check_output(self): place = core.CUDAPlace(0) if core.is_bfloat16_supported(place): - self.check_output_with_place(place) + self.check_output_with_place(place, check_pir=True) def test_check_grad(self): place = core.CUDAPlace(0) if core.is_bfloat16_supported(place): - self.check_grad_with_place(place, ["X"], "Out") + self.check_grad_with_place(place, ["X"], "Out", check_pir=True) cls_name = "{}_{}".format(parent.__name__, "BF16OP") TestFlipBF16.__name__ = cls_name diff --git a/test/legacy_test/test_logit_op.py b/test/legacy_test/test_logit_op.py index b2f2e21af25ee..35e1f944a2ef5 100644 --- a/test/legacy_test/test_logit_op.py +++ b/test/legacy_test/test_logit_op.py @@ -58,7 +58,7 @@ def set_attrs(self): self.eps = 1e-8 def test_check_output(self): - self.check_output() + self.check_output(check_pir=True) def test_check_grad(self): self.check_grad(['X'], ['Out'], user_defined_grads=[self.x_grad]) From 76badcc26c0505b2a47c6b2a06c17bc5310e4eb9 Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <2742392377@qq.com> Date: Mon, 23 Oct 2023 06:38:51 +0000 Subject: [PATCH 04/20] add test --- test/legacy_test/test_full_op.py | 2 ++ test/legacy_test/test_real_imag_op.py | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/test/legacy_test/test_full_op.py b/test/legacy_test/test_full_op.py index 74e928e58a52a..ba087a589dd58 100644 --- a/test/legacy_test/test_full_op.py +++ b/test/legacy_test/test_full_op.py @@ -19,10 +19,12 @@ import paddle from paddle import base from paddle.base import Program, program_guard +from paddle.pir_utils import test_with_pir_api # Test python API class TestFullAPI(unittest.TestCase): + @test_with_pir_api def test_api(self): positive_2_int32 = paddle.tensor.fill_constant([1], "int32", 2) diff --git a/test/legacy_test/test_real_imag_op.py b/test/legacy_test/test_real_imag_op.py index f714cef69e6d4..67d3881235508 100644 --- a/test/legacy_test/test_real_imag_op.py +++ b/test/legacy_test/test_real_imag_op.py @@ -57,7 +57,7 @@ def init_grad_input_output(self): ) def test_check_output(self): - self.check_output() + self.check_output(check_pir=True) def test_check_grad(self): self.check_grad( @@ -65,6 +65,7 @@ def test_check_grad(self): 'Out', user_defined_grads=[self.grad_x], user_defined_grad_outputs=[self.grad_out], + check_pir=True, ) From d06f03187c9738bc4ad6edabb5e114aa40edd7fd Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <2742392377@qq.com> Date: Mon, 23 Oct 2023 06:59:49 +0000 Subject: [PATCH 05/20] add --- test/legacy_test/test_expand_op.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/legacy_test/test_expand_op.py b/test/legacy_test/test_expand_op.py index 2d47c6966415b..9e361ddc25776 100644 --- a/test/legacy_test/test_expand_op.py +++ b/test/legacy_test/test_expand_op.py @@ -42,10 +42,10 @@ def init_data(self): self.expand_times = [2] def test_check_output(self): - self.check_output(check_dygraph=False) + self.check_output(check_pir=True) def test_check_grad(self): - self.check_grad(['X'], 'Out', check_dygraph=False) + self.check_grad(['X'], 'Out') class TestExpandOpRank2_Corner(TestExpandOpRank1): @@ -149,10 +149,10 @@ def init_data(self): self.expand_times = [2] def test_check_output(self): - self.check_output(check_dygraph=False) + self.check_output(check_pir=True) def test_check_grad(self): - self.check_grad(['X'], 'Out', check_dygraph=False) + self.check_grad(['X'], 'Out') class TestExpandOpRank2_tensor(TestExpandOpRank1_tensor): @@ -173,7 +173,7 @@ def setUp(self): self.outputs = {'Out': output} def test_check_output(self): - self.check_output(check_dygraph=False) + self.check_output(check_pir=True) # Situation 5: input x is Bool @@ -186,7 +186,7 @@ def setUp(self): self.outputs = {'Out': output} def test_check_output(self): - self.check_output(check_dygraph=False) + self.check_output(check_pir=True) # Situation 56: input x is Integer @@ -201,7 +201,7 @@ def setUp(self): self.outputs = {'Out': output} def test_check_output(self): - self.check_output(check_dygraph=False) + self.check_output(check_pir=True) if __name__ == "__main__": From a87ecfa71ee7b615ba17a9018d05b2907c20a74b Mon Sep 17 00:00:00 2001 From: 0x45f Date: Mon, 23 Oct 2023 10:04:09 +0000 Subject: [PATCH 06/20] Fix UTs --- test/legacy_test/test_broadcast_to_op.py | 3 + test/legacy_test/test_expand_as_v2_op.py | 10 +-- test/legacy_test/test_expand_op.py | 17 +++-- test/legacy_test/test_full_op.py | 79 ++++++++++++------------ test/legacy_test/test_real_imag_op.py | 5 +- 5 files changed, 58 insertions(+), 56 deletions(-) diff --git a/test/legacy_test/test_broadcast_to_op.py b/test/legacy_test/test_broadcast_to_op.py index 331addd30909b..a197f0ad05a93 100644 --- a/test/legacy_test/test_broadcast_to_op.py +++ b/test/legacy_test/test_broadcast_to_op.py @@ -19,6 +19,7 @@ import paddle from paddle import base from paddle.base import Program, program_guard +from paddle.pir_utils import test_with_pir_api paddle.enable_static() @@ -40,6 +41,7 @@ def test_errors(self): # Test python API class TestBroadcastToAPI(unittest.TestCase): + @test_with_pir_api def test_api(self): input = np.random.random([12, 14]).astype("float32") x = paddle.static.data(name='x', shape=[12, 14], dtype="float32") @@ -70,6 +72,7 @@ def test_api(self): np.testing.assert_array_equal(res_2, np.tile(input, (1, 1))) np.testing.assert_array_equal(res_3, np.tile(input, (1, 1))) + @test_with_pir_api def test_api_fp16_gpu(self): if paddle.base.core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) diff --git a/test/legacy_test/test_expand_as_v2_op.py b/test/legacy_test/test_expand_as_v2_op.py index feee0ff8d453f..13aa6863b9bd6 100755 --- a/test/legacy_test/test_expand_as_v2_op.py +++ b/test/legacy_test/test_expand_as_v2_op.py @@ -48,10 +48,10 @@ def if_enable_cinn(self): pass def test_check_output(self): - self.check_output(check_prim=True, check_pir=True) + self.check_output(check_prim=True) def test_check_grad(self): - self.check_grad(['X'], 'Out', check_prim=True, check_pir=True) + self.check_grad(['X'], 'Out', check_prim=True) class TestExpandAs_ZeroDim1(TestExpandAsBasic): @@ -104,11 +104,11 @@ def if_enable_cinn(self): self.enable_cinn = False def test_check_output(self): - self.check_output_with_place(place=paddle.CUDAPlace(0), check_pir=True) + self.check_output_with_place(place=paddle.CUDAPlace(0)) def test_check_grad(self): self.check_grad_with_place( - paddle.CUDAPlace(0), ['X'], 'Out', check_prim=True, check_pir=True + paddle.CUDAPlace(0), ['X'], 'Out', check_prim=True ) @@ -242,7 +242,7 @@ def setUp(self): self.outputs = {'Out': convert_float_to_uint16(output)} def test_check_output(self): - self.check_output_with_place(place=paddle.CUDAPlace(0), check_pir=True) + self.check_output_with_place(place=paddle.CUDAPlace(0)) def test_check_grad(self): pass diff --git a/test/legacy_test/test_expand_op.py b/test/legacy_test/test_expand_op.py index 9e361ddc25776..f86af46ba7e5d 100644 --- a/test/legacy_test/test_expand_op.py +++ b/test/legacy_test/test_expand_op.py @@ -17,7 +17,6 @@ import numpy as np from op_test import OpTest -import paddle from paddle import base @@ -25,8 +24,6 @@ class TestExpandOpRank1(OpTest): def setUp(self): self.op_type = "expand" - self.python_api = paddle.expand - self.public_python_api = paddle.expand self.init_data() self.dtype = ( "float32" if base.core.is_compiled_with_rocm() else "float64" @@ -42,10 +39,10 @@ def init_data(self): self.expand_times = [2] def test_check_output(self): - self.check_output(check_pir=True) + self.check_output(check_dygraph=False) def test_check_grad(self): - self.check_grad(['X'], 'Out') + self.check_grad(['X'], 'Out', check_dygraph=False) class TestExpandOpRank2_Corner(TestExpandOpRank1): @@ -149,10 +146,10 @@ def init_data(self): self.expand_times = [2] def test_check_output(self): - self.check_output(check_pir=True) + self.check_output(check_dygraph=False) def test_check_grad(self): - self.check_grad(['X'], 'Out') + self.check_grad(['X'], 'Out', check_dygraph=False) class TestExpandOpRank2_tensor(TestExpandOpRank1_tensor): @@ -173,7 +170,7 @@ def setUp(self): self.outputs = {'Out': output} def test_check_output(self): - self.check_output(check_pir=True) + self.check_output(check_dygraph=False) # Situation 5: input x is Bool @@ -186,7 +183,7 @@ def setUp(self): self.outputs = {'Out': output} def test_check_output(self): - self.check_output(check_pir=True) + self.check_output(check_dygraph=False) # Situation 56: input x is Integer @@ -201,7 +198,7 @@ def setUp(self): self.outputs = {'Out': output} def test_check_output(self): - self.check_output(check_pir=True) + self.check_output(check_dygraph=False) if __name__ == "__main__": diff --git a/test/legacy_test/test_full_op.py b/test/legacy_test/test_full_op.py index ba087a589dd58..0281d41252a27 100644 --- a/test/legacy_test/test_full_op.py +++ b/test/legacy_test/test_full_op.py @@ -26,55 +26,56 @@ class TestFullAPI(unittest.TestCase): @test_with_pir_api def test_api(self): - positive_2_int32 = paddle.tensor.fill_constant([1], "int32", 2) + with paddle.static.program_guard(paddle.static.Program()): + positive_2_int32 = paddle.tensor.fill_constant([1], "int32", 2) - positive_2_int64 = paddle.tensor.fill_constant([1], "int64", 2) - shape_tensor_int32 = paddle.static.data( - name="shape_tensor_int32", shape=[2], dtype="int32" - ) + positive_2_int64 = paddle.tensor.fill_constant([1], "int64", 2) + shape_tensor_int32 = paddle.static.data( + name="shape_tensor_int32", shape=[2], dtype="int32" + ) - shape_tensor_int64 = paddle.static.data( - name="shape_tensor_int64", shape=[2], dtype="int64" - ) + shape_tensor_int64 = paddle.static.data( + name="shape_tensor_int64", shape=[2], dtype="int64" + ) - out_1 = paddle.full(shape=[1, 2], dtype="float32", fill_value=1.1) + out_1 = paddle.full(shape=[1, 2], dtype="float32", fill_value=1.1) - out_2 = paddle.full( - shape=[1, positive_2_int32], dtype="float32", fill_value=1.1 - ) + out_2 = paddle.full( + shape=[1, positive_2_int32], dtype="float32", fill_value=1.1 + ) - out_3 = paddle.full( - shape=[1, positive_2_int64], dtype="float32", fill_value=1.1 - ) + out_3 = paddle.full( + shape=[1, positive_2_int64], dtype="float32", fill_value=1.1 + ) - out_4 = paddle.full( - shape=shape_tensor_int32, dtype="float32", fill_value=1.2 - ) + out_4 = paddle.full( + shape=shape_tensor_int32, dtype="float32", fill_value=1.2 + ) - out_5 = paddle.full( - shape=shape_tensor_int64, dtype="float32", fill_value=1.1 - ) + out_5 = paddle.full( + shape=shape_tensor_int64, dtype="float32", fill_value=1.1 + ) - out_6 = paddle.full( - shape=shape_tensor_int64, dtype=np.float32, fill_value=1.1 - ) + out_6 = paddle.full( + shape=shape_tensor_int64, dtype=np.float32, fill_value=1.1 + ) - val = paddle.tensor.fill_constant( - shape=[1], dtype=np.float32, value=1.1 - ) - out_7 = paddle.full( - shape=shape_tensor_int64, dtype=np.float32, fill_value=val - ) + val = paddle.tensor.fill_constant( + shape=[1], dtype=np.float32, value=1.1 + ) + out_7 = paddle.full( + shape=shape_tensor_int64, dtype=np.float32, fill_value=val + ) - exe = base.Executor(place=base.CPUPlace()) - res_1, res_2, res_3, res_4, res_5, res_6, res_7 = exe.run( - base.default_main_program(), - feed={ - "shape_tensor_int32": np.array([1, 2]).astype("int32"), - "shape_tensor_int64": np.array([1, 2]).astype("int64"), - }, - fetch_list=[out_1, out_2, out_3, out_4, out_5, out_6, out_7], - ) + exe = base.Executor(place=base.CPUPlace()) + res_1, res_2, res_3, res_4, res_5, res_6, res_7 = exe.run( + paddle.static.default_main_program(), + feed={ + "shape_tensor_int32": np.array([1, 2]).astype("int32"), + "shape_tensor_int64": np.array([1, 2]).astype("int64"), + }, + fetch_list=[out_1, out_2, out_3, out_4, out_5, out_6, out_7], + ) np.testing.assert_array_equal( res_1, np.full([1, 2], 1.1, dtype="float32") diff --git a/test/legacy_test/test_real_imag_op.py b/test/legacy_test/test_real_imag_op.py index 67d3881235508..cfc9ea2112c65 100644 --- a/test/legacy_test/test_real_imag_op.py +++ b/test/legacy_test/test_real_imag_op.py @@ -19,6 +19,7 @@ import paddle from paddle import base, static +from paddle.pir_utils import test_with_pir_api numpy_apis = { "real": np.real, @@ -65,7 +66,6 @@ def test_check_grad(self): 'Out', user_defined_grads=[self.grad_x], user_defined_grad_outputs=[self.grad_out], - check_pir=True, ) @@ -100,6 +100,7 @@ def setUp(self): self.places.append(paddle.CUDAPlace(0)) self._shape = [2, 20, 2, 3] + @test_with_pir_api def test_in_static_mode(self): def init_input_output(dtype): input = np.random.random(self._shape).astype( @@ -115,7 +116,7 @@ def init_input_output(dtype): out = paddle_apis[self.api](x) exe = static.Executor(place) - out_value = exe.run(feed=input_dict, fetch_list=[out.name]) + out_value = exe.run(feed=input_dict, fetch_list=[out]) np.testing.assert_array_equal(np_res, out_value[0]) def test_in_dynamic_mode(self): From e5c0a29be10c1fb784ea6168d14efb93cc046249 Mon Sep 17 00:00:00 2001 From: 0x45f Date: Mon, 23 Oct 2023 11:38:00 +0000 Subject: [PATCH 07/20] Revert broadcast_to --- python/paddle/tensor/manipulation.py | 2 +- test/legacy_test/test_broadcast_to_op.py | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index 5fbf4fef0fde8..10a5f239535ad 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -3499,7 +3499,7 @@ def broadcast_to(x, shape, name=None): [[1, 2, 3], [1, 2, 3]]) """ - if in_dynamic_or_pir_mode(): + if in_dynamic_mode(): return _C_ops.expand(x, shape) else: if isinstance(shape, Variable): diff --git a/test/legacy_test/test_broadcast_to_op.py b/test/legacy_test/test_broadcast_to_op.py index a197f0ad05a93..331addd30909b 100644 --- a/test/legacy_test/test_broadcast_to_op.py +++ b/test/legacy_test/test_broadcast_to_op.py @@ -19,7 +19,6 @@ import paddle from paddle import base from paddle.base import Program, program_guard -from paddle.pir_utils import test_with_pir_api paddle.enable_static() @@ -41,7 +40,6 @@ def test_errors(self): # Test python API class TestBroadcastToAPI(unittest.TestCase): - @test_with_pir_api def test_api(self): input = np.random.random([12, 14]).astype("float32") x = paddle.static.data(name='x', shape=[12, 14], dtype="float32") @@ -72,7 +70,6 @@ def test_api(self): np.testing.assert_array_equal(res_2, np.tile(input, (1, 1))) np.testing.assert_array_equal(res_3, np.tile(input, (1, 1))) - @test_with_pir_api def test_api_fp16_gpu(self): if paddle.base.core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) From 8617c38c475dfa476dfe4a732e0f311356226c37 Mon Sep 17 00:00:00 2001 From: 0x45f Date: Mon, 23 Oct 2023 11:50:12 +0000 Subject: [PATCH 08/20] Open expand_as UT --- test/legacy_test/test_expand_as_v2_op.py | 51 +++++++++++++----------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/test/legacy_test/test_expand_as_v2_op.py b/test/legacy_test/test_expand_as_v2_op.py index 13aa6863b9bd6..ab72b03d11124 100755 --- a/test/legacy_test/test_expand_as_v2_op.py +++ b/test/legacy_test/test_expand_as_v2_op.py @@ -20,6 +20,7 @@ import paddle from paddle import base from paddle.base import core +from paddle.pir_utils import test_with_pir_api class TestExpandAsBasic(OpTest): @@ -48,10 +49,10 @@ def if_enable_cinn(self): pass def test_check_output(self): - self.check_output(check_prim=True) + self.check_output(check_prim=True, check_pir=True) def test_check_grad(self): - self.check_grad(['X'], 'Out', check_prim=True) + self.check_grad(['X'], 'Out', check_prim=True, check_pir=True) class TestExpandAs_ZeroDim1(TestExpandAsBasic): @@ -104,11 +105,11 @@ def if_enable_cinn(self): self.enable_cinn = False def test_check_output(self): - self.check_output_with_place(place=paddle.CUDAPlace(0)) + self.check_output_with_place(place=paddle.CUDAPlace(0), check_pir=True) def test_check_grad(self): self.check_grad_with_place( - paddle.CUDAPlace(0), ['X'], 'Out', check_prim=True + paddle.CUDAPlace(0), ['X'], 'Out', check_prim=True, check_pir=True ) @@ -242,7 +243,7 @@ def setUp(self): self.outputs = {'Out': convert_float_to_uint16(output)} def test_check_output(self): - self.check_output_with_place(place=paddle.CUDAPlace(0)) + self.check_output_with_place(place=paddle.CUDAPlace(0), check_pir=True) def test_check_grad(self): pass @@ -261,26 +262,28 @@ def test_errors(self): # Test python API class TestExpandAsV2API(unittest.TestCase): + @test_with_pir_api def test_api(self): - input1 = np.random.random([12, 14]).astype("float32") - input2 = np.random.random([2, 12, 14]).astype("float32") - x = paddle.static.data(name='x', shape=[12, 14], dtype="float32") - - y = paddle.static.data( - name='target_tensor', - shape=[2, 12, 14], - dtype="float32", - ) - - out_1 = paddle.expand_as(x, y=y) - - exe = base.Executor(place=base.CPUPlace()) - res_1 = exe.run( - base.default_main_program(), - feed={"x": input1, "target_tensor": input2}, - fetch_list=[out_1], - ) - np.testing.assert_array_equal(res_1[0], np.tile(input1, (2, 1, 1))) + with paddle.static.program_guard(paddle.static.Program()): + input1 = np.random.random([12, 14]).astype("float32") + input2 = np.random.random([2, 12, 14]).astype("float32") + x = paddle.static.data(name='x', shape=[12, 14], dtype="float32") + + y = paddle.static.data( + name='target_tensor', + shape=[2, 12, 14], + dtype="float32", + ) + + out_1 = paddle.expand_as(x, y=y) + + exe = base.Executor(place=base.CPUPlace()) + res_1 = exe.run( + base.default_main_program(), + feed={"x": input1, "target_tensor": input2}, + fetch_list=[out_1], + ) + np.testing.assert_array_equal(res_1[0], np.tile(input1, (2, 1, 1))) if __name__ == "__main__": From 637e52b8f2586b315b880de5b3446e2d7ce3f940 Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <2742392377@qq.com> Date: Fri, 20 Oct 2023 09:54:47 +0000 Subject: [PATCH 09/20] expand --- python/paddle/tensor/attribute.py | 10 +++------- python/paddle/tensor/manipulation.py | 6 +++--- test/legacy_test/test_expand_as_v2_op.py | 10 +++++----- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/python/paddle/tensor/attribute.py b/python/paddle/tensor/attribute.py index f3dcaf06cd9bf..8bc7cff200b34 100644 --- a/python/paddle/tensor/attribute.py +++ b/python/paddle/tensor/attribute.py @@ -20,11 +20,7 @@ from paddle import _C_ops from ..base.data_feeder import check_type, check_variable_and_dtype -from ..base.framework import ( - in_dygraph_mode, - in_dynamic_or_pir_mode, - in_pir_mode, -) +from ..base.framework import in_dynamic_or_pir_mode, in_pir_mode from ..common_ops_import import Variable from ..framework import LayerHelper, core from .creation import _complex_to_real_dtype, assign @@ -300,7 +296,7 @@ def real(x, name=None): [[1., 2., 3.], [4., 5., 6.]]) """ - if in_dygraph_mode(): + if in_dynamic_or_pir_mode(): return _C_ops.real(x) else: check_variable_and_dtype(x, 'x', ['complex64', 'complex128'], 'real') @@ -348,7 +344,7 @@ def imag(x, name=None): [[6., 5., 4.], [3., 2., 1.]]) """ - if in_dygraph_mode(): + if in_dynamic_or_pir_mode(): return _C_ops.imag(x) else: check_variable_and_dtype(x, 'x', ['complex64', 'complex128'], 'imag') diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index ae61880c997be..5fbf4fef0fde8 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -1413,7 +1413,7 @@ def flip(x, axis, name=None): if isinstance(axis, int): axis = [axis] - if in_dynamic_mode(): + if in_dynamic_or_pir_mode(): return _C_ops.flip(x, axis) else: helper = LayerHelper("flip", **locals()) @@ -3430,7 +3430,7 @@ def expand_as(x, y, name=None): [[1, 2, 3], [1, 2, 3]]) """ - if in_dynamic_mode(): + if in_dynamic_or_pir_mode(): return _C_ops.expand_as(x, None, y.shape) else: check_variable_and_dtype( @@ -3499,7 +3499,7 @@ def broadcast_to(x, shape, name=None): [[1, 2, 3], [1, 2, 3]]) """ - if in_dynamic_mode(): + if in_dynamic_or_pir_mode(): return _C_ops.expand(x, shape) else: if isinstance(shape, Variable): diff --git a/test/legacy_test/test_expand_as_v2_op.py b/test/legacy_test/test_expand_as_v2_op.py index 13aa6863b9bd6..feee0ff8d453f 100755 --- a/test/legacy_test/test_expand_as_v2_op.py +++ b/test/legacy_test/test_expand_as_v2_op.py @@ -48,10 +48,10 @@ def if_enable_cinn(self): pass def test_check_output(self): - self.check_output(check_prim=True) + self.check_output(check_prim=True, check_pir=True) def test_check_grad(self): - self.check_grad(['X'], 'Out', check_prim=True) + self.check_grad(['X'], 'Out', check_prim=True, check_pir=True) class TestExpandAs_ZeroDim1(TestExpandAsBasic): @@ -104,11 +104,11 @@ def if_enable_cinn(self): self.enable_cinn = False def test_check_output(self): - self.check_output_with_place(place=paddle.CUDAPlace(0)) + self.check_output_with_place(place=paddle.CUDAPlace(0), check_pir=True) def test_check_grad(self): self.check_grad_with_place( - paddle.CUDAPlace(0), ['X'], 'Out', check_prim=True + paddle.CUDAPlace(0), ['X'], 'Out', check_prim=True, check_pir=True ) @@ -242,7 +242,7 @@ def setUp(self): self.outputs = {'Out': convert_float_to_uint16(output)} def test_check_output(self): - self.check_output_with_place(place=paddle.CUDAPlace(0)) + self.check_output_with_place(place=paddle.CUDAPlace(0), check_pir=True) def test_check_grad(self): pass From 2a5d33bd6dbb89ff679dc3f8382d495d73b54dec Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <2742392377@qq.com> Date: Mon, 23 Oct 2023 04:27:08 +0000 Subject: [PATCH 10/20] add pir --- python/paddle/nn/initializer/constant.py | 3 +-- python/paddle/tensor/logic.py | 4 ++-- python/paddle/tensor/math.py | 2 +- python/paddle/tensor/search.py | 2 +- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/python/paddle/nn/initializer/constant.py b/python/paddle/nn/initializer/constant.py index 5391142d503c3..d58cd56c1358c 100644 --- a/python/paddle/nn/initializer/constant.py +++ b/python/paddle/nn/initializer/constant.py @@ -72,10 +72,9 @@ def forward(self, var, block=None): if self._force_cpu: place = core.CPUPlace() if in_dygraph_mode(): - _C_ops.full_( + return _C_ops.full_( var, var.shape, float(self._value), var.dtype, place ) - return None else: return _C_ops.full( var.shape, float(self._value), var.dtype, place diff --git a/python/paddle/tensor/logic.py b/python/paddle/tensor/logic.py index 9b50993b89166..0c9bafb8b564c 100755 --- a/python/paddle/tensor/logic.py +++ b/python/paddle/tensor/logic.py @@ -139,7 +139,7 @@ def logical_and(x, y, out=None, name=None): [True , False, True , False]) """ - if in_dynamic_mode(): + if in_dynamic_or_pir_mode(): return _C_ops.logical_and(x, y) return _logical_op( @@ -413,7 +413,7 @@ def equal_all(x, y, name=None): Tensor(shape=[], dtype=bool, place=Place(cpu), stop_gradient=True, False) """ - if in_dynamic_mode(): + if in_dynamic_or_pir_mode(): return _C_ops.equal_all(x, y) else: helper = LayerHelper("equal_all", **locals()) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index ad60f962d73b9..b18f801165543 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -4437,7 +4437,7 @@ def isnan(x, name=None): Tensor(shape=[7], dtype=bool, place=Place(cpu), stop_gradient=True, [False, False, False, False, False, True , True ]) """ - if in_dynamic_mode(): + if in_dynamic_or_pir_mode(): return _C_ops.isnan(x) else: helper = LayerHelper("isnan_v2", **locals()) diff --git a/python/paddle/tensor/search.py b/python/paddle/tensor/search.py index 8fd2473231f93..c1392b5ccc121 100755 --- a/python/paddle/tensor/search.py +++ b/python/paddle/tensor/search.py @@ -459,7 +459,7 @@ def nonzero(x, as_tuple=False): shape = x.shape rank = len(shape) - if in_dynamic_mode(): + if in_dynamic_or_pir_mode(): outs = _C_ops.nonzero(x) else: check_variable_and_dtype( From 4e0a88cf783b30b31a3c614e1177bd6d04bd3afb Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <2742392377@qq.com> Date: Mon, 23 Oct 2023 06:02:55 +0000 Subject: [PATCH 11/20] add test --- python/paddle/tensor/math.py | 2 +- test/legacy_test/test_expand_op.py | 3 +++ test/legacy_test/test_flip.py | 16 ++++++++++------ test/legacy_test/test_logit_op.py | 2 +- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index b18f801165543..fb08264314c27 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -5209,7 +5209,7 @@ def logit(x, eps=None, name=None): """ if eps is None: eps = 0.0 - if in_dynamic_mode(): + if in_dynamic_or_pir_mode(): return _C_ops.logit(x, eps) else: check_variable_and_dtype( diff --git a/test/legacy_test/test_expand_op.py b/test/legacy_test/test_expand_op.py index f86af46ba7e5d..2d47c6966415b 100644 --- a/test/legacy_test/test_expand_op.py +++ b/test/legacy_test/test_expand_op.py @@ -17,6 +17,7 @@ import numpy as np from op_test import OpTest +import paddle from paddle import base @@ -24,6 +25,8 @@ class TestExpandOpRank1(OpTest): def setUp(self): self.op_type = "expand" + self.python_api = paddle.expand + self.public_python_api = paddle.expand self.init_data() self.dtype = ( "float32" if base.core.is_compiled_with_rocm() else "float64" diff --git a/test/legacy_test/test_flip.py b/test/legacy_test/test_flip.py index 4e5cc58ad3312..e4f729ded8234 100644 --- a/test/legacy_test/test_flip.py +++ b/test/legacy_test/test_flip.py @@ -100,10 +100,10 @@ def init_attrs(self): self.attrs = {"axis": self.axis} def test_check_output(self): - self.check_output(check_cinn=True) + self.check_output(check_cinn=True, check_pir=True) def test_check_grad(self): - self.check_grad(["X"], "Out", check_cinn=True) + self.check_grad(["X"], "Out", check_cinn=True, check_pir=True) def init_test_case(self): self.in_shape = (6, 4, 2, 3) @@ -167,12 +167,16 @@ def test_check_output(self): if core.is_compiled_with_cuda(): place = core.CUDAPlace(0) if core.is_float16_supported(place): - self.check_output_with_place(place, check_cinn=True) + self.check_output_with_place( + place, check_cinn=True, check_pir=True + ) def test_check_grad(self): place = core.CUDAPlace(0) if core.is_float16_supported(place): - self.check_grad_with_place(place, ["X"], "Out", check_cinn=True) + self.check_grad_with_place( + place, ["X"], "Out", check_cinn=True, check_pir=True + ) cls_name = "{}_{}".format(parent.__name__, "FP16OP") TestFlipFP16.__name__ = cls_name @@ -202,12 +206,12 @@ def init_dtype(self): def test_check_output(self): place = core.CUDAPlace(0) if core.is_bfloat16_supported(place): - self.check_output_with_place(place) + self.check_output_with_place(place, check_pir=True) def test_check_grad(self): place = core.CUDAPlace(0) if core.is_bfloat16_supported(place): - self.check_grad_with_place(place, ["X"], "Out") + self.check_grad_with_place(place, ["X"], "Out", check_pir=True) cls_name = "{}_{}".format(parent.__name__, "BF16OP") TestFlipBF16.__name__ = cls_name diff --git a/test/legacy_test/test_logit_op.py b/test/legacy_test/test_logit_op.py index b2f2e21af25ee..35e1f944a2ef5 100644 --- a/test/legacy_test/test_logit_op.py +++ b/test/legacy_test/test_logit_op.py @@ -58,7 +58,7 @@ def set_attrs(self): self.eps = 1e-8 def test_check_output(self): - self.check_output() + self.check_output(check_pir=True) def test_check_grad(self): self.check_grad(['X'], ['Out'], user_defined_grads=[self.x_grad]) From 2435fbd9a60520f61003d4afa84553ce8de1ccb8 Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <2742392377@qq.com> Date: Mon, 23 Oct 2023 06:38:51 +0000 Subject: [PATCH 12/20] add test --- test/legacy_test/test_full_op.py | 2 ++ test/legacy_test/test_real_imag_op.py | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/test/legacy_test/test_full_op.py b/test/legacy_test/test_full_op.py index 74e928e58a52a..ba087a589dd58 100644 --- a/test/legacy_test/test_full_op.py +++ b/test/legacy_test/test_full_op.py @@ -19,10 +19,12 @@ import paddle from paddle import base from paddle.base import Program, program_guard +from paddle.pir_utils import test_with_pir_api # Test python API class TestFullAPI(unittest.TestCase): + @test_with_pir_api def test_api(self): positive_2_int32 = paddle.tensor.fill_constant([1], "int32", 2) diff --git a/test/legacy_test/test_real_imag_op.py b/test/legacy_test/test_real_imag_op.py index f714cef69e6d4..67d3881235508 100644 --- a/test/legacy_test/test_real_imag_op.py +++ b/test/legacy_test/test_real_imag_op.py @@ -57,7 +57,7 @@ def init_grad_input_output(self): ) def test_check_output(self): - self.check_output() + self.check_output(check_pir=True) def test_check_grad(self): self.check_grad( @@ -65,6 +65,7 @@ def test_check_grad(self): 'Out', user_defined_grads=[self.grad_x], user_defined_grad_outputs=[self.grad_out], + check_pir=True, ) From 9c4d90c2eb36431a963e316c02c649c9f1bf22c2 Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <2742392377@qq.com> Date: Mon, 23 Oct 2023 06:59:49 +0000 Subject: [PATCH 13/20] add --- test/legacy_test/test_expand_op.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/legacy_test/test_expand_op.py b/test/legacy_test/test_expand_op.py index 2d47c6966415b..9e361ddc25776 100644 --- a/test/legacy_test/test_expand_op.py +++ b/test/legacy_test/test_expand_op.py @@ -42,10 +42,10 @@ def init_data(self): self.expand_times = [2] def test_check_output(self): - self.check_output(check_dygraph=False) + self.check_output(check_pir=True) def test_check_grad(self): - self.check_grad(['X'], 'Out', check_dygraph=False) + self.check_grad(['X'], 'Out') class TestExpandOpRank2_Corner(TestExpandOpRank1): @@ -149,10 +149,10 @@ def init_data(self): self.expand_times = [2] def test_check_output(self): - self.check_output(check_dygraph=False) + self.check_output(check_pir=True) def test_check_grad(self): - self.check_grad(['X'], 'Out', check_dygraph=False) + self.check_grad(['X'], 'Out') class TestExpandOpRank2_tensor(TestExpandOpRank1_tensor): @@ -173,7 +173,7 @@ def setUp(self): self.outputs = {'Out': output} def test_check_output(self): - self.check_output(check_dygraph=False) + self.check_output(check_pir=True) # Situation 5: input x is Bool @@ -186,7 +186,7 @@ def setUp(self): self.outputs = {'Out': output} def test_check_output(self): - self.check_output(check_dygraph=False) + self.check_output(check_pir=True) # Situation 56: input x is Integer @@ -201,7 +201,7 @@ def setUp(self): self.outputs = {'Out': output} def test_check_output(self): - self.check_output(check_dygraph=False) + self.check_output(check_pir=True) if __name__ == "__main__": From 78f823c49bf54cee4c6cc53bb7530a032e9bec13 Mon Sep 17 00:00:00 2001 From: 0x45f Date: Mon, 23 Oct 2023 10:04:09 +0000 Subject: [PATCH 14/20] Fix UTs --- test/legacy_test/test_broadcast_to_op.py | 3 + test/legacy_test/test_expand_as_v2_op.py | 10 +-- test/legacy_test/test_expand_op.py | 17 +++-- test/legacy_test/test_full_op.py | 79 ++++++++++++------------ test/legacy_test/test_real_imag_op.py | 5 +- 5 files changed, 58 insertions(+), 56 deletions(-) diff --git a/test/legacy_test/test_broadcast_to_op.py b/test/legacy_test/test_broadcast_to_op.py index 331addd30909b..a197f0ad05a93 100644 --- a/test/legacy_test/test_broadcast_to_op.py +++ b/test/legacy_test/test_broadcast_to_op.py @@ -19,6 +19,7 @@ import paddle from paddle import base from paddle.base import Program, program_guard +from paddle.pir_utils import test_with_pir_api paddle.enable_static() @@ -40,6 +41,7 @@ def test_errors(self): # Test python API class TestBroadcastToAPI(unittest.TestCase): + @test_with_pir_api def test_api(self): input = np.random.random([12, 14]).astype("float32") x = paddle.static.data(name='x', shape=[12, 14], dtype="float32") @@ -70,6 +72,7 @@ def test_api(self): np.testing.assert_array_equal(res_2, np.tile(input, (1, 1))) np.testing.assert_array_equal(res_3, np.tile(input, (1, 1))) + @test_with_pir_api def test_api_fp16_gpu(self): if paddle.base.core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) diff --git a/test/legacy_test/test_expand_as_v2_op.py b/test/legacy_test/test_expand_as_v2_op.py index feee0ff8d453f..13aa6863b9bd6 100755 --- a/test/legacy_test/test_expand_as_v2_op.py +++ b/test/legacy_test/test_expand_as_v2_op.py @@ -48,10 +48,10 @@ def if_enable_cinn(self): pass def test_check_output(self): - self.check_output(check_prim=True, check_pir=True) + self.check_output(check_prim=True) def test_check_grad(self): - self.check_grad(['X'], 'Out', check_prim=True, check_pir=True) + self.check_grad(['X'], 'Out', check_prim=True) class TestExpandAs_ZeroDim1(TestExpandAsBasic): @@ -104,11 +104,11 @@ def if_enable_cinn(self): self.enable_cinn = False def test_check_output(self): - self.check_output_with_place(place=paddle.CUDAPlace(0), check_pir=True) + self.check_output_with_place(place=paddle.CUDAPlace(0)) def test_check_grad(self): self.check_grad_with_place( - paddle.CUDAPlace(0), ['X'], 'Out', check_prim=True, check_pir=True + paddle.CUDAPlace(0), ['X'], 'Out', check_prim=True ) @@ -242,7 +242,7 @@ def setUp(self): self.outputs = {'Out': convert_float_to_uint16(output)} def test_check_output(self): - self.check_output_with_place(place=paddle.CUDAPlace(0), check_pir=True) + self.check_output_with_place(place=paddle.CUDAPlace(0)) def test_check_grad(self): pass diff --git a/test/legacy_test/test_expand_op.py b/test/legacy_test/test_expand_op.py index 9e361ddc25776..f86af46ba7e5d 100644 --- a/test/legacy_test/test_expand_op.py +++ b/test/legacy_test/test_expand_op.py @@ -17,7 +17,6 @@ import numpy as np from op_test import OpTest -import paddle from paddle import base @@ -25,8 +24,6 @@ class TestExpandOpRank1(OpTest): def setUp(self): self.op_type = "expand" - self.python_api = paddle.expand - self.public_python_api = paddle.expand self.init_data() self.dtype = ( "float32" if base.core.is_compiled_with_rocm() else "float64" @@ -42,10 +39,10 @@ def init_data(self): self.expand_times = [2] def test_check_output(self): - self.check_output(check_pir=True) + self.check_output(check_dygraph=False) def test_check_grad(self): - self.check_grad(['X'], 'Out') + self.check_grad(['X'], 'Out', check_dygraph=False) class TestExpandOpRank2_Corner(TestExpandOpRank1): @@ -149,10 +146,10 @@ def init_data(self): self.expand_times = [2] def test_check_output(self): - self.check_output(check_pir=True) + self.check_output(check_dygraph=False) def test_check_grad(self): - self.check_grad(['X'], 'Out') + self.check_grad(['X'], 'Out', check_dygraph=False) class TestExpandOpRank2_tensor(TestExpandOpRank1_tensor): @@ -173,7 +170,7 @@ def setUp(self): self.outputs = {'Out': output} def test_check_output(self): - self.check_output(check_pir=True) + self.check_output(check_dygraph=False) # Situation 5: input x is Bool @@ -186,7 +183,7 @@ def setUp(self): self.outputs = {'Out': output} def test_check_output(self): - self.check_output(check_pir=True) + self.check_output(check_dygraph=False) # Situation 56: input x is Integer @@ -201,7 +198,7 @@ def setUp(self): self.outputs = {'Out': output} def test_check_output(self): - self.check_output(check_pir=True) + self.check_output(check_dygraph=False) if __name__ == "__main__": diff --git a/test/legacy_test/test_full_op.py b/test/legacy_test/test_full_op.py index ba087a589dd58..0281d41252a27 100644 --- a/test/legacy_test/test_full_op.py +++ b/test/legacy_test/test_full_op.py @@ -26,55 +26,56 @@ class TestFullAPI(unittest.TestCase): @test_with_pir_api def test_api(self): - positive_2_int32 = paddle.tensor.fill_constant([1], "int32", 2) + with paddle.static.program_guard(paddle.static.Program()): + positive_2_int32 = paddle.tensor.fill_constant([1], "int32", 2) - positive_2_int64 = paddle.tensor.fill_constant([1], "int64", 2) - shape_tensor_int32 = paddle.static.data( - name="shape_tensor_int32", shape=[2], dtype="int32" - ) + positive_2_int64 = paddle.tensor.fill_constant([1], "int64", 2) + shape_tensor_int32 = paddle.static.data( + name="shape_tensor_int32", shape=[2], dtype="int32" + ) - shape_tensor_int64 = paddle.static.data( - name="shape_tensor_int64", shape=[2], dtype="int64" - ) + shape_tensor_int64 = paddle.static.data( + name="shape_tensor_int64", shape=[2], dtype="int64" + ) - out_1 = paddle.full(shape=[1, 2], dtype="float32", fill_value=1.1) + out_1 = paddle.full(shape=[1, 2], dtype="float32", fill_value=1.1) - out_2 = paddle.full( - shape=[1, positive_2_int32], dtype="float32", fill_value=1.1 - ) + out_2 = paddle.full( + shape=[1, positive_2_int32], dtype="float32", fill_value=1.1 + ) - out_3 = paddle.full( - shape=[1, positive_2_int64], dtype="float32", fill_value=1.1 - ) + out_3 = paddle.full( + shape=[1, positive_2_int64], dtype="float32", fill_value=1.1 + ) - out_4 = paddle.full( - shape=shape_tensor_int32, dtype="float32", fill_value=1.2 - ) + out_4 = paddle.full( + shape=shape_tensor_int32, dtype="float32", fill_value=1.2 + ) - out_5 = paddle.full( - shape=shape_tensor_int64, dtype="float32", fill_value=1.1 - ) + out_5 = paddle.full( + shape=shape_tensor_int64, dtype="float32", fill_value=1.1 + ) - out_6 = paddle.full( - shape=shape_tensor_int64, dtype=np.float32, fill_value=1.1 - ) + out_6 = paddle.full( + shape=shape_tensor_int64, dtype=np.float32, fill_value=1.1 + ) - val = paddle.tensor.fill_constant( - shape=[1], dtype=np.float32, value=1.1 - ) - out_7 = paddle.full( - shape=shape_tensor_int64, dtype=np.float32, fill_value=val - ) + val = paddle.tensor.fill_constant( + shape=[1], dtype=np.float32, value=1.1 + ) + out_7 = paddle.full( + shape=shape_tensor_int64, dtype=np.float32, fill_value=val + ) - exe = base.Executor(place=base.CPUPlace()) - res_1, res_2, res_3, res_4, res_5, res_6, res_7 = exe.run( - base.default_main_program(), - feed={ - "shape_tensor_int32": np.array([1, 2]).astype("int32"), - "shape_tensor_int64": np.array([1, 2]).astype("int64"), - }, - fetch_list=[out_1, out_2, out_3, out_4, out_5, out_6, out_7], - ) + exe = base.Executor(place=base.CPUPlace()) + res_1, res_2, res_3, res_4, res_5, res_6, res_7 = exe.run( + paddle.static.default_main_program(), + feed={ + "shape_tensor_int32": np.array([1, 2]).astype("int32"), + "shape_tensor_int64": np.array([1, 2]).astype("int64"), + }, + fetch_list=[out_1, out_2, out_3, out_4, out_5, out_6, out_7], + ) np.testing.assert_array_equal( res_1, np.full([1, 2], 1.1, dtype="float32") diff --git a/test/legacy_test/test_real_imag_op.py b/test/legacy_test/test_real_imag_op.py index 67d3881235508..cfc9ea2112c65 100644 --- a/test/legacy_test/test_real_imag_op.py +++ b/test/legacy_test/test_real_imag_op.py @@ -19,6 +19,7 @@ import paddle from paddle import base, static +from paddle.pir_utils import test_with_pir_api numpy_apis = { "real": np.real, @@ -65,7 +66,6 @@ def test_check_grad(self): 'Out', user_defined_grads=[self.grad_x], user_defined_grad_outputs=[self.grad_out], - check_pir=True, ) @@ -100,6 +100,7 @@ def setUp(self): self.places.append(paddle.CUDAPlace(0)) self._shape = [2, 20, 2, 3] + @test_with_pir_api def test_in_static_mode(self): def init_input_output(dtype): input = np.random.random(self._shape).astype( @@ -115,7 +116,7 @@ def init_input_output(dtype): out = paddle_apis[self.api](x) exe = static.Executor(place) - out_value = exe.run(feed=input_dict, fetch_list=[out.name]) + out_value = exe.run(feed=input_dict, fetch_list=[out]) np.testing.assert_array_equal(np_res, out_value[0]) def test_in_dynamic_mode(self): From 388f25e6d07bc1590921f9bd18787fd967b6cd0b Mon Sep 17 00:00:00 2001 From: 0x45f Date: Mon, 23 Oct 2023 11:38:00 +0000 Subject: [PATCH 15/20] Revert broadcast_to --- python/paddle/tensor/manipulation.py | 2 +- test/legacy_test/test_broadcast_to_op.py | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index 5fbf4fef0fde8..10a5f239535ad 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -3499,7 +3499,7 @@ def broadcast_to(x, shape, name=None): [[1, 2, 3], [1, 2, 3]]) """ - if in_dynamic_or_pir_mode(): + if in_dynamic_mode(): return _C_ops.expand(x, shape) else: if isinstance(shape, Variable): diff --git a/test/legacy_test/test_broadcast_to_op.py b/test/legacy_test/test_broadcast_to_op.py index a197f0ad05a93..331addd30909b 100644 --- a/test/legacy_test/test_broadcast_to_op.py +++ b/test/legacy_test/test_broadcast_to_op.py @@ -19,7 +19,6 @@ import paddle from paddle import base from paddle.base import Program, program_guard -from paddle.pir_utils import test_with_pir_api paddle.enable_static() @@ -41,7 +40,6 @@ def test_errors(self): # Test python API class TestBroadcastToAPI(unittest.TestCase): - @test_with_pir_api def test_api(self): input = np.random.random([12, 14]).astype("float32") x = paddle.static.data(name='x', shape=[12, 14], dtype="float32") @@ -72,7 +70,6 @@ def test_api(self): np.testing.assert_array_equal(res_2, np.tile(input, (1, 1))) np.testing.assert_array_equal(res_3, np.tile(input, (1, 1))) - @test_with_pir_api def test_api_fp16_gpu(self): if paddle.base.core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) From 575c509c9ebec60a26e96c64563f36f968150e19 Mon Sep 17 00:00:00 2001 From: 0x45f Date: Mon, 23 Oct 2023 11:50:12 +0000 Subject: [PATCH 16/20] Open expand_as UT --- test/legacy_test/test_expand_as_v2_op.py | 51 +++++++++++++----------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/test/legacy_test/test_expand_as_v2_op.py b/test/legacy_test/test_expand_as_v2_op.py index 13aa6863b9bd6..ab72b03d11124 100755 --- a/test/legacy_test/test_expand_as_v2_op.py +++ b/test/legacy_test/test_expand_as_v2_op.py @@ -20,6 +20,7 @@ import paddle from paddle import base from paddle.base import core +from paddle.pir_utils import test_with_pir_api class TestExpandAsBasic(OpTest): @@ -48,10 +49,10 @@ def if_enable_cinn(self): pass def test_check_output(self): - self.check_output(check_prim=True) + self.check_output(check_prim=True, check_pir=True) def test_check_grad(self): - self.check_grad(['X'], 'Out', check_prim=True) + self.check_grad(['X'], 'Out', check_prim=True, check_pir=True) class TestExpandAs_ZeroDim1(TestExpandAsBasic): @@ -104,11 +105,11 @@ def if_enable_cinn(self): self.enable_cinn = False def test_check_output(self): - self.check_output_with_place(place=paddle.CUDAPlace(0)) + self.check_output_with_place(place=paddle.CUDAPlace(0), check_pir=True) def test_check_grad(self): self.check_grad_with_place( - paddle.CUDAPlace(0), ['X'], 'Out', check_prim=True + paddle.CUDAPlace(0), ['X'], 'Out', check_prim=True, check_pir=True ) @@ -242,7 +243,7 @@ def setUp(self): self.outputs = {'Out': convert_float_to_uint16(output)} def test_check_output(self): - self.check_output_with_place(place=paddle.CUDAPlace(0)) + self.check_output_with_place(place=paddle.CUDAPlace(0), check_pir=True) def test_check_grad(self): pass @@ -261,26 +262,28 @@ def test_errors(self): # Test python API class TestExpandAsV2API(unittest.TestCase): + @test_with_pir_api def test_api(self): - input1 = np.random.random([12, 14]).astype("float32") - input2 = np.random.random([2, 12, 14]).astype("float32") - x = paddle.static.data(name='x', shape=[12, 14], dtype="float32") - - y = paddle.static.data( - name='target_tensor', - shape=[2, 12, 14], - dtype="float32", - ) - - out_1 = paddle.expand_as(x, y=y) - - exe = base.Executor(place=base.CPUPlace()) - res_1 = exe.run( - base.default_main_program(), - feed={"x": input1, "target_tensor": input2}, - fetch_list=[out_1], - ) - np.testing.assert_array_equal(res_1[0], np.tile(input1, (2, 1, 1))) + with paddle.static.program_guard(paddle.static.Program()): + input1 = np.random.random([12, 14]).astype("float32") + input2 = np.random.random([2, 12, 14]).astype("float32") + x = paddle.static.data(name='x', shape=[12, 14], dtype="float32") + + y = paddle.static.data( + name='target_tensor', + shape=[2, 12, 14], + dtype="float32", + ) + + out_1 = paddle.expand_as(x, y=y) + + exe = base.Executor(place=base.CPUPlace()) + res_1 = exe.run( + base.default_main_program(), + feed={"x": input1, "target_tensor": input2}, + fetch_list=[out_1], + ) + np.testing.assert_array_equal(res_1[0], np.tile(input1, (2, 1, 1))) if __name__ == "__main__": From 7c67b77ed5e0107ea4382beeac004d2099b44cbc Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <2742392377@qq.com> Date: Mon, 23 Oct 2023 14:02:50 +0000 Subject: [PATCH 17/20] add logit tests --- test/legacy_test/test_logit_op.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/test/legacy_test/test_logit_op.py b/test/legacy_test/test_logit_op.py index 35e1f944a2ef5..641fc68e1832d 100644 --- a/test/legacy_test/test_logit_op.py +++ b/test/legacy_test/test_logit_op.py @@ -61,7 +61,9 @@ def test_check_output(self): self.check_output(check_pir=True) def test_check_grad(self): - self.check_grad(['X'], ['Out'], user_defined_grads=[self.x_grad]) + self.check_grad( + ['X'], ['Out'], user_defined_grads=[self.x_grad], check_pir=True + ) class TestLogitOpFp32(TestLogitOp): @@ -71,10 +73,12 @@ def set_attrs(self): self.eps = 1e-8 def test_check_output(self): - self.check_output() + self.check_output(check_pir=True) def test_check_grad(self): - self.check_grad(['X'], ['Out'], user_defined_grads=[self.x_grad]) + self.check_grad( + ['X'], ['Out'], user_defined_grads=[self.x_grad], check_pir=True + ) class TestLogitOpFp16(TestLogitOp): @@ -84,10 +88,12 @@ def set_attrs(self): self.eps = 1e-8 def test_check_output(self): - self.check_output() + self.check_output(check_pir=True) def test_check_grad(self): - self.check_grad(['X'], ['Out'], user_defined_grads=[self.x_grad]) + self.check_grad( + ['X'], ['Out'], user_defined_grads=[self.x_grad], check_pir=True + ) @unittest.skipIf( @@ -115,7 +121,7 @@ def set_attrs(self): def test_check_output(self): if core.is_compiled_with_cuda(): place = core.CUDAPlace(0) - self.check_output_with_place(place) + self.check_output_with_place(place, check_pir=True) def test_check_grad(self): if core.is_compiled_with_cuda(): @@ -125,6 +131,7 @@ def test_check_grad(self): ['X'], ['Out'], user_defined_grads=[self.x_grad], + check_pir=True, ) From 43f26af31261638da04ab7105e653e024655c434 Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <2742392377@qq.com> Date: Mon, 23 Oct 2023 14:20:58 +0000 Subject: [PATCH 18/20] add more tests --- test/legacy_test/test_compare_reduce_op.py | 8 ++++---- test/legacy_test/test_logical_op.py | 1 + test/legacy_test/test_nonzero_api.py | 6 ++++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/test/legacy_test/test_compare_reduce_op.py b/test/legacy_test/test_compare_reduce_op.py index e281407c242b0..fdd08b2990cfe 100644 --- a/test/legacy_test/test_compare_reduce_op.py +++ b/test/legacy_test/test_compare_reduce_op.py @@ -32,7 +32,7 @@ def setUp(self): self.op_type = op_type def test_output(self): - self.check_output() + self.check_output(check_pir=True) cls_name = "{}_{}_{}".format(op_type, typename, 'not_equal_all') Cls.__name__ = cls_name @@ -51,7 +51,7 @@ def setUp(self): self.op_type = op_type def test_output(self): - self.check_output() + self.check_output(check_pir=True) cls_name = "{}_{}_{}".format(op_type, typename, 'not_shape_equal_all') Cls.__name__ = cls_name @@ -69,7 +69,7 @@ def setUp(self): self.op_type = op_type def test_output(self): - self.check_output() + self.check_output(check_pir=True) cls_name = "{}_{}_{}".format(op_type, typename, 'equal_all') Cls.__name__ = cls_name @@ -89,7 +89,7 @@ def setUp(self): self.op_type = op_type def test_output(self): - self.check_output() + self.check_output(check_pir=True) cls_name = "{}_{}_{}".format(op_type, typename, 'equal_all') Cls.__name__ = cls_name diff --git a/test/legacy_test/test_logical_op.py b/test/legacy_test/test_logical_op.py index 98e15878cdfb6..81dec36e2f698 100755 --- a/test/legacy_test/test_logical_op.py +++ b/test/legacy_test/test_logical_op.py @@ -67,6 +67,7 @@ } +# @test_with_pir_api def run_static(x_np, y_np, op_str, use_gpu=False, binary_op=True): paddle.enable_static() startup_program = Program() diff --git a/test/legacy_test/test_nonzero_api.py b/test/legacy_test/test_nonzero_api.py index a57e1d9803c22..a14c72a22a149 100644 --- a/test/legacy_test/test_nonzero_api.py +++ b/test/legacy_test/test_nonzero_api.py @@ -29,6 +29,7 @@ def call_nonzero(x): class TestNonZeroAPI(unittest.TestCase): def test_nonzero_api_as_tuple(self): + paddle.enable_static() data = np.array([[True, False], [False, True]]) with program_guard(Program(), Program()): x = paddle.static.data(name='x', shape=[-1, 2], dtype='float32') @@ -61,6 +62,7 @@ def test_nonzero_api_as_tuple(self): np.testing.assert_allclose(expect_out, np.array(res), rtol=1e-05) def test_nonzero_api(self): + paddle.enable_static() data = np.array([[True, False], [False, True]]) with program_guard(Program(), Program()): x = paddle.static.data(name='x', shape=[-1, 2], dtype='float32') @@ -108,7 +110,7 @@ def setUp(self): self.outputs = self.return_outputs() def test_check_output(self): - self.check_output() + self.check_output(check_pir=True) def init_shape(self): self.shape = [8, 8] @@ -156,7 +158,7 @@ def setUp(self): self.outputs = self.return_outputs() def test_check_output(self): - self.check_output() + self.check_output(check_pir=True) def init_shape(self): self.shape = [12, 9] From f7ddbb7a340a0768e40fac6ef08bd29fc658b733 Mon Sep 17 00:00:00 2001 From: 0x45f Date: Tue, 24 Oct 2023 02:54:24 +0000 Subject: [PATCH 19/20] Fix program --- test/legacy_test/test_expand_as_v2_op.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/legacy_test/test_expand_as_v2_op.py b/test/legacy_test/test_expand_as_v2_op.py index ab72b03d11124..6b11c2f8dee99 100755 --- a/test/legacy_test/test_expand_as_v2_op.py +++ b/test/legacy_test/test_expand_as_v2_op.py @@ -279,7 +279,7 @@ def test_api(self): exe = base.Executor(place=base.CPUPlace()) res_1 = exe.run( - base.default_main_program(), + paddle.static.default_main_program(), feed={"x": input1, "target_tensor": input2}, fetch_list=[out_1], ) From 22e408f11faafad02033e629e51c20d258295316 Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <2742392377@qq.com> Date: Tue, 24 Oct 2023 07:56:59 +0000 Subject: [PATCH 20/20] fix bug --- python/paddle/nn/initializer/constant.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/paddle/nn/initializer/constant.py b/python/paddle/nn/initializer/constant.py index d58cd56c1358c..5391142d503c3 100644 --- a/python/paddle/nn/initializer/constant.py +++ b/python/paddle/nn/initializer/constant.py @@ -72,9 +72,10 @@ def forward(self, var, block=None): if self._force_cpu: place = core.CPUPlace() if in_dygraph_mode(): - return _C_ops.full_( + _C_ops.full_( var, var.shape, float(self._value), var.dtype, place ) + return None else: return _C_ops.full( var.shape, float(self._value), var.dtype, place