code: def trace(
      x: Union[ivy.Array, ivy.NativeArray],
      /,
      *,
      offset: int = 0,
      axis1: int = 0,
      axis2: int = 1,
      out: Optional[ivy.Array] = None,
  ) -> ivy.Array:
      """Return the sum along the specified diagonals of a matrix (or a stack of
      matrices) ``x``.
  
      **Special cases**
  
      Let ``N`` equal the number of elements over which to compute the sum.
  
      - If ``N`` is ``0``, the sum is ``0`` (i.e., the empty sum).
  
      For both real-valued and complex floating-point operands,
      special cases must be handled as if the operation is implemented
      by successive application of :func:`ivy.add`:
  
      Parameters
      ----------
      x
          input array having shape ``(..., M, N)`` and whose innermost two dimensions form
          ``MxN`` matrices. Should have a numeric data type.
      offset
          offset specifying the off-diagonal relative to the main diagonal.
          -   ``offset = 0``: the main diagonal.
          -   ``offset > 0``: off-diagonal above the main diagonal.
          -   ``offset < 0``: off-diagonal below the main diagonal.
  
          Default: ``0``.
      axis1
          axis to be used as the first axis of the 2-D sub-arrays from which the
          diagonals should be taken.
          Defaults to ``0.`` .
      axis2
          axis to be used as the second axis of the 2-D sub-arrays from which the
          diagonals should be taken.
          Defaults to ``1.`` .
      out
          optional output array, for writing the result to. It must have a shape that the
          inputs broadcast to.
  
      Returns
      -------
      ret
           an array containing the traces and whose shape is determined by removing the
           last two dimensions and storing the traces in the last array dimension. For
           example, if ``x`` has rank ``k`` and shape ``(I, J, K, ..., L, M, N)``, then an
           output array has rank ``k-2`` and shape ``(I, J, K, ..., L)`` where
  
           ::
  
             out[i, j, k, ..., l] = trace(a[i, j, k, ..., l, :, :])
  
           The returned array must have the same data type as ``x``.
  
      Examples
      --------
      With :class:`ivy.Array` inputs:
  
      >>> x = ivy.array([[2., 0., 3.],
      ...                [3., 5., 6.]])
      >>> y = ivy.trace(x, offset=0)
      >>> print(y)
      ivy.array(7.)
  
      >>> x = ivy.array([[[1., 2.],
      ...                 [3., 4.]],
      ...                [[5., 6.],
      ...                 [7., 8.]]])
      >>> y = ivy.trace(x, offset=1)
      >>> print(y)
      ivy.array([3., 4.])
  
      >>> x = ivy.array([[1., 2., 3.],
      ...                [4., 5., 6.],
      ...                [7., 8., 9.]])
      >>> y = ivy.zeros(1)
      >>> ivy.trace(x, offset=1,out=y)
      >>> print(y)
      ivy.array(8.)
  
      With :class:`ivy.NativeArray` inputs:
  
      >>> x = ivy.native_array([[2., 0., 3.],[3., 5., 6.]])
      >>> y = ivy.trace(x, offset=0)
      >>> print(y)
      ivy.array(7.)
  
      >>> x = ivy.native_array([[0, 1, 2],
      ...                       [3, 4, 5],
      ...                       [6, 7, 8]])
      >>> y = ivy.trace(x, offset=1)
      >>> print(y)
      ivy.array(6)
  
      With :class:`ivy.Container` inputs:
  
      >>> x = ivy.Container(
      ...        a = ivy.array([[7, 1, 2],
      ...                       [1, 3, 5],
      ...                       [0, 7, 4]]),
      ...        b = ivy.array([[4, 3, 2],
      ...                       [1, 9, 5],
      ...                       [7, 0, 6]])
      ...    )
      >>> y = ivy.trace(x, offset=0)
      >>> print(y)
      {
          a: ivy.array(14),
          b: ivy.array(19)
      }
  
      >>> x = ivy.Container(
      ...        a = ivy.array([[7, 1, 2],
      ...                       [1, 3, 5],
      ...                       [0, 7, 4]]),
      ...        b = ivy.array([[4, 3, 2],
      ...                       [1, 9, 5],
      ...                       [7, 0, 6]])
      ...    )
      >>> y = ivy.trace(x, offset=1)
      >>> print(y)
      {
          a: ivy.array(6),
          b: ivy.array(8)
      }
  
      With multiple ivy.Container inputs:
  
      >>> x = ivy.Container(
      ...        a = ivy.array([[7, 1, 3],
      ...                       [8, 6, 5],
      ...                       [9, 7, 2]]),
      ...        b = ivy.array([[4, 3, 2],
      ...                       [1, 9, 5],
      ...                       [7, 0, 6]])
      ...    )
      >>> offset = ivy.Container(a=1, b=0)
      >>> y = ivy.trace(x, offset)
      >>> print(y)
      {
          a: ivy.array(6),
          b: ivy.array(19)
      }
  
      With Array instance method example:
  
      >>> x = ivy.array([[2., 0., 11.],
      ...                [3., 5., 12.],
      ...                [1., 6., 13.],
      ...                [8., 9., 14.]])
      >>> y = x.trace(offset=1)
      >>> print(y)
      ivy.array(12.)
  
      With Container instance method example:
  
      >>> x = ivy.Container(
      ...        a=ivy.array([[2., 0., 11.],
      ...                     [3., 5., 12.]]),
      ...        b=ivy.array([[1., 6., 13.],
      ...                     [8., 9., 14.]])
      ...    )
      >>> y = x.trace(offset=0)
      >>> print(y)
      {
          a: ivy.array(7.),
          b: ivy.array(10.)
      }
      """
      return current_backend(x).trace(
          x, offset=offset, axis1=axis1, axis2=axis2, 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 23 for calling a global function with example usage: trace() and returns: current_backend