code: def _apply_grid_transform(
      img: Tensor, grid: Tensor, mode: str, fill: Optional[List[float]]
  ) -> Tensor:
      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:
          dummy = paddle.ones(
              (img.shape[0], 1, img.shape[2], img.shape[3]),
              dtype=img.dtype,
          )
          img = paddle.concat((img, dummy), axis=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)
          len_fill = len(fill) if isinstance(fill, (tuple, list)) else 1
          fill_img = (
              paddle.to_tensor(fill, dtype=img.dtype)
              .reshape([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
      return img
  
imports: import math
  import numbers
  import warnings
  from enum import Enum
  from typing import Any
  from typing import Dict
  from typing import List
  from typing import Optional
  from typing import Sequence
  from typing import Tuple
  from typing import Union
  import paddle
  import paddle.nn
  from paddle import Tensor
  from paddle.nn import functional
  from paddle.nn.functional import grid_sample
  from paddle.vision import transforms
label_desc: Use image node_id 10 for calling a global function with example usage: _apply_grid_transform(img, grid, mode, fill) and returns: img