code: def downsample_2d(x, k=None, factor=2, gain=1):
      r"""Downsample a batch of 2D images with the given filter.
  
      Accepts a batch of 2D images of the shape `[N, C, H, W]` or `[N, H, W, C]`
      and downsamples each image with the given filter. The filter is normalized
      so that
      if the input pixels are constant, they will be scaled by the specified
      `gain`.
      Pixels outside the image are assumed to be zero, and the filter is padded
      with
      zeros so that its shape is a multiple of the downsampling factor.
      Args:
          x:            Input tensor of the shape `[N, C, H, W]` or `[N, H, W,
            C]`.
          k:            FIR filter of the shape `[firH, firW]` or `[firN]`
            (separable). The default is `[1] * factor`, which corresponds to
            average pooling.
          factor:       Integer downsampling factor (default: 2).
          gain:         Scaling factor for signal magnitude (default: 1.0).
  
      Returns:
          Tensor of the shape `[N, C, H // factor, W // factor]`
      """
  
      assert isinstance(factor, int) and factor >= 1
      if k is None:
          k = [1] * factor
      k = _setup_kernel(k) * gain
      p = k.shape[0] - factor
      return upfirdn2d(
          x,
          torch.tensor(k, device=x.device),
          down=factor,
          pad=((p + 1) // 2, p // 2),
      )
  
file_path: espnet/espnet2/enh/layers/ncsnpp_utils/up_or_down_sampling.py
imports: import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from espnet2.enh.layers.ncsnpp_utils.upfirdn2d import upfirdn2d
label_desc: Use image node_id 9 for calling a global function with example usage: downsample_2d(x, k, factor, gain) and returns: upfirdn2d