class_docstr: Relative positional encoding for TransformerXL's layers
  See : Appendix B in https://arxiv.org/abs/1901.02860
  Args:
      d_model (int): embedding dim
      dropout_rate (float): dropout rate
      max_len (int): maximum input length
      xscale (bool): whether to scale the input by sqrt(d_model)
      dropout_rate_emb (float): dropout rate for the positional embeddings
class_docstr_tok: ["Relative","positional","encoding","for","TransformerXL","'s","layers","See",":","Appendix","B","in","https",":","\/\/arxiv.org\/abs\/1901.02860","Args",":","d_model","(","int",")",":","embedding","dim","dropout_rate","(","float",")",":","dropout","rate","max_len","(","int",")",":","maximum","input","length","xscale","(","bool",")",":","whether","to","scale","the","input","by","sqrt","(","d_model",")","dropout_rate_emb","(","float",")",":","dropout","rate","for","the","positional","embeddings"]
docstr: Reset and extend the positional encodings if needed.
docstr_tok: ["Reset","and","extend","the","positional","encodings","if","needed","."]
code: def extend_pe(self, length, device):
      """Reset and extend the positional encodings if needed."""
      needed_size = 2 * length - 1
      if hasattr(self, "pe") and self.pe.size(1) >= needed_size:
          return
      # positions would be from negative numbers to positive
      # positive positions would be used for left positions and negative for right positions
      positions = torch.arange(
          length - 1, -length, -1, dtype=torch.float32, device=device
      ).unsqueeze(1)
      self.create_pe(positions=positions)
  
imports: import math
  from functools import lru_cache
  from typing import List, Tuple
  import torch
  import torch.nn
  import torch.nn.functional
  from nemo.utils import avoid_float16_autocast_context
label_desc: Use image node_id 1 for calling the RelPositionalEncoding obj's underlying member method code with example usage: obj.extend_pe(length, device) without return types