Skip to content

perf(models): fold the MLA rope score into SDPA on multi-token steps - #3083

Merged
inureyes merged 2 commits into
mainfrom
perf/issue-3071-mla-prefill-sdpa
Nov 15, 2026
Merged

inureyes merged 2 commits into
mainfrom
perf/issue-3071-mla-prefill-sdpa

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

DeepSeek V3-family (Kimi-VL, Kimi-K2, Youtu) and DeepSeek V3.2 multi-token MLA steps no longer build the dense [B, H, L, S] rope-score tensor. mla::rope_folded_attention concatenates [q_nope | q_pe] and [k | k_pe] along the head dim, as mlx-lm's deepseek_v2.py does, and applies MLX causal mode, the caller's mask, or V3.2's top-k mask (now [B, 1, L, S], shared by every head). MLX does not fuse a query width (192, or 576 absorbed) that differs from the value width (128 / 512), so the call takes the fallback that the #3049 gate query-chunks to 256 MiB. The shared absorbed_attention uses the same call for a multi-token step (DeepSeek V2 with MLXCEL_MLA_ABSORBED=1). Single-token decode keeps the rope-mask form, unchanged.

DeepSeek V2's default path already concatenated, and its attention is already chunked since #3049, so it does not move: main reproduces the issue's 27.98 / 54.44 GB with every attention call bounded. That remainder is something other than the score tensor and is not addressed here.

Measurements (M5 Max, 28K-token prompt, greedy 48 tokens, cooled, order reversed between reps)

model prefill peak main peak branch prefill main / branch (rep 2) output
kimi-vl-a3b-thinking-4bit (19723 tok) 2048 chunks 19.99 GB 14.43 GB 28.07 / 25.78 s identical
kimi-vl-a3b-thinking-4bit single pass 76.31 GB 32.03 GB 35.37 / 30.88 s differs
deepseek-v2-lite-chat-4bit-mlx (28705 tok) 2048 chunks 27.98 GB 27.98 GB 42.84 / 42.81 s identical
deepseek-v2-lite-chat-4bit-mlx single pass 54.32 GB 54.32 GB 52.70 / 52.85 s identical
deepseek-v2-lite, MLXCEL_MLA_ABSORBED=1 2048 chunks 15.13 GB 15.13 GB 42.73 / 42.64 s identical

The Kimi-VL single-pass output diverges at a near-tie phrasing in the first reasoning sentence. Here is the teacher-forced logit_trace of main vs branch with --decided 2. At width 2048 from a cold start, 1 of 2627 decided positions disagrees (0.038%, largest gap 2.18). At width 2048 behind 2048 context, 0 of 2731 do. 90 to 93% of all disagreements land on the reference's rank 2, and perplexity is 0.04 to 0.06% lower. Full tables: docs/benchmark_results/mla-prefill-rope-fold-m5max-2026-11-15.md.

Test plan

  • mlxcel-core mla:: (53, including 5 new prefill_tests against the host f64 MLA reference: causal mode behind a cache, array mask, f16, absorbed latent shape, and the chunked causal fallback on the folded latent shape)
  • mlxcel models::deepseek_v3, deepseek_v32, deepseek_v2, youtu_vl_lm, mla_batched
  • fmt, narrow clippy for both crates, check_env_var_docs.py, check_benchmark_raw_logs.py, make verify-kernel-dtype-keys
  • Real checkpoints above. No V3.2 checkpoint fits the host, so its path is covered by unit tests only.

Closes #3071

DeepSeek V3 (and Kimi-VL, Kimi-K2, Youtu through it) and DeepSeek V3.2 computed the rope half of the MLA score as `(q_pe * scale) @ k_peᵀ`, added the causal mask (promoting it to f32), and passed the dense `[B, H, L, S]` result to SDPA as an additive mask. It existed before SDPA ran, so the query-axis chunk gate could not bound it.

Multi-token steps now go through `mla::rope_folded_attention`, which concatenates `[q_nope | q_pe]` and `[k | k_pe]` along the head dim and applies MLX causal mode, the caller's mask, or V3.2's top-k mask (now `[B, 1, L, S]`). MLX does not fuse a query width that differs from the value width, so the call takes the fallback that the #3049 gate query-chunks. The shared absorbed attention takes the same call for a multi-token step. Single-token decode keeps the rope-mask form.

kimi-vl-a3b-thinking-4bit at 19.7K tokens on M5 Max: peak 19.3-20.0 to 14.4 GB at 2048 chunks and 76.3 to 32.0 GB single pass, prefill 8-24% faster. Teacher-forced trace: 1/2627 and 0/2731 decided positions disagree. DeepSeek V2-Lite is unchanged and byte-identical.

Closes #3071
@inureyes inureyes added status:done Completed type:performance Performance improvements priority:high High priority area:models Model architectures, weights, loading, metadata area:inference Generation, sampling, decoding (incl. speculative, DRY) platform:macos macOS (Apple Silicon) specific labels Nov 15, 2026
@inureyes
inureyes marked this pull request as ready for review November 15, 2026 12:12
@inureyes
inureyes merged commit cd6715c into main Nov 15, 2026
1 check passed
@inureyes
inureyes deleted the perf/issue-3071-mla-prefill-sdpa branch November 15, 2026 12:12
inureyes added a commit that referenced this pull request Nov 15, 2026
…3086)

#3083 moved DeepSeek V3's multi-token MLA attention to causal-mode SDPA, so `deepseek_v3.rs` no longer calls `create_causal_mask`. That made `utils::tests::create_causal_mask_roster_count_matches_repo` fail on main, because the doc comment states 51 caller files and the grep finds 50. No PR job runs the `mlxcel-core` lib suite, so CI did not catch it.

The count is now 50, and the MLA group names DeepSeekV2, which still calls it, in place of DeepSeekV3.

Validation: the roster test passes.
inureyes added a commit that referenced this pull request Nov 16, 2026
…3114)

## Summary

GLM4MoeLite (and its pipeline stage), KimiLinear's MLA layers, KimiK3 and LongcatFlashNgram run every multi-token step through `mla::rope_folded_attention` (#3083) with MLX's causal mode, in both the absorbed and materializing branches. They no longer build the f32 `[L, L + offset]` mask or the dense `[B, H, L, S]` rope-score tensor, and the Kimi models no longer build hand-made score and softmax tensors. A caller mask is still applied as given (the GLM4MoeLite MTP drafter keeps its own). Single-token decode is unchanged. The GLM4MoeLite stage executor passes no mask, like the single-process model. Its now-unused `execute_with_computed_mask_last_row` helper is removed.

## Measurements (M5 Max, main `00cd2d69` vs branch, two reps, order reversed)

| model | prefill | peak main | peak branch | prefill main | prefill branch | greedy output |
|---|---|---|---|---|---|---|
| glm-4.7-flash-4bit, 22754 tokens | 2048 chunks | 28.82 GB | 20.51 GB | 49.7 / 35.2 s | 12.6 / 12.3 s | identical |
| glm-4.7-flash-4bit | single pass | Metal GPU timeout (82.8 GB) | 24.11 GB | fails both reps | 11.2 / 15.8 s | main produces none |
| kimi-k3-2l-mla-synth, 2831 tokens | 2048 chunks | 41.4 / 41.5 GB | 39.69 GB | 0.74 / 0.47 s | 0.44 / 0.50 s | identical |
| kimi-k3-2l-mla-synth | single pass | 41.51 GB | 39.69 GB | 0.51 / 0.50 s | 0.43 / 0.47 s | identical |

The GLM-4.7-Flash teacher-forced logit trace (rope folding reorders the score sum) shows 2 / 3335 (0.060%) disagreement on decided positions at width 2048 cold, and 3 / 3360 (0.089%) at width 4096 behind 2048 context. 81 to 83% of disagreements land at the reference's rank 2, and perplexity moves +0.23% / +0.05%. The overall 6% top-1 rate is near-ties on bf16 logits where one ulp is 0.5. Details are in `docs/benchmark_results/mla-prefill-rope-fold-remaining-m5max-2026-11-15.md`.

Covered only by unit tests (no local checkpoint): KimiLinear and LongcatFlashNgram (`longcat_flash`, `longcat_flash_ngram`). The GLM4MoeLite pipeline stage is also covered only by unit tests. `kimi-k3-2l-mla-synth` is a two-layer truncation with degenerate text. It exercises KimiK3's MLA layer but not real-quality output.

## Test plan

- [x] New tests on tiny random weights compare the folded prefill with the unchanged single-token path for GLM4MoeLite (cold, warm absorbed, warm materializing, explicit mask), KimiLinear (cold, warm absorbed, explicit mask) and LongcatFlashNgram (both variants). For KimiK3 they compare against the unabsorbed reference (cold, warm absorbed). The GLM4MoeLite two-stage pipeline split is compared with the single-process model, and the last-row check runs on the stage.
- [x] `models::glm4_moe_lite*` (37), `models::kimi_linear` (13), `models::kimi_k3` (30), `models::longcat_flash_ngram` (14), `stage_executor::glm4` (2), `mlxcel-core` `mla::` (53) and `create_causal_mask_roster_count_matches_repo` (roster 21 to 17) all pass.
- [x] `cargo fmt --check`, narrow clippy for `mlxcel` and `mlxcel-core`, `check_env_var_docs.py`, `check_benchmark_raw_logs.py`, `make verify-kernel-dtype-keys`

Closes #3099
@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 platform:macos macOS (Apple Silicon) specific priority:high High priority status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(models): DeepSeek MLA prefill builds a dense [B, H, L, L+offset] score tensor per layer

1 participant