code: def diag(
      x: Union[ivy.Array, ivy.NativeArray],
      /,
      *,
      k: int = 0,
      out: Optional[ivy.Array] = None,
  ) -> ivy.Array:
      """Return the specified diagonals of the input array, or an array with the
      input array's elements as diagonals.
  
      Parameters
      ----------
      x
          An array with rank >= 1.
      k
          An integer that controls which diagonal to consider.
          Positive value means superdiagonal,
          0 refers to the main diagonal,
          and negative value means subdiagonal.
      out
          optional output array, for writing the result to. It must have a shape that the
          inputs broadcast to.
  
      Returns
      -------
      ret
          If x is a 1-D array, the function returns a 2-D square array with the elements
          of input as diagonals.
          If x is a 2-D array, the function returns a 1-D array with the diagonal elements
          of x.
  
  
      This function conforms to the `Array API Standard
      <https://data-apis.org/array-api/latest/>`_. This docstring is an extension of the
      `docstring <https://data-apis.org/array-api/latest/
      extensions/generated/array_api.linalg.diagonal.html>`_
      in the standard.
  
      Both the description and the type hints above assumes an array input for simplicity,
      but this function is *nestable*, and therefore also accepts :class:`ivy.Container`
      instances in place of any of the arguments.
  
      Examples
      --------
      With :class:`ivy.Array` inputs:
  
      >>> x = ivy.array([[0, 1, 2],
      >>>                [3, 4, 5],
      >>>                [6, 7, 8]])
      >>> ivy.diag(x)
      ivy.array([0, 4, 8])
  
      >>> x = ivy.array([[0, 1, 2],
      >>>                [3, 4, 5],
      >>>                [6, 7, 8]])
      >>> ivy.diag(x, k=1)
      ivy.array([1, 5])
  
      >>> x = ivy.array([[0, 1, 2],
      >>>                [3, 4, 5],
      >>>                [6, 7, 8]])
      >>> ivy.diag(x, k=-1)
      ivy.array([3, 7])
  
      >>> x = ivy.array([[0, 1, 2],
      >>>                [3, 4, 5],
      >>>                [6, 7, 8]])
      >>> ivy.diag(ivy.diag(x))
      ivy.array([[0, 0, 0],
                 [0, 4, 0],
                 [0, 0, 8]])
      """
      return current_backend(x).diag(x, k=k, out=out)
  
imports: from typing import Union, Optional, Tuple, Literal, List, Sequence
  import ivy
  from ivy.utils.backend import current_backend
  from ivy.func_wrapper import handle_array_function, to_native_arrays_and_back, handle_out_argument, handle_nestable, handle_array_like_without_promotion, handle_device, handle_backend_invalid
  from ivy.utils.exceptions import handle_exceptions
label_desc: Use image node_id 26 for calling a global function with example usage: diag() and returns: current_backend