class_docstr: Multiscale Multi-head Attention block.
class_docstr_tok: ["Multiscale","Multi-head","Attention","block","."]
code: def forward(self, x):
      B, H, W, _ = x.shape
      # qkv with shape (3, B, nHead, H, W, C)
      qkv = (
          self.qkv(x)
          .reshape(B, H, W, 3, self.num_heads, -1)
          .permute(3, 0, 4, 1, 2, 5)
      )
      # q, k, v with shape (B * nHead, H, W, C)
      q, k, v = qkv.reshape(3, B * self.num_heads, H, W, -1).unbind(0)
  
      q = attention_pool(q, self.pool_q, self.norm_q)
      k = attention_pool(k, self.pool_k, self.norm_k)
      v = attention_pool(v, self.pool_v, self.norm_v)
  
      ori_q = q
      if self.window_size:
          q, q_hw_pad = window_partition(q, self.q_win_size)
          k, kv_hw_pad = window_partition(k, self.kv_win_size)
          v, _ = window_partition(v, self.kv_win_size)
          q_hw = (self.q_win_size, self.q_win_size)
          kv_hw = (self.kv_win_size, self.kv_win_size)
      else:
          q_hw = q.shape[1:3]
          kv_hw = k.shape[1:3]
  
      q = q.view(q.shape[0], np.prod(q_hw), -1)
      k = k.view(k.shape[0], np.prod(kv_hw), -1)
      v = v.view(v.shape[0], np.prod(kv_hw), -1)
  
      attn = (q * self.scale) @ k.transpose(-2, -1)
  
      if self.use_rel_pos:
          attn = add_decomposed_rel_pos(
              attn, q, self.rel_pos_h, self.rel_pos_w, q_hw, kv_hw
          )
  
      attn = attn.softmax(dim=-1)
      x = attn @ v
  
      x = x.view(x.shape[0], q_hw[0], q_hw[1], -1)
  
      if self.window_size:
          x = window_unpartition(
              x, self.q_win_size, q_hw_pad, ori_q.shape[1:3]
          )
  
      if self.residual_pooling:
          x += ori_q
  
      H, W = x.shape[1], x.shape[2]
      x = (
          x.view(B, self.num_heads, H, W, -1)
          .permute(0, 2, 3, 1, 4)
          .reshape(B, H, W, -1)
      )
      x = self.proj(x)
  
      return x
  
file_path: detectron2/detectron2/modeling/backbone/mvit.py
imports: import logging
  import numpy
  import torch
  import torch.nn
  from .backbone import Backbone
  from .utils import PatchEmbed, add_decomposed_rel_pos, get_abs_pos, window_partition, window_unpartition
label_desc: Use image node_id 2 for calling the MultiScaleAttention obj's underlying member method code with example usage: obj.forward(x) and returns: x