Skip to content

perf(mla): quantized folds and a fused kernel for absorbed decode - #3129

Merged
inureyes merged 4 commits into
mainfrom
perf/issue-3121-absorbed-mla-decode-cost
Nov 16, 2026
Merged

inureyes merged 4 commits into
mainfrom
perf/issue-3121-absorbed-mla-decode-cost

Conversation

@inureyes

Copy link
Copy Markdown
Member

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

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 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
@inureyes
inureyes marked this pull request as ready for review November 16, 2026 09:21
@inureyes
inureyes merged commit abbbe36 into main Nov 16, 2026
1 check passed
@inureyes
inureyes deleted the perf/issue-3121-absorbed-mla-decode-cost branch November 16, 2026 09:21
@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): remove the fixed per-step cost of absorbed MLA decode

1 participant