class_docstr: Compute 2D patch embeddings with cls token and positional encoding
docstr: 100 % borrowed from:
  https://github.com/huggingface/transformers/blob/main/src/transformers/models/vit/modeling_vit.py
  
  This method allows to interpolate the pre-trained position encodings, to be able to use the model on higher
  resolution images.
  
  Source:
  https://github.com/facebookresearch/dino/blob/de9ee3df6cf39fac952ab558447af1fa1365362a/vision_transformer.py
docstr_tok: ["100","%","borrowed","from",":","https",":","\/\/github.com\/huggingface\/transformers\/blob\/main\/src\/transformers\/models\/vit\/modeling_vit.py","This","method","allows","to","interpolate","the","pre-trained","position","encodings",",","to","be","able","to","use","the","model","on","higher","resolution","images",".","Source",":","https",":","\/\/github.com\/facebookresearch\/dino\/blob\/de9ee3df6cf39fac952ab558447af1fa1365362a\/vision_transformer.py"]
code: def interpolate_pos_encoding(
      self, embeddings: torch.Tensor, height: int, width: int
  ) -> torch.Tensor:
      """100 % borrowed from:
      https://github.com/huggingface/transformers/blob/main/src/transformers/models/vit/modeling_vit.py
  
      This method allows to interpolate the pre-trained position encodings, to be able to use the model on higher
      resolution images.
  
      Source:
      https://github.com/facebookresearch/dino/blob/de9ee3df6cf39fac952ab558447af1fa1365362a/vision_transformer.py
      """
      num_patches = embeddings.shape[1] - 1
      num_positions = self.positions.shape[1] - 1
      if num_patches == num_positions and height == width:
          return self.positions
      class_pos_embed = self.positions[:, 0]
      patch_pos_embed = self.positions[:, 1:]
      dim = embeddings.shape[-1]
      h0 = float(height // self.patch_size[0])
      w0 = float(width // self.patch_size[1])
      # we add a small number to avoid floating point error in the interpolation
      # see discussion at https://github.com/facebookresearch/dino/issues/8
      h0, w0 = h0 + 0.1, w0 + 0.1
      patch_pos_embed = patch_pos_embed.reshape(
          1,
          int(math.sqrt(num_positions)),
          int(math.sqrt(num_positions)),
          dim,
      )
      patch_pos_embed = patch_pos_embed.permute(0, 3, 1, 2)
      patch_pos_embed = nn.functional.interpolate(
          patch_pos_embed,
          scale_factor=(
              h0 / math.sqrt(num_positions),
              w0 / math.sqrt(num_positions),
          ),
          mode="bilinear",
          align_corners=False,
          recompute_scale_factor=True,
      )
      assert (
          int(h0) == patch_pos_embed.shape[-2]
      ), "height of interpolated patch embedding doesn't match"
      assert (
          int(w0) == patch_pos_embed.shape[-1]
      ), "width of interpolated patch embedding doesn't match"
  
      patch_pos_embed = patch_pos_embed.permute(0, 2, 3, 1).view(
          1, -1, dim
      )
      return torch.cat(
          (class_pos_embed.unsqueeze(0), patch_pos_embed), dim=1
      )
  
file_path: doctr/doctr/models/modules/vision_transformer/pytorch.py
imports: import math
  from typing import Tuple
  import torch
  from torch import nn
label_desc: Use image node_id 2 for calling the PatchEmbedding obj's underlying member method code with example usage: obj.interpolate_pos_encoding(embeddings, height, width) and returns: torch, self