Repository: ivy-llc/ivy

Issue:
pad



Discussion:

Resolving patch:
diff --git a/ivy/functional/frontends/paddle/vision/transforms.py b/ivy/functional/frontends/paddle/vision/transforms.py
--- a/ivy/functional/frontends/paddle/vision/transforms.py
+++ b/ivy/functional/frontends/paddle/vision/transforms.py
@@ -170,6 +170,36 @@ def normalize(img, mean, std, data_format="CHW", to_rgb=False):
         raise ValueError("Unsupported input format")
 
 
+@with_supported_dtypes(
+    {"2.5.1 and below": ("float32", "float64", "int32", "int64")}, "paddle"
+)
+@to_ivy_arrays_and_back
+def pad(img, padding, fill=0, padding_mode="constant"):
+    dim_size = img.ndim
+    if not hasattr(padding, "__len__"):
+        if dim_size == 2:
+            trans_padding = ((padding, padding), (padding, padding))
+        elif dim_size == 3:
+            trans_padding = ((0, 0), (padding, padding), (padding, padding))
+    elif len(padding) == 2:
+        if dim_size == 2:
+            trans_padding = ((padding[1], padding[1]), (padding[0], padding[0]))
+        elif dim_size == 3:
+            trans_padding = ((0, 0), (padding[1], padding[1]), (padding[0], padding[0]))
+    elif len(padding) == 4:
+        if dim_size == 2:
+            trans_padding = ((padding[1], padding[3]), (padding[0], padding[2]))
+        elif dim_size == 3:
+            trans_padding = ((0, 0), (padding[1], padding[3]), (padding[0], padding[2]))
+    else:
+        raise "padding can only be 1D with size 1, 2, 4 only"
+
+    if padding_mode in ["constant", "edge", "reflect", "symmetric"]:
+        return ivy.pad(img, trans_padding, mode=padding_mode, constant_values=fill)
+    else:
+        raise "Unsupported padding_mode"
+
+
 @with_supported_dtypes(
     {"2.5.1 and below": ("float32", "float64", "int32", "int64")}, "paddle"
 )


Test patch:
diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_vision/test_transforms.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_vision/test_transforms.py
--- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_vision/test_transforms.py
+++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_vision/test_transforms.py
@@ -128,6 +128,58 @@ def test_paddle_hflip(
     )
 
 
+# pad
+@handle_frontend_test(
+    fn_tree="paddle.vision.transforms.pad",
+    dtype_and_x=helpers.dtype_and_values(
+        available_dtypes=helpers.get_dtypes("valid"),
+        min_num_dims=3,
+        max_num_dims=3,
+        min_dim_size=3,
+        max_dim_size=5,
+    ),
+    padding=st.one_of(
+        st.integers(min_value=0, max_value=2),
+        st.tuples(
+            st.integers(min_value=0, max_value=2), st.integers(min_value=0, max_value=2)
+        ),
+        st.tuples(
+            st.integers(min_value=0, max_value=2),
+            st.integers(min_value=0, max_value=2),
+            st.integers(min_value=0, max_value=2),
+            st.integers(min_value=0, max_value=2),
+        ),
+    ),
+    fill=st.integers(min_value=-5, max_value=5),
+    padding_mode=st.sampled_from(["constant", "edge", "reflect"]),
+)
+def test_paddle_pad(
+    *,
+    dtype_and_x,
+    padding,
+    fill,
+    padding_mode,
+    on_device,
+    fn_tree,
+    frontend,
+    test_flags,
+    backend_fw,
+):
+    input_dtype, x = dtype_and_x
+    helpers.test_frontend_function(
+        input_dtypes=input_dtype,
+        frontend=frontend,
+        test_flags=test_flags,
+        fn_tree=fn_tree,
+        on_device=on_device,
+        img=x[0],
+        padding=padding,
+        fill=fill,
+        padding_mode=padding_mode,
+        backend_to_test=backend_fw,
+    )
+
+
 # to_tensor
 @handle_frontend_test(
     fn_tree="paddle.to_tensor",