Repository navigation
metal: head-dim-512 prefill route behind set_sdpa_d512_prefill (off by default), kSdpaD512PrefillVersion 1 (numen-tech/gemma4-qat#1972) - #34
Merged
Conversation
…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
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
jeethu
added a commit
to numen-tech/mlx-swift
that referenced
this pull request
Nov 21, 2026
Carry the head-dim-512 prefill route (numen-tech/mlx#34) and expose it in GPUFamilyRoute (numen-tech/gemma4-qat#1972)
This was referenced Nov 21, 2026
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.
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 asmx.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:Which fused path it takes depends on the GPU:
sdpa_full_self_attention_blocked)min(256, rows)rows (was a fixed 256)steel_attention_dsplit ... _bd512)With the flag off, every call routes exactly as before.
Other details:
eval_gpupicks the blocked path on shape alone, so a call keeps the path it was built for even if the flag changes before evaluation.eval_gpuon a GPU without NAX: no other fused kernel takes it, andforce_fusedthrows there. So the shape-only test changes nothing for a flag-off call.kSdpaD512PrefillVersion1 (device.h). numen-tech/mlx-swift reads it asGPUFamilyRoute.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.g14s, no NAX), blocked path:g17g, NAX), d512 kernel:Tests
python/tests/test_fast_sdpa.py::test_sdpa_d512_prefill_routecovers:Results:
g17g)g17g)MLX_METAL_NAX=1g14s)The four M2 Max failures are the
test_sdpa_vector_head_dim_512subtests: "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.