code: def grid_sample_3d(
      data: np.ndarray,
      grid: np.ndarray,
      method="bilinear",
      layout="NCDHW",
      padding_mode="zeros",
      align_corners=True,
  ):
      r"""grid_sample_3d for NCDHW layout"""
  
      assert method in (
          "bilinear",
          "nearest",
      ), f"{method} is not supported"
      assert layout == "NCDHW"
      assert padding_mode in (
          "zeros",
          "border",
          "reflection",
      ), f"{padding_mode} is not supported"
      assert len(data.shape) == len(grid.shape) == 5
  
      batch, channel = data.shape[:2]
      in_depth, in_height, in_width = data.shape[2:]
      out_depth, out_height, out_width = grid.shape[2:]
      out_shape = [batch, channel, out_depth, out_height, out_width]
      out = np.zeros(out_shape)
  
      def _get_pixel(b, c, d, h, w):
          if (
              0 <= d <= in_depth - 1
              and 0 <= h <= in_height - 1
              and 0 <= w <= in_width - 1
          ):
              return data[b, c, d, h, w]
          return 0
  
      def _unnormalize(d, h, w):
          if align_corners:
              new_d = (d + 1) * (in_depth - 1) / 2
              new_h = (h + 1) * (in_height - 1) / 2
              new_w = (w + 1) * (in_width - 1) / 2
          else:
              new_d = -0.5 + (d + 1) * in_depth / 2
              new_h = -0.5 + (h + 1) * in_height / 2
              new_w = -0.5 + (w + 1) * in_width / 2
          return (new_d, new_h, new_w)
  
      def _clip_coordinates(x, size):
          return min(max(x, 0), size - 1)
  
      def _reflect_coordinates(i, size):
          def __refelection(i, size, corner_start):
              def __reflect(index, size, corner_start):
                  index_align_corner = abs(corner_start - index)
                  size_times = index_align_corner // size
                  even = size_times % 2 == 0
                  extra = index_align_corner - size_times * size
                  return (
                      extra + corner_start
                      if even
                      else size - extra + corner_start
                  )
  
              if corner_start <= i <= size + corner_start:
                  new_i = i
              else:
                  new_i = __reflect(i, size, corner_start)
              return new_i
  
          if align_corners:
              x = __refelection(i, size - 1, 0)
          else:
              x = __refelection(i, size, -0.5)
          return x
  
      def _compute_source_index(b, d, h, w):
          z = grid[b, 2, d, h, w]
          y = grid[b, 1, d, h, w]
          x = grid[b, 0, d, h, w]
          z, y, x = _unnormalize(z, y, x)
  
          if padding_mode == "reflection":
              z = _reflect_coordinates(z, in_depth)
              y = _reflect_coordinates(y, in_height)
              x = _reflect_coordinates(x, in_width)
              z = _clip_coordinates(z, in_depth)
              y = _clip_coordinates(y, in_height)
              x = _clip_coordinates(x, in_width)
          elif padding_mode == "border":
              z = _clip_coordinates(z, in_depth)
              y = _clip_coordinates(y, in_height)
              x = _clip_coordinates(x, in_width)
          return (z, y, x)
  
      def _nearest_sample():
          for _b in range(batch):
              for _c in range(channel):
                  for _d in range(out_depth):
                      for _h in range(out_height):
                          for _w in range(out_width):
                              z, y, x = _compute_source_index(
                                  _b, _d, _h, _w
                              )
                              # python round is not used here,
                              # beacause it is done toward the even choice
                              new_z = (
                                  int(z + 0.5)
                                  if z > 0
                                  else int(z - 0.5)
                              )
                              new_y = (
                                  int(y + 0.5)
                                  if y > 0
                                  else int(y - 0.5)
                              )
                              new_x = (
                                  int(x + 0.5)
                                  if x > 0
                                  else int(x - 0.5)
                              )
                              out[_b, _c, _d, _h, _w] = _get_pixel(
                                  _b, _c, new_z, new_y, new_x
                              )
  
      def _triilinear_sample():
          for _b in range(batch):
              for _c in range(channel):
                  for _d in range(out_depth):
                      for _h in range(out_height):
                          for _w in range(out_width):
                              z, y, x = _compute_source_index(
                                  _b, _d, _h, _w
                              )
                              z0 = int(math.floor(z))
                              y0 = int(math.floor(y))
                              x0 = int(math.floor(x))
                              z1 = z0 + 1
                              y1 = y0 + 1
                              x1 = x0 + 1
  
                              out[_b, _c, _d, _h, _w] = (
                                  _get_pixel(_b, _c, z0, y0, x0)
                                  * (1 - (x - x0))
                                  * (1 - (y - y0))
                                  * (1 - (z - z0))
                                  + _get_pixel(_b, _c, z0, y0, x1)
                                  * (x - x0)
                                  * (1 - (y - y0))
                                  * (1 - (z - z0))
                                  + _get_pixel(_b, _c, z1, y1, x0)
                                  * (1 - (x - x0))
                                  * (y - y0)
                                  * (z - z0)
                                  + _get_pixel(_b, _c, z1, y1, x1)
                                  * (x - x0)
                                  * (y - y0)
                                  * (z - z0)
                                  + _get_pixel(_b, _c, z0, y1, x0)
                                  * (1 - (x - x0))
                                  * (y - y0)
                                  * (1 - (z - z0))
                                  + _get_pixel(_b, _c, z1, y0, x1)
                                  * (x - x0)
                                  * (1 - (y - y0))
                                  * (z - z0)
                                  + _get_pixel(_b, _c, z1, y0, x0)
                                  * (1 - (x - x0))
                                  * (1 - (y - y0))
                                  * (z - z0)
                                  + _get_pixel(_b, _c, z0, y1, x1)
                                  * (x - x0)
                                  * (y - y0)
                                  * (1 - (z - z0))
                              )
  
      if method == "bilinear":
          _triilinear_sample()
      else:  # method == "nearest":
          _nearest_sample()
  
      return out
  
imports: import math
  import numpy
label_desc: Use image node_id 3 for calling a global function with example usage: grid_sample_3d(data, grid, method, layout, padding_mode, align_corners) and returns: out, int, new_d,  new_h,  new_w, min, x, z,  y,  x, data, new_i, unknown