(window_size, window_shift, q, k, v, scale=None)
| 331 | |
| 332 | |
| 333 | def apply_window_attention(window_size, window_shift, q, k, v, scale=None): |
| 334 | # prep windows and masks |
| 335 | q_windows = shifted_window(window_size, window_shift, q) |
| 336 | k_windows = shifted_window(window_size, window_shift, k) |
| 337 | v_windows = shifted_window(window_size, window_shift, v) |
| 338 | b, heads, h, w, wh, ww, d_head = q_windows.shape |
| 339 | mask = make_shifted_window_masks(h, w, wh, ww, window_shift, device=q.device) |
| 340 | q_seqs = torch.reshape(q_windows, (b, heads, h, w, wh * ww, d_head)) |
| 341 | k_seqs = torch.reshape(k_windows, (b, heads, h, w, wh * ww, d_head)) |
| 342 | v_seqs = torch.reshape(v_windows, (b, heads, h, w, wh * ww, d_head)) |
| 343 | mask = torch.reshape(mask, (h, w, wh * ww, wh * ww)) |
| 344 | |
| 345 | # do the attention here |
| 346 | flops.op(flops.op_attention, q_seqs.shape, k_seqs.shape, v_seqs.shape) |
| 347 | qkv = F.scaled_dot_product_attention(q_seqs, k_seqs, v_seqs, mask, scale=scale) |
| 348 | |
| 349 | # unwindow |
| 350 | qkv = torch.reshape(qkv, (b, heads, h, w, wh, ww, d_head)) |
| 351 | return shifted_unwindow(window_shift, qkv) |
| 352 | |
| 353 | |
| 354 | # Transformer layers |
no test coverage detected