Commit 4f0b5bc
committed
feat(v4): ROI 8 NSA + ROI 9 CSA+HCA — real implementations (no more SDPA fallback)
Two new modules replace the dense-SDPA stubs with the actual sparse
attention patterns from the V4 papers:
ROI 8 — cppmega_v4.nn.nsa_v4.NativeSparseAttentionV4 (arxiv 2502.11089)
Three real branches + learned softmax-gated mixture:
- Compress: mean-pool K/V into coarse blocks (compress_block_size);
query attends to all coarse blocks with causal-in-blocks mask.
Returns block_scores for the Select branch.
- Select: per (B,H,S_q) top-k coarse blocks by Compress-score; build
a [B,H,S_q,S_kv] sparse mask covering selected blocks' token spans
AND enforcing causality; SDPA with that mask.
- Sliding: causal windowed attention over the last sliding_window
tokens.
Gate is a tiny Linear(d, 3) softmaxed per token — at zero-init all
three branches contribute equally.
ROI 9 — cppmega_v4.nn.csa_hca_v4.CSAHCAHybridV4 (arxiv 2512.24880)
Real m-token KV compression at two ratios with shared q/k/v
projections (DSV4 weight-sharing pattern):
- CSA: K/V mean-pooled every m_csa tokens → ⌈S/m_csa⌉ super-tokens.
- HCA: same with m_hca >> m_csa for very-long-ctx global summary.
Causal-in-supertokens mask. Optional select_indices argument plugs
in Lightning Indexer top-k filtering when caller wires it up.
Outputs of the two branches are softmax-gated per token via
Linear(d, 2).
Both modules are drop-in replacements for the existing
``sparse_attention_v4.NativeSparseAttention`` / ``CsaHcaHybridAttention``
scaffolds (same (B, S, H) → (B, S, H) signature). Old modules retained
for backward compat; new ones are imported under V4-suffixed names.
Where pure MLX hits its limit:
- The Select branch materializes the full [S_q, S_kv] sparse mask
instead of running a block-sparse kernel. A Metal/TileLang fused
block-sparse SDPA could replace this without changing the API
(tracked: cppmega_v4._tilelang.nsa_path_c.py).
- The Compress branch recomputes the mean-pool each forward; a
streaming/segment-tree cache amortizes this on long contexts
(tracked: ROI 8.B).
Tests (22 new): NSA config validation, per-branch primitives
(causal-in-blocks identity, top-k respects causality, sliding window=1
returns v), module shapes, gate balance at init, short-sequence
degeneration; CSA+HCA config validation, _compress_kv mean-pool,
_compressed_attention causal-in-super, select_indices filter, module
shape, short-sequence handling.
v4 suite: 254 passed / 2 skipped.1 parent 98ef7b2 commit 4f0b5bc
4 files changed
Lines changed: 718 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
0 commit comments