code: def unfold2d(
      input,
      *,
      kernel_size: Tuple[int, int],
      padding: Union[str, Tuple[int, int]],
      stride: Tuple[int, int],
      dilation: Tuple[int, int],
  ):
      """
      See :meth:`~torch.nn.functional.unfold`
      """
      *shape, H, W = input.shape
      if padding == "same":
          total_pad_H = dilation[0] * (kernel_size[0] - 1)
          total_pad_W = dilation[1] * (kernel_size[1] - 1)
          pad_H_left = math.floor(total_pad_H / 2)
          pad_H_right = total_pad_H - pad_H_left
          pad_W_left = math.floor(total_pad_W / 2)
          pad_W_right = total_pad_W - pad_W_left
  
      elif padding == "valid":
          pad_W_left, pad_W_right, pad_H_left, pad_H_right = (
              0,
              0,
              0,
              0,
          )
      else:
          pad_H_left, pad_H_right, pad_W_left, pad_W_right = (
              padding[0],
              padding[0],
              padding[1],
              padding[1],
          )
  
      H_effective = (
          H
          + pad_H_left
          + pad_H_right
          - (kernel_size[0] + (kernel_size[0] - 1) * (dilation[0] - 1))
      ) // stride[0] + 1
      W_effective = (
          W
          + pad_W_left
          + pad_W_right
          + -(kernel_size[1] + (kernel_size[1] - 1) * (dilation[1] - 1))
      ) // stride[1] + 1
      # F.pad's first argument is the padding of the *last* dimension
      input = F.pad(
          input, (pad_W_left, pad_W_right, pad_H_left, pad_H_right)
      )
      *shape_pad, H_pad, W_pad = input.shape
      strides = list(input.stride())
      strides = strides[:-2] + [
          W_pad * dilation[0],
          dilation[1],
          W_pad * stride[0],
          stride[1],
      ]
      out = input.as_strided(
          shape
          + [kernel_size[0], kernel_size[1], H_effective, W_effective],
          strides,
      )
  
      return out.reshape(input.size(0), -1, H_effective * W_effective)
  
file_path: opacus/opacus/utils/tensor_utils.py
imports: import math
  from typing import Iterator, List, Tuple, Union
  import numpy
  import torch
  import torch.nn.functional
label_desc: Use image node_id 4 for calling a global function with example usage: unfold2d(input) and returns: out