Round MLX_SDPA_BLOCKS up to a multiple of 32#3875
Open
pierre427 wants to merge 1 commit into
Open
Conversation
The 2-pass vector SDPA reduction consumes partials in simd-width (32) chunks and silently drops the tail, so a MLX_SDPA_BLOCKS override that is not a multiple of 32 corrupts the output (0.01-0.07 abs err vs 2e-5 baseline on a kL=8192 decode shape). Round the override up instead of applying it as-is, and add a regression test. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
angeloskath
approved these changes
Jul 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.
Summary
MLX_SDPA_BLOCKS(the 2-pass vector-SDPA block-count override added in #3455) isvalidated only for
> 0, but the kernel's second pass assumes the block count is amultiple of 32. Any other value silently corrupts the attention output on every decode
step — no error, no clamp. This PR rounds the override up to the next multiple of 32 and
adds a regression test.
Root cause
scaled_dot_product_attention.cppapplies the override as-is:but the pass-2 reduction loops in
sdpa_vector.hiterateblocks / BN(BN = 32) withinteger division:
so for
blocks % 32 != 0the tail partials are dropped from the max/sum/outputreductions. All built-in block choices are multiples of 32 (32–1024), so only the env
override can reach the broken case.
Measured
Apple M5, macOS 26.2, decode shape
B=1, 32 q-heads / 8 kv-heads,D=128,kL=8192,fp16, max abs err vs an fp32 reference (reproduces identically on 0.32.0 (PyPI) and
current main):
MLX_SDPA_BLOCKSSince the env var exists as a performance-tuning lever, anyone sweeping it (say,
MLX_SDPA_BLOCKS=48) corrupts decode without noticing.Fix
Round up rather than reject, so the knob keeps working for any positive value:
After the fix, the previously-corrupt values measure 2.1–2.5e-5 against the fp32
reference on the shape above — indistinguishable from the un-overridden path.
Alternative considered: ignore invalid values and keep the heuristic choice. Rounding
seemed friendlier for a tuning knob, but happy to switch if you prefer strict validation.
Test
test_sdpa_blocks_env_overrideintest_fast_sdpa.pyruns the decode shape above withMLX_SDPA_BLOCKS ∈ {16, 33, 48, 100}and asserts the output matches the un-overriddenresult. All four values fail before this change and pass after (verified both ways on an
M5; the full
test_fast_sdpa.pysuite passes with the change).(Noted in passing while reading this code, not addressed here: the pass-2 partials offset
arithmetic is 32-bit and would overflow once
B·H·qL·blocks·D ≥ 2³¹— reachable only atextreme shape × blocks combinations.)
🤖 Generated with Claude Code