class_docstr: Positional encoding.
  
  Args:
      d_model: Embedding dimension.
      dropout_rate: Dropout rate.
      max_len: Maximum input length.
      reverse: Whether to reverse the input position.
class_docstr_tok: ["Positional","encoding",".","Args",":","d_model",":","Embedding","dimension",".","dropout_rate",":","Dropout","rate",".","max_len",":","Maximum","input","length",".","reverse",":","Whether","to","reverse","the","input","position","."]
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:
          if self.pe.size(1) >= x.size(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
      pe = torch.zeros(x.size(1), self.d_model)
      if self.reverse:
          position = torch.arange(
              x.size(1) - 1, -1, -1.0, dtype=torch.float32
          ).unsqueeze(1)
      else:
          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[:, 0::2] = torch.sin(position * div_term)
      pe[:, 1::2] = torch.cos(position * div_term)
      pe = pe.unsqueeze(0)
      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 PositionalEncoding obj's underlying member method code with example usage: obj.extend_pe(x) without return types