code: def pad(
      input: Union[ivy.Array, ivy.NativeArray],
      pad_width: Union[Iterable[Tuple[int]], int],
      /,
      *,
      mode: Union[
          Literal[
              "constant",
              "dilated",
              "edge",
              "linear_ramp",
              "maximum",
              "mean",
              "median",
              "minimum",
              "reflect",
              "symmetric",
              "wrap",
              "empty",
          ],
          Callable,
      ] = "constant",
      stat_length: Union[Iterable[Tuple[int]], int] = 1,
      constant_values: Union[Iterable[Tuple[Number]], Number] = 0,
      end_values: Union[Iterable[Tuple[Number]], Number] = 0,
      reflect_type: Literal["even", "odd"] = "even",
      **kwargs: Optional[Any],
  ) -> ivy.Array:
      """Pad an array.
  
      Parameters
      ----------
      input
          Input array to pad.
      pad_width
          Number of values padded to the edges of each axis.
               - ((before_1, after_1), … (before_N, after_N)) yields unique pad widths
                 for each axis.
               - ((before, after),) yields same before and after pad for each axis.
               - pad (integer) is shortcut for before = after = pad width for all axes.
      mode
          One of the following string values or a user-supplied function.
               - "constant": Pads with a constant value.
               - "edge": Pads with the input's edge values.
               - "linear_ramp": Pads with the linear ramp between end_value
                 and the input's edge value.
               - "maximum": Pads with the maximum value of all or part of the vector
                 along each axis.
               - "mean": Pads with the mean value of all or part of the vector along
                 each axis.
               - "median": Pads with the median value of all or part of the vector
                 along each axis.
               - "minimum": Pads with the minimum value of all or part of the vector
                 along each axis.
               - "reflect": Pads with the reflection mirrored on the first and last
                 values of the vector along each axis.
               - "symmetric": Pads with the reflection of the vector mirrored along
                 the edge of the input.
               - "wrap": Pads with the wrap of the vector along the axis.
                 The first values are used to pad the end and the end values are used
                 to pad the beginning.
               - "empty": Pads with undefined values.
               - <function>: Pads with a user-defined padding function. The padding
                 function should modify a rank 1 array following the signature
                 `padding_func(vector, iaxis_pad_width, iaxis, kwargs)`, where:
                      - `vector` is a rank 1 array already padded with zeros. Padded
                        values are `vector[:iaxis_pad_width[0]]` and
                        `vector[-iaxis_pad_width[1]:]`.
                      - `iaxis_pad_width` is a 2-tuple of ints, where
                        `iaxis_pad_width[0]` represents the number of values padded at
                        the beginning of `vector` and `iaxis_pad_width[1]` represents
                        the number of values padded at the end of `vector`.
                      - `iaxis` is the axis currently being calculated.
                      - `kwargs` is a dict of keyword arguments the function requires.
      stat_length
          Used in "maximum", "mean", "median", and "minimum". Number of values at edge
          of each axis used to calculate the statistic value.
               - ((before_1, after_1), … (before_N, after_N)) yields unique statistic
                 lengths for each axis.
               - ((before, after),) yields same before and after statistic lengths for
                 each axis.
               - stat_length (integer) is a shortcut for before = after = stat_length
                 length for all axes.
               - None uses the entire axis.
      constant_values
          Used in "constant". The values to set the padded values for each axis.
               - ((before_1, after_1), ... (before_N, after_N)) yields unique pad
                 constants for each axis.
               - ((before, after),) yields same before and after constants for each axis.
               - constant (integer) is a shortcut for before = after = constant for
                 all axes.
      end_values
          Used in "linear_ramp". The values used for the ending value of the linear_ramp
          and that will form the edge of the padded array.
               - ((before_1, after_1), ... (before_N, after_N)) yields unique end values
                 for each axis.
               - ((before, after),) yields same before and after end values for each axis
               - end (integer) is a shortcut for before = after = end for all axes.
      reflect_type
          Used in "reflect", and "symmetric". The "even" style is the default with an
          unaltered reflection around the edge value. For the "odd" style, the extended
          part of the array is created by subtracting the reflected values from two
          times the edge value.
  
      Returns
      -------
      ret
          Padded array of the same rank as the input but with shape increased according
          to pad_width.
  
  
      Both the description and the type hints above assume 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` input:
  
      >>> x = ivy.array([[1, 2, 3], [4, 5, 6]])
      >>> padding = ((1, 1), (2, 2))
      >>> y = ivy.pad(x, padding, mode="constant", constant_values=0)
      >>> print(y)
      ivy.array([[0, 0, 0, 0, 0, 0, 0],
                 [0, 0, 1, 2, 3, 0, 0],
                 [0, 0, 4, 5, 6, 0, 0],
                 [0, 0, 0, 0, 0, 0, 0]])
  
      >>> x = ivy.array([[1, 2, 3], [4, 5, 6]])
      >>> padding = ((1, 1), (2, 2))
      >>> y = ivy.pad(x, padding, mode="reflect")
      >>> print(y)
      ivy.array([[6, 5, 4, 5, 6, 5, 4],
                 [3, 2, 1, 2, 3, 2, 1],
                 [6, 5, 4, 5, 6, 5, 4],
                 [3, 2, 1, 2, 3, 2, 1]])
  
      >>> x = ivy.array([[1, 2, 3], [4, 5, 6]])
      >>> padding = ((1, 1), (2, 2))
      >>> y = ivy.pad(x, padding, mode="symmetric")
      >>> print(y)
      ivy.array([[2, 1, 1, 2, 3, 3, 2],
                 [2, 1, 1, 2, 3, 3, 2],
                 [5, 4, 4, 5, 6, 6, 5],
                 [5, 4, 4, 5, 6, 6, 5]])
  
      With :class:`ivy.NativeArray` input:
  
      >>> x = ivy.native_array([[1, 2, 3], [4, 5, 6]])
      >>> padding = ((1, 1), (2, 2))
      >>> y = ivy.pad(x, padding, mode="constant", constant_values=7)
      >>> print(y)
      ivy.array([[7, 7, 7, 7, 7, 7, 7],
                 [7, 7, 1, 2, 3, 7, 7],
                 [7, 7, 4, 5, 6, 7, 7],
                 [7, 7, 7, 7, 7, 7, 7]])
  
      With :class:`ivy.Container` input:
  
      >>> x = ivy.Container(a=ivy.array([0, 1, 2]), b=ivy.array([4, 5, 6]))
      >>> padding = (1, 1)
      >>> y = ivy.pad(x, padding, mode="constant")
      >>> print(y)
      {
          a: ivy.array([0, 0, 1, 2, 0]),
          b: ivy.array([0, 4, 5, 6, 0])
      }
      """
      _check_arguments(
          mode,
          pad_width,
          stat_length,
          constant_values,
          end_values,
          reflect_type,
      )
      ndim = input.ndim
      if mode == "dilated":
          pad_width = _to_pairs(pad_width, ndim, m=3)
          if (
              not ivy.is_array(constant_values)
              or constant_values.dtype != input.dtype
          ):
              constant_values = ivy.asarray(
                  constant_values, dtype=input.dtype
              )
          return _interior_pad(input, constant_values, pad_width)
      pad_width = _to_pairs(pad_width, len(input.shape))
      if callable(mode):
          func = mode
          padded = _init_pad(input, pad_width, fill_value=0)
          for axis in range(ndim):
              padded = ivy.moveaxis(padded, axis, -1)
              inds = ivy.ndindex(padded.shape[:-1])
              for ind in inds:
                  padded[ind] = func(
                      padded[ind], pad_width[axis], axis, kwargs
                  )
          return padded
      padded = _init_pad(input, pad_width)
      stat_functions = {
          "maximum": ivy.max,
          "minimum": ivy.min,
          "mean": ivy.mean,
          "median": ivy.median,
      }
      if mode == "constant":
          constant_values = _to_pairs(constant_values, ndim)
          for axis, (width_pair, value_pair) in enumerate(
              zip(pad_width, constant_values)
          ):
              padded = _set_pad_area(
                  padded, axis, width_pair, value_pair
              )
      elif mode == "empty":
          pass
      elif mode == "edge":
          for axis, width_pair in enumerate(pad_width):
              edge_pair = _get_edges(padded, axis, width_pair)
              padded = _set_pad_area(
                  padded, axis, width_pair, edge_pair
              )
      elif mode == "linear_ramp":
          end_values = _to_pairs(end_values, ndim)
          for axis, (width_pair, value_pair) in enumerate(
              zip(pad_width, end_values)
          ):
              ramp_pair = _get_linear_ramps(
                  padded, axis, width_pair, value_pair
              )
              padded = _set_pad_area(
                  padded, axis, width_pair, ramp_pair
              )
      elif mode in stat_functions:
          func = stat_functions[mode]
          stat_length = _to_pairs(stat_length, ndim)
          for axis, (width_pair, length_pair) in enumerate(
              zip(pad_width, stat_length)
          ):
              stat_pair = _get_stats(
                  padded, axis, width_pair, length_pair, func
              )
              padded = _set_pad_area(
                  padded, axis, width_pair, stat_pair
              )
      elif mode in {"reflect", "symmetric"}:
          include_edge = True if mode == "symmetric" else False
          for axis, (left_index, right_index) in enumerate(pad_width):
              if input.shape[axis] == 1 and (
                  left_index > 0 or right_index > 0
              ):
                  edge_pair = _get_edges(
                      padded, axis, (left_index, right_index)
                  )
                  padded = _set_pad_area(
                      padded, axis, (left_index, right_index), edge_pair
                  )
                  continue
              while left_index > 0 or right_index > 0:
                  left_index, right_index, padded = _set_reflect_both(
                      padded,
                      axis,
                      (left_index, right_index),
                      reflect_type,
                      include_edge,
                  )
      elif mode == "wrap":
          for axis, (left_index, right_index) in enumerate(pad_width):
              padded = _set_wrap_both(
                  padded, axis, (left_index, right_index)
              )
      return padded
  
imports: from typing import Optional, Union, Tuple, Iterable, Sequence, Callable, Any, Literal, List
  from numbers import Number
  from functools import partial
  import math
  import ivy
  from ivy.func_wrapper import handle_out_argument, handle_partial_mixed_function, to_native_arrays_and_back, inputs_to_native_shapes, handle_nestable, handle_array_like_without_promotion, handle_view, inputs_to_ivy_arrays, handle_array_function, handle_device, handle_backend_invalid
  from ivy.functional.ivy.general import _numel
  from ivy.utils.backend import current_backend
  from ivy.utils.exceptions import handle_exceptions
label_desc: Use image node_id 26 for calling a global function with example usage: pad() and returns: padded, _interior_pad, padded