From 0840c3f72546955c113cd9412aac3479a0eb8e60 Mon Sep 17 00:00:00 2001 From: BeingGod Date: Thu, 14 Sep 2023 15:59:14 +0000 Subject: [PATCH 1/6] [PIR] Migrate paddle.nn.functional.pad into pir --- python/paddle/nn/functional/common.py | 4 ++-- test/legacy_test/test_pad2d_op.py | 4 ++-- test/legacy_test/test_pad3d_op.py | 8 ++++---- test/legacy_test/test_pad_constant_like.py | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/python/paddle/nn/functional/common.py b/python/paddle/nn/functional/common.py index d2bb5523f810d..9ed7db452455a 100644 --- a/python/paddle/nn/functional/common.py +++ b/python/paddle/nn/functional/common.py @@ -18,7 +18,7 @@ from paddle import _C_ops from paddle.base.layer_helper import LayerHelper from paddle.common_ops_import import Variable, default_main_program -from paddle.framework import core, in_dynamic_mode +from paddle.framework import core, in_dynamic_mode, in_dynamic_or_new_ir_mode from paddle.tensor.creation import full from ...base.data_feeder import ( @@ -1655,7 +1655,7 @@ def pad(x, pad, mode='constant', value=0.0, data_format="NCHW", name=None): paddings = pad pad_value = value - if in_dynamic_mode(): + if in_dynamic_or_new_ir_mode(): out = _C_ops.pad(x, paddings, float(pad_value)) return out diff --git a/test/legacy_test/test_pad2d_op.py b/test/legacy_test/test_pad2d_op.py index 9ceeb411444fa..8b16189adda5b 100644 --- a/test/legacy_test/test_pad2d_op.py +++ b/test/legacy_test/test_pad2d_op.py @@ -64,10 +64,10 @@ def setUp(self): self.outputs = {'Out': out} def test_check_output(self): - self.check_output(check_dygraph=False) + self.check_output(check_dygraph=False, check_new_ir=True) def test_check_grad_normal(self): - self.check_grad(['X'], 'Out', check_dygraph=False) + self.check_grad(['X'], 'Out', check_dygraph=False, check_new_ir=True) def initTestCase(self): self.shape = (2, 3, 4, 5) diff --git a/test/legacy_test/test_pad3d_op.py b/test/legacy_test/test_pad3d_op.py index cbd8d142e6467..6202eae2fa212 100644 --- a/test/legacy_test/test_pad3d_op.py +++ b/test/legacy_test/test_pad3d_op.py @@ -91,10 +91,10 @@ def setUp(self): self.outputs['Out'] = convert_float_to_uint16(self.outputs['Out']) def test_check_output(self): - self.check_output() + self.check_output(check_new_ir=True) def test_check_grad_normal(self): - self.check_grad(['X'], 'Out') + self.check_grad(['X'], 'Out', check_new_ir=True) def get_dtype(self): return np.float64 @@ -251,12 +251,12 @@ def get_dtype(self): def test_check_output(self): place = core.CUDAPlace(0) - self.check_output_with_place(place, atol=1e-2) + self.check_output_with_place(place, atol=1e-2, check_new_ir=True) def test_check_grad_normal(self): place = core.CUDAPlace(0) self.check_grad_with_place( - place, ['X'], 'Out', max_relative_error=1e-2 + place, ['X'], 'Out', max_relative_error=1e-2, check_new_ir=True ) cls_name = "{}_{}".format(parent.__name__, "BF16OP") diff --git a/test/legacy_test/test_pad_constant_like.py b/test/legacy_test/test_pad_constant_like.py index 9e2970cefa698..3261880e2131a 100644 --- a/test/legacy_test/test_pad_constant_like.py +++ b/test/legacy_test/test_pad_constant_like.py @@ -38,10 +38,10 @@ def setUp(self): } def test_check_output(self): - self.check_output() + self.check_output(check_new_ir=True) def test_check_grad_normal(self): - self.check_grad(['Y'], 'Out') + self.check_grad(['Y'], 'Out', check_new_ir=True) def initTestCase(self): self.x_shape = (16, 40) From 5d2eced2f31fcb49a0c6b16fda96d69c32658831 Mon Sep 17 00:00:00 2001 From: 0x45f Date: Fri, 22 Sep 2023 06:46:00 +0000 Subject: [PATCH 2/6] Fix name error --- python/paddle/nn/functional/common.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python/paddle/nn/functional/common.py b/python/paddle/nn/functional/common.py index 9a0ddbfb5adc9..c7364ee27b65c 100644 --- a/python/paddle/nn/functional/common.py +++ b/python/paddle/nn/functional/common.py @@ -21,7 +21,7 @@ from paddle.framework import ( core, in_dynamic_mode, - in_dynamic_or_new_ir_mode, + in_dynamic_or_pir_mode, in_pir_mode, ) from paddle.tensor.creation import full @@ -1658,7 +1658,7 @@ def pad(x, pad, mode='constant', value=0.0, data_format="NCHW", name=None): paddings = pad pad_value = value - if in_dynamic_or_new_ir_mode(): + if in_dynamic_or_pir_mode(): out = _C_ops.pad(x, paddings, float(pad_value)) return out @@ -1756,8 +1756,8 @@ def pad(x, pad, mode='constant', value=0.0, data_format="NCHW", name=None): unsqueezed_dim = [1] x = unsqueeze(x, axis=unsqueezed_dim) - if in_dynamic_mode(): - if isinstance(pad, Variable): + if in_dynamic_or_pir_mode(): + if isinstance(pad, (Variable, pir.OpResult)): pad = pad.tolist() out = _C_ops.pad3d(x, pad, mode, value, data_format) else: From 4a1eef8a3cc40aa853924caa199b1f2012653713 Mon Sep 17 00:00:00 2001 From: 0x45f Date: Fri, 22 Sep 2023 09:32:50 +0000 Subject: [PATCH 3/6] Fix UTs error --- .../dialect/op_generator/vjp_interface_gen_op_list.py | 4 ++++ paddle/fluid/primitive/codegen/gen.py | 4 ++++ python/paddle/nn/functional/common.py | 4 ++-- test/legacy_test/test_pad2d_op.py | 4 ++-- test/legacy_test/test_pad3d_op.py | 6 ++++-- test/legacy_test/test_pad_constant_like.py | 4 ++-- test/legacy_test/test_pad_op.py | 10 +++++----- 7 files changed, 23 insertions(+), 13 deletions(-) diff --git a/paddle/fluid/pir/dialect/op_generator/vjp_interface_gen_op_list.py b/paddle/fluid/pir/dialect/op_generator/vjp_interface_gen_op_list.py index 34b1c15afd946..ae948bdf9484d 100644 --- a/paddle/fluid/pir/dialect/op_generator/vjp_interface_gen_op_list.py +++ b/paddle/fluid/pir/dialect/op_generator/vjp_interface_gen_op_list.py @@ -58,6 +58,8 @@ 'slice_double', 'poisson', 'gumbel_softmax', + 'pad', + 'pad3d', ] vjp_interface_implementation_gen_op_list = [ "tanh", @@ -94,4 +96,6 @@ 'slice_double', 'poisson', 'gumbel_softmax', + 'pad', + 'pad3d', ] diff --git a/paddle/fluid/primitive/codegen/gen.py b/paddle/fluid/primitive/codegen/gen.py index 48ef5e1a800bd..28cab69034259 100644 --- a/paddle/fluid/primitive/codegen/gen.py +++ b/paddle/fluid/primitive/codegen/gen.py @@ -72,6 +72,8 @@ 'scale_grad', 'poisson_grad', 'gumbel_softmax_grad', + 'pad_grad', + 'pad3d_grad', ] @@ -169,6 +171,8 @@ 'gumbel_softmax_grad', 'split', 'transpose', + 'pad_grad', + 'pad3d_grad', ] diff --git a/python/paddle/nn/functional/common.py b/python/paddle/nn/functional/common.py index c7364ee27b65c..7918b9223b71e 100644 --- a/python/paddle/nn/functional/common.py +++ b/python/paddle/nn/functional/common.py @@ -1712,7 +1712,7 @@ def pad(x, pad, mode='constant', value=0.0, data_format="NCHW", name=None): unsqueezed_dim = [] - if isinstance(pad, Variable): + if isinstance(pad, (Variable, pir.OpResult)): if data_format in ["NCL", "NCHW", "NCDHW"]: data_format = "NCDHW" if x_dim == 3: @@ -1757,7 +1757,7 @@ def pad(x, pad, mode='constant', value=0.0, data_format="NCHW", name=None): x = unsqueeze(x, axis=unsqueezed_dim) if in_dynamic_or_pir_mode(): - if isinstance(pad, (Variable, pir.OpResult)): + if isinstance(pad, Variable): pad = pad.tolist() out = _C_ops.pad3d(x, pad, mode, value, data_format) else: diff --git a/test/legacy_test/test_pad2d_op.py b/test/legacy_test/test_pad2d_op.py index f72c440d72618..8c1545ec718bc 100644 --- a/test/legacy_test/test_pad2d_op.py +++ b/test/legacy_test/test_pad2d_op.py @@ -64,10 +64,10 @@ def setUp(self): self.outputs = {'Out': out} def test_check_output(self): - self.check_output(check_dygraph=False, check_new_ir=True) + self.check_output(check_dygraph=False) def test_check_grad_normal(self): - self.check_grad(['X'], 'Out', check_dygraph=False, check_new_ir=True) + self.check_grad(['X'], 'Out', check_dygraph=False) def initTestCase(self): self.shape = (2, 3, 4, 5) diff --git a/test/legacy_test/test_pad3d_op.py b/test/legacy_test/test_pad3d_op.py index 0a3dd7ac859ba..42efb91a166d1 100644 --- a/test/legacy_test/test_pad3d_op.py +++ b/test/legacy_test/test_pad3d_op.py @@ -214,10 +214,12 @@ def get_dtype(self): return np.float16 def test_check_output(self): - self.check_output(atol=1e-3) + self.check_output(atol=1e-3, check_new_ir=True) def test_check_grad_normal(self): - self.check_grad(['X'], 'Out', max_relative_error=1.5e-3) + self.check_grad( + ['X'], 'Out', max_relative_error=1.5e-3, check_new_ir=True + ) cls_name = "{}_{}".format(parent.__name__, "FP16OP") TestPad3dFp16.__name__ = cls_name diff --git a/test/legacy_test/test_pad_constant_like.py b/test/legacy_test/test_pad_constant_like.py index 65639df2ac6f3..e304bdf29e4c2 100644 --- a/test/legacy_test/test_pad_constant_like.py +++ b/test/legacy_test/test_pad_constant_like.py @@ -38,10 +38,10 @@ def setUp(self): } def test_check_output(self): - self.check_output(check_new_ir=True) + self.check_output() def test_check_grad_normal(self): - self.check_grad(['Y'], 'Out', check_new_ir=True) + self.check_grad(['Y'], 'Out') def initTestCase(self): self.x_shape = (16, 40) diff --git a/test/legacy_test/test_pad_op.py b/test/legacy_test/test_pad_op.py index 9e6aea80fd1bd..0eaeb056ae9cb 100644 --- a/test/legacy_test/test_pad_op.py +++ b/test/legacy_test/test_pad_op.py @@ -57,10 +57,10 @@ def get_dtype(self): return np.float64 def test_check_output(self): - self.check_output() + self.check_output(check_new_ir=True) def test_check_grad_normal(self): - self.check_grad(['X'], 'Out', check_prim=True) + self.check_grad(['X'], 'Out', check_new_ir=True) def initTestCase(self): self.shape = (16, 16) @@ -101,7 +101,7 @@ def get_dtype(self): return np.float16 def test_check_grad_normal(self): - self.check_grad(['X'], 'Out', check_prim=True) + self.check_grad(['X'], 'Out', check_prim=True, check_new_ir=True) cls_name = "{}_{}".format(parent.__name__, "Fp16") TestPadFp16.__name__ = cls_name @@ -253,11 +253,11 @@ def initTestCase(self): def test_check_output(self): place = core.CUDAPlace(0) - self.check_output_with_place(place) + self.check_output_with_place(place, check_new_ir=True) def test_check_grad(self): place = core.CUDAPlace(0) - self.check_grad_with_place(place, ['X'], 'Out', check_prim=True) + self.check_grad_with_place(place, ['X'], 'Out', check_new_ir=True) if __name__ == '__main__': From 10d18c5e9d0c691c2e6b66a85abb26efa2aa8ea9 Mon Sep 17 00:00:00 2001 From: 0x45f Date: Fri, 22 Sep 2023 09:35:52 +0000 Subject: [PATCH 4/6] Fix check prim --- test/legacy_test/test_pad_op.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/legacy_test/test_pad_op.py b/test/legacy_test/test_pad_op.py index 0eaeb056ae9cb..1a518bbe36fbb 100644 --- a/test/legacy_test/test_pad_op.py +++ b/test/legacy_test/test_pad_op.py @@ -60,7 +60,7 @@ def test_check_output(self): self.check_output(check_new_ir=True) def test_check_grad_normal(self): - self.check_grad(['X'], 'Out', check_new_ir=True) + self.check_grad(['X'], 'Out', check_prim=True, check_new_ir=True) def initTestCase(self): self.shape = (16, 16) @@ -257,7 +257,7 @@ def test_check_output(self): def test_check_grad(self): place = core.CUDAPlace(0) - self.check_grad_with_place(place, ['X'], 'Out', check_new_ir=True) + self.check_grad_with_place(place, ['X'], 'Out', check_prim=True, check_new_ir=True) if __name__ == '__main__': From 6e4b4e09c1e1d88d74ef63d148744afd8d8cf247 Mon Sep 17 00:00:00 2001 From: 0x45f Date: Fri, 22 Sep 2023 10:05:55 +0000 Subject: [PATCH 5/6] Format code --- test/legacy_test/test_pad_op.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/legacy_test/test_pad_op.py b/test/legacy_test/test_pad_op.py index 1a518bbe36fbb..8054d7c75ffb1 100644 --- a/test/legacy_test/test_pad_op.py +++ b/test/legacy_test/test_pad_op.py @@ -257,7 +257,9 @@ def test_check_output(self): def test_check_grad(self): place = core.CUDAPlace(0) - self.check_grad_with_place(place, ['X'], 'Out', check_prim=True, check_new_ir=True) + self.check_grad_with_place( + place, ['X'], 'Out', check_prim=True, check_new_ir=True + ) if __name__ == '__main__': From d847ffa436ec41746246b8c8588ebcb3182f5f71 Mon Sep 17 00:00:00 2001 From: 0x45f Date: Mon, 25 Sep 2023 14:12:55 +0000 Subject: [PATCH 6/6] Revert code --- paddle/fluid/primitive/codegen/gen.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/paddle/fluid/primitive/codegen/gen.py b/paddle/fluid/primitive/codegen/gen.py index 9fa8ac8215a36..0eef8470521ea 100644 --- a/paddle/fluid/primitive/codegen/gen.py +++ b/paddle/fluid/primitive/codegen/gen.py @@ -233,8 +233,6 @@ 'gumbel_softmax_grad', 'split', 'transpose', - 'pad_grad', - 'pad3d_grad', 'gather_nd_grad', 'stack_grad', 'squeeze_grad',