Skip to content

Commit

Permalink
【PIR API adaptor No.167、168、243、244】 Migrate pooling ops into pir (#5…
Browse files Browse the repository at this point in the history
  • Loading branch information
DrRyanHuang authored Nov 29, 2023
1 parent 7c92cc5 commit e7fdaf9
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 68 deletions.
16 changes: 8 additions & 8 deletions python/paddle/nn/functional/pooling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 10 additions & 7 deletions test/legacy_test/test_pool1d_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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"
)
Expand All @@ -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(
Expand All @@ -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],
)
Expand Down Expand Up @@ -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"
)
Expand All @@ -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],
)
Expand Down
10 changes: 6 additions & 4 deletions test/legacy_test/test_pool2d_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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],
)
Expand Down Expand Up @@ -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"
)
Expand All @@ -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],
)
Expand Down
27 changes: 22 additions & 5 deletions test/legacy_test/test_pool2d_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand All @@ -441,13 +447,15 @@ 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(
{'X'},
'Out',
max_relative_error=0.07,
check_dygraph=(not self.use_mkldnn),
check_pir=True,
)

def init_data_format(self):
Expand Down Expand Up @@ -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,
)


Expand Down
21 changes: 13 additions & 8 deletions test/legacy_test/test_pool3d_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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"
)
Expand All @@ -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],
)
Expand Down Expand Up @@ -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"
)
Expand All @@ -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],
)
Expand Down Expand Up @@ -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():
Expand All @@ -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,
},
Expand All @@ -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 (
Expand All @@ -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,
},
Expand Down
Loading

0 comments on commit e7fdaf9

Please sign in to comment.