Skip to content

Fused sparse-attention decode via page indirection for DSA and block-sparse models #904

Closed

Description

@inureyes

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 computes relu(q @ k^T) head-weighted scores plus causal mask and argpartition-selects the top index_topk KV 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.rs around lines 30-32). Dense fallback when kv_len <= index_topk.
  • Block-sparse (MiniMax-M3, phi3-small/phimoe): src/models/minimax_m3_indexer.rs (BlockSparseIndexer) builds an effective_mask that feeds the ordinary dense SDPA (minimax_m3_layers.rs lines ~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

  1. 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 = 1 in 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.
  2. 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).
  3. Fallbacks: kv_len <= index_topk dense fallback unchanged; any unsupported shape falls back to the existing gather/mask path; env kill switch MLXCEL_SPARSE_PAGED_ATTENTION=0.
  4. 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.rs
  • src/models/deepseek_v32.rs
  • src/models/minimax_m3_indexer.rs
  • src/models/minimax_m3_layers.rs

Line numbers are indicative as of current main; search for the named symbols.


Part of epic #909.

Activity

  1. added
    status:blocked
    type:performance
    priority:medium
    area:models
    area:core
    on Jul 24, 2026
  2. added
    status:in-progress
    and removed
    status:blocked
    on Aug 2, 2026
  3. added
    status:review
    and removed
    status:in-progress
    on Aug 2, 2026
  4. added
    status:done
    and removed
    status:review
    on Aug 3, 2026
  5. self-assigned this
    on Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:corearea:modelspriority:mediumstatus:donetype:performance

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions