MCPcopy Create free account
hub / github.com/Meshcapade/difflocks / apply_window_attention

Function apply_window_attention

k_diffusion/models/modules.py:333–351  ·  view source on GitHub ↗
(window_size, window_shift, q, k, v, scale=None)

Source from the content-addressed store, hash-verified

331
332
333def 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

Callers 1

forwardMethod · 0.85

Calls 4

shifted_windowFunction · 0.85
shifted_unwindowFunction · 0.85
opMethod · 0.80

Tested by

no test coverage detected