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 'Scaled Dot Product Attention' with rel. positional encoding.
  
  Args:
      query (torch.Tensor): Query tensor (#batch, time1, size).
      key (torch.Tensor): Key tensor (#batch, time2, size).
      value (torch.Tensor): Value tensor (#batch, time2, size).
      pos_emb (torch.Tensor): Positional embedding tensor (#batch, time1, size).
      mask (torch.Tensor): Mask tensor (#batch, 1, time2) or
          (#batch, time1, time2).
  
  Returns:
      torch.Tensor: Output tensor (#batch, time1, d_model).
docstr_tok: ["Compute","'Scaled","Dot","Product","Attention","'","with","rel",".","positional","encoding",".","Args",":","query","(","torch.Tensor",")",":","Query","tensor","(","#","batch",",","time1",",","size",")",".","key","(","torch.Tensor",")",":","Key","tensor","(","#","batch",",","time2",",","size",")",".","value","(","torch.Tensor",")",":","Value","tensor","(","#","batch",",","time2",",","size",")",".","pos_emb","(","torch.Tensor",")",":","Positional","embedding","tensor","(","#","batch",",","time1",",","size",")",".","mask","(","torch.Tensor",")",":","Mask","tensor","(","#","batch",",","1",",","time2",")","or","(","#","batch",",","time1",",","time2",")",".","Returns",":","torch.Tensor",":","Output","tensor","(","#","batch",",","time1",",","d_model",")","."]
code: def forward(self, query, key, value, pos_emb, mask):
      """Compute 'Scaled Dot Product Attention' with rel. positional encoding.
  
      Args:
          query (torch.Tensor): Query tensor (#batch, time1, size).
          key (torch.Tensor): Key tensor (#batch, time2, size).
          value (torch.Tensor): Value tensor (#batch, time2, size).
          pos_emb (torch.Tensor): Positional embedding tensor (#batch, time1, size).
          mask (torch.Tensor): Mask tensor (#batch, 1, time2) or
              (#batch, time1, time2).
  
      Returns:
          torch.Tensor: Output tensor (#batch, time1, d_model).
  
      """
      q, k, v = self.forward_qkv(query, key, value)
      q = q.transpose(1, 2)  # (batch, time1, head, d_k)
  
      n_batch_pos = pos_emb.size(0)
      p = self.linear_pos(pos_emb).view(
          n_batch_pos, -1, self.h, self.d_k
      )
      p = p.transpose(1, 2)  # (batch, head, time1, d_k)
  
      # (batch, head, time1, d_k)
      q_with_bias_u = (q + self.pos_bias_u).transpose(1, 2)
      # (batch, head, time1, d_k)
      q_with_bias_v = (q + self.pos_bias_v).transpose(1, 2)
  
      # compute attention score
      # first compute matrix a and matrix c
      # as described in https://arxiv.org/abs/1901.02860 Section 3.3
      # (batch, head, time1, time2)
      matrix_ac = torch.matmul(q_with_bias_u, k.transpose(-2, -1))
  
      # compute matrix b and matrix d
      # (batch, head, time1, time1)
      matrix_bd = torch.matmul(q_with_bias_v, p.transpose(-2, -1))
      matrix_bd = self.rel_shift(matrix_bd)
  
      scores = (matrix_ac + matrix_bd) / math.sqrt(
          self.d_k
      )  # (batch, head, time1, time2)
  
      return self.forward_attention(v, scores, mask)
  
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 3 for calling the LegacyRelPositionMultiHeadedAttention obj's underlying member method code with example usage: obj.forward(query, key, value, pos_emb, mask) and returns: self