code: def pad_nd(
      img: NdarrayTensor,
      to_pad: list[tuple[int, int]],
      mode: str = PytorchPadMode.CONSTANT,
      **kwargs,
  ) -> NdarrayTensor:
      """
      Pad `img` for a given an amount of padding in each dimension.
  
      `torch.nn.functional.pad` is used unless the mode or kwargs are not available in torch,
      in which case `np.pad` will be used.
  
      Args:
          img: data to be transformed, assuming `img` is channel-first and padding doesn't apply to the channel dim.
          to_pad: the amount to be padded in each dimension [(low_H, high_H), (low_W, high_W), ...].
              default to `self.to_pad`.
          mode: available modes: (Numpy) {``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``,
              ``"mean"``, ``"median"``, ``"minimum"``, ``"reflect"``, ``"symmetric"``, ``"wrap"``, ``"empty"``}
              (PyTorch) {``"constant"``, ``"reflect"``, ``"replicate"``, ``"circular"``}.
              One of the listed string values or a user supplied function. Defaults to ``"constant"``.
              See also: https://numpy.org/doc/stable/reference/generated/numpy.pad.html
              https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html
          kwargs: other arguments for the `np.pad` or `torch.pad` function.
              note that `np.pad` treats channel dimension as the first dimension.
      """
      if mode in {
          "linear_ramp",
          "maximum",
          "mean",
          "median",
          "minimum",
          "symmetric",
          "empty",
      }:
          return _np_pad(img, pad_width=to_pad, mode=mode, **kwargs)
      try:
          _pad = _np_pad
          if mode in {
              "constant",
              "reflect",
              "edge",
              "replicate",
              "wrap",
              "circular",
          } and img.dtype not in {
              torch.int16,
              torch.int64,
              torch.bool,
              torch.uint8,
          }:
              _pad = _pt_pad
          return _pad(img, pad_width=to_pad, mode=mode, **kwargs)
      except (ValueError, TypeError, RuntimeError) as err:
          if isinstance(err, NotImplementedError) or any(
              k in str(err)
              for k in (
                  "supported",
                  "unexpected keyword",
                  "implemented",
                  "value",
              )
          ):
              return _np_pad(img, pad_width=to_pad, mode=mode, **kwargs)
          raise ValueError(
              f"{img.shape} {to_pad} {mode} {kwargs} {img.dtype} {img.device if isinstance(img, torch.Tensor) else None}"
          ) from err
  
file_path: MONAI/monai/transforms/croppad/functional.py
imports: from __future__ import annotations
  import warnings
  import numpy
  import torch
  from torch.nn.functional import pad
  from monai.config.type_definitions import NdarrayTensor
  from monai.data.meta_obj import get_track_meta
  from monai.data.meta_tensor import MetaTensor
  from monai.data.utils import to_affine_nd
  from monai.transforms.inverse import TraceableTransform
  from monai.transforms.utils import convert_pad_mode, create_translate
  from monai.utils import PytorchPadMode, convert_to_dst_type, convert_to_numpy, convert_to_tensor, ensure_tuple
label_desc: Use image node_id 4 for calling a global function with example usage: pad_nd(img, to_pad, mode) and returns: _np_pad, _pad, _np_pad