Repository: ivy-llc/ivy

Issue:
gather



Discussion:

Resolving patch:
diff --git a/ivy/functional/backends/paddle/general.py b/ivy/functional/backends/paddle/general.py
--- a/ivy/functional/backends/paddle/general.py
+++ b/ivy/functional/backends/paddle/general.py
@@ -133,23 +133,7 @@ def to_list(x: paddle.Tensor, /) -> list:
 
 
 @with_unsupported_device_and_dtypes(
-    {
-        "2.4.2 and below": {
-            "cpu": (
-                "uint16",
-                "bfloat16",
-                "int8",
-                "int16",
-                "int32",
-                "int64",
-                "float16",
-                "complex64",
-                "complex128",
-                "bool",
-            )
-        }
-    },
-    backend_version,
+    {"2.4.2 and below": {"cpu": ("uint16", "bfloat16")}}, backend_version
 )
 def gather(
     params: paddle.Tensor,
@@ -160,27 +144,50 @@ def gather(
     batch_dims: Optional[int] = 0,
     out: Optional[paddle.Tensor] = None,
 ) -> paddle.Tensor:
-    axis = axis % paddle.Tensor.ndimension(params)
-    batch_dims = batch_dims % paddle.Tensor.ndimension(params)
+    
+
+    def _gather(params1):
+        with ivy.ArrayMode(False):
+            if batch_dims == 0:
+                result = paddle.gather(params1, ivy.reshape(indices, shape=[-1]), axis=axis)
+            #inputs are unstacked batch_dims times because paddle.gather does not support batch_dims
+            else:
+                params1_list = ivy.unstack(params1, axis=0)
+                indices_list = ivy.unstack(indices, axis=0)
+                for b in range(1, batch_dims):
+                    params1_list = [p2 for p1 in params1_list for p2 in ivy.unstack(p1, axis=0)]
+                    indices_list = [i2 for i1 in indices_list for i2 in ivy.unstack(i1, axis=0)]
+                result = []
+                for p, i in zip(params1_list, indices_list):
+                    result.append(
+                        paddle.gather(p, ivy.reshape(i, shape=[-1]), axis=axis - batch_dims)
+                    )
+                result = ivy.concat(result, axis=0)
+            new_shape = (
+                params1.shape[:axis] + indices.shape[batch_dims:] + params1.shape[axis + 1 :]
+            )
+            return ivy.reshape(result, shape=new_shape)
+        
+    
+    axis = axis % params.ndim
+    batch_dims = batch_dims % params.ndim
     ivy.utils.assertions.check_gather_input_valid(params, indices, axis, batch_dims)
-    if batch_dims == 0:
-        result = paddle.gather(params, paddle.reshape(indices, shape=[-1]), axis=axis)
-    else:
-        params_list = [p for p in params]
-        indices_list = [i for i in indices]
-        for b in range(1, batch_dims):
-            params_list = [p1 for p in params_list for p1 in p]
-            indices_list = [i1 for i in indices_list for i1 in i]
-        result = []
-        for p, i in zip(params_list, indices_list):
-            result.append(
-                paddle.gather(p, paddle.reshape(i, shape=[-1]), axis=axis - batch_dims)
+    if params.dtype in [
+        paddle.int8,
+        paddle.int16,
+        paddle.float16,
+        paddle.complex64,
+        paddle.complex128,
+        paddle.bool,
+    ]:
+        if paddle.is_complex(params):
+            return paddle.complex(
+                _gather(params.real()),
+                _gather(params.imag())
             )
-        result = paddle.concat(result, axis=0)
-    new_shape = (
-        params.shape[:axis] + indices.shape[batch_dims:] + params.shape[axis + 1 :]
-    )
-    return paddle.reshape(result, shape=new_shape)
+        return _gather(params.cast("float32")).cast(params.dtype)
+    return _gather(params)
+    
 
 
 @with_unsupported_device_and_dtypes(


Test patch: