Scott/Codex commited on
Commit ·
3c513bc
1
Parent(s): c3d5043
Add improved sublinear anchor coverage
Browse files- .gitignore +2 -0
- README.md +2 -0
- nB300_agillm4_vram_dblock.py +7 -1
- sublinear_improved.py +54 -0
- sublinear_improved_snippet.py +35 -0
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
*.py[cod]
|
README.md
CHANGED
|
@@ -83,4 +83,6 @@ and a tighter structured-sublinear attention profile (`window=128`, `stride=128`
|
|
| 83 |
an ETA around 326 days, under the 1y+90d target, while keeping ctx=1280, B=2,
|
| 84 |
DiffusionBlocks, gradient-checkpointed blocks, tied heads, and structured masks.
|
| 85 |
|
|
|
|
|
|
|
| 86 |
License: Apache-2.0 (matching the upstream method).
|
|
|
|
| 83 |
an ETA around 326 days, under the 1y+90d target, while keeping ctx=1280, B=2,
|
| 84 |
DiffusionBlocks, gradient-checkpointed blocks, tied heads, and structured masks.
|
| 85 |
|
| 86 |
+
Sublinear coverage update 2026-05-29: the saved AGILLM-4 trainer snapshot now matches the live run's improved sparse global memory. When the strided anchor list is larger than `--sublinear_max_anchors`, it keeps anchors evenly spread across the whole sequence instead of only the most recent anchors, and it always includes a small set of first-token attention sinks. At the live 128/128/128 profile this preserves full-span coverage for 32k-style contexts instead of going blind to the deep past after the 16k anchor horizon, with essentially the same key budget. See `sublinear_improved_snippet.py` for the minimal drop-in block and `sublinear_improved.py` for the coverage demo/standalone selector.
|
| 87 |
+
|
| 88 |
License: Apache-2.0 (matching the upstream method).
|
nB300_agillm4_vram_dblock.py
CHANGED
|
@@ -1417,9 +1417,15 @@ class TuneableAttentionMHA(nn.Module):
|
|
| 1417 |
dtype=torch.long,
|
| 1418 |
)
|
| 1419 |
if anchors.numel() > self.sublinear_max_anchors:
|
| 1420 |
-
|
|
|
|
|
|
|
| 1421 |
else:
|
| 1422 |
anchors = torch.empty(0, device=device, dtype=torch.long)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1423 |
|
| 1424 |
offsets = torch.arange(
|
| 1425 |
-self.sublinear_window,
|
|
|
|
| 1417 |
dtype=torch.long,
|
| 1418 |
)
|
| 1419 |
if anchors.numel() > self.sublinear_max_anchors:
|
| 1420 |
+
# even-coverage: span the WHOLE past at fixed budget (not just recent tail)
|
| 1421 |
+
_sel = torch.linspace(0, anchors.numel() - 1, self.sublinear_max_anchors, device=device).round().long().unique()
|
| 1422 |
+
anchors = anchors[_sel]
|
| 1423 |
else:
|
| 1424 |
anchors = torch.empty(0, device=device, dtype=torch.long)
|
| 1425 |
+
# attention sinks: always keep the first few tokens (StreamingLLM)
|
| 1426 |
+
_sink = int(getattr(self, "sublinear_sinks", 4))
|
| 1427 |
+
if _sink > 0 and k_len > 0:
|
| 1428 |
+
anchors = torch.cat([torch.arange(min(_sink, k_len), device=device, dtype=torch.long), anchors]).unique()
|
| 1429 |
|
| 1430 |
offsets = torch.arange(
|
| 1431 |
-self.sublinear_window,
|
sublinear_improved.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Improved sublinear-attention anchor selection for AGILLM-4.
|
| 2 |
+
|
| 3 |
+
AGILLM-4's `sublinear` attention = local sliding window + strided "landmark"
|
| 4 |
+
anchors. The original capped the anchor set with `anchors[-max_anchors:]`, which
|
| 5 |
+
DROPS the entire deep past once N > max_anchors*stride (the trainer goes blind to
|
| 6 |
+
everything older than the recent tail). This patch keeps whole-sequence coverage at
|
| 7 |
+
the SAME key budget, plus StreamingLLM-style attention sinks.
|
| 8 |
+
|
| 9 |
+
Drop-in replacement for the anchor-cap block inside MHA._sublinear_attention.
|
| 10 |
+
"""
|
| 11 |
+
import torch
|
| 12 |
+
|
| 13 |
+
def select_anchors(k_len, stride, max_anchors, sinks, device):
|
| 14 |
+
"""Even-coverage strided landmarks over the FULL past + attention sinks."""
|
| 15 |
+
start = stride - 1
|
| 16 |
+
if stride > 0 and max_anchors > 0 and start < k_len:
|
| 17 |
+
anchors = torch.arange(start, k_len, stride, device=device, dtype=torch.long)
|
| 18 |
+
if anchors.numel() > max_anchors:
|
| 19 |
+
# even-coverage subsample across the whole sequence (NOT the recent tail)
|
| 20 |
+
sel = torch.linspace(0, anchors.numel() - 1, max_anchors, device=device).round().long().unique()
|
| 21 |
+
anchors = anchors[sel]
|
| 22 |
+
else:
|
| 23 |
+
anchors = torch.empty(0, device=device, dtype=torch.long)
|
| 24 |
+
if sinks > 0 and k_len > 0: # always keep the first few tokens
|
| 25 |
+
anchors = torch.cat([torch.arange(min(sinks, k_len), device=device, dtype=torch.long), anchors]).unique()
|
| 26 |
+
return anchors
|
| 27 |
+
|
| 28 |
+
# --- exact patch applied to nB300_agillm4.py MHA._sublinear_attention ---
|
| 29 |
+
PATCH = '''
|
| 30 |
+
# replace:
|
| 31 |
+
# if anchors.numel() > self.sublinear_max_anchors:
|
| 32 |
+
# anchors = anchors[-self.sublinear_max_anchors :]
|
| 33 |
+
# with:
|
| 34 |
+
if anchors.numel() > self.sublinear_max_anchors:
|
| 35 |
+
_sel = torch.linspace(0, anchors.numel() - 1, self.sublinear_max_anchors, device=device).round().long().unique()
|
| 36 |
+
anchors = anchors[_sel]
|
| 37 |
+
# ... and after the else branch add:
|
| 38 |
+
_sink = int(getattr(self, "sublinear_sinks", 4))
|
| 39 |
+
if _sink > 0 and k_len > 0:
|
| 40 |
+
anchors = torch.cat([torch.arange(min(_sink, k_len), device=device, dtype=torch.long), anchors]).unique()
|
| 41 |
+
'''
|
| 42 |
+
|
| 43 |
+
if __name__ == "__main__":
|
| 44 |
+
# Structural coverage demo at the live config, N beyond max_anchors*stride.
|
| 45 |
+
N, W, stride, maxA, sinks = 32768, 128, 128, 128, 4
|
| 46 |
+
i = N - 1
|
| 47 |
+
loc = set(range(i - W, i + 1))
|
| 48 |
+
allA = list(range(stride - 1, i + 1, stride))
|
| 49 |
+
OLD = sorted(loc | set(allA[-maxA:]))
|
| 50 |
+
sel = torch.linspace(0, len(allA) - 1, maxA).round().long().tolist()
|
| 51 |
+
NEW = sorted(loc | {allA[s] for s in sel} | set(range(sinks)))
|
| 52 |
+
print(f"N={N} (cap bites: {len(allA)} anchors > {maxA})")
|
| 53 |
+
print(f"OLD covers {min(OLD)}..{max(OLD)} -> blind to 0..{min(OLD)-1}; first-half keys={sum(x<N//2 for x in OLD)}")
|
| 54 |
+
print(f"NEW covers {min(NEW)}..{max(NEW)} -> full span; first-half keys={sum(x<N//2 for x in NEW)}")
|
sublinear_improved_snippet.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Improved AGILLM-4 sublinear attention anchor selection.
|
| 2 |
+
|
| 3 |
+
Drop this block into `_sublinear_attention` in place of the recent-tail anchor
|
| 4 |
+
selection. It keeps the same local-window + anchor key budget shape, but avoids
|
| 5 |
+
losing the deep past once `num_anchors > sublinear_max_anchors`.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
anchor_start = self.sublinear_stride - 1
|
| 9 |
+
if self.sublinear_stride > 0 and self.sublinear_max_anchors > 0 and anchor_start < k_len:
|
| 10 |
+
anchors = torch.arange(
|
| 11 |
+
anchor_start,
|
| 12 |
+
k_len,
|
| 13 |
+
self.sublinear_stride,
|
| 14 |
+
device=device,
|
| 15 |
+
dtype=torch.long,
|
| 16 |
+
)
|
| 17 |
+
if anchors.numel() > self.sublinear_max_anchors:
|
| 18 |
+
# Span the whole sequence instead of keeping only the recent tail.
|
| 19 |
+
sel = torch.linspace(
|
| 20 |
+
0,
|
| 21 |
+
anchors.numel() - 1,
|
| 22 |
+
self.sublinear_max_anchors,
|
| 23 |
+
device=device,
|
| 24 |
+
).round().long().unique()
|
| 25 |
+
anchors = anchors[sel]
|
| 26 |
+
else:
|
| 27 |
+
anchors = torch.empty(0, device=device, dtype=torch.long)
|
| 28 |
+
|
| 29 |
+
# StreamingLLM-style attention sinks: preserve the first tokens as stable global memory.
|
| 30 |
+
sink = int(getattr(self, "sublinear_sinks", 4))
|
| 31 |
+
if sink > 0 and k_len > 0:
|
| 32 |
+
anchors = torch.cat([
|
| 33 |
+
torch.arange(min(sink, k_len), device=device, dtype=torch.long),
|
| 34 |
+
anchors,
|
| 35 |
+
]).unique()
|