mlx icon indicating copy to clipboard operation
mlx copied to clipboard

[Performance] Vector SDPA achieves only ~25% of memory bandwidth for GQA (4 KV heads, head_dim 256) at long context on M1 Ultra

Open KaiserCaiser opened this issue 4 months ago • 9 comments

Describe the issue

Single-query (decode-time) mx.fast.scaled_dot_product_attention runs far below memory-bandwidth speed for a GQA shape with few KV heads and large head_dim at long context. On an M1 Ultra with mlx 0.32.0:

  • Shape: q=(1, 32, 1, 256), k=v=(1, 4, 100000, 256), bf16, mask=None
  • Measured: 2.2-2.4 ms per call
  • KV bytes touched: 2 × 4 × 100000 × 256 × 2B ≈ 0.41 GB → ~170-190 GB/s achieved
  • The same machine sustains ~680 GB/s on a plain reduction over a large contiguous array (benchmark below), so the kernel achieves ~25-28% of practically available bandwidth.

For a 16-full-attention-layer model (Qwen3.6-27B hybrid) this makes decode attention cost ~39 ms/token at 100k context — about 43% of the total token time — and is the dominant reason decode throughput trails bandwidth-proportional expectations at long context.

MLX_SDPA_BLOCKS (new in 0.32.0) was swept at this shape: the default selection is already optimal (8/16/32/64/128 are all equal or worse), so this does not look like a block-count tuning issue.

q_len=2 costs 5.39 ms (≈2.2×), so batched-verify speculative decoding at long context is also gated by the same kernel.

To Reproduce

import time
import mlx.core as mx

B, Hq, Hkv, D, S = 1, 32, 4, 256, 100000

# reference: practically achievable bandwidth on this machine
rows = 8 * 1024**3 // (4 * 65536)
a = mx.random.normal((rows, 65536))
mx.eval(a)
mx.eval(a.sum())
t0 = time.time()
for _ in range(5):
    mx.eval(a.sum())
bw = 8 / ((time.time() - t0) / 5)
print(f"reduction bandwidth: {bw:.0f} GB/s")

k = mx.random.normal((B, Hkv, S, D), dtype=mx.bfloat16)
v = mx.random.normal((B, Hkv, S, D), dtype=mx.bfloat16)
q = mx.random.normal((B, Hq, 1, D), dtype=mx.bfloat16)
mx.eval(k, v, q)
scale = D ** -0.5

mx.eval(mx.fast.scaled_dot_product_attention(q, k, v, scale=scale, mask=None))
t0 = time.time()
n = 50
for _ in range(n):
    mx.eval(mx.fast.scaled_dot_product_attention(q, k, v, scale=scale, mask=None))
dt = (time.time() - t0) / n
kv_gb = 2 * Hkv * S * D * 2 / 1e9
print(f"sdpa q_len=1: {dt*1000:.2f} ms  -> {kv_gb/dt:.0f} GB/s "
      f"({kv_gb/dt/bw*100:.0f}% of reduction bandwidth)")

Output on M1 Ultra (128GB), macOS 26.2, mlx 0.32.0 (pip):

reduction bandwidth: 676 GB/s
sdpa q_len=1: 2.17 ms  -> 189 GB/s (28% of reduction bandwidth)

Expected behavior

Decode-time SDPA at long context is memory-bound; something closer to 50-80% of achievable bandwidth would be expected (llama.cpp's Metal FA achieves ~75%+ on comparable GQA shapes on M-series).

Happy to run patches/benchmarks on this machine (M1 Ultra 128GB, real workload = 27B hybrid model at 80-160k context).


This investigation (bandwidth baseline, kernel benchmarks, and this report) was performed by Claude (model: claude-fable-5) via Claude Code, operating directly on the reporter's hardware.

KaiserCaiser avatar Jul 12 '26 17:07 KaiserCaiser

Two follow-ups:

Correction on the model shape. The real model (Qwen3.6-27B) uses num_attention_heads=24, not 32 — the 32 in my report came from a library default rather than the checkpoint's config.json. The benchmark above is self-contained (it measures the 32-head shape it constructs), and the KV bytes / bandwidth math is unchanged since query-head count doesn't affect KV size. At the true shape the stock kernel measures essentially the same:

  • q=(1, 24, 1, 256), k=v=(1, 4, 100000, 256), bf16: 2.15 ms (~190 GB/s)

The gap is closable. A hand-written split-S flash-decoding kernel (via mx.fast.metal_kernel, ~100 lines MSL) at the true shape reaches 1.41 ms (~290 GB/s, 1.52×) on the same M1 Ultra, with max|Δ| ≤ 5e-4 vs mx.fast.scaled_dot_product_attention on random inputs. Structure: one threadgroup per (kv_head, key-split), cooperative uint4 staging of K/V rows into threadgroup memory, each simdgroup covering 2 query heads × 1/2 of the keys (register spills kill it beyond ~2 heads/simdgroup), split partials merged with a logsumexp pass. In the full model this recovers ~15.7 ms/token at 100k context (16 attention layers), +19% end-to-end decode.

A load-only variant of the same threadgroup structure sustains ~475 GB/s, so there is likely further headroom for a native kernel with proper software pipelining. Happy to share the kernel source or benchmark patches on this hardware.

(Same setup as the report: investigation by Claude claude-fable-5 via Claude Code on the reporter's M1 Ultra.)

KaiserCaiser avatar Jul 12 '26 19:07 KaiserCaiser

one threadgroup per (kv_head, key-split), cooperative uint4 staging of K/V rows into threadgroup memory

You could reduce a lot of shmem traffic by changing how you partitioned work into simdgroups.

Could you try whether the following will help:

  • dispatch is the same as yours. One TG per (kv_head, key-split)
  • simdgroup further splits work for TG's key-split. Each simdgroup is responsible for attending all n_group query heads for this kv head, and accumulate partial o/denominator/max for this simdgroup's kv partition. Note that this is not a fixed 1/2 kv split, so this simdgroup only needs its partition of partial kv in register, which is reused across all n_group query heads.
  • then, as you already did, merge simdgroup partials in a TG-reduction pass.

Note that this way, the entire TG only reads kv once, and it avoids any shmem->register traffic on the kv.

hxu296 avatar Jul 13 '26 05:07 hxu296

Thanks — tried your scheme on the same M1 Ultra (implemented via mx.fast.metal_kernel, so register allocation is compiler-scheduled; caveat below):

  • As suggested (each simdgroup: all 6 query heads × a contiguous key sub-partition, KV read device→registers, no threadgroup memory): correctness is fine, but 4.84 ms @ S=100k (~85 GB/s) — 3.4× slower than the staged variant. The signature matches register spilling: ~110+ live floats/lane (6 heads × 512 B accumulators + q).
  • Same kernel with fp16 accumulators (halves the persistent per-lane state): jumps 3.3× to 1.47 ms @100k / 1.83 ms @140k, i.e. it lands on the same plateau as my shared-memory staging variant (1.41 / 2.12 ms) — and beats it at 140k. Surprisingly, max|Δ| vs mx.fast.sdpa stays ≤ 1.2e-4 even at 100k on random inputs.

So your premise holds (the shmem round-trip is avoidable), but at metal_kernel level the binding constraint on M1 Ultra becomes the register file: full-precision state for all n_group heads doesn't fit. Both routes plateau at ~280–310 GB/s vs ~475 GB/s for a load-only kernel with the same dispatch — closing that last gap presumably needs software pipelining / register control beyond what metal_kernel exposes. Happy to benchmark a native-kernel patch on this hardware.

(claude-fable-5 via Claude Code, same setup as above.)

KaiserCaiser avatar Jul 13 '26 06:07 KaiserCaiser

Thanks for testing it out and collecting this data.

I benchmarked the shmem staging variant vs. direct register kv-reuse variant on a M5-class machine. And it didn't throttle on register pressure. This is presumably due to the introduction of GPU dynamic caching since M3, which manages register allocation more intelligently.

Perhaps for pre-M3 class of machines, shmem staging is the more profitable path.

hxu296 avatar Jul 13 '26 07:07 hxu296

That explains the data cleanly — thanks for running the M5 comparison. Dynamic caching (M3+) vs fixed register allocation (M1/M2) accounts for exactly the divergence we measured.

For completeness, one more data point for anyone landing on this thread with pre-M3 hardware: we ended up shipping the register kv-reuse variant with fp16 accumulators in production on the M1 Ultra — it matches the shmem-staging variant at 100k (1.47 vs 1.41 ms) and beats it at 140k (1.83 vs 2.12 ms), with max|Δ| ≤ 1.2e-4 vs mx.fast.sdpa. So the summary across architectures seems to be:

  • M3+: register kv-reuse with fp32 accumulators (dynamic caching absorbs the pressure)
  • M1/M2: either shmem staging, or register kv-reuse with fp16 accumulators — both plateau at ~280–310 GB/s at metal_kernel level

Either way the stock vector SDPA's ~190 GB/s at this shape leaves a consistent ~1.5× on the table, which is the original issue. Happy to keep benchmarking patches on the M1 Ultra side.

(claude-fable-5 via Claude Code, same setup.)

KaiserCaiser avatar Jul 13 '26 07:07 KaiserCaiser

Regarding f16 accumulators. It seems flashinfer has a mode to use fp16 for the [email protected] (since the reduction applies to the short head_dim and values are scaled) but still used fp32 for the score@v (since the reduction applies to the kv_dim).

Image

hxu296 avatar Jul 13 '26 23:07 hxu296

Measured this on an M5 Max (40 core, applegpu_g17s, 128 GB) to add a second data point. No fix, but a few things are now ruled out and the scaling has a clean shape.

All numbers below are bf16, single query token, warm up and measure by wall clock rather than by iteration count. That matters more than I expected: with a fixed iteration count the same config drifted by up to 3x between processes because a 0.3 ms kernel does not run long enough to ramp GPU clocks off idle. Everything here warms for 1 s, then takes the median of five 0.4 s windows. Spread within a run is under 4 percent and run to run agreement is within about 3 percent.

Measured peak bandwidth on this machine is 530 GB/s.

KV head count is the variable that matters

n_q=32, L=32768, D=128, varying the number of KV heads:

n_kv ms GB/s % of peak
1 0.307 54.7 10.3
2 0.326 102.8 19.4
4 0.351 191.3 36.1
8 0.451 297.6 56.2
32 1.148 467.8 88.3

The interesting part is the wall time, not the bandwidth. Going from 1 to 8 KV heads multiplies the KV data by 8 while time only moves from 0.307 to 0.451 ms. So at low KV head counts the kernel is not bandwidth bound at all, it is sitting on a fixed cost of roughly 0.3 ms at this context length. Bandwidth utilization is just that floor expressed as a ratio, which is why it looks like a bandwidth problem.

At 32 KV heads, where there is enough work to hide the floor, the kernel reaches 88 percent of peak. So the streaming path itself is fine.

MLX_SDPA_BLOCKS is not the lever

Confirming the reporter's finding independently, since it is the obvious thing to suspect given the above:

config heuristic 256 512 1024 2048
n_kv=1, L=32768, D=128 10.3 10.2 10.3 9.8 8.6
n_kv=4, L=32768, D=128 36.4 36.6 35.8 34.6 29.4
n_kv=4, L=100000, D=256 59.0 50.8 54.2 57.4 56.9

Percent of peak. The heuristic is at or within noise of the best value everywhere, and forcing more blocks makes things worse. Adding threadgroups along the blocks axis does not recover the floor.

Worth noting for anyone else testing this: the env override rounds up to a multiple of 32, so values below 32 all collapse to 32 and look identical.

Head dimension is not the cause either

n_q=32, n_kv=4, L=32768:

head_dim % of peak
64 21.8
96 28.3
128 35.7
192 18.6
256 48.6

256 is the best case, not the worst, so the large head dimension in the original report is not what is hurting. Larger head dims do more work per KV element loaded, which hides the floor better.

192 is the outlier, and that one is explained: sdpa_vector_supported_head_dim accepts 64, 96, 128 and 256, and accepts 192 only when the value head dim is 128. A 192/192 configuration falls through to the generic path. That is existing behaviour rather than a defect, just flagging it so the dip in the table is not mistaken for a second bug.

Context length

The reported configuration, n_q=32, n_kv=4, D=256:

L ms GB/s % of peak
8192 0.243 138.2 25.9
32768 0.524 256.3 48.0
65536 0.879 305.5 57.2
100000 1.208 339.2 63.6

Efficiency climbs with context, consistent with amortizing a fixed cost rather than with a bandwidth limit.

Summary

On this hardware the reported configuration reaches 63.6 percent of peak rather than the 25 to 28 percent seen on M1 Ultra, so severity looks hardware dependent, but the underlying shape is the same. The limiter appears to be a fixed per call cost that dominates whenever KV head count is small, and it is not addressable by splitting the KV sequence into more blocks. That suggests the work decomposition itself, rather than a tuning parameter.

Happy to run further experiments on M5 Max if that is useful to whoever picks this up.

erwinzhang7 avatar Aug 05 '26 14:08 erwinzhang7

Thanks for the benchmarks but we use issues to track things we plan to work on and a vague issue about something is not good enough without a solution is not going to help so I'm closing this issue.

zcbenz avatar Aug 16 '26 12:08 zcbenz

A data point on M3 Ultra, in case it helps whoever picks up sdpa_vector.

MiMo-V2's global layers have 16 query heads per KV head, with 192-wide queries/keys and 128-wide values. Before writing a kernel, we zero-padded that cache to 256/256 so sdpa_vector applies. One layer's decode attention, at 64 query / 4 KV heads:

  • sdpa_vector on the padded cache: 2098 µs at 131k keys and 4352 µs at 262k, streaming ~250 GB/s.
  • A split-context kernel on the native cache: 760 µs and 1385 µs, ~440–485 GB/s. It stages each KV head's keys and values in threadgroup memory once, and runs that head's 16 query heads as the rows of simdgroup matrix products.

In production (MiMo-V2.6-Pro on two M3 Ultras, tensor-parallel), decode went from 14.1 to 19.0 tok/s at 140k, and from 10.0 to 16.7 at 277k.

The kernel is an mx.fast.metal_kernel in ml-explore/mlx-lm#1927. It's hard-wired to MiMo's widths, so it isn't a drop-in for sdpa_vector.

Co-authored with Claude Opus 5.5

aidiffuser avatar Sep 27 '26 10:09 aidiffuser