class_docstr: Vision transformer with locally-initializable self-attention layers
class_docstr_tok: ["Vision","transformer","with","locally-initializable","self-attention","layers"]
docstr: Interpolate a passed position embedding to fit the position embedding of
  this model. Typically called when fine-tuning on higher resolution
  images than the model was pre-trained on. Called by
  viss/utils/checkpoint.py when loading state_dict.
docstr_tok: ["Interpolate","a","passed","position","embedding","to","fit","the","position","embedding","of","this","model",".","Typically","called","when","fine-tuning","on","higher","resolution","images","than","the","model","was","pre-trained","on",".","Called","by","viss\/utils\/checkpoint.py","when","loading","state_dict","."]
code: def interpolate_position_embedding(self, pos_embedding):
      """
      Interpolate a passed position embedding to fit the position embedding of
      this model. Typically called when fine-tuning on higher resolution
      images than the model was pre-trained on. Called by
      viss/utils/checkpoint.py when loading state_dict.
      """
      # shape of pos_embedding is (1, seq_length, hidden_dim)
      n, seq_length, hidden_dim = pos_embedding.shape
      if n != 1:
          raise ValueError(
              f"Unexpected position embedding shape: {pos_embedding.shape}"
          )
      if hidden_dim != self.embed_dim:
          raise ValueError(
              f"Position embedding hidden_dim incorrect: {self.embed_dim}"
              f", expected: {self.embed_dim}"
          )
      new_seq_length = self.seq_length
  
      if new_seq_length != seq_length:
          # need to interpolate the weights for the position embedding
          # we do this by reshaping the positions embeddings to a 2d grid,
          # performing an interpolation in the (h, w) space and then
          # reshaping back to a 1d grid
  
          # (seq_length, 1, hidden_dim) -> (1, hidden_dim, seq_length)
          pos_embedding = pos_embedding.permute(0, 2, 1)
          seq_length_1d = int(math.sqrt(seq_length))
          assert (
              seq_length_1d * seq_length_1d == seq_length
          ), "seq_length is not a perfect square"
  
          logging.info(
              "Interpolating the position embeddings from image "
              f"{seq_length_1d * self.patch_size} to size"
              f" {self.image_size}"
          )
  
          # (1, hidden_dim, seq_length) -> (1, hidden_dim, seq_l_1d, seq_l_1d)
          pos_embedding = pos_embedding.reshape(
              1, hidden_dim, seq_length_1d, seq_length_1d
          )
          new_seq_length_1d = self.image_size // self.patch_size
  
          # use bicubic interpolation - it gives significantly better
          # results in the test `test_resolution_change`
          new_pos_embedding = torch.nn.functional.interpolate(
              pos_embedding,
              size=new_seq_length_1d,
              mode="bicubic",
              align_corners=True,
          )
  
          # (1, hidden_dim, new_seq_l_1d, new_seq_l_1d) ->
          # (1, hidden_dim, new_seq_l)
          new_pos_embedding = new_pos_embedding.reshape(
              1, hidden_dim, new_seq_length
          )
          # (1, hidden_dim, new_seq_length) -> (new_seq_length, 1, hidden_dim)
          new_pos_embedding = new_pos_embedding.permute(0, 2, 1)
          return new_pos_embedding
      else:
          return pos_embedding
  
imports: import copy
  import logging
  import math
  import torch
  import torch.nn
  from vissl.models.model_helpers import DropPath, to_2tuple, trunc_normal_
  from vissl.models.trunks import register_model_trunk
  from vissl.utils.hydra_config import AttrDict
label_desc: Use image node_id 6 for calling the ConViT obj's underlying member method code with example usage: obj.interpolate_position_embedding(pos_embedding) and returns: new_pos_embedding, pos_embedding