Repository navigation
perf(mla): quantized folds and a fused kernel for absorbed decode - #3129
Merged
Merged
Conversation
Absorbed MLA decode carried a fixed per-step cost that made DeepSeek V2's latent layout 10% slower than the decompressed one at short context. Profiling (issue comment) split it between the dense f16 W_UK/W_UV folds and the unfused 512-wide attention with a rope-score mask. A quantized kv_b_proj now folds without dequantizing: its packed rows are grouped along the rank axis, so both contractions run as batched quantized_matmul on the checkpoint's own planes (30 MiB instead of 113 MiB on V2-Lite). Unmasked single-query absorbed attention runs on a new Metal kernel (two key halves plus a merge) for windows up to 8192 keys, used by DeepSeek V2, the V3 single-token path and the shared per-row latent decode. MLXCEL_MLA_FUSED_DECODE=0 restores the composed path; a new absorbed_fused stats path records which one ran. Refs #3121
Re-measured deepseek-v2-lite on a quiet M5 Max after the quantized fold and fused kernel. Absorbed decode at pp512/tg128 is 201.4 tok/s against 205.5 decompressed (2.0% behind, from 11%), 6.2% ahead at 2048 tokens and 56% ahead at 16384. The teacher-forced trace shows 0 decided-position disagreements at decode width 1 behind 1024, 4096 and 8192 tokens, with the stats counters confirming the fused path ran. docs/mla-absorbed-decode.md now states the quantized fold and that both default-switch conditions hold; the switch itself is left for a separate change. Refs #3121
inureyes
marked this pull request as ready for review
November 16, 2026 09:21
This was referenced Nov 16, 2026
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.
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_UVfolds (0.36 ms), a rope-score mask (0.17 ms), and an unfused, latency-bound 512-wide attention block (0.93 ms).kv_b_projkeeps its packed planes per head asQuantizedMultiLinear, soabsorb,unabsorbandexpandrun as batchedquantized_matmulon 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.MLXCEL_MLA_ABSORBED=1, the DeepSeek V3 single-token path, andmla_batched::mla_latent_decode_rows(V3, V3.2, GLM-4.7-Flash, Kimi Linear, Kimi K3).MLXCEL_MLA_FUSED_DECODE=0restores the composed path, and a newabsorbed_fusedstats 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:
Teacher-forced trace at decode width 1, with decompressed as the reference:
absorbed_fusedon the fused arms.Both of the conditions
docs/mla-absorbed-decode.mdsets for switching DeepSeek V2's default to absorbed now hold. This PR does not switch it.Not covered
Tests
mla::fused_decode_testscheck the kernel against the host f64 reference, and they fail on Apple Silicon if the kernel is not live.absorb_tests.mla::(57 tests) and the DeepSeek, GLM and Kimi model tests (383) pass.mlxcel-coreandmlxcellib suites and workspace clippy were run before merge.Details:
docs/benchmark_results/mla-absorbed-decode-cost-m5max-2026-11-16.md.Closes #3121