Repository: ivy-llc/ivy

Issue:
max_pool2d



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
@@ -100,6 +100,41 @@ def avg_pool2d(
     )
 
 
+@to_ivy_arrays_and_back
+@with_supported_dtypes({"2.5.1 and below": ("float32", "float64")}, "paddle")
+def max_pool2d(
+    x,
+    kernel_size,
+    stride=None,
+    padding=0,
+    return_mask=False,
+    ceil_mode=False,
+    data_format="NCHW",
+    name=None,
+):
+    if stride is None:
+        stride = kernel_size
+    kernel_size = _broadcast_pooling_helper(kernel_size, "2d", name="kernel_size")
+    padding = _broadcast_pooling_helper(padding, "2d", name="padding")
+
+    if data_format not in ["NCHW", "NHWC"]:
+        raise ValueError(
+            "Attr(data_format) should be 'NCHW' or 'NHWC'. Received "
+            "Attr(data_format): %s."
+            % str(data_format)
+        )
+
+    if data_format == "NHWC" and return_mask:
+        raise ValueError(
+            "When setting return_mask to true, data_format must be set to NCHW in"
+            " API:max_pool2d"
+        )
+
+    return ivy.max_pool2d(
+        x, kernel_size, stride, padding, data_format=data_format, ceil_mode=ceil_mode
+    )
+
+
 @to_ivy_arrays_and_back
 @with_supported_dtypes({"2.5.2 and below": ("float32", "float64")}, "paddle")
 def max_unpool1d(


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
@@ -276,6 +276,60 @@ def test_paddle_avg_pool2d(
     )
 
 
+# max_pool2d
+@handle_frontend_test(
+    fn_tree="paddle.nn.functional.pooling.max_pool2d",
+    dtype_x_k_s=helpers.arrays_for_pooling(
+        min_dims=4, max_dims=4, min_side=2, max_side=4
+    ),
+    ceil_mode=st.sampled_from([True]),
+    data_format=st.sampled_from(["NCHW", "NHWC"]),
+)
+def test_paddle_max_pool2d(
+    dtype_x_k_s,
+    ceil_mode,
+    data_format,
+    *,
+    test_flags,
+    backend_fw,
+    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)
+
+    if padding == "VALID" and ceil_mode:
+        ceil_mode = False
+
+    helpers.test_frontend_function(
+        input_dtypes=input_dtype,
+        test_flags=test_flags,
+        backend_to_test=backend_fw,
+        frontend=frontend,
+        fn_tree=fn_tree,
+        on_device=on_device,
+        x=x[0],
+        kernel_size=kernel,
+        stride=stride,
+        padding=padding,
+        ceil_mode=ceil_mode,
+        data_format=data_format,
+    )
+
+
 # max_unpool1d
 @handle_frontend_test(
     fn_tree="paddle.nn.functional.max_unpool1d",