Repository navigation
Description
Blocked by #899 (fused v2 CSR kernel in the production decode path).
Background
mlxcel's sparse-attention families select which KV to attend to but then pay dense costs anyway:
- DSA (DeepSeek-V3.2 lightning indexer, shared with GLM-MoE DSA):
src/models/deepseek_v32_indexer.rs,indexer_top_indices()around lines 199-238 computesrelu(q @ k^T)head-weighted scores plus causal mask andargpartition-selects the topindex_topkKV positions. For decode (L == 1) the model then gathers the selected K/V into a dense contiguous copy and runs standard SDPA; longer prefills build a sparse additive mask over the full score matrix (deepseek_v32.rsaround lines 30-32). Dense fallback whenkv_len <= index_topk. - Block-sparse (MiniMax-M3, phi3-small/phimoe):
src/models/minimax_m3_indexer.rs(BlockSparseIndexer) builds aneffective_maskthat feeds the ordinary dense SDPA (minimax_m3_layers.rslines ~88-197). The masked-out blocks are still computed and then discarded.
Once a CSR paged kernel exists, no dedicated sparse kernel is needed: sparsity reduces to page indirection. Block-sparse (BSR) structure maps directly onto paged attention by treating each selected column block of width C as a KV page: the BSR indptr/indices become the paged kv_indptr/kv_indices with page_size = C, and the ordinary paged kernel runs unmodified over only the selected blocks. For token-granular sparsity, page_size degenerates to 1 and the kernel's page indirection becomes a per-token gather executed inside the attention kernel; no materialized gathered KV copy exists at any point.
With #899 in place, mlxcel's fused v2 kernel already consumes (indices, indptr, last_page_len). This issue feeds it sparse selections instead of full page tables.
Task
- DSA decode: replace the gather-then-SDPA flow in the DSA decode path with a CSR plan built from the indexer's selected token positions. Two sub-cases:
- If the selection is quantized to pool blocks (extend the indexer to score/select at block granularity, top-
ceil(index_topk/block_size)blocks), reuse the v2 kernel as-is with the pool's block size. Evaluate quality impact of block-granular selection on a pinned oracle before adopting. - Otherwise support
page_size = 1in the v2 kernel (per-token indices into the pool; the pool layout[num_blocks, block_size, n_kv_heads, head_dim]addresses any token as(block, entry)), i.e. vector-sparse gather inside the kernel. This keeps token-exact selection semantics.
Pick one as the landing path based on the quality/perf measurements; keep the other documented.
- If the selection is quantized to pool blocks (extend the indexer to score/select at block granularity, top-
- MiniMax-M3 / phi3-small decode: emit the indexer's per-query-block selected KV blocks as CSR pages for the v2 kernel instead of building
effective_mask. Prefill stays on the current mask path (out of scope; note follow-up). - Fallbacks:
kv_len <= index_topkdense fallback unchanged; any unsupported shape falls back to the existing gather/mask path; env kill switchMLXCEL_SPARSE_PAGED_ATTENTION=0. - Memory: remove the materialized gathered K/V copy on the DSA decode path and account the saving.
Performance validation (mandatory)
- Long-context decode benchmark on a DSA model (DeepSeek-V3.2 family) at context {16K, 32K}: decode tok/s and per-step peak transient memory, before/after; same for MiniMax-M3 at its documented sparse configuration.
- Required outcomes: decode tok/s improves at 16K+ context (the gather copy plus dense SDPA over gathered KV is replaced by one fused pass); transient memory per step drops by the gathered-KV size; short-context (< index_topk) behavior unchanged.
- Record in
docs/benchmark_results/sparse-paged-decode-<hw>-<date>.md, Metal and (when available) GB10.
Regression guard (mandatory)
- Token parity: greedy decode on pinned prompts for DeepSeek-V3.2 and MiniMax-M3 must match the current gather/mask implementation exactly when using token-exact selection (page_size = 1 path). If the block-granular selection path is chosen instead, quality deltas must be measured on the repo's pinned oracles and explicitly accepted in the PR before merge.
- Dense fallback (
kv_len <= index_topk) byte-identical; kill switch restores the old path. - Full model test suites for the affected families pass; non-sparse families show zero behavior/perf change (spot-check one dense model within noise).
References
src/models/deepseek_v32_indexer.rssrc/models/deepseek_v32.rssrc/models/minimax_m3_indexer.rssrc/models/minimax_m3_layers.rs
Line numbers are indicative as of current main; search for the named symbols.
Part of epic #909.
Activity
feat(minimax-m3): route block-sparse decode through page indirection
perf(core): gate fused sparse decode on sparsity, not just launch size
feat(core): sparse attention as page indirection over a contiguous cache
feat(minimax-m3): route block-sparse decode through page indirection
perf(core): gate fused sparse decode on sparsity, not just launch size
docs(benchmarks): record sparse paged decode validation on M1 Ultra
perf(core): fused sparse-attention decode via page indirection (#1005)
perf(core): fused sparse-attention decode via page indirection (lablu…