Repository navigation
Description
Background
mlxcel supports many MLA (multi-head latent attention) families: DeepSeek-V2/V3/V3.2 (src/models/deepseek_v2.rs with q_lora_rank/kv_lora_rank and the qk_rope_head_dim/qk_nope_head_dim split, lines ~19-107; deepseek_v3.rs, deepseek_v32.rs), MiniCPM3, dots1. The current implementation follows the upstream porting style: the compressed latent is up-projected to full per-head K/V, the decompressed keys/values are cached, and attention runs as dense SDPA. That forfeits MLA's main serving advantage: with matrix absorption, the KV cache holds only the compressed latent ckv (kv_lora_rank, typically 512) plus the shared rope stream kpe (typically 64) per token, one "head" total, instead of num_heads x (qk_head_dim + v_head_dim). For DeepSeek-V2-Lite-class models this is an order-of-magnitude KV memory reduction, which directly raises servable context length and batch size, and decode becomes bandwidth-proportional to 576 floats/token instead of the full per-head K/V.
The absorption identity: score contribution q_nope^T (W_UK c) = (W_UK^T q_nope)^T c, so folding W_UK into the query projection lets attention run directly against the latent c (= ckv). Likewise the output attn @ (W_UV c) becomes (attn @ c) @ W_UV (fold W_UV after attention). Scores are then q_nope_absorbed . ckv + q_pe . kpe (dims 512 + 64, i.e. each score is the sum of two dot products) and the attention output lives in the 512-dim latent space with num_heads query heads sharing the single latent "KV head". Established absorbed-decode implementations use a split (ckv, kpe) cache and, for long contexts, split-KV with partial-state merging plus persistent work-queue scheduling; Stage 2 below follows that shape.
Task
Staged, with a hard parity gate between stages:
- Stage 1: cache format + absorbed decode via composed MLX ops (no custom kernel).
- Add an MLA cache mode storing
(ckv, kpe)per token per layer (a new cache type besideKVCacheinsrc/lib/mlxcel-core/src/cache.rs, or anMlaKVCachein the family code). - At load time, fold
W_UKinto the query path andW_UVinto the output path for the decode graph (weight-level transform at load, aligned with howsrc/models/sanitize.rsdoes load-time repacks; do not mutate checkpoints on disk). - Decode attention becomes two
dot_general-style contractions plus softmax overkv_lenwith 1 latent head andnum_headsqueries (GQA-like shape: many q heads, one shared latent). Prefill may keep the current decompressed path initially (absorption is a decode win; prefill is compute-bound). - Gate per family behind
MLXCEL_MLA_ABSORBED=1(default off until Stage 1 parity passes on all supported families).
- Add an MLA cache mode storing
- Stage 2: fused MLA decode kernel (only after Stage 1 lands and its measurements justify it): a v2-style split-KV kernel over the
(ckv, kpe)cache reusing the Fused paged-attention decode v2: CSR page table, cross-CTA split-KV, and variable-length merge kernels #898 merge kernel for partials. CUDA first (GB10), Metal follow-up. This stage may be split into its own PR/issue if Stage 1 review prefers. - Interactions to check explicitly: the CUDA RMSNorm overlay for
kv_a_layernorm(fix(cuda): restore DeepSeek-V2 output by reverting the regressed RMSNorm kernel #829,patches/mlx/backend/cuda/rms_norm.cu) sits on this family's path; CUDA graph capture is disabled for DeepSeek-V2 on GB10 (fix(cuda): restore DeepSeek-V2 output by reverting the regressed RMSNorm kernel #829) so do not assume graph replay; speculative/MTP for DeepSeek (MTP drafters) must keep working against the new cache type or explicitly fall back to the decompressed path (document which).
Performance validation (mandatory)
- KV memory: bytes/token/layer before vs after on DeepSeek-V2-Lite (expect ~
num_kv_heads*(qk_head_dim+v_head_dim)->576per token); demonstrate the increased max servable context under a fixed memory budget withmlxcel inspect --max-tokens. - Decode throughput: tok/s on DeepSeek-V2-Lite (Metal and GB10) at context {4K, 16K, 32K}, batch {1, 4}, Stage 1 vs baseline; long-context is where the bandwidth reduction must show.
- Prefill: measure and report; Stage 1 must not regress prefill beyond noise (it does not touch prefill).
- Record in
docs/benchmark_results/mla-absorbed-decode-<hw>-<date>.md.
Regression guard (mandatory)
- Token parity: greedy decode on the repo's pinned DeepSeek oracles must match the decompressed baseline exactly, per family and per backend, before the flag defaults on. Absorption is mathematically exact; any drift indicates a bug or an accumulation-order issue and must be root-caused (small fp16 reassociation differences, if accepted, need the pinned-oracle sign-off process).
- All MLA-family model tests pass with the flag on and off; non-MLA families untouched.
- Speculative/MTP on DeepSeek: either passes with the new cache or falls back with a test proving the fallback engages; no silent breakage.
MLXCEL_MLA_ABSORBED=0restores the current path byte-identically; fullcargo test -p mlxcel-corepasses.
References
src/models/deepseek_v2.rs,src/models/deepseek_v3.rs,src/models/deepseek_v32.rssrc/models/sanitize.rssrc/lib/mlxcel-core/src/cache.rs
Line numbers are indicative as of current main; search for the named symbols.
Part of epic #909.
Activity
feat(models): route DeepSeek-V2 MLA decode through the absorbed laten…
docs: document absorbed MLA decode, its flags, and what it does not v…
docs(benchmarks): record MLA absorbed decode validation on M1 Ultra
feat(core): matrix-absorbed MLA decode over a compressed-latent KV cache
feat(models): route DeepSeek-V2 MLA decode through the absorbed laten…
docs: document absorbed MLA decode, its flags, and what it does not v…
docs(benchmarks): record MLA absorbed decode validation on M1 Ultra
perf(sampling): sorting-free top-p sampling with dual-pivot rejection…
perf(core): compute shared prompt prefixes once per decode batch (#1004)
feat(core): matrix-absorbed MLA decode over a compressed-latent KV ca…
feat(core): matrix-absorbed MLA decode over a compressed-latent KV ca…
perf(sampling): sorting-free top-p sampling with dual-pivot rejection…
perf(core): compute shared prompt prefixes once per decode batch (lab…