Repository: ivy-llc/ivy

Issue:
avg_pool2d
#15055 


Discussion:

Resolving patch:
diff --git a/ivy/functional/frontends/paddle/nn/functional/pooling.py b/ivy/functional/frontends/paddle/nn/functional/pooling.py
--- a/ivy/functional/frontends/paddle/nn/functional/pooling.py
+++ b/ivy/functional/frontends/paddle/nn/functional/pooling.py
@@ -1,10 +1,55 @@
 # local
 import ivy
+from ivy.func_wrapper import with_supported_dtypes
+from ivy.functional.frontends.paddle.func_wrapper import to_ivy_arrays_and_back
+from ivy.functional.frontends.torch.nn.functional.pooling_functions import (
+    _broadcast_pooling_helper,
+)
 from ivy.func_wrapper import with_unsupported_dtypes
 
-from ivy.functional.frontends.paddle.func_wrapper import (
-    to_ivy_arrays_and_back,
-)
+
+@to_ivy_arrays_and_back
+@with_supported_dtypes({"2.5.0 and below": ("float32", "float64")}, "paddle")
+def avg_pool2d(
+    x,
+    kernel_size,
+    stride=None,
+    padding=0,
+    ceil_mode=False,
+    exclusive=True,
+    divisor_override=None,
+    data_format="NCHW",
+    name=None,
+):
+    kernel_size = _broadcast_pooling_helper(kernel_size, "2d", name="kernel_size")
+    stride = _broadcast_pooling_helper(stride, "2d", name="stride")
+    padding = _broadcast_pooling_helper(padding, "2d", name="padding")
+    kernel_pads = list(zip(kernel_size, padding))
+
+    # Padding should be less than or equal to half of kernel size
+    if not all([pad <= kernel / 2 for kernel, pad in kernel_pads]):
+        raise ValueError(
+            "pad should be smaller than or equal to half of kernel size, "
+            f"but got padding={padding}, kernel_size={kernel_size}. "
+        )
+
+    # Figure out padding string
+    if all([pad == ivy.ceil((kernel - 1) / 2) for kernel, pad in kernel_pads]):
+        padding = "SAME"
+    else:
+        padding = "VALID"
+
+    count_include_pad = False if exclusive else True
+    return ivy.avg_pool2d(
+        x,
+        kernel_size,
+        stride,
+        padding,
+        data_format=data_format,
+        count_include_pad=count_include_pad,
+        ceil_mode=ceil_mode,
+        divisor_override=divisor_override,
+    )
 
 
 @to_ivy_arrays_and_back


Test patch:
diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_pooling.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_pooling.py
--- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_pooling.py
+++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_pooling.py
@@ -4,6 +4,69 @@
 # local
 import ivy_tests.test_ivy.helpers as helpers
 from ivy_tests.test_ivy.helpers import handle_frontend_test
+from ivy_tests.test_ivy.test_frontends.test_torch.test_nn.test_functional import (
+    test_pooling_functions,
+)
+
+
+# avg_pool2d
+@handle_frontend_test(
+    fn_tree="paddle.nn.functional.pooling.avg_pool2d",
+    dtype_x_k_s=helpers.arrays_for_pooling(
+        min_dims=4,
+        max_dims=4,
+        min_side=1,
+        max_side=4,
+    ),
+    ceil_mode=st.booleans(),
+    exclusive=st.booleans(),
+    data_format=st.sampled_from(["NCHW", "NHWC"]),
+    divisor_override=st.one_of(st.none(), st.integers(min_value=1, max_value=4)),
+)
+def test_paddle_avg_pool2d(
+    dtype_x_k_s,
+    exclusive,
+    ceil_mode,
+    data_format,
+    divisor_override,
+    *,
+    test_flags,
+    frontend,
+    fn_tree,
+    on_device,
+):
+    input_dtype, x, kernel, stride, padding = dtype_x_k_s
+
+    if data_format == "NCHW":
+        x[0] = x[0].reshape(
+            (x[0].shape[0], x[0].shape[3], x[0].shape[1], x[0].shape[2])
+        )
+
+    if len(stride) == 1:
+        stride = (stride[0], stride[0])
+
+    if padding == "SAME":
+        padding = test_pooling_functions.calculate_same_padding(
+            kernel, stride, x[0].shape[2:]
+        )
+    else:
+        padding = (0, 0)
+
+    helpers.test_frontend_function(
+        input_dtypes=input_dtype,
+        test_flags=test_flags,
+        frontend=frontend,
+        fn_tree=fn_tree,
+        on_device=on_device,
+        x=x[0],
+        kernel_size=kernel,
+        stride=stride,
+        padding=padding,
+        ceil_mode=ceil_mode,
+        exclusive=exclusive,
+        divisor_override=divisor_override,
+        data_format=data_format,
+    )
 
 
 # avg_pool1d