docstr: Apply QKV attention.
  
  :param qkv: an [N x (H * 3 * C) x T] tensor of Qs, Ks, and Vs.
  :return: an [N x (H * C) x T] tensor after attention.
docstr_tok: ["Apply","QKV","attention",".",":","param","qkv",":","an","[","N","x","(","H","*","3","*","C",")","x","T","]","tensor","of","Qs",",","Ks",",","and","Vs.",":","return",":","an","[","N","x","(","H","*","C",")","x","T","]","tensor","after","attention","."]
code: def forward(self, qkv, mask=None, qk_bias=0):
      """
      Apply QKV attention.
  
      :param qkv: an [N x (H * 3 * C) x T] tensor of Qs, Ks, and Vs.
      :return: an [N x (H * C) x T] tensor after attention.
      """
      bs, width, length = qkv.shape
      assert width % (3 * self.n_heads) == 0
      ch = width // (3 * self.n_heads)
      q, k, v = qkv.reshape(bs * self.n_heads, ch * 3, length).split(
          ch, dim=1
      )
      scale = 1 / math.sqrt(math.sqrt(ch))
      weight = torch.einsum(
          "bct,bcs->bts", q * scale, k * scale
      )  # More stable with f16 than dividing afterwards
      weight = weight + qk_bias
      if mask is not None:
          mask = mask.repeat(self.n_heads, 1, 1)
          weight[mask.logical_not()] = -torch.inf
      weight = torch.softmax(weight.float(), dim=-1).type(weight.dtype)
      a = torch.einsum("bts,bcs->bct", weight, v)
  
      return a.reshape(bs, -1, length)
  
imports: import math
  import torch
  from torch import nn
  from torch.nn import functional
label_desc: Use image node_id 2 for calling the QKVAttention obj's underlying member method code with example usage: obj.forward(qkv, mask, qk_bias) and returns: a