code: def _apply_grid_transform(
      img: Tensor,
      grid: Tensor,
      mode: str,
      fill: Optional[Union[int, float, List[float]]],
  ) -> Tensor:
      img, need_cast, need_squeeze, out_dtype = _cast_squeeze_in(
          img, [grid.dtype]
      )
  
      if img.shape[0] > 1:
          # Apply same grid to a batch of images
          grid = grid.expand(
              img.shape[0], grid.shape[1], grid.shape[2], grid.shape[3]
          )
  
      # Append a dummy mask for customized fill colors, should be faster than grid_sample() twice
      if fill is not None:
          mask = torch.ones(
              (img.shape[0], 1, img.shape[2], img.shape[3]),
              dtype=img.dtype,
              device=img.device,
          )
          img = torch.cat((img, mask), dim=1)
  
      img = grid_sample(
          img,
          grid,
          mode=mode,
          padding_mode="zeros",
          align_corners=False,
      )
  
      # Fill with required color
      if fill is not None:
          mask = img[:, -1:, :, :]  # N * 1 * H * W
          img = img[:, :-1, :, :]  # N * C * H * W
          mask = mask.expand_as(img)
          fill_list, len_fill = (
              (fill, len(fill))
              if isinstance(fill, (tuple, list))
              else ([float(fill)], 1)
          )
          fill_img = (
              torch.tensor(
                  fill_list, dtype=img.dtype, device=img.device
              )
              .view(1, len_fill, 1, 1)
              .expand_as(img)
          )
          if mode == "nearest":
              mask = mask < 0.5
              img[mask] = fill_img[mask]
          else:  # 'bilinear'
              img = img * mask + (1.0 - mask) * fill_img
  
      img = _cast_squeeze_out(img, need_cast, need_squeeze, out_dtype)
      return img
  
imports: import warnings
  from typing import List, Optional, Tuple, Union
  import torch
  from torch import Tensor
  from torch.nn.functional import conv2d, grid_sample, interpolate, pad
label_desc: Use image node_id 28 for calling a global function with example usage: _apply_grid_transform(img, grid, mode, fill) and returns: img