From edd578a15c76ed27c5ed6913ba240ecc3ff6374c Mon Sep 17 00:00:00 2001 From: jiangcheng Date: Thu, 13 Apr 2023 11:33:36 +0800 Subject: [PATCH] [CINN] optest add cinn check test (#52205) * [CINN] optest add cinn check test * replace set self.check_cinn to pass check_cinn by function parameter * fix ci bug * add cinn atol/rtol --- .../fluid/tests/unittests/CMakeLists.txt | 2 + .../fluid/tests/unittests/eager_op_test.py | 72 +++++++++++++++++-- .../fluid/tests/unittests/test_clip_op.py | 5 +- .../fluid/tests/unittests/test_scale_op.py | 6 +- 4 files changed, 74 insertions(+), 11 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/CMakeLists.txt b/python/paddle/fluid/tests/unittests/CMakeLists.txt index 909b658c098..e0d89932a29 100755 --- a/python/paddle/fluid/tests/unittests/CMakeLists.txt +++ b/python/paddle/fluid/tests/unittests/CMakeLists.txt @@ -1115,6 +1115,8 @@ set(TEST_CINN_OPS test_mean_op test_unsqueeze2_op test_meshgrid_op + test_scale_op + test_clip_op test_scatter_op test_gather_op test_layer_norm_op diff --git a/python/paddle/fluid/tests/unittests/eager_op_test.py b/python/paddle/fluid/tests/unittests/eager_op_test.py index 9b0868edfa7..0bcb34f956b 100644 --- a/python/paddle/fluid/tests/unittests/eager_op_test.py +++ b/python/paddle/fluid/tests/unittests/eager_op_test.py @@ -494,6 +494,28 @@ class OpTest(unittest.TestCase): def disable_cal_ref_output(self): self.is_calc_ref = False + def _enable_check_cinn_test(self, place, inputs, outputs): + # if the test not run in cuda or the paddle not compile with CINN, skip cinn test + if ( + core.is_compiled_with_cinn() + and core.is_compiled_with_cuda() + and isinstance(place, fluid.CUDAPlace) + ): + return False + # CINN not support bfloat16 now, skip cinn test + if self.is_bfloat16_op(): + return False + # CINN not support 0D-tensor now, skip cinn test + for var in inputs.values(): + if len(var.shape()) == 0: + return False + for var in outputs.values(): + if len(var.shape) == 0: + return False + # CINN not support dynamic shape now, skip cinn test + # TODO(thisjiang): cannot check dynamic shape op automatic, should do manually now + return True + # set the self.output_dtype . def infer_dtype_from_inputs_outputs(self, inputs, outputs): def is_np_data(input): @@ -1044,6 +1066,7 @@ class OpTest(unittest.TestCase): loss=None, enable_inplace=None, for_inplace_test=None, + check_cinn=False, ): with paddle.fluid.framework._static_guard(): program = Program() @@ -1087,9 +1110,21 @@ class OpTest(unittest.TestCase): for out_name, out_dup in Operator.get_op_outputs(self.op_type): fetch_list.append(str(out_name)) - if enable_inplace is not None: + enable_cinn_test = check_cinn and self._enable_check_cinn_test( + place, feed_map, outputs + ) + if enable_cinn_test: + if hasattr(self, 'cinn_atol'): + self.atol = self.cinn_atol + if hasattr(self, 'cinn_rtol'): + self.rtol = self.cinn_rtol + + if (enable_inplace is not None) or enable_cinn_test: build_strategy = fluid.BuildStrategy() - build_strategy.enable_inplace = enable_inplace + if enable_inplace is not None: + build_strategy.enable_inplace = enable_inplace + if enable_cinn_test: + build_strategy.build_cinn_pass = check_cinn compiled_prog = fluid.CompiledProgram( program, build_strategy=build_strategy @@ -1518,6 +1553,7 @@ class OpTest(unittest.TestCase): check_dygraph=True, check_prim=False, inplace_atol=None, + check_cinn=False, ): core._set_prim_all_enabled(False) core.set_prim_eager_enabled(False) @@ -1626,7 +1662,7 @@ class OpTest(unittest.TestCase): np.testing.assert_allclose( actual_np, expect_np, - atol=atol, + atol=self.atol if hasattr(self, 'atol') else atol, rtol=self.rtol if hasattr(self, 'rtol') else rtol, equal_nan=equal_nan, err_msg=( @@ -1645,7 +1681,7 @@ class OpTest(unittest.TestCase): np.allclose( actual_np, expect_np, - atol=atol, + atol=self.atol if hasattr(self, 'atol') else atol, rtol=self.rtol if hasattr(self, 'rtol') else rtol, equal_nan=equal_nan, ), @@ -1721,7 +1757,7 @@ class OpTest(unittest.TestCase): def calculate_output(self): outs, fetch_list = self.op_test._calc_output( - place, no_check_set=no_check_set + place, no_check_set=no_check_set, check_cinn=check_cinn ) self.outputs = outs self.fetch_list = fetch_list @@ -2077,6 +2113,7 @@ class OpTest(unittest.TestCase): check_dygraph=True, check_prim=False, inplace_atol=None, + check_cinn=False, ): self.__class__.op_type = self.op_type @@ -2100,6 +2137,7 @@ class OpTest(unittest.TestCase): check_dygraph=check_dygraph, check_prim=check_prim, inplace_atol=inplace_atol, + check_cinn=check_cinn, ) if check_dygraph: outs, dygraph_dygraph_outs, fetch_list = res @@ -2257,6 +2295,7 @@ class OpTest(unittest.TestCase): check_prim=False, only_check_prim=False, atol=1e-5, + check_cinn=False, ): if hasattr(self, "use_custom_device") and self.use_custom_device: check_dygraph = False @@ -2278,6 +2317,7 @@ class OpTest(unittest.TestCase): check_prim=check_prim, only_check_prim=only_check_prim, atol=atol, + check_cinn=check_cinn, ) def check_grad_with_place( @@ -2296,6 +2336,7 @@ class OpTest(unittest.TestCase): only_check_prim=False, numeric_place=None, atol=1e-5, + check_cinn=False, ): if hasattr(self, "use_custom_device") and self.use_custom_device: check_dygraph = False @@ -2427,6 +2468,7 @@ class OpTest(unittest.TestCase): output_names, no_grad_set, user_defined_grad_outputs, + check_cinn=check_cinn, ) # comparison of bf16 results will happen as fp32 # loop over list of grads and convert bf16 to fp32 @@ -2655,6 +2697,7 @@ class OpTest(unittest.TestCase): no_grad_set, user_defined_grad_outputs=None, parallel=False, + check_cinn=False, ): with paddle.fluid.framework._static_guard(): prog = Program() @@ -2721,11 +2764,26 @@ class OpTest(unittest.TestCase): ) fetch_list = grad_inputs - if parallel: + enable_cinn_test = check_cinn and self._enable_check_cinn_test( + place, feed_dict, outputs + ) + if enable_cinn_test: + if hasattr(self, 'cinn_atol'): + self.atol = self.cinn_atol + if hasattr(self, 'cinn_rtol'): + self.rtol = self.cinn_rtol + + if parallel or enable_cinn_test: use_cuda = False if isinstance(place, fluid.CUDAPlace): use_cuda = True - compiled_prog = fluid.CompiledProgram(prog) + + build_strategy = None + if enable_cinn_test: + build_strategy = fluid.BuildStrategy() + build_strategy.build_cinn_pass = check_cinn + + compiled_prog = fluid.CompiledProgram(prog, build_strategy) prog = compiled_prog executor = fluid.Executor(place) res = list( diff --git a/python/paddle/fluid/tests/unittests/test_clip_op.py b/python/paddle/fluid/tests/unittests/test_clip_op.py index eca97092a64..b807d01ada0 100644 --- a/python/paddle/fluid/tests/unittests/test_clip_op.py +++ b/python/paddle/fluid/tests/unittests/test_clip_op.py @@ -49,10 +49,13 @@ class TestClipOp(OpTest): input[np.abs(input - max_v) < self.max_relative_error] = 0.5 self.inputs['X'] = input self.outputs = {'Out': np.clip(self.inputs['X'], min_v, max_v)} + self.check_cinn = ('Min' not in self.inputs) and ( + 'Max' not in self.inputs + ) def test_check_output(self): paddle.enable_static() - self.check_output() + self.check_output(check_cinn=self.check_cinn) paddle.disable_static() def test_check_grad_normal(self): diff --git a/python/paddle/fluid/tests/unittests/test_scale_op.py b/python/paddle/fluid/tests/unittests/test_scale_op.py index c28ba1d1725..137a31c89fb 100644 --- a/python/paddle/fluid/tests/unittests/test_scale_op.py +++ b/python/paddle/fluid/tests/unittests/test_scale_op.py @@ -42,7 +42,7 @@ class TestScaleOp(OpTest): pass def test_check_output(self): - self.check_output() + self.check_output(check_cinn=True) def test_check_grad(self): self.check_grad(['X'], 'Out') @@ -66,7 +66,7 @@ class TestScaleOpScaleVariable(OpTest): pass def test_check_output(self): - self.check_output() + self.check_output(check_cinn=True) def test_check_grad(self): self.check_grad(['X'], 'Out') @@ -148,7 +148,7 @@ class TestScaleFp16Op(TestScaleOp): self.dtype = np.float16 def test_check_output(self): - self.check_output() + self.check_output(check_cinn=True) def test_check_grad(self): self.check_grad(["X"], "Out") -- GitLab