class_docstr: Relative positional encoding module (new implementation).
  
  Args:
      d_model: Embedding dimension.
      dropout_rate: Dropout rate.
      max_len: Maximum input length.
class_docstr_tok: ["Relative","positional","encoding","module","(","new","implementation",")",".","Args",":","d_model",":","Embedding","dimension",".","dropout_rate",":","Dropout","rate",".","max_len",":","Maximum","input","length","."]
docstr: Reset the positional encodings.
docstr_tok: ["Reset","the","positional","encodings","."]
code: def extend_pe(self, x):
      """Reset the positional encodings."""
      if self.pe is not None:
          # self.pe contains both positive and negative parts
          # the length of self.pe is 2 * input_len - 1
          if self.pe.size(1) >= x.size(1) * 2 - 1:
              if self.pe.dtype != x.dtype or self.pe.device != x.device:
                  self.pe = self.pe.to(dtype=x.dtype, device=x.device)
              return
      # Suppose `i` means to the position of query vecotr and `j` means the
      # position of key vector. We use position relative positions when keys
      # are to the left (i>j) and negative relative positions otherwise (i<j).
      pe_positive = torch.zeros(x.size(1), self.d_model)
      pe_negative = torch.zeros(x.size(1), self.d_model)
      position = torch.arange(
          0, x.size(1), dtype=torch.float32
      ).unsqueeze(1)
      div_term = torch.exp(
          torch.arange(0, self.d_model, 2, dtype=torch.float32)
          * -(math.log(10000.0) / self.d_model)
      )
      pe_positive[:, 0::2] = torch.sin(position * div_term)
      pe_positive[:, 1::2] = torch.cos(position * div_term)
      pe_negative[:, 0::2] = torch.sin(-1 * position * div_term)
      pe_negative[:, 1::2] = torch.cos(-1 * position * div_term)
  
      # Reserve the order of positive indices and concat both positive and
      # negative indices. This is used to support the shifting trick
      # as in https://arxiv.org/abs/1901.02860
      pe_positive = torch.flip(pe_positive, [0]).unsqueeze(0)
      pe_negative = pe_negative[1:].unsqueeze(0)
      pe = torch.cat([pe_positive, pe_negative], dim=1)
      self.pe = pe.to(device=x.device, dtype=x.dtype)
  
file_path: fairseq/fairseq/modules/positional_encoding.py
imports: import torch.nn
  import math
  import torch
label_desc: Use image node_id 2 for calling the RelPositionalEncoding obj's underlying member method code with example usage: obj.extend_pe(x) without return types