Metal: split-K attention reduce and qwen4 indexer select dispatched at 1024 threads/threadgroup with no per-pipeline limit check
TL;DR
Two decode-hot dispatch families in ds4_metal.m request 1024 threads per threadgroup without ever consulting pipeline.maxTotalThreadsPerThreadgroup:
- the dsv4 split-K flash-attention reduce (
kernel_flash_attn_ext_vec_reduce/..._rope), dispatched at32 * nwg= 1024 withnwgbaked into the pipeline as function constant 501, and - the qwen4 indexer top-k select kernels, dispatched at a literal
MTLSizeMake(1024, 1, 1).
maxTotalThreadsPerThreadgroup is per pipeline, not per device — it shrinks with kernel register pressure. On a GPU where these pipelines' limit lands below 1024, the threadgroup is truncated to the allowed size. Neither kernel's work accounting adapts:
- Reduce kernels: the split-K reduction strides by the baked
NWGconstant (for (short i = sgitg; i < DV4; i += NWG_),metal/flash_attn.metal:1433). simdgroups beyond the cap never run, so theirDV4/NWGslices of every output row are never summed or written — the destination keeps stale data from the previous step. Wrong attention output on every decode token. - qwen4 IDX_SELECT kernels are width-generic (
const uint nth = ntg.x,metal/qwen4.metal:1738, strided histogram passes), so a smaller threadgroup leaves parts of the selection passes unscheduled — staleselblock indices → wrong sparse-KV blocks, again every decode token.
The host width and the kernel stride are two halves of one contract nothing checks. A dispatch over the pipeline's own limit surfaces either as a validation error or as a silent truncation depending on driver/GPU generation; where it is silent, the engine keeps serving confidently wrong tokens. This is the same bug class as oMLX #4063 (silently wrong decode attention on M1 Max).
Note this is not a "query the limit everywhere" gap: the older dsv4 dense paths do query the limit (~60 sites in ds4_metal.m, first at :4964, last at :45586). The hazard is that the two newest decode-hot families query nothing.
Sites (line numbers at upstream main 0aaea5a)
All pipelines below are created with plain newComputePipelineStateWithFunction: — no pipeline-descriptor limit declaration, and no runtime query at or near the dispatch:
| family | file:line | width | notes |
|---|---|---|---|
| flash-attn reduce dispatch | ds4_metal.m:27938, :28731, :29311, :29674 |
MTLSizeMake(32u * nwg, 1, 1) |
nwg defaults to 32 (:29373) → 1024; DS4_METAL_FLASH_NWG clamped to [1,32] at :29364–:29383; runs every decode step |
| reduce pipeline getters | ds4_gpu_get_flash_attn_reduce_rope_pipeline ds4_metal.m:3783, ds4_gpu_get_flash_attn_reduce_pipeline :3831 |
— | bake nwg as function constant 501 (:3800, :3852); neither checks the created pipeline's limit |
| qwen4 IDX_SELECT dispatches | ds4_metal.m:49185, :49189, :49461 |
MTLSizeMake(1024, 1, 1) |
top-k select (_pre, plain, _rows) |
| qwen4 dispatch chokepoint | qwen4_dispatch_resident ds4_metal.m:48352 |
— | passes the caller's tg straight to [enc dispatchThreadgroups:grid threadsPerThreadgroup:tg] at :48448; the entire qwen4 path contains zero maxTotalThreadsPerThreadgroup queries |
Contrast: the single-pass packed reduce alternative already guards itself — packed_pipeline.maxTotalThreadsPerThreadgroup >= packed_threads at ds4_metal.m:29467. The split-K path is the one left unchecked.
Clamping the dispatch width alone is not a valid fix for the reduce kernels: it would contradict the baked NWG stride and change the reduction, so the contract needs a guard (or an nwg re-creation at a width the pipeline can actually run).
Minimal fix shape (fail-closed guard, ~15 lines)
- In
ds4_gpu_get_flash_attn_reduce{,_rope}_pipeline: after PSO creation, comparepipeline.maxTotalThreadsPerThreadgroupagainst32 * nwg; if below, print one stderr line naming the kernel/limit and returnnil(callers already handle a nil reduce pipeline), or re-create with a smallernwg/ fall back to the guarded packed path (:29467). - In
qwen4_dispatch_resident, beforedispatchThreadgroups:: iftg.width > pipeline.maxTotalThreadsPerThreadgroup, one stderr line per kernel andreturn 0(callers propagate; loud beats silently truncated). Width-adaptive clamping can come later per kernel, with the width-invariance proofs each kernel needs.
/* ds4_gpu_get_flash_attn_reduce_pipeline(): after newComputePipelineStateWithFunction: */
if (pipeline.maxTotalThreadsPerThreadgroup < (NSUInteger)(32u * nwg)) {
fprintf(stderr, "ds4: kernel_flash_attn_ext_vec_reduce needs %u threads/threadgroup "
"but pipeline limit is %lu — refusing (set DS4_METAL_FLASH_NWG<=N)\n",
32u * (uint32_t)nwg, (unsigned long)pipeline.maxTotalThreadsPerThreadgroup);
return nil; /* caller's existing nil-handling takes over */
}
/* qwen4_dispatch_resident(): before [enc dispatchThreadgroups:...] */
if (tg.width > pipeline.maxTotalThreadsPerThreadgroup) {
fprintf(stderr, "ds4: %s dispatch width %lu exceeds pipeline limit %lu\n",
qwen4_kernel_names[kernel], (unsigned long)tg.width,
(unsigned long)pipeline.maxTotalThreadsPerThreadgroup);
return 0;
}
Plus, to make device-generation questions answerable in one run: log maxTotalThreadsPerThreadgroup / threadExecutionWidth once per pipeline at creation behind DS4_METAL_LOG_TG_LIMITS=1.
Repro sketch
Needs an M1/M2-class Apple GPU where these PSOs' per-pipeline limits land below 1024 (register-pressure dependent per pipeline; not guaranteed even there). One-shot diagnostic that settles it on any machine: add the DS4_METAL_LOG_TG_LIMITS=1 dump, run a short decode, and inspect the limits reported for kernel_flash_attn_ext_vec_reduce* and kernel_qwen4_idx_select*. On machines where a limit < 1024 appears, the guards above should fire loudly; without them, greedy decode output diverges from the same model run with DS4_METAL_FLASH_NWG=1 (single-warp reduce, which stays under any plausible limit) — a cheap bit-exactness A/B oracle.
Scope / audience
Measured limits on M3/M4/M5-class GPUs sit at or above these widths, so this likely never fires on current hardware — it is an older-silicon correctness hazard. Upstream M1 Max field reports exist (e.g. #607), so the audience is real. The structural half — two decode-hot families with zero limit queries while the older paths query diligently — is what makes correctness device-generation roulette rather than an invariant.
(Reported from the nazerim/ds4 fork, where we shipped the guard shape above; happy to PR the reduce-getter + chokepoint checks if useful.)
This is a Metal performance bug where split-K attention reduce and qwen4 indexer select are dispatched at 1024 threads per threadgroup without checking per-pipeline limits.
This can cause GPU hangs or incorrect results when the hardware has fewer threads available than expected. Add a runtime check that clamps the threadgroup size to the actual device limit before dispatch.
Also consider adding a fallback path for devices that don't support 1024 threads per threadgroup.
Thanks — two clarifications from measurements on our fork:
-
This is a correctness bug, not a performance one: where the per-pipeline limit falls below 1024 and the driver does not raise a validation error, the dispatch is silently truncated and both families keep producing wrong output on every decode token (stale reduce destinations / stale
selblock indices) — the engine serves confidently wrong tokens. -
"Clamp the threadgroup size to the actual device limit before dispatch" is not safe by itself, for two reasons:
- the limit to query is per pipeline (
pipeline.maxTotalThreadsPerThreadgroup, shrinks with register pressure), not a device-wide constant — a device-level assumption misses exactly the pipelines that are at risk; - the split-K reduce bakes
NWGinto the PSO as function constant 501 and the kernel strides by that same constant — clamping the dispatch width below the baked stride leaves DV4/NWG slices never written (and changing the width without re-creating the pipeline contradicts the compiled kernel). The width-generic idx_select is easier to adapt, but still needs the per-kernel width-invariance proof before a clamp would be trustworthy.
- the limit to query is per pipeline (
That is why the issue proposes the fail-closed guard at both sites (loud refusal, ~15 lines) rather than a clamp. We shipped exactly this guard shape on our fork (qwen4 chokepoint guard + rms-norm twin swaps, plus a per-pipeline DS4_METAL_LOG_TG_LIMITS=1 limit dump) — it never fires on M3/M4/M5-class hardware, which is also why the clamp question only becomes live on older silicon. Still happy to PR the two guard sites if useful.