class_docstr: Multi-Head Attention layer with relative position encoding (old version).
  
  Details can be found in https://github.com/espnet/espnet/pull/2816.
  
  Paper: https://arxiv.org/abs/1901.02860
  
  Args:
      n_head (int): The number of heads.
      n_feat (int): The number of features.
      dropout_rate (float): Dropout rate.
      zero_triu (bool): Whether to zero the upper triangular part of attention matrix.
class_docstr_tok: ["Multi-Head","Attention","layer","with","relative","position","encoding","(","old","version",")",".","Details","can","be","found","in","https",":","\/\/github.com\/espnet\/espnet\/pull\/2816",".","Paper",":","https",":","\/\/arxiv.org\/abs\/1901.02860","Args",":","n_head","(","int",")",":","The","number","of","heads",".","n_feat","(","int",")",":","The","number","of","features",".","dropout_rate","(","float",")",":","Dropout","rate",".","zero_triu","(","bool",")",":","Whether","to","zero","the","upper","triangular","part","of","attention","matrix","."]
docstr: Compute relative positional encoding.
  
  Args:
      x (torch.Tensor): Input tensor (batch, head, time1, time2).
  
  Returns:
      torch.Tensor: Output tensor.
docstr_tok: ["Compute","relative","positional","encoding",".","Args",":","x","(","torch.Tensor",")",":","Input","tensor","(","batch",",","head",",","time1",",","time2",")",".","Returns",":","torch.Tensor",":","Output","tensor","."]
code: def rel_shift(self, x):
      """Compute relative positional encoding.
  
      Args:
          x (torch.Tensor): Input tensor (batch, head, time1, time2).
  
      Returns:
          torch.Tensor: Output tensor.
  
      """
      zero_pad = torch.zeros(
          (*x.size()[:3], 1), device=x.device, dtype=x.dtype
      )
      x_padded = torch.cat([zero_pad, x], dim=-1)
  
      x_padded = x_padded.view(*x.size()[:2], x.size(3) + 1, x.size(2))
      x = x_padded[:, :, 1:].view_as(x)
  
      if self.zero_triu:
          ones = torch.ones((x.size(2), x.size(3)))
          x = (
              x
              * torch.tril(ones, x.size(3) - x.size(2))[
                  None, None, :, :
              ]
          )
  
      return x
  
file_path: espnet/espnet/nets/pytorch_backend/transformer/attention.py
imports: import math
  import torch
  from torch import nn
label_desc: Use image node_id 2 for calling the LegacyRelPositionMultiHeadedAttention obj's underlying member method code with example usage: obj.rel_shift(x) and returns: x