docstr: Pad an input tensor x so that an integer number of patches of <patch_size>
      can be extracted from an input, i.e. we can do "valid" patch extraction
  
  Args:
      x: tensor to be padded; expected shape [B, C, H, W].
      patch_size: size of the patch.
  
  Returns: a tuple of 3 containing:
      - x_padded: tensor of shape [B, C, H`, W`] where sizes H` and W` are such
          that (size - patch_size + stride) % stride == 0
      - n_pathces_H: number of patches in the height dimension
      - n_patches_W: number of patches in the width dimension
docstr_tok: ["Pad","an","input","tensor","x","so","that","an","integer","number","of","patches","of","<","patch_size",">","can","be","extracted","from","an","input",",","i.e",".","we","can","do","``","valid","''","patch","extraction","Args",":","x",":","tensor","to","be","padded",";","expected","shape","[","B",",","C",",","H",",","W","]",".","patch_size",":","size","of","the","patch",".","Returns",":","a","tuple","of","3","containing",":","-","x_padded",":","tensor","of","shape","[","B",",","C",",","H","`",",","W","`","]","where","sizes","H","`","and","W","`","are","such","that","(","size","-","patch_size","+","stride",")","%","stride","==","0","-","n_pathces_H",":","number","of","patches","in","the","height","dimension","-","n_patches_W",":","number","of","patches","in","the","width","dimension"]
code: def _pad(self, x: Tensor, patch_size: int) -> Tuple[Tensor, int, int]:
      """Pad an input tensor x so that an integer number of patches of <patch_size>
          can be extracted from an input, i.e. we can do "valid" patch extraction
  
      Args:
          x: tensor to be padded; expected shape [B, C, H, W].
          patch_size: size of the patch.
  
      Returns: a tuple of 3 containing:
          - x_padded: tensor of shape [B, C, H`, W`] where sizes H` and W` are such
              that (size - patch_size + stride) % stride == 0
          - n_pathces_H: number of patches in the height dimension
          - n_patches_W: number of patches in the width dimension
      """
      assert (
          x.dim() == 4
      ), f"expected x to be of dim [B, C, H, W], got {x.shape}"
      if patch_size < self.stride:
          raise ValueError(
              f"<patch_size>={patch_size} must be greater than <stride>={self.stride}"
          )
      missing = patch_size - self.stride
      if missing % 2 != 0:
          raise ValueError(
              f"patch_size - self.stride={patch_size - self.stride} must be even!"
          )
      else:
          m = missing // 2
  
      H, W = x.shape[-2:]
      # H` and W` -- size of H and W after padding
      H_padded = math.ceil(H / self.stride) * self.stride
      W_padded = math.ceil(W / self.stride) * self.stride
      # number of patches in the height and width dimensions
      n_patches_H = H_padded // self.stride
      n_patches_W = W_padded // self.stride
      # need to specify padding for all dimensions explicitly: recall F.pad operates
      # backwards in dim, i.e. first pair refers to Width, second paid is H etc...
      pad_sizes = (m, W_padded - W + m, m, H_padded - H + m)
      return (
          F.pad(x, pad_sizes, mode=self.pad_mode),
          n_patches_H,
          n_patches_W,
      )
  
file_path: NeuralCompression/projects/torch_vct/neural/patcher.py
imports: import math
  from typing import NamedTuple, Optional, Tuple, Union
  import torch
  import torch.nn
  import torch.nn.functional
  from torch import Tensor
label_desc: Use image node_id 2 for calling the Patcher obj's underlying member method code with example usage: obj._pad(x, patch_size) and returns: F,  n_patches_H,  n_patches_W