code: def ndc_grid_sample(
      input: torch.Tensor,
      grid_ndc: torch.Tensor,
      *,
      align_corners: bool = False,
      **grid_sample_kwargs,
  ) -> torch.Tensor:
      """
      Samples a tensor `input` of shape `(B, dim, H, W)` at 2D locations
      specified by a tensor `grid_ndc` of shape `(B, ..., 2)` using
      the `torch.nn.functional.grid_sample` function.
      `grid_ndc` is specified in PyTorch3D NDC coordinate frame.
  
      Args:
          input: The tensor of shape `(B, dim, H, W)` to be sampled.
          grid_ndc: A tensor of shape `(B, ..., 2)` denoting the set of
              2D locations at which `input` is sampled.
              See [1] for a detailed description of the NDC coordinates.
          align_corners: Forwarded to the `torch.nn.functional.grid_sample`
              call. See its docstring.
          grid_sample_kwargs: Additional arguments forwarded to the
              `torch.nn.functional.grid_sample` call. See the corresponding
              docstring for a listing of the corresponding arguments.
  
      Returns:
          sampled_input: A tensor of shape `(B, dim, ...)` containing the samples
              of `input` at 2D locations `grid_ndc`.
  
      References:
          [1] https://pytorch3d.org/docs/cameras
      """
  
      batch, *spatial_size, pt_dim = grid_ndc.shape
      if batch != input.shape[0]:
          raise ValueError(
              "'input' and 'grid_ndc' have to have the same batch size."
          )
      if input.ndim != 4:
          raise ValueError("'input' has to be a 4-dimensional Tensor.")
      if pt_dim != 2:
          raise ValueError(
              "The last dimension of 'grid_ndc' has to be == 2."
          )
  
      grid_ndc_flat = grid_ndc.reshape(batch, -1, 1, 2)
  
      # pyre-fixme[6]: For 2nd param expected `Tuple[int, int]` but got `Size`.
      grid_flat = ndc_to_grid_sample_coords(
          grid_ndc_flat, input.shape[2:]
      )
  
      sampled_input_flat = torch.nn.functional.grid_sample(
          input,
          grid_flat,
          align_corners=align_corners,
          **grid_sample_kwargs,
      )
  
      sampled_input = sampled_input_flat.reshape(
          [batch, input.shape[1], *spatial_size]
      )
  
      return sampled_input
  
imports: import copy
  import inspect
  import warnings
  from typing import Any, List, Optional, Tuple, TypeVar, Union
  import numpy
  import torch
  import torch.nn
  from ..common.datatypes import Device, make_device
label_desc: Use image node_id 3 for calling a global function with example usage: ndc_grid_sample(input, grid_ndc) and returns: sampled_input