code: def ref_warp_by_grid(
      inp, grid, mode, padding_mode, align_corners, channel_last
  ):
      if channel_last:
          size = grid.shape[1:-1]
          n_sdim = 2 if len(size) == 2 else 3
          t = ChannelLastToFirstTranspose(inp.ndim, n_sdim)
          inp = t(inp)
  
      if mode == "linear":
          if len(inp.shape) == 4:
              out = warp_by_grid_linear_2d(
                  inp,
                  grid,
                  mode,
                  padding_mode,
                  align_corners,
                  channel_last,
              )
          if len(inp.shape) == 5:
              out = warp_by_grid_linear_3d(
                  inp,
                  grid,
                  mode,
                  padding_mode,
                  align_corners,
                  channel_last,
              )
      if mode == "nearest":
          if len(inp.shape) == 4:
              out = warp_by_grid_nearest_2d(
                  inp,
                  grid,
                  mode,
                  padding_mode,
                  align_corners,
                  channel_last,
              )
          if len(inp.shape) == 5:
              out = warp_by_grid_nearest_3d(
                  inp,
                  grid,
                  mode,
                  padding_mode,
                  align_corners,
                  channel_last,
              )
  
      out = t.inv(out) if channel_last else out
      return out
  
imports: import pytest
  import numpy
  import nnabla
  import nnabla.functions
  from nbla_test_utils import list_context
  from refs import generate_transformation_2d, generate_transformation_3d, affine_grid_2d, affine_grid_3d, ChannelLastToFirstTranspose
  from itertools import product
label_desc: Use image node_id 13 for calling a global function with example usage: ref_warp_by_grid(inp, grid, mode, padding_mode, align_corners, channel_last) and returns: out