code: def forward(
      self,
      q: Tensor,
      k: Tensor,
      v: Tensor,
      *,
      hw: Optional[Tuple[int, int]] = None,
      mask: Optional[Tensor] = None,
      require_weights: bool = False,
      determinate: bool = False,
  ) -> AttentionOutput:
      qkv_inp = q, k, v
      if self.hook is not None:
          qkv_inp = self.hook.before_forward(qkv_inp)
      # `mask` represents slots which will be zeroed
      if self.qkv_same:
          qkv = F.linear(q, self.in_w, self.qkv_bias)
          if not determinate:
              q, k, v = qkv.chunk(3, dim=-1)
          else:
              qkv = qkv.view(-1, int(q.shape[1]), 3, self.embed_dim)
              q, k, v = map(
                  partial(torch.squeeze, dim=2), qkv.split(1, dim=2)
              )
      elif self.kv_same:
          # B, Nq, Din -> B, Nq, D
          q = F.linear(q, self.q_w, self.q_bias)
          # B, Nk, Dk -> B, Nk, D
          if self.reduction is not None:
              if hw is None:
                  msg = "`hw` should be provided when `reduction` is applied"
                  raise ValueError(msg)
              k = self._reduce(k, hw)
          k, v = F.linear(k, self.kv_w, self.kv_bias).chunk(2, dim=-1)
      else:
          if self.qkv_bias is not None:
              q_bias, k_bias, v_bias = self.qkv_bias.chunk(3)
          else:
              q_bias = self.q_bias
              k_bias = self.k_bias
              v_bias = self.v_bias
          # B, Nq, Din -> B, Nq, D
          q = F.linear(q, self.q_w, q_bias)
          # B, Nk, Dk -> B, Nk, D
          k = F.linear(self._reduce(k, hw), self.k_w, k_bias)
          # B, Nv, Dv -> B, Nv, D
          v = F.linear(self._reduce(v, hw), self.v_w, v_bias)
      if self.hook is not None:
          if self.reduction is not None:
              raise ValueError(
                  "currently `hook` does not support `reduction`"
              )
          q, k, v = self.hook.after_forward(qkv_inp, (q, k, v))
      q, k, v = map(self.activation, [q, k, v])
      # B, N*, D -> B * N_head, N*, D_head
      q, k, v = map(self._to_heads, [q, k, v], [determinate] * 3)
      if mask is not None:
          # B, Nq, Nk -> B, N_head, Nq, Nk
          mask = mask.repeat(self.num_heads, 1, 1)
          mask = mask.view(-1, self.num_heads, *mask.shape[1:])
      if not self.customize_sdp and not require_weights:
          weights = None
          if mask is not None:
              mask = ~mask
          output = sdp_attn(q, k, v, self.training, mask, self.dropout)
      else:
          # B, N_head, Nq, Nk
          raw_weights = torch.matmul(q, k.transpose(-2, -1))
          if mask is not None:
              raw_weights.masked_fill_(mask, float("-inf"))
          # scale
          raw_weights = raw_weights / self.scaling
          # B, N_head, Nq, Nk -> B, N_head, Nq, Nk
          weights = self._get_weights(raw_weights)
          if 0.0 < self.dropout < 1.0:
              weights = F.dropout(weights, self.dropout, self.training)
          weights = self._weights_callback(weights)
          # B, N_head, Nq, D_head
          output = torch.matmul(weights, v)
      # B, N_head, Nq, D_head -> B, Nq, N_head, D_head
      output = output.transpose(1, 2).contiguous()
      # B, Nq, N_head, D_head -> B, Nq, D
      seq_len = output.shape[1]
      if determinate:
          seq_len = int(seq_len)
      output = output.view(-1, seq_len, self.embed_dim)
      # B, Nq, D -> B, Nq, Din
      net = self.out_linear(output)
      net = self.activation(net)
      return AttentionOutput(net, weights)
  
imports: import math
  import torch
  import numpy
  import torch.nn
  import torch.nn.functional
  from torch import Tensor
  from typing import Any
  from typing import Dict
  from typing import Type
  from typing import Tuple
  from typing import Optional
  from typing import NamedTuple
  from functools import partial
  from torch.nn import Module
  from cftool.misc import safe_execute
  from cftool.misc import WithRegister
  from .convs import conv_nd
  from .convs import Conv2d
  from .utils import zero_module
  from .common import Lambda
  from .hooks import IAttentionHook
  from .hijacks import IAttention
  from .hijacks import IHijackMixin
  from .hijacks import HijackConv2d
  from .hijacks import HijackLinear
  from .hijacks import HijackCustomLinear
  from .activations import Activation
  from ...misc.toolkit import sdp_attn
  from ...misc.toolkit import gradient_checkpoint
label_desc: Use image node_id 6 for calling the Attention obj's underlying member method code with example usage: obj.forward(q, k, v) and returns: AttentionOutput