Repository: ivy-llc/ivy

Issue:
diagonal
Paddle Diagonal


Discussion:

Resolving patch:
diff --git a/ivy/functional/frontends/paddle/linalg.py b/ivy/functional/frontends/paddle/linalg.py
--- a/ivy/functional/frontends/paddle/linalg.py
+++ b/ivy/functional/frontends/paddle/linalg.py
@@ -59,6 +59,26 @@ def cross(x, y, /, *, axis=9, name=None):
     return ivy.cross(x, y, axis=axis)
 
 
+# diagonal
+@with_supported_dtypes(
+    {
+        "2.5.1 and below": (
+            "int32",
+            "int64",
+            "float64",
+            "complex128",
+            "float32",
+            "complex64",
+            "bool",
+        )
+    },
+    "paddle",
+)
+@to_ivy_arrays_and_back
+def diagonal(x, offset=0, axis1=0, axis2=1, name=None):
+    return ivy.diagonal(x, offset=offset, axis1=axis1, axis2=axis2)
+
+
 @with_supported_dtypes({"2.4.1 and above": ("float64", "float32")}, "paddle")
 @to_ivy_arrays_and_back
 def dist(x, y, p=2):
diff --git a/ivy/functional/frontends/paddle/tensor/tensor.py b/ivy/functional/frontends/paddle/tensor/tensor.py
--- a/ivy/functional/frontends/paddle/tensor/tensor.py
+++ b/ivy/functional/frontends/paddle/tensor/tensor.py
@@ -140,6 +140,23 @@ def asin(self, name=None):
     def cosh(self, name=None):
         return paddle_frontend.cosh(self)
 
+    @with_supported_dtypes(
+        {
+            "2.5.1 and below": (
+                "int32",
+                "int64",
+                "float64",
+                "complex128",
+                "float32",
+                "complex64",
+                "bool",
+            )
+        },
+        "paddle",
+    )
+    def diagonal(self, offset, axis1=0, axis2=1, name=None):
+        return paddle_frontend.diagonal(self, offset=offset, axis1=axis1, axis2=axis2)
+
     @with_supported_dtypes({"2.5.1 and below": ("float32", "float64")}, "paddle")
     def log(self, name=None):
         return paddle_frontend.log(self)


Test patch:
diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_linalg.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_linalg.py
--- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_linalg.py
+++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_linalg.py
@@ -171,6 +171,17 @@ def _transpose_helper(draw):
     return dtype, x, perm
 
 
+@st.composite
+def dims_and_offset(draw, shape):
+    shape_actual = draw(shape)
+    dim1 = draw(helpers.get_axis(shape=shape, force_int=True))
+    dim2 = draw(helpers.get_axis(shape=shape, force_int=True))
+    offset = draw(
+        st.integers(min_value=-shape_actual[dim1], max_value=shape_actual[dim1])
+    )
+    return dim1, dim2, offset
+
+
 # Helpers #
 # ------ #
 
@@ -688,6 +699,49 @@ def test_paddle_eigvalsh(
     )
 
 
+# diagonal
+@handle_frontend_test(
+    fn_tree="paddle.diagonal",
+    dtype_and_values=helpers.dtype_and_values(
+        available_dtypes=helpers.get_dtypes("valid"),
+        shape=st.shared(helpers.get_shape(min_num_dims=2), key="shape"),
+    ),
+    axis_and_offset=dims_and_offset(
+        shape=st.shared(helpers.get_shape(min_num_dims=2), key="shape")
+    ),
+)
+def test_paddle_linalg_diagonal(
+    dtype_and_values,
+    axis_and_offset,
+    on_device,
+    fn_tree,
+    frontend,
+    test_flags,
+    backend_fw,
+):
+    input_dtype, value = dtype_and_values
+    axis1, axis2, offset = axis_and_offset
+    input = value[0]
+    num_dims = len(np.shape(input))
+    assume(axis1 != axis2)
+    if axis1 < 0:
+        assume(axis1 + num_dims != axis2)
+    if axis2 < 0:
+        assume(axis1 != axis2 + num_dims)
+    helpers.test_frontend_function(
+        input_dtypes=input_dtype,
+        on_device=on_device,
+        frontend=frontend,
+        backend_to_test=backend_fw,
+        test_flags=test_flags,
+        fn_tree=fn_tree,
+        x=input,
+        offset=offset,
+        axis1=axis1,
+        axis2=axis2,
+    )
+
+
 @handle_frontend_test(
     fn_tree="paddle.lu_unpack",
     dtype_x=_get_dtype_and_square_matrix(real_and_complex_only=True),
diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_tensor.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_tensor.py
--- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_tensor.py
+++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_tensor.py
@@ -222,6 +222,18 @@ def _reshape_helper(draw):
     return dtypes, x, reshape_shape
 
 
+# diagonal
+@st.composite
+def dims_and_offset(draw, shape):
+    shape_actual = draw(shape)
+    dim1 = draw(helpers.get_axis(shape=shape, force_int=True))
+    dim2 = draw(helpers.get_axis(shape=shape, force_int=True))
+    offset = draw(
+        st.integers(min_value=-shape_actual[dim1], max_value=shape_actual[dim1])
+    )
+    return dim1, dim2, offset
+
+
 # --- Main --- #
 # ------------ #
 
@@ -1763,6 +1775,56 @@ def test_paddle_tensor_device(
     )
 
 
+# diagonal
+@handle_frontend_method(
+    class_tree=CLASS_TREE,
+    init_tree="paddle.to_tensor",
+    method_name="diagonal",
+    dtype_and_values=helpers.dtype_and_values(
+        available_dtypes=helpers.get_dtypes("valid"),
+        shape=st.shared(helpers.get_shape(min_num_dims=2), key="shape"),
+    ),
+    dims_and_offset=dims_and_offset(
+        shape=st.shared(helpers.get_shape(min_num_dims=2), key="shape")
+    ),
+)
+def test_paddle_tensor_diagonal(
+    dtype_and_values,
+    dims_and_offset,
+    frontend,
+    frontend_method_data,
+    backend_fw,
+    init_flags,
+    method_flags,
+    on_device,
+):
+    input_dtype, value = dtype_and_values
+    dim1, dim2, offset = dims_and_offset
+    input = value[0]
+    num_dims = len(np.shape(input))
+    assume(dim1 != dim2)
+    if dim1 < 0:
+        assume(dim1 + num_dims != dim2)
+    if dim2 < 0:
+        assume(dim1 != dim2 + num_dims)
+    helpers.test_frontend_method(
+        init_input_dtypes=[input_dtype[0]],
+        init_all_as_kwargs_np={"x": input},
+        method_input_dtypes=[input_dtype[0]],
+        method_all_as_kwargs_np={
+            "offset": offset,
+            "axis1": dim1,
+            "axis2": dim2,
+        },
+        frontend=frontend,
+        frontend_method_data=frontend_method_data,
+        backend_to_test=backend_fw,
+        init_flags=init_flags,
+        method_flags=method_flags,
+        on_device=on_device,
+    )
+
+
 # digamma
 @handle_frontend_method(
     class_tree=CLASS_TREE,