Skip to content

docs(mla): keep DeepSeek V2's decompressed cache default by measurement - #3118

Merged
inureyes merged 2 commits into
mainfrom
update/issue-3102-mla-default-layout
Nov 16, 2026
Merged

inureyes merged 2 commits into
mainfrom
update/issue-3102-mla-default-layout

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

The issue gives the rule: the latent MLA cache becomes DeepSeek V2's default only if absorbed decode holds throughput within a few percent and the teacher-forced trace shows no material disagreement. Measured on deepseek-v2-lite-chat-4bit-mlx (M5 Max), the trace condition holds and the throughput condition does not, so the default stays decompressed and MLXCEL_MLA_ABSORBED=1 stays opt-in. No model code changes.

measurement decompressed (default) absorbed
decode pp512/tg128, median tok/s (5 samples each) 200.6 180.7 (-9.9%)
decode behind 2048-token prompt, tg128 168.1 161.9 (-3.7%)
decode behind 16384-token prompt, tg128 70.9 111.2 (+57%)
peak at 28,705 tokens, 2048-token pieces 20.20 GB 13.52 GB
peak at 28,705 tokens, single pass 48.55 GB 41.02 GB
trace, decided positions that disagree (gap >= 2) reference 0 of 3,653

The traces run at prefill width 2048 (cold and behind 2048 cached) and decode width 1 (behind 1024 and 8192). MLXCEL_TRACE_MLA_STATS counters confirm that the absorbed arm took absorbed_composed on every traced decode step. Prefill time could not be compared because concurrent compiles on the shared host spread one cell from 59 to 95 s. Earlier quiet-host runs (#3084, #3091) found no prefill difference between the layouts. Greedy text differs between the arms only at a near-tie digit inside an answer both arms get wrong, and which arm gets which text swaps between chunk modes.

Changes

  • docs/mla-absorbed-decode.md: new "Default layout for DeepSeek V2" section with the numbers, the reason, and what would change the decision (the absorbed step's fixed per-step cost, about 0.55 ms at 512 context, which pays back only past about 3K tokens). The environment-variable row and CHANGELOG Known Issues entry point to it.
  • docs/benchmark_results/mla-v2-default-layout-m5max-2026-11-15.md plus decode.csv, prefill.csv, logit_trace.csv.
  • examples/logit_trace.rs: the MLXCEL_TRACE_BATCH_ROWS arm returned before MLXCEL_TRACE_MLA_STATS was read, so width-one decode traces printed no path counters. Both arms now print them.

No other family shares the choice: only deepseek_v2 reads the flag, V3 and V3.2 always absorb, and minicpm3 and dots1 have no absorbed path.

Test plan

  • cargo fmt --check, cargo clippy -p mlxcel --example logit_trace --features metal,accelerate -- -D warnings
  • check_env_var_docs.py, check_benchmark_raw_logs.py, make verify-kernel-dtype-keys
  • utils::tests::create_causal_mask_roster_count_matches_repo
  • The rebuilt logit_trace batched arm prints # mla_paths (counters quoted above)

Closes #3102

@inureyes inureyes added status:done Completed type:performance Performance improvements priority:medium Medium priority area:models Model architectures, weights, loading, metadata area:inference Generation, sampling, decoding (incl. speculative, DRY) labels Nov 16, 2026
Issue #3102 asked whether DeepSeek V2 should cache the MLA latent by default. On deepseek-v2-lite-chat-4bit-mlx (M5 Max) the absorbed layout lowers the 28.7K-token peak from 20.20 to 13.52 GB at 2048-token pieces (48.55 to 41.02 GB single pass), decodes 57% faster behind a 16K prompt, and its teacher-forced trace disagrees at 0 of 3,653 decided positions at prefill width 2048 and decode width 1. It decodes 9.9% slower at pp512/tg128 and 3.7% slower behind a 2K prompt, which fails the issue's short-context throughput condition, so the default stays decompressed and MLXCEL_MLA_ABSORBED=1 stays opt-in.

The numbers, the reason and what would change the decision are recorded in docs/mla-absorbed-decode.md, with every sample in a new benchmark result and CSV directory. No other family reads the flag.

examples/logit_trace now prints the MLA dispatch counters in its batched decode arm, which returned before the counters were read, so the width-one decode traces here could prove which path each arm took.

Closes #3102
@inureyes
inureyes force-pushed the update/issue-3102-mla-default-layout branch from 4d6ba7d to e99dc83 Compare November 16, 2026 06:21
@inureyes
inureyes marked this pull request as ready for review November 16, 2026 06:21
@inureyes
inureyes merged commit 3efb3fb into main Nov 16, 2026
14 checks passed
@inureyes
inureyes deleted the update/issue-3102-mla-default-layout branch November 16, 2026 06:22
inureyes added a commit that referenced this pull request Nov 16, 2026
)

Absorbed MLA decode carried a fixed per-step cost that kept DeepSeek V2's latent layout about 10% behind the decompressed one at short context (#3102, PR #3118). Profiling at 512 keys (posted on the issue) traced it to three costs: dense f16 `W_UK`/`W_UV` folds (0.36 ms), a rope-score mask (0.17 ms), and an unfused, latency-bound 512-wide attention block (0.93 ms).

- **Quantized fold.** A quantized `kv_b_proj` keeps its packed planes per head as `QuantizedMultiLinear`, so `absorb`, `unabsorb` and `expand` run as batched `quantized_matmul` on the same weights the decompressed path reads: 30.4 MiB instead of about 113 MiB on V2-Lite. An unquantized layer, an NVFP4 global scale, runtime LoRA terms or a mode mismatch keep the dense fold.
- **Fused Metal kernel.** Unmasked single-query absorbed attention is one partial pass over two key halves plus a merge, with f32 online softmax, for windows up to 8192 keys. It is used by DeepSeek V2 under `MLXCEL_MLA_ABSORBED=1`, the DeepSeek V3 single-token path, and `mla_batched::mla_latent_decode_rows` (V3, V3.2, GLM-4.7-Flash, Kimi Linear, Kimi K3). `MLXCEL_MLA_FUSED_DECODE=0` restores the composed path, and a new `absorbed_fused` stats path records which one ran.

## Measurements

deepseek-v2-lite-chat-4bit-mlx on a quiet M5 Max, GPU lock, 75 s cooldowns, decode tok/s:

| prompt | main absorbed | branch absorbed | branch fold only | decompressed |
|---|---|---|---|---|
| 512 (median of 4) | 182.6 | 201.4 | 187.8 | 205.5 |
| 2048 | 148-161 | 180.7 | | 170.1 |
| 16384 (over the kernel limit) | 113.5 | 114.4 | | 73.5 |

Teacher-forced trace at decode width 1, with decompressed as the reference:
- Fused shows 0 of 209 decided-position disagreements behind 1024 and 0 of 106 behind 4096, where all 256 positions agree.
- Behind 8192 the window is over the kernel limit and the composed path ran (0 of 78).
- The stats counters confirm `absorbed_fused` on the fused arms.

Both of the conditions `docs/mla-absorbed-decode.md` sets for switching DeepSeek V2's default to absorbed now hold. This PR does not switch it.

## Not covered

- Always-absorbed families were not run end to end on a checkpoint; their call sites are covered by unit tests.
- Peak memory was not re-measured; only the fold weights changed.
- An earlier sweep that overlapped another session's build was discarded.

## Tests

- New `mla::fused_decode_tests` check the kernel against the host f64 reference, and they fail on Apple Silicon if the kernel is not live.
- Quantized-fold cases were added to `absorb_tests`.
- `mla::` (57 tests) and the DeepSeek, GLM and Kimi model tests (383) pass.
- The full `mlxcel-core` and `mlxcel` lib suites and workspace clippy were run before merge.

Details: `docs/benchmark_results/mla-absorbed-decode-cost-m5max-2026-11-16.md`.

Closes #3121
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:inference Generation, sampling, decoding (incl. speculative, DRY) area:models Model architectures, weights, loading, metadata priority:medium Medium priority status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(models): decide DeepSeek V2's default MLA cache layout by measurement

1 participant