Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug fix] Fix fp16 dtype checking for AvgPool1D op #50929

Merged
merged 2 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions python/paddle/fluid/tests/unittests/test_pool1d_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import paddle.fluid as fluid
import paddle.fluid.core as core
import paddle.nn.functional as F
import paddle.static as static


def adaptive_start_index(index, input_size, output_size):
Expand Down Expand Up @@ -139,6 +140,32 @@ def check_avg_static_results(self, place):
)
np.testing.assert_allclose(fetches[0], result_np, rtol=1e-05)

def check_avg_static_results_fp16(self, place):
with static.program_guard(static.Program()):
if core.is_compiled_with_cuda():
input = static.data(
name="input", shape=[2, 3, 32], dtype="float16"
)
result = F.avg_pool1d(input, kernel_size=2, stride=2, padding=0)

input_np = np.random.random([2, 3, 32]).astype("float16")
result_np = avg_pool1D_forward_naive(
input_np,
ksize=[2],
strides=[2],
paddings=[0],
ceil_mode=False,
)

place = paddle.CUDAPlace(0)
exe = paddle.static.Executor(place)
fetches = exe.run(
static.default_main_program(),
feed={"input": input_np},
fetch_list=[result],
)
np.testing.assert_allclose(fetches[0], result_np, rtol=1e-03)

def check_avg_dygraph_results(self, place):
with fluid.dygraph.guard(place):
input_np = np.random.random([2, 3, 32]).astype("float32")
Expand Down Expand Up @@ -272,6 +299,7 @@ def test_pool1d(self):
self.check_max_dygraph_padding_same(place)
self.check_avg_dygraph_padding_same(place)
self.check_max_dygraph_return_index_results(place)
self.check_avg_static_results_fp16(place)


class TestPool1DError_API(unittest.TestCase):
Expand Down
6 changes: 4 additions & 2 deletions python/paddle/nn/functional/pooling.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def avg_pool1d(
Args:
x (Tensor): The input tensor of pooling operator which is a 3-D tensor with
shape [N, C, L]. where `N` is batch size, `C` is the number of channels,
`L` is the length of the feature. The data type is float32 or float64.
`L` is the length of the feature. The data type is float16, float32 or float64.
kernel_size (int|list|tuple): The pool kernel size. If pool kernel size is a tuple or list,
it must contain an integer.
stride (int|list|tuple): The pool stride size. If pool stride size is a tuple or list,
Expand Down Expand Up @@ -223,7 +223,9 @@ def avg_pool1d(
"""NCL to NCHW"""
data_format = "NCHW"
if not in_dynamic_mode():
check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'avg_pool1d')
check_variable_and_dtype(
x, 'x', ['float16', 'float32', 'float64'], 'avg_pool1d'
)
_check_input(x, 3)
x = unsqueeze(x, [2])
kernel_size = utils.convert_to_list(kernel_size, 1, 'kernel_size')
Expand Down