class_docstr: Custom Encoder with extended position embedding that is shift-invariant.
class_docstr_tok: ["Custom","Encoder","with","extended","position","embedding","that","is","shift-invariant","."]
docstr: Rearange positions prior to feeding into transformer so that the existing ones,
  i.e. the first self.n_positions_init, do not change from before.
  
  :param LongTensor[batch,seqlen] positions:
      Positions for input IDs
  
  :return LongTensor[batch,seqlen] new_positions
      return positions that's shift-invariant
docstr_tok: ["Rearange","positions","prior","to","feeding","into","transformer","so","that","the","existing","ones",",","i.e",".","the","first","self.n_positions_init",",","do","not","change","from","before",".",":","param","LongTensor","[","batch",",","seqlen","]","positions",":","Positions","for","input","IDs",":","return","LongTensor","[","batch",",","seqlen","]","new_positions","return","positions","that","'s","shift-invariant"]
code: def rearrange_positions(
      self, positions: torch.LongTensor
  ) -> torch.LongTensor:
      """
      Rearange positions prior to feeding into transformer so that the existing ones,
      i.e. the first self.n_positions_init, do not change from before.
  
      :param LongTensor[batch,seqlen] positions:
          Positions for input IDs
  
      :return LongTensor[batch,seqlen] new_positions
          return positions that's shift-invariant
      """
      # max positions per sample in each batch, return batch * 1
      if self.n_positions <= self.n_positions_init:
          return positions
      pos_offset = (
          (
              torch.max(positions, dim=1).values
              - self.n_positions_init
              + 1
          )
          .unsqueeze(1)
          .clamp(min=0)
      )
      # shift the positions to the right by (max_position - self.original_n_positions).clamp_(min=0)
      # notice that remainder always return non-negative values (unlike torch.fmod), e.g. torch.remainder([-1, -2], 10) = [9, 8]
      return torch.remainder(positions - pos_offset, self.n_positions)
  
file_path: ParlAI/projects/msc/agents/long_tga.py
imports: import torch
  from typing import Optional, Tuple
  from parlai.agents.transformer.modules import TransformerDecoder, TransformerEncoder, TransformerGeneratorModel
  from parlai.agents.transformer.transformer import TransformerGeneratorAgent
  from parlai.core.opt import Opt
  from parlai.core.params import ParlaiParser
label_desc: Use image node_id 3 for calling the ShiftInvariantForwardEmbeddingMixin obj's underlying member method code with example usage: obj.rearrange_positions(positions) and returns: torch, positions