Skip to content

perf(models): fold the remaining hand-masked MLA prefills into SDPA - #3114

Merged
inureyes merged 2 commits into
mainfrom
update/issue-3099-mla-prefill-sdpa
Nov 16, 2026
Merged

inureyes merged 2 commits into
mainfrom
update/issue-3099-mla-prefill-sdpa

Conversation

@inureyes

Copy link
Copy Markdown
Member

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

  • 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.
  • 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.
  • 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

GLM4MoeLite (and its pipeline stage), KimiLinear's MLA layers, KimiK3 and LongcatFlashNgram built an f32 [L, L + offset] causal mask per forward and added it to a dense [B, H, L, S] rope-score tensor passed to SDPA as an additive mask, or to hand-built score and softmax tensors. Their multi-token steps now call mla::rope_folded_attention with MLX's causal mode in both the absorbed and materializing branches; a caller mask is still applied as given, and single-token decode is unchanged. The GLM4MoeLite stage executor passes no mask, matching the single-process model, and the now-unused execute_with_computed_mask_last_row helper is removed.

On GLM-4.7-Flash at 22.7K tokens (M5 Max) 2048-chunk prefill drops from 35-50 s to 12.3 s and peak from 28.8 to 20.5 GB with identical greedy output; the single pass, which hit a Metal GPU timeout at 82.8 GB on main, completes at 24.1 GB. Teacher-forced trace: 0.06-0.09% disagreement on decided positions.

New unit tests compare the folded prefill against the unchanged single-token path (or KimiK3's unabsorbed reference) on tiny random weights for every family, and a two-stage GLM4MoeLite split against the single-process model. The create_causal_mask roster drops from 21 to 17 files.

Closes #3099
@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 05:49
@inureyes
inureyes merged commit e544c33 into main Nov 16, 2026
@inureyes
inureyes deleted the update/issue-3099-mla-prefill-sdpa branch November 16, 2026 05:49
inureyes added a commit that referenced this pull request Nov 18, 2026
…ts (#3152)

* update(mla): absorb DeepSeek V2 by default on ROCm, add gfx1151 results

Issue #3133 measured the 2026-11 MLA and prefill-memory changes on CUDA (#3149) and left ROCm unmeasured for lack of a host. On a gfx1151 host the composed absorbed path decodes 3.4% behind the decompressed layout at 512 tokens (median of five runs per arm), 17% ahead behind 2K and 65% ahead behind 16K, where the phase peak drops from 16.06 to 12.39 GB, with 0 decided disagreements in the teacher-forced trace, so #3102's switch rule holds there too.

`mla::default_absorbs_on` now makes the unset `MLXCEL_MLA_ABSORBED` absorb on ROCm as well; the CPU backend and Metal with the fused kernel off keep the decompressed default. `kv_arch` already reads `mla::absorbed_enabled()`, so V2-Lite at 32K now estimates 972.0 MiB of KV instead of 8.44 GiB.

Adds the gfx1151 decode, trace and prefill-peak results: GLM-4.7-Flash fits a 28K single pass in 21.13 GB where the pre-#3114 tree asks for 62.72 GB, prefills 3.0x faster with 2048-token chunks, and gpt-oss-20b stays unchanged as designed. Llama 4 Scout did not fit on the host and is not recorded.

* docs(mla): record ROCm results provenance and qualify the sizing claim

The gfx1151 record is the evidence behind the new ROCm default, so its provenance and its weakest cell have to stand on their own. The CSVs now carry the measured commit (`c27b86f4`) like the CUDA rows, section 7 says which binary produced the `inspect` rows, and the record lists the MLX pin and ROCm overlay commit.

Section 4 notes the 26.43 GB gpt-oss branch run instead of presenting both arms as 26.21 GB, the GLM failure is recorded as an allocation rather than an inferred mask array, and the Gate section replaces `PENDING` with what actually ran: the two narrow suites pass and the full local `make verify-rocm-held` was stopped by the memory guard, so hosted CI carries the rest.

The docs no longer say the composed path is level with or ahead at 512 tokens on ROCm; the measured median is 3.4% behind there, and the claim is now within a few percent at 512 and ahead from 2K. The top-level layout summary and the family table now name ROCm.

Refs #3133

* docs(mla): record the completed ROCm gate for the gfx1151 run

The full local gate did run: a second attempt with the job cap lowered to three passed every prerequisite and the workspace tests (192 binaries, 15,954 passed). The first attempt was the one the memory guard stopped, so the record now says both.

Refs #3133

* docs: add technical report for PR #3152
@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): fold the remaining hand-masked MLA prefills into SDPA

1 participant