Skip to content

Fix CustomKernel Metal dispatch silently truncating the requested threadgrid - #4535

Open
pseudobacon wants to merge 1 commit into
ml-explore:mainfrom
pseudobacon:fix/metal-custom-kernel-dispatch
Open

pseudobacon wants to merge 1 commit into
ml-explore:mainfrom
pseudobacon:fix/metal-custom-kernel-dispatch

Conversation

@pseudobacon

Copy link
Copy Markdown

Fixes #4534.

CustomKernel::eval_gpu computed the Metal threadgroup dimensions as min(tx, gx) / min(ty, gy) / min(tz, gz) and dispatched through dispatch_threads, which interprets its grid argument as thread counts. The effective threadgroup count per dimension was therefore ceil(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 the requested threadgroup dimensions unclamped via dispatch_threadgroups.
  • Add a tests/gpu_tests.cpp regression test: a probe kernel writes each executing thread's unique index; the test verifies exact coverage of the requested gx*ty*tz threads (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 with grid.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.

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.

This branch has not been deployed

No deployments
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.

[BUG] fast.metal_kernel silently truncates the Metal dispatch: group_dims clamped against grid dimensions in CustomKernel::eval_gpu

1 participant