[BUG] float16 head_dim 256 SDPA still exceeds the M1/M2 threadgroup limit: #4643's `_tk1` fallback covers bfloat16 and float32 only
Describe the bug
On M1/M2, mx.fast.scaled_dot_product_attention with float16 inputs and head_dim 256 still fails on the 2-pass vector route when 32 × gqa_factor × q_len is between 928 and 1024 and the mask is an array or None:
RuntimeError: Maximum threads per threadgroup is 896 but requested 960 for kernel sdpa_vector_2pass_1_float16_t_256_256_boolmask_qnt_nc_nosinks_128.
#4643 fixed this error by switching to a _tk1 kernel when the default one is over the limit, but only for bfloat16/256 and float32/192 and 256. has_tk1_variant and the instantiations leave out float16/256. The v0.32.4 metallib has these three and no float16 one:
sdpa_vector_2pass_1_bfloat16_t_256_256_tk1
sdpa_vector_2pass_1_float_192_192_tk1
sdpa_vector_2pass_1_float_256_256_tk1
test_sdpa_vector_2pass_threadgroup_limit includes the float16/256 shapes, but it passes mask="causal", and causal calls at these shapes don't fail. I saw no error at 960 or 1024 threads with 256 to 8192 keys at batch 1 and 4, so the test passes without exercising the failing path.
This shows up in MTP verification with float16 copies of Qwen3.8-27B (24/4 heads, verify width 5, so 960 threads) and Qwen3.6-35B-A3B (16/2 heads, width 4, so 1024), reported in jundot/omlx#4609. float16 copies are common on M1, where bfloat16 is emulated.
To reproduce
import mlx.core as mx
B, hq, hkv, ql, L, D = 1, 24, 4, 5, 1024, 256
q = mx.random.normal((B, hq, ql, D)).astype(mx.float16)
k = mx.random.normal((B, hkv, L, D)).astype(mx.float16)
v = mx.random.normal((B, hkv, L, D)).astype(mx.float16)
mask = mx.ones((B, 1, ql, L), dtype=mx.bool_) # mask=None fails the same way
mx.eval(mx.fast.scaled_dot_product_attention(q, k, v, scale=D**-0.5, mask=mask))
On the same machine, the same call with bfloat16 runs, and float16 with mask="causal" runs.
A sweep at batch 4 with 8192 keys, varying heads and q_len 1 to 8:
| dtype | threads ≤ 896 | 928 to 1024 | > 1024 |
|---|---|---|---|
| float16 | ok | fails | ok |
| bfloat16 | ok | ok | ok |
| float32 | ok | ok | ok |
It needs head_dim 256 (64 and 128 run at the same shapes) and the 2-pass route (256 keys runs, 1024 keys and up fails).
Expected behavior
The same as bfloat16: fall back to a TK=1 kernel. The fix looks like adding instantiate_sdpa_vector_tk1(float16_t, 256) and a float16/256 case to has_tk1_variant. I haven't built MLX with that change.
Desktop
- Mac Studio, Apple M1 Ultra, 128 GB (
applegpu_g13d) - macOS 27.2 (26B5112a)
- MLX 0.32.4 (the build bundled with oMLX 0.7.1rc1)