class_docstr: Window based multi-head self attention module with relative position bias based on: "Liu et al.,
  Swin Transformer: Hierarchical Vision Transformer using Shifted Windows
  <https://arxiv.org/abs/2103.14030>"
  https://github.com/microsoft/Swin-Transformer
docstr: Args:
      dim: number of feature channels.
      num_heads: number of attention heads.
      window_size: local window size.
      qkv_bias: add a learnable bias to query, key, value.
      attn_drop: attention dropout rate.
      proj_drop: dropout rate of output.
docstr_tok: ["Args",":","dim",":","number","of","feature","channels",".","num_heads",":","number","of","attention","heads",".","window_size",":","local","window","size",".","qkv_bias",":","add","a","learnable","bias","to","query",",","key",",","value",".","attn_drop",":","attention","dropout","rate",".","proj_drop",":","dropout","rate","of","output","."]
code: def __init__(
      self,
      dim: int,
      num_heads: int,
      window_size: Sequence[int],
      qkv_bias: bool = False,
      attn_drop: float = 0.0,
      proj_drop: float = 0.0,
  ) -> None:
      """
      Args:
          dim: number of feature channels.
          num_heads: number of attention heads.
          window_size: local window size.
          qkv_bias: add a learnable bias to query, key, value.
          attn_drop: attention dropout rate.
          proj_drop: dropout rate of output.
      """
  
      super().__init__()
      self.dim = dim
      self.window_size = window_size
      self.num_heads = num_heads
      head_dim = dim // num_heads
      self.scale = head_dim**-0.5
      mesh_args = torch.meshgrid.__kwdefaults__
  
      if len(self.window_size) == 3:
          self.relative_position_bias_table = nn.Parameter(
              torch.zeros(
                  (2 * self.window_size[0] - 1)
                  * (2 * self.window_size[1] - 1)
                  * (2 * self.window_size[2] - 1),
                  num_heads,
              )
          )
          coords_d = torch.arange(self.window_size[0])
          coords_h = torch.arange(self.window_size[1])
          coords_w = torch.arange(self.window_size[2])
          if mesh_args is not None:
              coords = torch.stack(
                  torch.meshgrid(
                      coords_d, coords_h, coords_w, indexing="ij"
                  )
              )
          else:
              coords = torch.stack(
                  torch.meshgrid(coords_d, coords_h, coords_w)
              )
          coords_flatten = torch.flatten(coords, 1)
          relative_coords = (
              coords_flatten[:, :, None] - coords_flatten[:, None, :]
          )
          relative_coords = relative_coords.permute(
              1, 2, 0
          ).contiguous()
          relative_coords[:, :, 0] += self.window_size[0] - 1
          relative_coords[:, :, 1] += self.window_size[1] - 1
          relative_coords[:, :, 2] += self.window_size[2] - 1
          relative_coords[:, :, 0] *= (2 * self.window_size[1] - 1) * (
              2 * self.window_size[2] - 1
          )
          relative_coords[:, :, 1] *= 2 * self.window_size[2] - 1
      elif len(self.window_size) == 2:
          self.relative_position_bias_table = nn.Parameter(
              torch.zeros(
                  (2 * window_size[0] - 1) * (2 * window_size[1] - 1),
                  num_heads,
              )
          )
          coords_h = torch.arange(self.window_size[0])
          coords_w = torch.arange(self.window_size[1])
          if mesh_args is not None:
              coords = torch.stack(
                  torch.meshgrid(coords_h, coords_w, indexing="ij")
              )
          else:
              coords = torch.stack(torch.meshgrid(coords_h, coords_w))
          coords_flatten = torch.flatten(coords, 1)
          relative_coords = (
              coords_flatten[:, :, None] - coords_flatten[:, None, :]
          )
          relative_coords = relative_coords.permute(
              1, 2, 0
          ).contiguous()
          relative_coords[:, :, 0] += self.window_size[0] - 1
          relative_coords[:, :, 1] += self.window_size[1] - 1
          relative_coords[:, :, 0] *= 2 * self.window_size[1] - 1
  
      relative_position_index = relative_coords.sum(-1)
      self.register_buffer(
          "relative_position_index", relative_position_index
      )
      self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
      self.attn_drop = nn.Dropout(attn_drop)
      self.proj = nn.Linear(dim, dim)
      self.proj_drop = nn.Dropout(proj_drop)
      trunc_normal_(self.relative_position_bias_table, std=0.02)
      self.softmax = nn.Softmax(dim=-1)
  
imports: from __future__ import annotations
  import itertools
  from collections.abc import Sequence
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  import torch.utils.checkpoint
  from torch.nn import LayerNorm
  from typing_extensions import Final
  from monai.networks.blocks import MLPBlock
  from monai.networks.blocks import PatchEmbed, UnetOutBlock, UnetrBasicBlock, UnetrUpBlock
  from monai.networks.layers import DropPath, trunc_normal_
  from monai.utils import ensure_tuple_rep, look_up_option, optional_import
  from monai.utils.deprecate_utils import deprecated_arg
label_desc: Use image node_id 1 to create a new WindowAttention object from inherited base classes: nn with example: obj = WindowAttention(dim, num_heads, window_size, qkv_bias, attn_drop, proj_drop)