functional mask fix
Browse files- aoti_attention.py +10 -7
aoti_attention.py
CHANGED
|
@@ -99,13 +99,16 @@ def sparse_attention_functional(
|
|
| 99 |
k_pool = key_tiled.view(batch, heads, n_tiles, 64, dim).sum(dim=3, dtype=torch.float32) / tile_divisor
|
| 100 |
scores = torch.matmul(q_pool, k_pool.transpose(-2, -1)) / math.sqrt(dim)
|
| 101 |
|
| 102 |
-
# Prefix-exempt top-k mask, assembled from fresh tensors (functional, export-traceable)
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
| 109 |
mask = torch.cat(
|
| 110 |
[
|
| 111 |
torch.ones((batch, heads, num_prefix_tiles, n_tiles), dtype=torch.bool, device=query.device),
|
|
|
|
| 99 |
k_pool = key_tiled.view(batch, heads, n_tiles, 64, dim).sum(dim=3, dtype=torch.float32) / tile_divisor
|
| 100 |
scores = torch.matmul(q_pool, k_pool.transpose(-2, -1)) / math.sqrt(dim)
|
| 101 |
|
| 102 |
+
# Prefix-exempt top-k mask, assembled from fresh tensors (functional, export-traceable):
|
| 103 |
+
# video query rows keep the top-k VIDEO kv tiles, prefix kv tiles are always selected,
|
| 104 |
+
# prefix query rows are dense over everything.
|
| 105 |
+
video_kv_scores = scores[..., num_prefix_tiles:, num_prefix_tiles:] # [B, H, nq_video, kv_video]
|
| 106 |
+
indices = video_kv_scores.topk(topk, dim=-1, sorted=False).indices
|
| 107 |
+
mask_video_kv = torch.zeros_like(video_kv_scores, dtype=torch.bool).scatter(-1, indices, True)
|
| 108 |
+
prefix_cols = torch.ones(
|
| 109 |
+
(batch, heads, num_q_video, num_prefix_tiles), dtype=torch.bool, device=query.device
|
| 110 |
+
)
|
| 111 |
+
mask_video = torch.cat([prefix_cols, mask_video_kv], dim=-1) # [B, H, nq_video, n_tiles]
|
| 112 |
mask = torch.cat(
|
| 113 |
[
|
| 114 |
torch.ones((batch, heads, num_prefix_tiles, n_tiles), dtype=torch.bool, device=query.device),
|