Repository navigation
perf(models): fold the MLA rope score into SDPA on multi-token steps - #3083
Merged
Merged
Conversation
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
marked this pull request as ready for review
November 15, 2026 12:12
This was referenced Nov 15, 2026
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.
This was referenced Nov 15, 2026
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
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
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_attentionconcatenates[q_nope | q_pe]and[k | k_pe]along the head dim, as mlx-lm'sdeepseek_v2.pydoes, 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 sharedabsorbed_attentionuses the same call for a multi-token step (DeepSeek V2 withMLXCEL_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)
MLXCEL_MLA_ABSORBED=1The Kimi-VL single-pass output diverges at a near-tie phrasing in the first reasoning sentence. Here is the teacher-forced
logit_traceof 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-coremla::(53, including 5 newprefill_testsagainst 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)mlxcelmodels::deepseek_v3,deepseek_v32,deepseek_v2,youtu_vl_lm,mla_batchedcheck_env_var_docs.py,check_benchmark_raw_logs.py,make verify-kernel-dtype-keysCloses #3071