From 187703928492a91f6beae033f5ee1aa41ada6169 Mon Sep 17 00:00:00 2001 From: WangZhen <23097963+0x45f@users.noreply.github.com> Date: Fri, 20 Oct 2023 11:03:45 +0800 Subject: [PATCH] [PIR]Open more TestCase UTs (#57977) --- python/paddle/base/dygraph/base.py | 2 +- test/legacy_test/test_activation_op.py | 1 + test/legacy_test/test_allclose_op.py | 2 + test/legacy_test/test_clip_op.py | 53 ++++++++++--------- .../test_flatten_contiguous_range_op.py | 5 +- test/legacy_test/test_reduce_op.py | 19 ++++--- 6 files changed, 50 insertions(+), 32 deletions(-) diff --git a/python/paddle/base/dygraph/base.py b/python/paddle/base/dygraph/base.py index 5fad89935d4c7e..cadb6bcb089377 100644 --- a/python/paddle/base/dygraph/base.py +++ b/python/paddle/base/dygraph/base.py @@ -595,7 +595,7 @@ def guard(place=None): if place is not None: expected_place = _get_paddle_place(place) else: - expected_place = framework._current_expected_place() + expected_place = framework._current_expected_place_() with framework.program_guard(train, startup): with framework.unique_name.guard(): diff --git a/test/legacy_test/test_activation_op.py b/test/legacy_test/test_activation_op.py index 2fbc85815ff58f..d1da7d941a679e 100644 --- a/test/legacy_test/test_activation_op.py +++ b/test/legacy_test/test_activation_op.py @@ -2383,6 +2383,7 @@ def setUp(self): def executed_api(self): self.relu = F.relu + @test_with_pir_api def test_static_api(self): with static_guard(): with paddle.static.program_guard(paddle.static.Program()): diff --git a/test/legacy_test/test_allclose_op.py b/test/legacy_test/test_allclose_op.py index 54e78867e7443f..474f3edb3063f4 100644 --- a/test/legacy_test/test_allclose_op.py +++ b/test/legacy_test/test_allclose_op.py @@ -19,6 +19,7 @@ import paddle from paddle.base import core +from paddle.pir_utils import test_with_pir_api class TestAllcloseOp(OpTest): @@ -174,6 +175,7 @@ def test_equal_nan(): class TestAllcloseOpFp16(unittest.TestCase): + @test_with_pir_api def test_fp16(self): x_data = np.random.rand(10, 10).astype('float16') y_data = np.random.rand(10, 10).astype('float16') diff --git a/test/legacy_test/test_clip_op.py b/test/legacy_test/test_clip_op.py index 6ac2b0a17e7ca4..1fad87de2d1dce 100644 --- a/test/legacy_test/test_clip_op.py +++ b/test/legacy_test/test_clip_op.py @@ -20,6 +20,7 @@ import paddle from paddle import base from paddle.base import Program, core, program_guard +from paddle.pir_utils import test_with_pir_api class TestClipOp(OpTest): @@ -266,16 +267,11 @@ class TestClipAPI(unittest.TestCase): def _executed_api(self, x, min=None, max=None): return paddle.clip(x, min, max) + @test_with_pir_api def test_clip(self): paddle.enable_static() data_shape = [1, 9, 9, 4] data = np.random.random(data_shape).astype('float32') - images = paddle.static.data( - name='image', shape=data_shape, dtype='float32' - ) - min = paddle.static.data(name='min', shape=[1], dtype='float32') - max = paddle.static.data(name='max', shape=[1], dtype='float32') - place = ( base.CUDAPlace(0) if base.core.is_compiled_with_cuda() @@ -283,23 +279,31 @@ def test_clip(self): ) exe = base.Executor(place) - out_1 = self._executed_api(images, min=min, max=max) - out_2 = self._executed_api(images, min=0.2, max=0.9) - out_3 = self._executed_api(images, min=0.3) - out_4 = self._executed_api(images, max=0.7) - out_5 = self._executed_api(images, min=min) - out_6 = self._executed_api(images, max=max) - out_7 = self._executed_api(images, max=-1.0) - out_8 = self._executed_api(images) - out_9 = self._executed_api( - paddle.cast(images, 'float64'), min=0.2, max=0.9 - ) - out_10 = self._executed_api( - paddle.cast(images * 10, 'int32'), min=2, max=8 - ) - out_11 = self._executed_api( - paddle.cast(images * 10, 'int64'), min=2, max=8 - ) + main = paddle.static.Program() + startup = paddle.static.Program() + with paddle.static.program_guard(main, startup): + images = paddle.static.data( + name='image', shape=data_shape, dtype='float32' + ) + min = paddle.static.data(name='min', shape=[1], dtype='float32') + max = paddle.static.data(name='max', shape=[1], dtype='float32') + out_1 = self._executed_api(images, min=min, max=max) + out_2 = self._executed_api(images, min=0.2, max=0.9) + out_3 = self._executed_api(images, min=0.3) + out_4 = self._executed_api(images, max=0.7) + out_5 = self._executed_api(images, min=min) + out_6 = self._executed_api(images, max=max) + out_7 = self._executed_api(images, max=-1.0) + out_8 = self._executed_api(images) + out_9 = self._executed_api( + paddle.cast(images, 'float64'), min=0.2, max=0.9 + ) + out_10 = self._executed_api( + paddle.cast(images * 10, 'int32'), min=2, max=8 + ) + out_11 = self._executed_api( + paddle.cast(images * 10, 'int64'), min=2, max=8 + ) ( res1, @@ -314,7 +318,7 @@ def test_clip(self): res10, res11, ) = exe.run( - base.default_main_program(), + main, feed={ "image": data, "min": np.array([0.2]).astype('float32'), @@ -430,6 +434,7 @@ def test_errors(self): class TestClipOpFp16(unittest.TestCase): + @test_with_pir_api def test_fp16(self): paddle.enable_static() data_shape = [1, 9, 9, 4] diff --git a/test/legacy_test/test_flatten_contiguous_range_op.py b/test/legacy_test/test_flatten_contiguous_range_op.py index 83354d87b705bf..82ba03f559efc2 100644 --- a/test/legacy_test/test_flatten_contiguous_range_op.py +++ b/test/legacy_test/test_flatten_contiguous_range_op.py @@ -19,6 +19,7 @@ import paddle from paddle.base import core +from paddle.pir_utils import test_with_pir_api class TestFlattenOp(OpTest): @@ -461,6 +462,7 @@ class TestStaticFlattenPythonAPI(unittest.TestCase): def execute_api(self, x, start_axis=0, stop_axis=-1): return paddle.flatten(x, start_axis, stop_axis) + @test_with_pir_api def test_static_api(self): paddle.enable_static() np_x = np.random.rand(2, 3, 4, 4).astype('float32') @@ -481,6 +483,7 @@ class TestStaticFlattenInferShapePythonAPI(unittest.TestCase): def execute_api(self, x, start_axis=0, stop_axis=-1): return paddle.flatten(x, start_axis, stop_axis) + @test_with_pir_api def test_static_api(self): paddle.enable_static() main_prog = paddle.static.Program() @@ -489,7 +492,7 @@ def test_static_api(self): name="x", shape=[-1, 3, -1, -1], dtype='float32' ) out = self.execute_api(x, start_axis=2, stop_axis=3) - self.assertTrue((-1, 3, -1) == out.shape) + self.assertTrue((-1, 3, -1) == tuple(out.shape)) class TestStaticInplaceFlattenPythonAPI(TestStaticFlattenPythonAPI): diff --git a/test/legacy_test/test_reduce_op.py b/test/legacy_test/test_reduce_op.py index a7595cd1331c83..a88f2650a005dc 100644 --- a/test/legacy_test/test_reduce_op.py +++ b/test/legacy_test/test_reduce_op.py @@ -1713,21 +1713,25 @@ def setUp(self): self.places.append(base.CUDAPlace(0)) def check_static_result(self, place): - with base.program_guard(base.Program(), base.Program()): + main = paddle.static.Program() + startup = paddle.static.Program() + with base.program_guard(main, startup): input = paddle.static.data(name="input", shape=[4, 4], dtype="bool") result = paddle.all(x=input) input_np = np.random.randint(0, 2, [4, 4]).astype("bool") exe = base.Executor(place) fetches = exe.run( - base.default_main_program(), + main, feed={"input": input_np}, fetch_list=[result], ) self.assertTrue((fetches[0] == np.all(input_np)).all()) def check_static_float_result(self, place): - with base.program_guard(base.Program(), base.Program()): + main = paddle.static.Program() + startup = paddle.static.Program() + with base.program_guard(main, startup): input = paddle.static.data( name="input", shape=[4, 4], dtype="float" ) @@ -1736,26 +1740,29 @@ def check_static_float_result(self, place): exe = base.Executor(place) fetches = exe.run( - base.default_main_program(), + main, feed={"input": input_np}, fetch_list=[result], ) self.assertTrue((fetches[0] == np.all(input_np)).all()) def check_static_int_result(self, place): - with base.program_guard(base.Program(), base.Program()): + main = paddle.static.Program() + startup = paddle.static.Program() + with base.program_guard(main, startup): input = paddle.static.data(name="input", shape=[4, 4], dtype="int") result = paddle.all(x=input) input_np = np.random.randint(0, 2, [4, 4]).astype("int") exe = base.Executor(place) fetches = exe.run( - base.default_main_program(), + main, feed={"input": input_np}, fetch_list=[result], ) self.assertTrue((fetches[0] == np.all(input_np)).all()) + @test_with_pir_api def test_static(self): for place in self.places: self.check_static_result(place=place)