class_docstr: Abstract class of monotonic attentions
class_docstr_tok: ["Abstract","class","of","monotonic","attentions"]
docstr: Compute energy from query and key
  q_func_value is a tuple looks like
  (q_proj_func, q_tensor)
  q_tensor size: bsz, tgt_len, emb_dim
  k_tensor size: bsz, src_len, emb_dim
  key_padding_mask size: bsz, src_len
  attn_mask: bsz, src_len
docstr_tok: ["Compute","energy","from","query","and","key","q_func_value","is","a","tuple","looks","like","(","q_proj_func",",","q_tensor",")","q_tensor","size",":","bsz",",","tgt_len",",","emb_dim","k_tensor","size",":","bsz",",","src_len",",","emb_dim","key_padding_mask","size",":","bsz",",","src_len","attn_mask",":","bsz",",","src_len"]
code: def energy_from_qk(
      self,
      query: Tensor,
      key: Tensor,
      energy_type: str,
      key_padding_mask: Optional[Tensor] = None,
      bias: int = 0,
  ):
      """
      Compute energy from query and key
      q_func_value is a tuple looks like
      (q_proj_func, q_tensor)
      q_tensor size: bsz, tgt_len, emb_dim
      k_tensor size: bsz, src_len, emb_dim
      key_padding_mask size: bsz, src_len
      attn_mask: bsz, src_len
      """
  
      length, bsz, _ = query.size()
      q = self.q_in_proj[energy_type].forward(query)
      q = (
          q.contiguous()
          .view(length, bsz * self.num_heads, self.head_dim)
          .transpose(0, 1)
      )
      q = q * self.scaling
      length, bsz, _ = key.size()
      k = self.k_in_proj[energy_type].forward(key)
      k = (
          k.contiguous()
          .view(length, bsz * self.num_heads, self.head_dim)
          .transpose(0, 1)
      )
  
      energy = torch.bmm(q, k.transpose(1, 2)) + bias
  
      if key_padding_mask is not None:
          energy = energy.masked_fill(
              key_padding_mask.unsqueeze(1).to(torch.bool),
              -float("inf"),
          )
  
      return energy
  
imports: import math
  import torch
  from torch import Tensor
  import torch.nn
  from examples.simultaneous_translation.utils.p_choose_strategy import learnable_p_choose, waitk_p_choose
  from examples.simultaneous_translation.utils.monotonic_attention import expected_alignment_from_p_choose, expected_soft_attention, mass_preservation
  from fairseq.modules import MultiheadAttention
  from .None import register_monotonic_attention
  from typing import Dict, Optional
label_desc: Use image node_id 3 for calling the MonotonicAttention obj's underlying member method code with example usage: obj.energy_from_qk(query, key, energy_type, key_padding_mask, bias) and returns: energy