class_docstr: A basic Swin Transformer layer for one stage.
  
  Args:
      dim (int): Number of input channels.
      depth (int): Number of blocks.
      num_heads (int): Number of attention heads.
      window_size (int): Local window size.
      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 | tuple[float], optional): Stochastic depth rate. Default: 0.0
      norm_layer (nn.Module, optional): Normalization layer. Default: nn.LayerNorm
      downsample (nn.Module | None, optional): Downsample layer at the end of the layer. Default: None
      use_checkpoint (bool): Whether to use checkpointing to save memory. Default: False.
class_docstr_tok: ["A","basic","Swin","Transformer","layer","for","one","stage",".","Args",":","dim","(","int",")",":","Number","of","input","channels",".","depth","(","int",")",":","Number","of","blocks",".","num_heads","(","int",")",":","Number","of","attention","heads",".","window_size","(","int",")",":","Local","window","size",".","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","|","tuple","[","float","]",",","optional",")",":","Stochastic","depth","rate",".","Default",":","0.0","norm_layer","(","nn.Module",",","optional",")",":","Normalization","layer",".","Default",":","nn.LayerNorm","downsample","(","nn.Module","|","None",",","optional",")",":","Downsample","layer","at","the","end","of","the","layer",".","Default",":","None","use_checkpoint","(","bool",")",":","Whether","to","use","checkpointing","to","save","memory",".","Default",":","False","."]
code: def create_mask(self, x, H, W):
      # calculate attention mask for SW-MSA
      # 保证Hp和Wp是window_size的整数倍
      Hp = int(np.ceil(H / self.window_size)) * self.window_size
      Wp = int(np.ceil(W / self.window_size)) * self.window_size
      # 拥有和feature map一样的通道排列顺序，方便后续window_partition
      img_mask = torch.zeros(
          (1, Hp, Wp, 1), device=x.device
      )  # [1, Hp, Wp, 1]
      h_slices = (
          slice(0, -self.window_size),
          slice(-self.window_size, -self.shift_size),
          slice(-self.shift_size, None),
      )
      w_slices = (
          slice(0, -self.window_size),
          slice(-self.window_size, -self.shift_size),
          slice(-self.shift_size, None),
      )
      cnt = 0
      for h in h_slices:
          for w in w_slices:
              img_mask[:, h, w, :] = cnt
              cnt += 1
  
      mask_windows = window_partition(
          img_mask, self.window_size
      )  # [nW, Mh, Mw, 1]
      mask_windows = mask_windows.view(
          -1, self.window_size * self.window_size
      )  # [nW, Mh*Mw]
      attn_mask = mask_windows.unsqueeze(1) - mask_windows.unsqueeze(
          2
      )  # [nW, 1, Mh*Mw] - [nW, Mh*Mw, 1]
      # [nW, Mh*Mw, Mh*Mw]
      attn_mask = attn_mask.masked_fill(
          attn_mask != 0, float(-100.0)
      ).masked_fill(attn_mask == 0, float(0.0))
      return attn_mask
  
imports: import torch
  import torch.nn
  import torch.nn.functional
  import torch.utils.checkpoint
  import numpy
  from typing import Optional
label_desc: Use image node_id 2 for calling the BasicLayer obj's underlying member method code with example usage: obj.create_mask(x, H, W) and returns: attn_mask