diff --git a/python/paddle/nn/functional/pooling.py b/python/paddle/nn/functional/pooling.py index 07bdc48243314..e43daa332aea3 100755 --- a/python/paddle/nn/functional/pooling.py +++ b/python/paddle/nn/functional/pooling.py @@ -253,7 +253,7 @@ def avg_pool1d( # use 2d to implenment 1d should expand padding in advance. padding = _expand_low_nd_padding(padding) - if in_dygraph_mode(): + if in_dynamic_or_pir_mode(): output = _C_ops.pool2d( x, kernel_size, @@ -505,7 +505,7 @@ def avg_pool3d( _check_value_limitation(kernel_size, "kernel_size", min_limit=1e-3) _check_value_limitation(stride, "stride", min_limit=1e-3) - if in_dygraph_mode(): + if in_dynamic_or_pir_mode(): pool_out = _C_ops.pool3d( x, kernel_size, @@ -629,7 +629,7 @@ def max_pool1d( # use 2d to implenment 1d should expand padding in advance. padding = _expand_low_nd_padding(padding) - if in_dygraph_mode(): + if in_dynamic_or_pir_mode(): if return_mask: pool_out = _C_ops.max_pool2d_with_index( x, kernel_size, stride, padding, False, False @@ -829,7 +829,7 @@ def max_unpool1d( x, kernel_size, stride, padding, output_size ) - if in_dygraph_mode(): + if in_dynamic_or_pir_mode(): output = _C_ops.unpool( x, indices, kernel_size, stride, padding, output_size, data_format ) @@ -979,7 +979,7 @@ def max_unpool2d( x, kernel_size, stride, padding, output_size ) - if in_dygraph_mode(): + if in_dynamic_or_pir_mode(): output = _C_ops.unpool( x, indices, kernel_size, stride, padding, output_size, data_format ) @@ -1126,7 +1126,7 @@ def max_unpool3d( x, kernel_size, stride, padding, output_size ) - if in_dygraph_mode(): + if in_dynamic_or_pir_mode(): output = _C_ops.unpool3d( x, indices, kernel_size, stride, padding, output_size, data_format ) @@ -1423,7 +1423,7 @@ def max_pool3d( "When setting return_mask to true, data_format must be set to NCDHW in API:max_pool3d" ) - if in_dygraph_mode(): + if in_dynamic_or_pir_mode(): if return_mask: output = _C_ops.max_pool3d_with_index( x, kernel_size, stride, padding, False, False @@ -1651,7 +1651,7 @@ def adaptive_avg_pool2d(x, output_size, data_format='NCHW', name=None): output_size = _convert_to_tensor_list(output_size) if in_dynamic_or_pir_mode(): - if in_dygraph_mode(): + if in_dynamic_mode(): x = x._use_gpudnn(False) return _C_ops.pool2d( x, diff --git a/test/legacy_test/test_pool1d_api.py b/test/legacy_test/test_pool1d_api.py index 1381470481c53..4545ea4109e50 100644 --- a/test/legacy_test/test_pool1d_api.py +++ b/test/legacy_test/test_pool1d_api.py @@ -20,6 +20,7 @@ import paddle.nn.functional as F from paddle import base from paddle.base import core +from paddle.pir_utils import test_with_pir_api def adaptive_start_index(index, input_size, output_size): @@ -121,8 +122,9 @@ def setUp(self): if core.is_compiled_with_cuda(): self.places.append(base.CUDAPlace(0)) + @test_with_pir_api def check_avg_static_results(self, place): - with base.program_guard(base.Program(), base.Program()): + with paddle.static.program_guard(paddle.static.Program()): input = paddle.static.data( name="input", shape=[2, 3, 32], dtype="float32" ) @@ -133,14 +135,14 @@ def check_avg_static_results(self, place): input_np, ksize=[2], strides=[2], paddings=[0], ceil_mode=False ) - exe = base.Executor(place) + exe = paddle.static.Executor(place) fetches = exe.run( - base.default_main_program(), feed={"input": input_np}, fetch_list=[result], ) np.testing.assert_allclose(fetches[0], result_np, rtol=1e-05) + @test_with_pir_api def check_avg_static_results_fp16(self, place): with paddle.static.program_guard(paddle.static.Program()): input = paddle.static.data( @@ -161,7 +163,6 @@ def check_avg_static_results_fp16(self, place): place = paddle.CUDAPlace(0) exe = paddle.static.Executor(place) fetches = exe.run( - paddle.static.default_main_program(), feed={"input": input_np}, fetch_list=[result], ) @@ -206,8 +207,11 @@ def check_avg_dygraph_padding_results(self, place): result = avg_pool1d_dg(input) np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05) + @test_with_pir_api def check_max_static_results(self, place): - with base.program_guard(base.Program(), base.Program()): + with paddle.static.program_guard( + paddle.static.Program(), paddle.static.Program() + ): input = paddle.static.data( name="input", shape=[2, 3, 32], dtype="float32" ) @@ -218,9 +222,8 @@ def check_max_static_results(self, place): input_np, ksize=[2], strides=[2], paddings=[0] ) - exe = base.Executor(place) + exe = paddle.static.Executor(place) fetches = exe.run( - base.default_main_program(), feed={"input": input_np}, fetch_list=[result], ) diff --git a/test/legacy_test/test_pool2d_api.py b/test/legacy_test/test_pool2d_api.py index 84615340fe051..638ab44e74a18 100644 --- a/test/legacy_test/test_pool2d_api.py +++ b/test/legacy_test/test_pool2d_api.py @@ -36,7 +36,9 @@ def setUp(self): self.places.append(base.CUDAPlace(0)) def check_avg_static_results(self, place): - with base.program_guard(base.Program(), base.Program()): + with paddle.static.program_guard( + paddle.static.Program(), paddle.static.Program() + ): input = paddle.static.data( name="input", shape=[2, 3, 32, 32], dtype="float32" ) @@ -53,7 +55,6 @@ def check_avg_static_results(self, place): exe = base.Executor(place) fetches = exe.run( - paddle.static.default_main_program(), feed={"input": input_np}, fetch_list=[result], ) @@ -128,7 +129,9 @@ def check_avg_dygraph_ceilmode_results(self, place): np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05) def check_max_static_results(self, place): - with base.program_guard(base.Program(), base.Program()): + with paddle.static.program_guard( + paddle.static.Program(), paddle.static.Program() + ): input = paddle.static.data( name="input", shape=[2, 3, 32, 32], dtype="float32" ) @@ -145,7 +148,6 @@ def check_max_static_results(self, place): exe = base.Executor(place) fetches = exe.run( - paddle.static.default_main_program(), feed={"input": input_np}, fetch_list=[result], ) diff --git a/test/legacy_test/test_pool2d_op.py b/test/legacy_test/test_pool2d_op.py index 1ea560e211f23..c27ba701bab89 100644 --- a/test/legacy_test/test_pool2d_op.py +++ b/test/legacy_test/test_pool2d_op.py @@ -19,6 +19,7 @@ import paddle from paddle.base import core +from paddle.framework import in_dynamic_mode def adaptive_start_index(index, input_size, output_size): @@ -298,11 +299,12 @@ def pool2d_wrapper_not_use_cudnn( adaptive=False, padding_algorithm="EXPLICIT", ): - tmp = X._use_gpudnn(False) + if in_dynamic_mode(): + X = X._use_gpudnn(False) if data_format == "AnyLayout": data_format = "NCDHW" return paddle._C_ops.pool2d( - tmp, + X, ksize, strides, paddings, @@ -425,9 +427,13 @@ def test_check_output(self): atol=1e-5, check_dygraph=(not self.use_mkldnn), check_cinn=True, + check_pir=True, ) else: - self.check_output(check_dygraph=(not self.use_mkldnn)) + self.check_output( + check_dygraph=(not self.use_mkldnn), + check_pir=True, + ) def test_check_grad(self): if self.dtype == np.float16: @@ -441,6 +447,7 @@ def test_check_grad(self): 'Out', check_dygraph=(not self.use_mkldnn), check_cinn=True, + check_pir=True, ) elif self.pool_type != "max": self.check_grad( @@ -448,6 +455,7 @@ def test_check_grad(self): 'Out', max_relative_error=0.07, check_dygraph=(not self.use_mkldnn), + check_pir=True, ) def init_data_format(self): @@ -1011,11 +1019,20 @@ def test_check_grad(self): if self.has_cudnn() and self.pool_type == "max": place = core.CUDAPlace(0) self.check_grad_with_place( - place, {'X'}, 'Out', max_relative_error=1.00, check_cinn=True + place, + {'X'}, + 'Out', + max_relative_error=1.00, + check_cinn=True, + check_pir=True, ) elif self.pool_type == "max": self.check_grad( - {'X'}, 'Out', max_relative_error=1.00, check_cinn=True + {'X'}, + 'Out', + max_relative_error=1.00, + check_cinn=True, + check_pir=True, ) diff --git a/test/legacy_test/test_pool3d_api.py b/test/legacy_test/test_pool3d_api.py index a2286740391e4..ecd675bb5dc48 100644 --- a/test/legacy_test/test_pool3d_api.py +++ b/test/legacy_test/test_pool3d_api.py @@ -25,6 +25,7 @@ from paddle import base from paddle.base import core from paddle.nn.functional import avg_pool3d, max_pool3d +from paddle.pir_utils import test_with_pir_api class TestPool3D_API(unittest.TestCase): @@ -34,8 +35,11 @@ def setUp(self): if core.is_compiled_with_cuda(): self.places.append(base.CUDAPlace(0)) + @test_with_pir_api def check_avg_static_results(self, place): - with base.program_guard(base.Program(), base.Program()): + with paddle.static.program_guard( + paddle.static.Program(), paddle.static.Program() + ): input = paddle.static.data( name="input", shape=[2, 3, 32, 32, 32], dtype="float32" ) @@ -50,9 +54,8 @@ def check_avg_static_results(self, place): pool_type='avg', ) - exe = base.Executor(place) + exe = paddle.static.Executor(place) fetches = exe.run( - base.default_main_program(), feed={"input": input_np}, fetch_list=[result], ) @@ -139,8 +142,11 @@ def check_avg_dygraph_ceilmode_results(self, place): result = avg_pool3d_dg(input) np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05) + @test_with_pir_api def check_max_static_results(self, place): - with base.program_guard(base.Program(), base.Program()): + with paddle.static.program_guard( + paddle.static.Program(), paddle.static.Program() + ): input = paddle.static.data( name="input", shape=[2, 3, 32, 32, 32], dtype="float32" ) @@ -155,9 +161,8 @@ def check_max_static_results(self, place): pool_type='max', ) - exe = base.Executor(place) + exe = paddle.static.Executor(place) fetches = exe.run( - base.default_main_program(), feed={"input": input_np}, fetch_list=[result], ) @@ -366,6 +371,7 @@ def test_pool3d(self): self.check_max_dygraph_ndhwc_results(place) self.check_max_dygraph_ceilmode_results(place) + @test_with_pir_api def test_static_fp16_gpu(self): paddle.enable_static() if paddle.base.core.is_compiled_with_cuda(): @@ -384,7 +390,6 @@ def test_static_fp16_gpu(self): exe = paddle.static.Executor(place) res = exe.run( - paddle.static.default_main_program(), feed={ "x": input, }, @@ -393,6 +398,7 @@ def test_static_fp16_gpu(self): np.testing.assert_array_equal(res[0].shape, [1, 2, 1, 16, 16]) + @test_with_pir_api def test_static_bf16_gpu(self): paddle.enable_static() if ( @@ -414,7 +420,6 @@ def test_static_bf16_gpu(self): exe = paddle.static.Executor(place) res = exe.run( - paddle.static.default_main_program(), feed={ "x": input, }, diff --git a/test/legacy_test/test_pool3d_op.py b/test/legacy_test/test_pool3d_op.py index 350d62098befa..f80044f2c0505 100644 --- a/test/legacy_test/test_pool3d_op.py +++ b/test/legacy_test/test_pool3d_op.py @@ -19,6 +19,7 @@ import paddle from paddle.base import core +from paddle.framework import in_dynamic_mode def adaptive_start_index(index, input_size, output_size): @@ -282,11 +283,12 @@ def pool3d_wrapper_not_use_cudnn( adaptive=False, padding_algorithm="EXPLICIT", ): - tmp = X._use_gpudnn(False) + if in_dynamic_mode(): + X = X._use_gpudnn(False) if data_format == "AnyLayout": data_format = "NCDHW" return paddle._C_ops.pool3d( - tmp, + X, ksize, strides, paddings, @@ -392,9 +394,9 @@ def has_cudnn(self): def test_check_output(self): if self.has_cudnn(): place = core.CUDAPlace(0) - self.check_output_with_place(place, atol=1e-5) + self.check_output_with_place(place, atol=1e-5, check_pir=True) else: - self.check_output() + self.check_output(check_pir=True) def test_check_grad(self): if ( @@ -403,15 +405,17 @@ def test_check_grad(self): place = core.CUDAPlace(0) if core.is_compiled_with_rocm(): self.check_grad_with_place( - place, {'X'}, 'Out', max_relative_error=1e-2 + place, {'X'}, 'Out', max_relative_error=1e-2, check_pir=True ) else: - self.check_grad_with_place(place, {'X'}, 'Out') + self.check_grad_with_place(place, {'X'}, 'Out', check_pir=True) elif self.pool_type != "max": if core.is_compiled_with_rocm(): - self.check_grad({'X'}, 'Out', max_relative_error=1e-2) + self.check_grad( + {'X'}, 'Out', max_relative_error=1e-2, check_pir=True + ) else: - self.check_grad({'X'}, 'Out') + self.check_grad({'X'}, 'Out', check_pir=True) def init_data_format(self): self.data_format = "NCDHW" @@ -535,9 +539,13 @@ def test_check_output(self): place = core.CUDAPlace(0) if core.is_float16_supported(place): if core.is_compiled_with_rocm(): - self.check_output_with_place(place, atol=1e-2) + self.check_output_with_place( + place, atol=1e-2, check_pir=True + ) else: - self.check_output_with_place(place, atol=1e-3) + self.check_output_with_place( + place, atol=1e-3, check_pir=True + ) cls_name = "{}_{}".format(parent.__name__, "CUDNNFp16Op") TestCUDNNFp16Case.__name__ = cls_name @@ -557,7 +565,9 @@ def test_check_output(self): if core.is_compiled_with_cuda(): place = core.CUDAPlace(0) if core.is_float16_supported(place): - self.check_output_with_place(place, atol=1e-2) + self.check_output_with_place( + place, atol=1e-2, check_pir=True + ) cls_name = "{}_{}".format(parent.__name__, "Fp16Op") TestFp16Case.__name__ = cls_name @@ -577,7 +587,7 @@ def init_kernel_type(self): def test_check_output(self): place = core.CUDAPlace(0) - self.check_output_with_place(place) + self.check_output_with_place(place, check_pir=True) cls_name = "{}_{}".format(parent.__name__, "CUDNNBf16Op") TestCUDNNBf16Case.__name__ = cls_name @@ -597,7 +607,7 @@ def init_kernel_type(self): def test_check_output(self): place = core.CUDAPlace(0) - self.check_output_with_place(place) + self.check_output_with_place(place, check_pir=True) cls_name = "{}_{}".format(parent.__name__, "Bf16Op") TestBf16Case.__name__ = cls_name @@ -908,10 +918,12 @@ def test_check_grad(self): if self.has_cudnn() and self.pool_type == "max": place = core.CUDAPlace(0) self.check_grad_with_place( - place, {'X'}, 'Out', max_relative_error=1.00 + place, {'X'}, 'Out', max_relative_error=1.00, check_pir=True ) elif self.pool_type == "max": - self.check_grad({'X'}, 'Out', max_relative_error=1.00) + self.check_grad( + {'X'}, 'Out', max_relative_error=1.00, check_pir=True + ) class TestCase5_channel_last_Max(TestCase5_Max): diff --git a/test/legacy_test/test_unpool1d_op.py b/test/legacy_test/test_unpool1d_op.py index 8d6435cc2a86f..989137d876a06 100644 --- a/test/legacy_test/test_unpool1d_op.py +++ b/test/legacy_test/test_unpool1d_op.py @@ -18,6 +18,7 @@ import paddle import paddle.nn.functional as F +from paddle.pir_utils import test_with_pir_api paddle.enable_static() paddle.seed(2022) @@ -135,6 +136,7 @@ def test_case(self): class TestUnpool1DOpAPI_static(unittest.TestCase): + @test_with_pir_api def test_case(self): paddle.enable_static() places = [paddle.CPUPlace()] @@ -157,9 +159,8 @@ def test_case(self): output, indices, kernel_size=2, stride=None ) - exe = paddle.base.Executor(place) + exe = paddle.static.Executor(place) fetches = exe.run( - paddle.base.default_main_program(), feed={"x": input_data}, fetch_list=[output_unpool], return_numpy=True, diff --git a/test/legacy_test/test_unpool3d_op.py b/test/legacy_test/test_unpool3d_op.py index eca6b989dbdbc..42a7150fd912f 100644 --- a/test/legacy_test/test_unpool3d_op.py +++ b/test/legacy_test/test_unpool3d_op.py @@ -20,6 +20,7 @@ import paddle import paddle.nn.functional as F from paddle.base import core +from paddle.pir_utils import test_with_pir_api paddle.enable_static() paddle.seed(2022) @@ -143,10 +144,10 @@ def setUp(self): self.outputs = {'Out': output.astype('float64')} def test_check_output(self): - self.check_output() + self.check_output(check_pir=True) def test_check_grad(self): - self.check_grad(['X'], 'Out') + self.check_grad(['X'], 'Out', check_pir=True) def init_test_case(self): self.unpool3d_forward_naive = unpool3dmax_forward_naive @@ -373,6 +374,7 @@ def test_case(self): class TestUnpool3DOpAPI_static(unittest.TestCase): + @test_with_pir_api def test_case(self): paddle.enable_static() places = [paddle.CPUPlace()] @@ -412,9 +414,8 @@ def test_case(self): output, indices, kernel_size=2, stride=None ) - exe = paddle.base.Executor(place) + exe = paddle.static.Executor(place) fetches = exe.run( - paddle.base.default_main_program(), feed={"x": input_data}, fetch_list=[output_unpool], return_numpy=True, diff --git a/test/legacy_test/test_unpool_op.py b/test/legacy_test/test_unpool_op.py index 2d76b32cde4b6..1f8f72f3ae08d 100644 --- a/test/legacy_test/test_unpool_op.py +++ b/test/legacy_test/test_unpool_op.py @@ -21,7 +21,9 @@ import paddle import paddle.nn.functional as F -from paddle.base import Program, core, program_guard +from paddle.base import core +from paddle.framework import in_pir_mode +from paddle.pir_utils import test_with_pir_api def _unpool_output_size(x, kernel_size, stride, padding, output_size): @@ -130,10 +132,10 @@ def setUp(self): self.outputs = {'Out': output.astype('float64')} def test_check_output(self): - self.check_output() + self.check_output(check_pir=True) def test_check_grad(self): - self.check_grad(['X'], 'Out') + self.check_grad(['X'], 'Out', check_pir=True) def init_test_case(self): self.unpool2d_forward_naive = unpool2dmax_forward_naive @@ -399,10 +401,10 @@ def test_case(self): class TestUnpoolOpAPI_st(unittest.TestCase): + @test_with_pir_api def test_case(self): import paddle import paddle.nn.functional as F - from paddle import base from paddle.base import core paddle.enable_static() @@ -422,11 +424,9 @@ def test_case(self): place = core.CUDAPlace(0) else: place = core.CPUPlace() - exe = base.Executor(place) - exe.run(base.default_startup_program()) + exe = paddle.static.Executor(place) results = exe.run( - paddle.base.default_main_program(), feed={"x": input_data}, fetch_list=[unpool_out], return_numpy=True, @@ -446,10 +446,12 @@ def init_info(self): self.shapes = [[1, 3, 6, 6]] self.save_path = os.path.join(self.temp_dir.name, self.path_prefix()) + @test_with_pir_api def test_static(self): - main_prog = Program() - starup_prog = Program() - with program_guard(main_prog, starup_prog): + paddle.enable_static() + main_prog = paddle.static.Program() + starup_prog = paddle.static.Program() + with paddle.static.program_guard(main_prog, starup_prog): fc = paddle.nn.Linear(6, 6) x = paddle.randn(self.shapes[0]) x.stop_gradient = False @@ -459,16 +461,21 @@ def test_static(self): sgd = paddle.optimizer.SGD() sgd.minimize(paddle.mean(out)) - self.assertTrue(self.var_prefix() in str(main_prog)) + + if not in_pir_mode(): + self.assertTrue(self.var_prefix() in str(main_prog)) exe = paddle.static.Executor() exe.run(starup_prog) res = exe.run(fetch_list=[out]) np.testing.assert_array_equal(res[0].shape, [1, 3, 7, 7]) - paddle.static.save_inference_model(self.save_path, [x], [out], exe) - # Test for Inference Predictor - infer_outs = self.infer_prog() - np.testing.assert_array_equal(res[0].shape, [1, 3, 7, 7]) + if not in_pir_mode(): + paddle.static.save_inference_model( + self.save_path, [x], [out], exe + ) + # Test for Inference Predictor + infer_outs = self.infer_prog() + np.testing.assert_array_equal(res[0].shape, [1, 3, 7, 7]) def path_prefix(self): return 'unpool_var'