From 7d3b08d965e64d9d8a44a4c31e88a454192005b5 Mon Sep 17 00:00:00 2001 From: helen88 Date: Tue, 5 Jul 2022 17:49:41 +0800 Subject: [PATCH] refactor mean op, *test=kunlun (#44000) * refactor mean op, *test=kunlun * refactor mean op, *test=kunlun * refactor mean op,*test=kunlun * refactor mean op,*test=kunlun --- .../fluid/tests/unittests/xpu/CMakeLists.txt | 3 - .../tests/unittests/xpu/test_mean_op_xpu.py | 113 +++++++++--------- 2 files changed, 58 insertions(+), 58 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/xpu/CMakeLists.txt b/python/paddle/fluid/tests/unittests/xpu/CMakeLists.txt index 6267526f33..c6aaf36313 100644 --- a/python/paddle/fluid/tests/unittests/xpu/CMakeLists.txt +++ b/python/paddle/fluid/tests/unittests/xpu/CMakeLists.txt @@ -16,9 +16,6 @@ if(WITH_XPU_BKCL) list(APPEND DIST_TEST_OPS test_gen_bkcl_id_op) endif() -list(REMOVE_ITEM TEST_OPS test_concat_op_xpu) -list(REMOVE_ITEM TEST_OPS test_mean_op_xpu) - foreach(TEST_OP ${TEST_OPS}) py_test_modules(${TEST_OP} MODULES ${TEST_OP}) endforeach() diff --git a/python/paddle/fluid/tests/unittests/xpu/test_mean_op_xpu.py b/python/paddle/fluid/tests/unittests/xpu/test_mean_op_xpu.py index 892a5b6840..cd21dcca4c 100644 --- a/python/paddle/fluid/tests/unittests/xpu/test_mean_op_xpu.py +++ b/python/paddle/fluid/tests/unittests/xpu/test_mean_op_xpu.py @@ -28,29 +28,66 @@ from paddle.fluid import Program, program_guard np.random.seed(10) +import op_test +from op_test_xpu import XPUOpTest +from xpu.get_test_cover_info import create_test_class, get_xpu_op_support_types, XPUOpTestWrapper + +paddle.enable_static() + + +class XPUTestMeanOp(XPUOpTestWrapper): + + def __init__(self): + self.op_name = 'mean' + self.use_dynamic_create_class = False + + class TestMeanOp(XPUOpTest): + + def setUp(self): + self.init_dtype() + self.set_xpu() + self.op_type = "mean" + self.place = paddle.XPUPlace(0) + self.set_shape() + self.inputs = {'X': np.random.random(self.shape).astype(self.dtype)} + self.outputs = {'Out': np.mean(self.inputs["X"]).astype(np.float16)} + + def init_dtype(self): + self.dtype = self.in_type + + def set_shape(self): + self.shape = (10, 10) + + def set_xpu(self): + self.__class__.use_xpu = True + self.__class__.no_need_check_grad = True + self.__class__.op_type = self.dtype + + def test_check_output(self): + self.check_output_with_place(self.place) + + def test_checkout_grad(self): + self.check_grad_with_place(self.place, ['X'], 'Out') -class TestMeanOp(XPUOpTest): + class TestMeanOp1(TestMeanOp): - def setUp(self): - self.op_type = "mean" - self.init_dtype_type() - self.inputs = {'X': np.random.random((10, 10)).astype(self.dtype)} - self.outputs = {'Out': np.mean(self.inputs["X"]).astype(np.float16)} + def set_shape(self): + self.shape = (5) - def init_dtype_type(self): - self.dtype = np.float32 + class TestMeanOp2(TestMeanOp): - def test_check_output(self): - if paddle.is_compiled_with_xpu(): - paddle.enable_static() - place = paddle.XPUPlace(0) - self.check_output_with_place(place, atol=2e-3) + def set_shape(self): + self.shape = (5, 7, 8) - def test_checkout_grad(self): - if paddle.is_compiled_with_xpu(): - paddle.enable_static() - place = paddle.XPUPlace(0) - self.check_grad_with_place(place, ['X'], 'Out') + class TestMeanOp3(TestMeanOp): + + def set_shape(self): + self.shape = (10, 5, 7, 8) + + class TestMeanOp4(TestMeanOp): + + def set_shape(self): + self.shape = (2, 2, 3, 3, 3) class TestMeanOpError(unittest.TestCase): @@ -71,43 +108,9 @@ class TestMeanOpError(unittest.TestCase): fluid.layers.softmax(input3) -class TestXPUMeanOp(TestMeanOp): - - def init_dtype_type(self): - self.dtype = np.float32 - - def test_check_output(self): - if paddle.is_compiled_with_xpu(): - paddle.enable_static() - place = paddle.XPUPlace(0) - self.check_output_with_place(place) - - def test_checkout_grad(self): - if paddle.is_compiled_with_xpu(): - paddle.enable_static() - place = paddle.XPUPlace(0) - self.check_grad_with_place(place, ['X'], 'Out') - - -class TestXPUMeanOpFp16(TestMeanOp): - - def init_dtype_type(self): - self.dtype = np.float16 - - def test_check_output(self): - if paddle.is_compiled_with_xpu(): - paddle.enable_static() - place = paddle.XPUPlace(0) - self.check_output_with_place(place) - - def test_checkout_grad(self): - if paddle.is_compiled_with_xpu(): - paddle.enable_static() - place = paddle.XPUPlace(0) - self.check_grad_with_place(place, ['X'], - 'Out', - max_relative_error=1.e1) - +support_types = get_xpu_op_support_types('mean') +for stype in support_types: + create_test_class(globals(), XPUTestMeanOp, stype) if __name__ == "__main__": unittest.main() -- GitLab