class_docstr: Swin Transformer Block.
  Args:
      dim (int): Number of input channels.
      input_resolution (tuple[int]): Input resulotion.
      num_heads (int): Number of attention heads.
      window_size (int): Window size.
      shift_size (int): Shift size for SW-MSA.
      mlp_ratio (float): Ratio of mlp hidden dim to embedding dim.
      qkv_bias (bool, optional): If True, add a learnable bias to query, key, value. Default: True
      drop (float, optional): Dropout rate. Default: 0.0
      attn_drop (float, optional): Attention dropout rate. Default: 0.0
      drop_path (float, optional): Stochastic depth rate. Default: 0.0
      act_layer (nn.Module, optional): Activation layer. Default: nn.GELU
      norm_layer (nn.Module, optional): Normalization layer.  Default: nn.LayerNorm
       pretrained_window_size (int): Window size in pre-training.
class_docstr_tok: ["Swin","Transformer","Block",".","Args",":","dim","(","int",")",":","Number","of","input","channels",".","input_resolution","(","tuple","[","int","]",")",":","Input","resulotion",".","num_heads","(","int",")",":","Number","of","attention","heads",".","window_size","(","int",")",":","Window","size",".","shift_size","(","int",")",":","Shift","size","for","SW-MSA",".","mlp_ratio","(","float",")",":","Ratio","of","mlp","hidden","dim","to","embedding","dim",".","qkv_bias","(","bool",",","optional",")",":","If","True",",","add","a","learnable","bias","to","query",",","key",",","value",".","Default",":","True","drop","(","float",",","optional",")",":","Dropout","rate",".","Default",":","0.0","attn_drop","(","float",",","optional",")",":","Attention","dropout","rate",".","Default",":","0.0","drop_path","(","float",",","optional",")",":","Stochastic","depth","rate",".","Default",":","0.0","act_layer","(","nn.Module",",","optional",")",":","Activation","layer",".","Default",":","nn.GELU","norm_layer","(","nn.Module",",","optional",")",":","Normalization","layer",".","Default",":","nn.LayerNorm","pretrained_window_size","(","int",")",":","Window","size","in","pre-training","."]
code: def forward(self, x):
      h, w = self.input_resolution
      b, l, c = x.shape
      assert l == h * w, "input feature has wrong size"
  
      shortcut = x
      if not self.is_v2:
          x = self.norm1(x)
      x = x.view(b, h, w, c)
  
      # cyclic shift
      if self.shift_size > 0:
          shifted_x = torch.roll(
              x,
              shifts=(-self.shift_size, -self.shift_size),
              dims=(1, 2),
          )
      else:
          shifted_x = x
  
      # partition windows
      x_windows = window_partition(
          shifted_x, self.window_size
      )  # nW*B, window_size, window_size, C
      x_windows = x_windows.view(
          -1, self.window_size * self.window_size, c
      )  # nW*B, window_size*window_size, C
  
      # W-MSA/SW-MSA
      attn_windows = self.attn(
          x_windows, mask=self.attn_mask
      )  # nW*B, window_size*window_size, C
  
      # merge windows
      attn_windows = attn_windows.view(
          -1, self.window_size, self.window_size, c
      )
      shifted_x = window_reverse(
          attn_windows, self.window_size, h, w
      )  # B H' W' C
  
      # reverse cyclic shift
      if self.shift_size > 0:
          x = torch.roll(
              shifted_x,
              shifts=(self.shift_size, self.shift_size),
              dims=(1, 2),
          )
      else:
          x = shifted_x
      x = x.view(b, h * w, c)
      if self.is_v2:
          x = self.norm1(x)
      # FFN
      x = shortcut + self.drop_path(x)
      if self.is_v2:
          x = x + self.drop_path(self.norm2(self.mlp(x)))
      else:
          x = x + self.drop_path(self.mlp(self.norm2(x)))
  
      return x
  
file_path: towhee/towhee/models/swin_transformer/swin_transformer_block.py
imports: import torch
  from torch import nn
  from towhee.models.layers.mlp import Mlp
  from towhee.models.layers.droppath import DropPath
  from towhee.models.utils.general_utils import to_2tuple
  from towhee.models.utils.window_reverse import window_reverse
  from towhee.models.utils.window_partition import window_partition
  from towhee.models.layers.window_attention import WindowAttention
label_desc: Use image node_id 2 for calling the SwinTransformerBlock obj's underlying member method code with example usage: obj.forward(x) and returns: x