Skip to content

metal: head-dim-512 prefill route behind set_sdpa_d512_prefill (off by default), kSdpaD512PrefillVersion 1 (numen-tech/gemma4-qat#1972) - #34

Merged
jeethu merged 2 commits into
prism-v0.32.4-fixesfrom
d512-prefill-1972
Nov 21, 2026
Merged

jeethu merged 2 commits into
prism-v0.32.4-fixesfrom
d512-prefill-1972

Conversation

@jeethu

@jeethu jeethu commented Nov 21, 2026

Copy link
Copy Markdown
Member

For numen-tech/gemma4-qat#1972: the Gemma 4 K=V rows' head-dim-512 prefill transient. Off by default, so nothing changes for a process that never sets the flag.

What changes

metal::set_sdpa_d512_prefill(bool) sets a process flag, off by default. metal::sdpa_d512_prefill() reads it. Python exposes both as mx.metal._set_sdpa_d512_prefill / _sdpa_d512_prefill.

The flag is read only when an op is built (ScaledDotProductAttention::use_fallback). With it on, a head-dim-512 call that meets all of these takes a fused path from 9 query rows instead of 1,024:

  • causal;
  • no array mask;
  • no sinks;
  • no logsumexp output.

Which fused path it takes depends on the GPU:

GPU Path Change
No NAX, half precision blocked (sdpa_full_self_attention_blocked) the 1,024-row floor becomes 9; the score buffer holds min(256, rows) rows (was a fixed 256)
NAX the d512 kernel (steel_attention_dsplit ... _bd512) the 1,024-row floor becomes 9, and the 1,024 query-block floor is dropped

With the flag off, every call routes exactly as before.

Other details:

  • eval_gpu picks the blocked path on shape alone, so a call keeps the path it was built for even if the flag changes before evaluation.
  • Before this PR, a head-dim-512 call below 1,024 rows could not reach eval_gpu on a GPU without NAX: no other fused kernel takes it, and force_fused throws there. So the shape-only test changes nothing for a flag-off call.
  • Version constant: kSdpaD512PrefillVersion 1 (device.h). numen-tech/mlx-swift reads it as GPUFamilyRoute.sdpaD512PrefillVersion.

Why

The Gemma 4 12B, 26B A4B and 31B prefill in 512-row chunks. Below the old floors, every global layer of every chunk materializes [query heads x 512 x kv_len] scores in the unfused matmul + softmax.

The app sets the flag per load, for those three models only, and holds it while a loaded engine holds it. E2B and E4B also have head-dim-512 global layers, but never set it.

Measurements (numen-tech/gemma4-qat#1972 Step A)

Full write-up: https://github.com/numen-tech/gemma4-qat/issues/1972#issuecomment-7063858044. Every run below is Release numen-cli, greedy, fp16 KV.

  • M2 Max (g14s, no NAX), blocked path:
    • logits are bit-identical to the unfused path (dmax 0 on all 16 rows), on every 31B and 26B pair from 8k to 55k tokens;
    • peak footprint: 31B −0.26 GB at 18.9k and −0.56 GB at 33.5k; 26B −0.25 GB at 33.5k and −0.40 GB at 55.6k;
    • prefill, 31B at 8,169 tokens, ABBA: off 62.93 / 62.90 / 62.92 s, on 62.83 / 62.79 / 62.80 s.
  • M5 Air (g17g, NAX), d512 kernel:
    • peak footprint, 26B: −0.16 GB at 8k, −0.32 GB at 18.9k, −0.58 GB at 33.5k;
    • logits within dmax 0.38 and KL 5e-5;
    • prefill, 26B at 18.9k, ABBA over 3 rounds: on/off 0.994;
    • the 12B: the 12B at 8,169 tokens, ABBA over 3 rounds: on/off 0.986 (rounds 1.008 / 0.940 / 1.009; single runs on the fanless Air spread by about 5 %, so this says only that no slowdown shows above that noise). A throwaway that sent the route to the blocked path on NAX instead read 0.970 (26B) and 0.984 (12B), with the 256-row scores kept, so the NAX kernel stays.

Tests

python/tests/test_fast_sdpa.py::test_sdpa_d512_prefill_route covers:

  • with the flag off, a 9-1,023-row causal call stays unfused;
  • with it on, the call takes exactly one fused path, blocked or NAX, and matches the reference at 1e-3 (fp16) or 1e-2 (bf16), at shapes from 9 to 512 rows, GQA 8:1 and 16:2, B = 2;
  • a call built with the flag on and evaluated after it is turned off keeps its path, bit for bit;
  • the 8-row call, the array-mask call and the head-dim-256 call never take it.

Results:

Machine Run Result
M5 Air (g17g) default (no NAX in Python) 30 passed, 3 skipped
M5 Air (g17g) MLX_METAL_NAX=1 31 passed, 2 skipped
M2 Max (g14s) default 30 passed, 4 failed, 3 skipped

The four M2 Max failures are the test_sdpa_vector_head_dim_512 subtests: "Maximum threads per threadgroup is 640 but requested 1024". They fail the same way at the base, 897320f, on that machine (29 passed, 4 failed), and this PR does not touch the vector kernels.

Not run: CUDA, and a gen-15 or gen-16 GPU (M3 or M4) at the Python level.

…y default): the fused d512 paths take a causal call from 9 rows; kSdpaD512PrefillVersion 1 (numen-tech/gemma4-qat#1972)

Off by default. set_sdpa_d512_prefill(true) lets a causal head-dim-512
attention of 9 or more query rows with no array mask, sinks or logsumexp
take the blocked d512 path on a GPU without NAX, and the NAX d512 kernel
(without its query-block floor) on a NAX GPU, instead of the unfused
matmul + softmax that materializes [heads x rows x keys] scores. The flag
is read when the op is built (use_fallback); eval_gpu routes a non-NAX
head-dim-512 call on its shape alone, so a call built with the route keeps
it if the flag goes off before evaluation. The blocked path sizes its
buffers to min(256, rows). Python: mx.metal._set_sdpa_d512_prefill and
_sdpa_d512_prefill; test_sdpa_d512_prefill_route.
…ore tracing, so each trace holds the attention alone; the 8-row and array-mask cases take no blocked kernel
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-21T14:28:21.149700Z 431ab92 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant