Repository navigation
perf(mla): absorb DeepSeek V2 decode by default where the fused kernel runs - #3136
Merged
Merged
Conversation
…l runs #3121 brought absorbed MLA decode to within 2% of the decompressed layout at pp512 and ahead from 2K tokens on, with no decided-position disagreement, so both of #3102's switch conditions hold on Metal. MLXCEL_MLA_ABSORBED becomes a tri-state (mla::AbsorbedMode). An on spelling forces absorbed on every backend and keeps the stderr fold report, and an off spelling forces the decompressed layout. Unset or unrecognized resolves to absorbed only where mla::fused_decode_enabled() holds (Metal port, MLXCEL_MLA_FUSED_DECODE not off). Other backends keep decompressed until #3133 measures them. Under the default the fold report is a tracing info line, and a decline still goes to stderr. The existing per-cache declines (non-FP16 KV modes, paged caches) are unchanged. The mlx-lm greedy parity test is now teacher-forced and tolerates a different argmax only within 0.0625 logits of the top. The fused kernel ties `.` and `,` at step 1, where the decompressed reference has them one f16 ULP apart, and in free-running mode that one tie changed every later token. Kimi-VL (deepseek_v3, always absorbed) was validated on the fused kernel: absorbed_fused covers every decode step, 0 decided disagreements, and decode is 7% faster. Results are in docs/benchmark_results/mla-absorbed-default-m5max-2026-11-16.md. Closes #3131
8 tasks done
inureyes
marked this pull request as ready for review
November 16, 2026 12:54
This was referenced Nov 16, 2026
inureyes
added a commit
that referenced
this pull request
Nov 16, 2026
…t out (#3137) ## Summary Since #3131 and #3136, DeepSeek V2 caches the MLA latent by default wherever the fused Metal kernel runs, but `kv_arch` still sized every `deepseek_v2` config as the decompressed per-head cache: 10240 bytes per token per layer on DeepSeek-V2-Lite against the 1152 actually cached (8.9x). The figure feeds the prompt-cache snapshot budget, `mlxcel inspect` / `--estimate-memory` and the KV cache advisor. This is the sizing item of #3133, done before the release so the new default and its estimates ship together. - `kv_arch` resolves the `deepseek_v2` layout with the loader's own decision: latent when `mla::absorbed_enabled()` holds and the KV cache is FP16 (`deepseek_v2_latent_layout`), decompressed under `MLXCEL_MLA_ABSORBED=0`, on a backend without the fused kernel, or with an int8 cache (`latent_layout_supports_mode` refuses it at runtime). The paged-cache decline cannot reach `deepseek_v2`, which does not opt into the paged decode backend. - The always-latent families in `MLA_LATENT_CACHE_FAMILIES` (GLM-4.7-Flash, Kimi Linear, Kimi K2, ...) were also sized per head; they now size the latent. - The advisor no longer recommends int8 KV for an absorbing DeepSeek V2: int8 turns absorption off and returns to the decompressed cache, about 4x the fp16 latent. Without absorption the int8 advice is unchanged. - CHANGELOG: the Known Issues entry that still said V2 keeps the decompressed cache by default now describes CUDA and ROCm only, and a Fixed entry covers the sizing. ## Validation - `execution::` and `server::prompt_cache::` lib tests pass on Metal (406), including new tests for the latent and decompressed sizes, the int8 decline, the layout rule, the process-level decision, the latent-pair families and the advisor rule. The old `mla_v2_uses_decompressed_per_head` becomes `mla_v2_uses_decompressed_per_head_without_absorption`. clippy `--lib --tests -D warnings` and fmt pass. - `mlxcel inspect` on the published DeepSeek-V2-Lite-Chat-4bit-mlx `config.json` at 32768 tokens: KV cache 8.44 GiB before, 972.0 MiB after (M3 Ultra, absorbed by default; 31104 instead of 276480 bytes per token), 8.44 GiB with `MLXCEL_MLA_ABSORBED=0`, 4.22 GiB (decompressed) with an int8 cache. Not done here: the CUDA/ROCm measurements in #3133, which decide whether the default (and with it the sizing) changes on those backends. Refs #3133
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
MLXCEL_MLA_ABSORBEDis a tri-state (mla::AbsorbedMode). On spellings force absorbed decode on every backend and off spellings force decompressed. Unset or unrecognized resolves to absorbed only wheremla::fused_decode_enabled()holds (Metal port,MLXCEL_MLA_FUSED_DECODEnot off), because the evidence for the switch is Metal plus fused kernel only. CUDA and ROCm keep decompressed by default until test(bench): measure the 2026-11 memory and MLA changes on CUDA and ROCm #3133 measures them. A unit test covers the resolution table.tracinginfo line. Explicit=1and any decline still print to stderr. The per-cache declines (non-FP16 KV modes, paged caches) are unchanged.tests/deepseek_v2_greedy_parity.rsis now teacher-forced and tolerates a different argmax only within 0.0625 logits of the top (at most 2 of 20 steps). The fused kernel scores.and,both at 29.094 at step 1, where the decompressed reference has 29.109 / 29.094 (one f16 ULP). Free-running, that one tie changed every later token. A bidirectional prefill (fix(models): deepseek_v2 generates degenerate output on a real checkpoint while mlx-lm is correct on the same weights #991) still misses by whole logits.docs/mla-absorbed-decode.md,docs/environment-variables.md, CHANGELOG, and new results indocs/benchmark_results/mla-absorbed-default-m5max-2026-11-16.md(CSV + md).Measurements (M5 Max, release build of this branch)
The host was shared with another unit's builds, so absolute rates run 10 to 18% below #3121's quiet sweep. Every GPU run held the lock and the arms were interleaved. Throughput is
mlxcel-bench-decodetg128, median tok/s.=0Teacher-forced trace (
examples/logit_trace,compare_logit_traces.py --decided 2, width-one decode,MLXCEL_TRACE_MLA_STATS=1):=0=0Every disagreement falls where the reference's top two were within 0.11 logits.
Test plan
cargo test -p mlxcel-core --lib mla::,-p mlxcel --lib deepseek(164 tests)tests/deepseek_v2_greedy_parity.rsandtests/deepseek_vl_v2_parity.rsunder the default,MLXCEL_MLA_ABSORBED=0,=1andMLXCEL_MLA_FUSED_DECODE=0mlxcel-coreandmlxcel(--lib --tests),cargo fmt --check,make verify-env-var-docsCloses #3131