Repository navigation
docs(mla): keep DeepSeek V2's decompressed cache default by measurement - #3118
Merged
Merged
Conversation
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
force-pushed
the
update/issue-3102-mla-default-layout
branch
from
November 16, 2026 06:21
4d6ba7d to
e99dc83
Compare
inureyes
marked this pull request as ready for review
November 16, 2026 06:21
This was referenced Nov 16, 2026
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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=1stays opt-in. No model code changes.The traces run at prefill width 2048 (cold and behind 2048 cached) and decode width 1 (behind 1024 and 8192).
MLXCEL_TRACE_MLA_STATScounters confirm that the absorbed arm tookabsorbed_composedon 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.mdplusdecode.csv,prefill.csv,logit_trace.csv.examples/logit_trace.rs: theMLXCEL_TRACE_BATCH_ROWSarm returned beforeMLXCEL_TRACE_MLA_STATSwas read, so width-one decode traces printed no path counters. Both arms now print them.No other family shares the choice: only
deepseek_v2reads the flag, V3 and V3.2 always absorb, andminicpm3anddots1have no absorbed path.Test plan
cargo fmt --check,cargo clippy -p mlxcel --example logit_trace --features metal,accelerate -- -D warningscheck_env_var_docs.py,check_benchmark_raw_logs.py,make verify-kernel-dtype-keysutils::tests::create_causal_mask_roster_count_matches_repologit_tracebatched arm prints# mla_paths(counters quoted above)Closes #3102