Repository: ivy-llc/ivy

Issue:
pixel_unshuffle



Discussion:

Resolving patch:
diff --git a/ivy/functional/frontends/paddle/nn/functional/vision.py b/ivy/functional/frontends/paddle/nn/functional/vision.py
--- a/ivy/functional/frontends/paddle/nn/functional/vision.py
+++ b/ivy/functional/frontends/paddle/nn/functional/vision.py
@@ -170,3 +170,45 @@ def pixel_shuffle(x, upscale_factor, data_format="NCHW"):
     return ivy.reshape(
         ivy.permute_dims(input_reshaped, (0, 1, 4, 2, 5, 3)), (b, oh, ow, oc)
     )
+
+
+@to_ivy_arrays_and_back
+def pixel_unshuffle(x, downscale_factor, data_format="NCHW"):
+    if len(ivy.shape(x)) != 4:
+        raise ValueError(
+            "Input x should be 4D tensor, but received x with the shape of {}".format(
+                ivy.shape(x)
+            )
+        )
+
+    if not isinstance(downscale_factor, int):
+        raise ValueError("Downscale factor must be int type")
+
+    if downscale_factor <= 0:
+        raise ValueError("Downscale factor must be positive")
+
+    if data_format not in ["NCHW", "NHWC"]:
+        raise ValueError(
+            "Attr(data_format) should be 'NCHW' or 'NHWC'."
+            "But recevie Attr(data_format): {} ".format(data_format)
+        )
+
+    if data_format == "NCHW":
+        b, c, h, w = ivy.shape(x)
+        oc = c * downscale_factor**2
+        oh = h // downscale_factor
+        ow = w // downscale_factor
+
+        x = ivy.reshape(x, (b, c, oh, downscale_factor, ow, downscale_factor))
+        x = ivy.permute_dims(x, (0, 1, 3, 5, 2, 4))
+        x = ivy.reshape(x, (b, oc, oh, ow))
+    else:
+        b, h, w, c = ivy.shape(x)
+        oc = c * downscale_factor**2
+        oh = h // downscale_factor
+        ow = w // downscale_factor
+
+        x = ivy.reshape(x, (b, downscale_factor, oh, downscale_factor, ow, c))
+        x = ivy.permute_dims(x, (0, 1, 3, 5, 2, 4))
+        x = ivy.reshape(x, (b, oh, ow, oc))
+    return x


Test patch:
diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_vision.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_vision.py
--- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_vision.py
+++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_vision.py
@@ -174,3 +174,47 @@ def test_paddle_pixel_shuffle(
         data_format=data_format,
         backend_to_test=backend_fw,
     )
+
+
+# pixel_unshuffle
+@handle_frontend_test(
+    fn_tree="paddle.nn.functional.pixel_unshuffle",
+    dtype_and_x=helpers.dtype_and_values(
+        available_dtypes=["float32", "float64"],
+        min_value=0,
+        min_num_dims=4,
+        max_num_dims=4,
+        min_dim_size=3,
+    ),
+    factor=helpers.ints(min_value=1),
+    data_format=st.sampled_from(["NCHW", "NHWC"]),
+)
+def test_paddle_pixel_unshuffle(
+    *,
+    dtype_and_x,
+    factor,
+    data_format,
+    on_device,
+    fn_tree,
+    frontend,
+    test_flags,
+    backend_fw,
+):
+    input_dtype, x = dtype_and_x
+    if data_format == "NCHW":
+        assume(ivy.shape(x[0])[2] % factor == 0)
+        assume(ivy.shape(x[0])[3] % factor == 0)
+    else:
+        assume(ivy.shape(x[0])[1] % factor == 0)
+        assume(ivy.shape(x[0])[2] % factor == 0)
+    helpers.test_frontend_function(
+        input_dtypes=input_dtype,
+        frontend=frontend,
+        test_flags=test_flags,
+        fn_tree=fn_tree,
+        on_device=on_device,
+        x=x[0],
+        downscale_factor=factor,
+        data_format=data_format,
+        backend_to_test=backend_fw,
+    )