Fix CustomKernel Metal dispatch silently truncating the requested threadgrid - #4535
Open
pseudobacon wants to merge 1 commit into
Open
pseudobacon wants to merge 1 commit into
pseudobacon wants to merge 1 commit into
Conversation
CustomKernel::eval_gpu computed group_dims as min(threadgroup, grid) per dimension and dispatched through dispatch_threads. dispatch_threads interprets its grid argument as thread counts, so the effective threadgroup count became ceil(min(tx,gx)/max_tx_per_threadgroup), a small prefix of the requested grid whenever a grid dimension is smaller than the matching threadgroup dimension (the common case). The truncation is stable across repeated same-geometry dispatches, so re-dispatching cannot clear it; only a full-coverage check of the requested grid detects it. Use dispatch_threadgroups with the requested threadgroup dimensions unclamped, and add a regression test that dispatches a probe kernel over several (grid, threadgroup) geometries - including the production per-row geometry with grid.x < threadgroup width - and verifies exact coverage of every requested thread on the first dispatch and on a repeated same-geometry dispatch.
pseudobacon
force-pushed
the
fix/metal-custom-kernel-dispatch
branch
from
September 19, 2026 17:06
89bbbfc to
346eff7
Compare
This branch has not been deployed
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.
Fixes #4534.
CustomKernel::eval_gpucomputed the Metal threadgroup dimensions asmin(tx, gx)/min(ty, gy)/min(tz, gz)and dispatched throughdispatch_threads, which interprets its grid argument as thread counts. The effective threadgroup count per dimension was thereforeceil(min(tx,gx)/tx_per_group)— a small prefix of the requested grid, stable across repeated same-geometry dispatches, undetectable by any check other than full coverage of the requested grid.dispatch_threadgroups.tests/gpu_tests.cppregression test: a probe kernel writes each executing thread's unique index; the test verifies exact coverage of the requestedgx*ty*tzthreads (output ==1..N) on the first dispatch at a fresh geometry and on a repeated same-geometry dispatch, across three (grid, threadgroup) pairs including the production per-row geometry withgrid.x (24) < threadgroup width (256).Measured impact on the reporter's workload before this fix: a custom flash-SDPA kernel timed 3.5–4.8× faster than the dense incumbent at truncated (buggy) dispatch and 57.7× slower at true full work.