Check threadgroup size in the 1-pass sdpa_vector dispatch - #4018
Merged
Conversation
sdpa_vector dispatches a fixed 1024-thread threadgroup without validating it against the pipeline's maxTotalThreadsPerThreadgroup. Both dispatches in sdpa_vector_2pass already call check_kernel_threadgroup_size; this adds the same call to the 1-pass path. An over-limit dispatchThreadgroups is dropped by the driver: the kernel does not run, no error is raised, and the output is returned untouched. This turns that silent failure into the same exception the 2-pass path already raises. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
zcbenz
approved these changes
Aug 6, 2026
zcbenz
left a comment
Member
There was a problem hiding this comment.
It makes sense adding the check, thanks!
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.
sdpa_vectordispatches a fixed 1024-thread threadgroup but never validates it against the pipeline'smaxTotalThreadsPerThreadgroup. Both dispatches insdpa_vector_2passalready callcheck_kernel_threadgroup_size; this adds the same call to the 1-pass path.Today this is latent — I'm not aware of a head dim currently routed to the vector kernel that trips the limit on any shipping device. What makes it worth a line is how the failure presents when it does trip. An over-limit
dispatchThreadgroupsis dropped by the driver: the kernel never runs, no error is raised at any level, and the output buffer is returned untouched — all zeros. There is no signal from MLX at all;MTL_DEBUG_LAYER=1is what eventually surfaced it for me.I hit it while prototyping a d=512 instantiation for #3885 on an M1 Max, where register pressure caps both d=512 pipelines at 832 threads (
maxTotalThreadsPerThreadgroup= 832 forsdpa_vector_bfloat16_t_512_512, vs 1024 for..._256_256on the same device). The 2-pass path threw a clear exception from the check that's already there. The 1-pass path silently returned zeros, and I spent a while trusting benchmark numbers from a kernel that never ran.The cost is one comparison per dispatch on an already-cached pipeline object. The benefit is that a register-limited pipeline fails the same loud way on both vector paths instead of one of them producing wrong results quietly. That protection is independent of #3885 — it applies to any future head dim, kernel change, or device whose register allocation lands under 1024.
No test: triggering this requires a pipeline that register-limits below 1024, which is device- and head-dim-specific and not reproducible in CI. The two existing
check_kernel_threadgroup_sizecall sites are untested for the same reason.