Skip to content

MLA matrix-absorbed decode path with compressed-latent KV cache for DeepSeek-family models #907

Closed

Description

@inureyes

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:

  1. 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 beside KVCache in src/lib/mlxcel-core/src/cache.rs, or an MlaKVCache in the family code).
    • At load time, fold W_UK into the query path and W_UV into the output path for the decode graph (weight-level transform at load, aligned with how src/models/sanitize.rs does load-time repacks; do not mutate checkpoints on disk).
    • Decode attention becomes two dot_general-style contractions plus softmax over kv_len with 1 latent head and num_heads queries (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).
  2. 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.
  3. 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) -> 576 per token); demonstrate the increased max servable context under a fixed memory budget with mlxcel 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=0 restores the current path byte-identically; full cargo test -p mlxcel-core passes.

References

  • src/models/deepseek_v2.rs, src/models/deepseek_v3.rs, src/models/deepseek_v32.rs
  • src/models/sanitize.rs
  • src/lib/mlxcel-core/src/cache.rs

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


Part of epic #909.

Activity

  1. added
    status:ready
    type:performance
    priority:low
    area:models
    area:core
    on Jul 24, 2026
  2. added
    status:in-progress
    and removed
    status:ready
    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 2, 2026
  5. added a commit that references this issue on Aug 20, 2026
    a93d365
  6. self-assigned this
    on Aug 31, 2026
  7. added 3 commits that reference this issue on Oct 27, 2026
    8fb56d6
    74340f7
    0ea24e2
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:lowstatus:donetype:performance

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions