class_docstr: See :func:`shifted_window_attention`.
class_docstr_tok: ["See",":","func",":","`","shifted_window_attention","`","."]
code: def __init__(
      self,
      dim: int,
      window_size: List[int],
      shift_size: List[int],
      num_heads: int,
      qkv_bias: bool = True,
      proj_bias: bool = True,
      attention_dropout: float = 0.0,
      dropout: float = 0.0,
  ):
      super().__init__()
      if len(window_size) != 2 or len(shift_size) != 2:
          raise ValueError(
              "window_size and shift_size must be of length 2"
          )
      self.window_size = window_size
      self.shift_size = shift_size
      self.num_heads = num_heads
      self.attention_dropout = attention_dropout
      self.dropout = dropout
  
      self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
      self.proj = nn.Linear(dim, dim, bias=proj_bias)
  
      # define a parameter table of relative position bias
      self.relative_position_bias_table = nn.Parameter(
          torch.zeros(
              (2 * window_size[0] - 1) * (2 * window_size[1] - 1),
              num_heads,
          )
      )  # 2*Wh-1 * 2*Ww-1, nH
  
      # get pair-wise relative position index for each token inside the window
      coords_h = torch.arange(self.window_size[0])
      coords_w = torch.arange(self.window_size[1])
      coords = torch.stack(
          torch.meshgrid(coords_h, coords_w, indexing="ij")
      )  # 2, Wh, Ww
      coords_flatten = torch.flatten(coords, 1)  # 2, Wh*Ww
      relative_coords = (
          coords_flatten[:, :, None] - coords_flatten[:, None, :]
      )  # 2, Wh*Ww, Wh*Ww
      relative_coords = relative_coords.permute(
          1, 2, 0
      ).contiguous()  # Wh*Ww, Wh*Ww, 2
      relative_coords[:, :, 0] += (
          self.window_size[0] - 1
      )  # shift to start from 0
      relative_coords[:, :, 1] += self.window_size[1] - 1
      relative_coords[:, :, 0] *= 2 * self.window_size[1] - 1
      relative_position_index = relative_coords.sum(-1).view(
          -1
      )  # Wh*Ww*Wh*Ww
      self.register_buffer(
          "relative_position_index", relative_position_index
      )
  
      nn.init.trunc_normal_(self.relative_position_bias_table, std=0.02)
  
      self.embed_dim = dim
  
imports: from typing import List, Optional
  import torch
  from torch import Tensor, nn
  from torch.nn import functional
  from cvnets.layers import Dropout, LinearLayer, StochasticDepth, get_normalization_layer
  from cvnets.layers.activation import build_activation_layer
  from cvnets.modules import BaseModule
label_desc: Use image node_id 1 to create a new ShiftedWindowAttention object from inherited base classes: BaseModule with example: obj = ShiftedWindowAttention(dim, window_size, shift_size, num_heads, qkv_bias, proj_bias, attention_dropout, dropout)