From 4479aa7ca79367e964285148bbe39817845595a6 Mon Sep 17 00:00:00 2001 From: xiaoguoguo626807 <100397923+xiaoguoguo626807@users.noreply.github.com> Date: Fri, 22 Sep 2023 09:31:53 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90PIR=E3=80=91modify=20Subtract=20optest?= =?UTF-8?q?=20(#57608)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * modify ci bug * add sub test * modify pd name --- paddle/fluid/pir/transforms/inplace_pass.cc | 3 +- python/paddle/autograd/ir_backward.py | 2 +- test/legacy_test/test_elementwise_sub_op.py | 52 +++++++++++++++------ 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/paddle/fluid/pir/transforms/inplace_pass.cc b/paddle/fluid/pir/transforms/inplace_pass.cc index 3f419f20fb99a..6010af208fae6 100644 --- a/paddle/fluid/pir/transforms/inplace_pass.cc +++ b/paddle/fluid/pir/transforms/inplace_pass.cc @@ -204,7 +204,8 @@ static std::unordered_map GetInplaceOps( // NOTE(zhangbo): add_grad cpu kernel can't do inplace, for the reason shown // in the function: CommonElementwiseBroadcastBackward // (paddle/phi/kernels/funcs/elementwise_grad_base.h) - if ((upper_op_name == "pd_op.add_grad") && + if ((upper_op_name == "pd_op.add_grad" || + upper_op_name == "pd_op.subtract_grad") && (upper_op_attrs.at("kernel_key") .dyn_cast() .data() diff --git a/python/paddle/autograd/ir_backward.py b/python/paddle/autograd/ir_backward.py index f8a2aae71b0cd..bb73c1d670cee 100644 --- a/python/paddle/autograd/ir_backward.py +++ b/python/paddle/autograd/ir_backward.py @@ -288,7 +288,7 @@ def inverse_sort_op(ops): sorted_list = [] for op in ops: for x in op.operands(): - if x.source().get_defining_op() in ops_set: + if x.source() and x.source().get_defining_op() in ops_set: pending_count[x.source().get_defining_op()] += 1 queue = collections.deque() diff --git a/test/legacy_test/test_elementwise_sub_op.py b/test/legacy_test/test_elementwise_sub_op.py index d3e96d158d98c..62176d28595fc 100644 --- a/test/legacy_test/test_elementwise_sub_op.py +++ b/test/legacy_test/test_elementwise_sub_op.py @@ -44,10 +44,12 @@ def init_dtype(self): self.dtype = 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', 'Y'], 'Out', check_prim=self.check_prim) + self.check_grad( + ['X', 'Y'], 'Out', check_prim=self.check_prim, check_new_ir=True + ) def test_check_grad_ingore_x(self): self.check_grad( @@ -56,6 +58,7 @@ def test_check_grad_ingore_x(self): max_relative_error=0.005, no_grad_set=set("X"), check_prim=self.check_prim, + check_new_ir=True, ) def test_check_grad_ingore_y(self): @@ -65,6 +68,7 @@ def test_check_grad_ingore_y(self): max_relative_error=0.005, no_grad_set=set('Y'), check_prim=self.check_prim, + check_new_ir=True, ) def if_check_prim(self): @@ -116,7 +120,12 @@ def test_check_grad_normal(self): def test_check_grad_ingore_x(self): place = core.CUDAPlace(0) self.check_grad_with_place( - place, ['Y'], 'Out', no_grad_set=set("X"), max_relative_error=0.1 + place, + ['Y'], + 'Out', + no_grad_set=set("X"), + max_relative_error=0.1, + check_new_ir=True, ) def test_check_grad_ingore_y(self): @@ -128,6 +137,7 @@ def test_check_grad_ingore_y(self): no_grad_set=set('Y'), max_relative_error=0.1, check_prim=True, + check_new_ir=True, ) @@ -372,10 +382,12 @@ def setUp(self): } def test_check_output(self): - self.check_output(check_dygraph=False) + self.check_output(check_dygraph=False, check_new_ir=False) def test_check_grad_normal(self): - self.check_grad(['X', 'Y'], 'Out', check_dygraph=False) + self.check_grad( + ['X', 'Y'], 'Out', check_dygraph=False, check_new_ir=False + ) def test_check_grad_ingore_x(self): self.check_grad( @@ -384,6 +396,7 @@ def test_check_grad_ingore_x(self): max_relative_error=0.005, no_grad_set=set("X"), check_dygraph=False, + check_new_ir=False, ) def test_check_grad_ingore_y(self): @@ -393,6 +406,7 @@ def test_check_grad_ingore_y(self): max_relative_error=0.005, no_grad_set=set('Y'), check_dygraph=False, + check_new_ir=False, ) @@ -427,24 +441,36 @@ def setUp(self): def test_check_output(self): place = core.CUDAPlace(0) - self.check_output_with_place(place, check_dygraph=False) + self.check_output_with_place( + place, check_dygraph=False, check_new_ir=False + ) def test_check_grad_normal(self): place = core.CUDAPlace(0) self.check_grad_with_place( - place, ['X', 'Y'], 'Out', check_dygraph=False + place, ['X', 'Y'], 'Out', check_dygraph=False, check_new_ir=False ) def test_check_grad_ingore_x(self): place = core.CUDAPlace(0) self.check_grad_with_place( - place, ['Y'], 'Out', no_grad_set=set("X"), check_dygraph=False + place, + ['Y'], + 'Out', + no_grad_set=set("X"), + check_dygraph=False, + check_new_ir=False, ) def test_check_grad_ingore_y(self): place = core.CUDAPlace(0) self.check_grad_with_place( - place, ['X'], 'Out', no_grad_set=set('Y'), check_dygraph=False + place, + ['X'], + 'Out', + no_grad_set=set('Y'), + check_dygraph=False, + check_new_ir=False, ) @@ -810,13 +836,11 @@ def init_input_output(self): self.out = self.x - self.y def test_check_output(self): - self.check_output() + self.check_output(check_new_ir=False) def test_check_grad_normal(self): self.check_grad( - ['X', 'Y'], - 'Out', - check_prim=self.check_prim, + ['X', 'Y'], 'Out', check_prim=self.check_prim, check_new_ir=False ) def test_check_grad_ingore_x(self): @@ -825,6 +849,7 @@ def test_check_grad_ingore_x(self): 'Out', no_grad_set=set("X"), check_prim=self.check_prim, + check_new_ir=False, ) def test_check_grad_ingore_y(self): @@ -833,6 +858,7 @@ def test_check_grad_ingore_y(self): 'Out', no_grad_set=set('Y'), check_prim=self.check_prim, + check_new_ir=False, ) def if_enable_cinn(self):