class_docstr: Vision transformer with locally-initializable self-attention layers
class_docstr_tok: ["Vision","transformer","with","locally-initializable","self-attention","layers"]
docstr: Interpolates position embeddings for on a single instance basis (e.g.
  for a single forward pass) wrt passed data, in contrast to
  interpolate_position_embedding(), which interpolates
  the stored parameter. This function is needed if passing images of a
  size different from that which the model was initialized to accept.
docstr_tok: ["Interpolates","position","embeddings","for","on","a","single","instance","basis","(","e.g",".","for","a","single","forward","pass",")","wrt","passed","data",",","in","contrast","to","interpolate_position_embedding","(",")",",","which","interpolates","the","stored","parameter",".","This","function","is","needed","if","passing","images","of","a","size","different","from","that","which","the","model","was","initialized","to","accept","."]
code: def interpolate_pos_embedding_to_data(self, x, pos_embed):
      """
      Interpolates position embeddings for on a single instance basis (e.g.
      for a single forward pass) wrt passed data, in contrast to
      interpolate_position_embedding(), which interpolates
      the stored parameter. This function is needed if passing images of a
      size different from that which the model was initialized to accept.
      """
      data_seq_length = x.shape[1]
      pos_embed_seq_length = pos_embed.shape[1]
      if data_seq_length == pos_embed_seq_length:
          return pos_embed
      dim = x.shape[-1]
      pos_embed = pos_embed.reshape(
          1,
          int(math.sqrt(pos_embed_seq_length)),
          int(math.sqrt(pos_embed_seq_length)),
          dim,
      )
      pos_embed = pos_embed.permute(0, 3, 1, 2)
      pos_embed = nn.functional.interpolate(
          pos_embed,
          scale_factor=math.sqrt(
              data_seq_length / pos_embed_seq_length
          ),
          mode="bicubic",
      )
      pos_embed = pos_embed.permute(0, 2, 3, 1).view(1, -1, dim)
      return pos_embed
  
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 7 for calling the ConViT obj's underlying member method code with example usage: obj.interpolate_pos_embedding_to_data(x, pos_embed) and returns: pos_embed, pos_embed