Shifted Window Multihead Self-Attention Module. Args: embed_dims (int): Number of input channels. num_heads (int): Number of attention heads. window_size (int): The height and width of the window. shift_size (int, optional): The shift step of each window towards
| 123 | |
| 124 | |
| 125 | class ShiftWindowMSA(BaseModule): |
| 126 | """Shifted Window Multihead Self-Attention Module. |
| 127 | |
| 128 | Args: |
| 129 | embed_dims (int): Number of input channels. |
| 130 | num_heads (int): Number of attention heads. |
| 131 | window_size (int): The height and width of the window. |
| 132 | shift_size (int, optional): The shift step of each window towards |
| 133 | right-bottom. If zero, act as regular window-msa. Defaults to 0. |
| 134 | qkv_bias (bool, optional): If True, add a learnable bias to q, k, v. |
| 135 | Default: True |
| 136 | qk_scale (float | None, optional): Override default qk scale of |
| 137 | head_dim ** -0.5 if set. Defaults: None. |
| 138 | attn_drop_rate (float, optional): Dropout ratio of attention weight. |
| 139 | Defaults: 0. |
| 140 | proj_drop_rate (float, optional): Dropout ratio of output. |
| 141 | Defaults: 0. |
| 142 | dropout_layer (dict, optional): The dropout_layer used before output. |
| 143 | Defaults: dict(type='DropPath', drop_prob=0.). |
| 144 | init_cfg (dict, optional): The extra config for initialization. |
| 145 | Default: None. |
| 146 | """ |
| 147 | |
| 148 | def __init__(self, |
| 149 | embed_dims, |
| 150 | num_heads, |
| 151 | window_size, |
| 152 | shift_size=0, |
| 153 | qkv_bias=True, |
| 154 | qk_scale=None, |
| 155 | attn_drop_rate=0, |
| 156 | proj_drop_rate=0, |
| 157 | dropout_layer=dict(type='DropPath', drop_prob=0.), |
| 158 | init_cfg=None): |
| 159 | super().__init__(init_cfg=init_cfg) |
| 160 | |
| 161 | self.window_size = window_size |
| 162 | self.shift_size = shift_size |
| 163 | assert 0 <= self.shift_size < self.window_size |
| 164 | |
| 165 | self.w_msa = WindowMSA( |
| 166 | embed_dims=embed_dims, |
| 167 | num_heads=num_heads, |
| 168 | window_size=to_2tuple(window_size), |
| 169 | qkv_bias=qkv_bias, |
| 170 | qk_scale=qk_scale, |
| 171 | attn_drop_rate=attn_drop_rate, |
| 172 | proj_drop_rate=proj_drop_rate, |
| 173 | init_cfg=None) |
| 174 | |
| 175 | self.drop = build_dropout(dropout_layer) |
| 176 | |
| 177 | def forward(self, query, hw_shape): |
| 178 | B, L, C = query.shape |
| 179 | H, W = hw_shape |
| 180 | assert L == H * W, 'input feature has wrong size' |
| 181 | query = query.view(B, H, W, C) |
| 182 |