code: def _run(
      self, X, grid, mode=None, padding_mode=None, align_corners=None
  ):
      # This implementation supports GridSample arbitrary dimensions.
  
      mode = mode or self.mode  # type: ignore
      padding_mode = padding_mode or self.padding_mode  # type: ignore
      align_corners = align_corners or self.align_corners  # type: ignore
  
      x_dims = X.shape
      grid_dims = grid.shape
      N = x_dims[0]
      C = x_dims[1]
      y_dims = (N, C, *grid_dims[1:-1])
  
      if np.prod(y_dims) == 0:
          return np.array([], dtype=X.dtype)
  
      Y = np.empty(y_dims, dtype=X.dtype)
  
      for n in range(N):
          grid_data = grid[n]
  
          for c in range(C):
              # Because the indices in the grid_data are always in the "reverse" dimensional order.
              # To interpolate for certain positions, we either have to transpose the X_data or
              # reverse the indices.
              # In this implementation, we took the latter approach.
              X_data = X[n, c]
  
              num_dims = len(x_dims[2:])
              dims = x_dims[2:]
  
              # Prepare borders.
              border = self._prepare_border(
                  dims, align_corners=align_corners
              )
  
              for ox in _get_all_coords(Y[n, c]):
                  # normalized coordinates.
                  nx = grid_data[tuple(ox)]
                  nx = nx[::-1]
                  # denormalized coordinates.
                  x = self._gs_denormalize_coordinates(
                      n=nx, dims=dims, align_corners=align_corners
                  )
                  if mode == "nearest":
                      # PyTorch round the index to nearest even.
                      # https://github.com/pytorch/pytorch/pull/97000
                      x = np.rint(x)
                  # https://github.com/pytorch/pytorch/blob/v2.0.0/aten/src/ATen/native/GridSampler.h#L142
                  for i, v in enumerate(x):
                      x_min = border[i]
                      x_max = border[i + num_dims]
                      if v < x_min or v > x_max:
                          if padding_mode == "border":
                              x[i] = self._clamp(v, 0, dims[i] - 1)
                          elif padding_mode == "reflection":
                              x[i] = self._gs_reflect(v, x_min, x_max)
  
                  if mode == "nearest":
                      x = x.astype(np.int32)
                      Y[n][c][tuple(ox)] = self._pixel_at_ndarray(
                          ndarray=X_data,
                          x=x,
                          border=border,
                          padding_mode=padding_mode,
                      )
  
                  elif mode == "linear":
                      Y[n][c][
                          tuple(ox)
                      ] = self._gs_linear_interpolation_nd_with_x(
                          data=X_data,
                          x=x,
                          border=border,
                          padding_mode=padding_mode,
                      )
  
                  elif mode == "cubic":
                      Y[n][c][
                          tuple(ox)
                      ] = self._gs_cubic_interpolation_nd_with_x(
                          data=X_data,
                          x=x,
                          border=border,
                          padding_mode=padding_mode,
                      )
                  else:
                      raise RuntimeError(
                          "GridSample interpolation only supports nearest, linear, and cubic modes."
                      )
  
      return (Y.astype(X.dtype),)
  
imports: import numbers
  from typing import List
  import numpy
  from onnx.reference.op_run import OpRun
  from onnx.reference.ops.op_resize import _get_all_coords
label_desc: Use image node_id 16 for calling the GridSample obj's underlying member method code with example usage: obj._run(X, grid, mode, padding_mode, align_corners) and returns: Y, np