class_docstr: Given sequences P and Q, computes attention weights for each element in P by
  matching Q with each element in P.
  * a_i_j = softmax(p_i . q_j) where softmax is computed by summing over q_j
class_docstr_tok: ["Given","sequences","P","and","Q",",","computes","attention","weights","for","each","element","in","P","by","matching","Q","with","each","element","in","P.","*","a_i_j","=","softmax","(","p_i",".","q_j",")","where","softmax","is","computed","by","summing","over","q_j"]
docstr: Input:
      p: batch_size * p_seq_len * dim
      q: batch_size * q_seq_len * dim
      q_mask: batch_size * q_seq_len (1 for padding, 0 for true)
  Output:
      matched_seq: batch_size * doc_seq_len * dim
docstr_tok: ["Input",":","p",":","batch_size","*","p_seq_len","*","dim","q",":","batch_size","*","q_seq_len","*","dim","q_mask",":","batch_size","*","q_seq_len","(","1","for","padding",",","0","for","true",")","Output",":","matched_seq",":","batch_size","*","doc_seq_len","*","dim"]
code: def forward(
      self, p: torch.Tensor, q: torch.Tensor, q_mask: torch.Tensor
  ):
      """
      Input:
          p: batch_size * p_seq_len * dim
          q: batch_size * q_seq_len * dim
          q_mask: batch_size * q_seq_len (1 for padding, 0 for true)
      Output:
          matched_seq: batch_size * doc_seq_len * dim
      """
      p_transform = F.relu(self.linear(p))
      q_transform = F.relu(self.linear(q))
  
      # Compute scores s_ij: bsz * doc_seq_len * ques_seq_len
      attn_scores = p_transform.bmm(q_transform.transpose(2, 1))
  
      # Mask padding: set a very low score for ques tokens that are pads.
      q_mask = q_mask.unsqueeze(1).expand(attn_scores.size())
      attn_scores.data.masked_fill_(q_mask.data, -float("inf"))
  
      # Normalize with softmax: bsz * doc_seq_len * ques_seq_len
      attn_scores_flattened = F.softmax(
          attn_scores.view(-1, q.size(1)), dim=-1
      )
      return attn_scores_flattened.view(-1, p.size(1), q.size(1))
  
file_path: pytext/pytext/models/representations/attention.py
imports: import torch
  import torch.nn
  import torch.nn.functional
  from pytext.models.module import Module
  from pytext.utils.usage import log_class_usage
label_desc: Use image node_id 3 for calling the SequenceAlignedAttention obj's underlying member method code with example usage: obj.forward(p, q, q_mask) and returns: attn_scores_flattened