Skip to content

perf(xla): tune the GQA decode score matmul on ROCm (#2921) - #2934

Merged
inureyes merged 4 commits into
mainfrom
perf/issue-2921-decode-attention
Nov 10, 2026
Merged

inureyes merged 4 commits into
mainfrom
perf/issue-2921-decode-attention

Conversation

@inureyes

@inureyes inureyes commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

Summary

The single-sequence decode attention cost on ROCm gfx1151 was dominated by one contraction: the grouped-query score matmul. rocprofv3 put it at 1187 us of 1510 us of attention per step, 16 dispatches of 74.20 us each for a 256x64 by 64x4 f16 matmul. The context matmul of the same MAC count ran at 6.42 us per dispatch, so the cost was not in the emitted op sequence but in the lowering config IREE picks for that shape.

This PR gives that contraction an explicit lowering config in the in-repo ROCm tuning spec. Attention per decode step drops from 1510 us to 338 us (score dispatch 74.20 us to 1.47 us), decode throughput at n=128 / capacity 256 goes from 113.6 tok/s to 136.4 tok/s, and greedy output stays byte-identical to both the pre-change native path and the f16 path.

Profile: one decode step, per-dispatch attention table

Protocol: rocprofv3 kernel trace of the same binary at -n 1 and -n 128, 127 decode steps, per-kernel calls and times subtracted so graph construction and sampling drop out. llama-3.2-1b-instruct-4bit, MLXCEL_XLA_QUANT=native, MLXCEL_XLA_CONTEXT_CAPACITY=256, gfx1151, 16 layers.

Bytes are per call from the tensor shapes. The KV write dispatch is a one-token dynamic_update_slice that IREE lowers to slow_memcpy (grid 512x64, 64 threads per workgroup); the trace does not expose transferred bytes for it.

Before

Role Kernel Calls/step us/call us/step Bytes/call
scores main_dispatch_8_batch_matmul_8x4x256x64_f16xf16xf32 16 74.20 1187.17 292 KB
KV read convert elementwise_transpose_256x8x64_f32xf16 (2 per layer) 32 3.23 103.29 768 KB
context main_dispatch_11_batch_matmul_8x4x64x256_f16xf16xf32 16 6.42 102.69 280 KB
q/k rope+scale elementwise_8x2x32_f32, elementwise_32x64_f32xf32xf32xf32xf16 32 1.32 42.36 4 to 12 KB
KV write slow_memcpy (2 per layer) 32 0.93 29.64 see note
mask/scale main_dispatch_14_elementwise_8192_f32xf32xf16 16 1.65 26.45 80 KB
softmax main_dispatch_9_softmax_32x256xf32_generic 16 1.13 18.15 64 KB
attention total 160 1509.75 us

Whole-graph kernel time in the same step: 5291 us.

After

Role Kernel Calls/step us/call us/step Bytes/call
KV read convert elementwise_transpose_256x8x64_f32xf16 (2 per layer) 32 3.21 102.79 768 KB
context main_dispatch_11_batch_matmul_8x4x64x256_f16xf16xf32 16 6.41 102.54 280 KB
q/k rope+scale same two kernels 32 1.29 41.22 4 to 12 KB
KV write slow_memcpy (2 per layer) 32 0.90 28.75 see note
scores main_dispatch_8_batch_matmul_8x4x256x64_f16xf16xf32 16 1.47 23.46 292 KB
mask/scale main_dispatch_14_elementwise_8192_f32xf32xf16 16 1.36 21.82 80 KB
softmax main_dispatch_9_softmax_32x256xf32_generic 16 1.07 17.06 64 KB
attention total 160 337.64 us

Whole-graph kernel time: 4099 us.

The table selects the score contraction: 79 percent of attention time in 10 percent of the dispatches, at 4.0 GB/s of effective traffic against 45 GB/s for the context matmul next door. Bytes did not dominate anywhere else, so per the issue's preference order this is a lowering/static-shape problem, not an elementwise-removal or dispatch-count problem. Attention dispatch count per layer is unchanged at 10 before and after (score 1, context 1, KV read convert 2, q/k 2, KV write 2, mask 1, softmax 1), which is why a dispatch-count reduction was not the lever either.

Why not an emitter change

Two emitter-level rewrites of the score path were implemented and measured before this fix: folding the attention scale into q, and emitting K in head-major layout to avoid the transpose. Both are compiled-graph no-ops. IREE canonicalizes the scale fold and the transpose away before dispatch formation, so the dispatch IDs, names, call counts and per-call times are identical to HEAD, and the score dispatch stays at 74 us. The emitter was reverted to HEAD; the one change that reached the kernel was the lowering config, applied through the tuning spec the backend already passes to iree-compile.

Change

src/lib/mlxcel-xla/assets/rocm/bigk_vecmat_tuning.mlir gains a second match, match_gqa_score, next to the existing match_bigk_vecmat from #2873. It matches a batched f16 by f16 into f32 contraction of [group, head_dim] against [head_dim, keys] with batch = n_kv = 8, group = 4, head_dim = 64 and at least 128 keys, and applies workgroup = [1, 4, 32, 1], thread = [1, 1, 1, 0], reduction = [0, 0, 0, 64] with a [32, 4, 1] workgroup: all four query rows and 32 keys per workgroup, the whole 64-wide reduction in one step. The default it replaces was workgroup = [1, 1, 128, 1], thread = [1, 1, 128, 0], 32-thread workgroup, 144 VGPRs, grid 2048. In the trace the dispatch goes from a 32-thread workgroup with 144 VGPRs to a 128-thread [32, 4, 1] workgroup with 96 VGPRs.

The match is narrow on purpose: only the shape that was measured gets the config, and everything else, including other group sizes and head widths, keeps the compiler default. IREE's StableHLO input lowering collapses the ragged rank-4 dot (batching_dims = [0, 1] x [0, 2], contracting [3] x [3]) to the same rank-3 contraction as the single-sequence one, with bsz * n_kv as the batch, so group, head width and key count alone do not exclude it; prefill collapses to a different M and does not match. The batch predicate pins the batch to the measured n_kv = 8: the matcher gives 16 matches on the single decode graph, 0 on prefill, 0 on ragged B=4 and 0 on ragged B=8, so every multi-row graph keeps the compiler default. The batch bench at B=4 and B=8 stays REFERENCE-EXACT. The spec path is content-addressed, so the vmfb cache key changes with the spec and no stale artifact is reused.

src/lib/mlxcel-xla/src/tuning.rs documents both matches and adds a test that keeps the narrow match and its entrypoint wiring in place. src/lib/mlxcel-xla/src/emitter/mod.rs adds a focused test that pins the emitted AttnLayout::Single sequence the spec depends on: the batched f16 by f16 into f32 score dot of [8, 4, 64] against [256, 8, 64] in all 16 layers, the scale then mask then max-subtract then exp order after it, and the matching context dot. A non-batched dot, another dtype or another rank would silently stop matching and put the 74 us dispatch back.

No model math changed: the emitted StableHLO for Single, Ragged and Prefill is byte-identical to HEAD, so no golden was refreshed and the Ragged and Prefill assets are untouched.

Changes during review

The first version of the match did not constrain the batch dim. The implementation review showed that IREE's StableHLO input lowering collapses the ragged and uniform-B dot_general to the same rank-3 contraction, so a config measured on one row also reached the ragged score matmul, which this issue keeps out of scope. The match now pins batch = n_kv = 8, the matcher on the lowered goldens gives 16 matches on single decode and 0 on prefill, ragged B=4 and ragged B=8, the tuning test asserts the predicate, and the technical report was updated to match.

Results

Metric Before After
Attention per decode step 1509.75 us 337.64 us
Score dispatch 74.20 us/call 1.47 us/call
Decode step, n=128, capacity 256, wall 8.80 ms (113.6 tok/s) 7.33 ms median (136.4 tok/s), 7.07 ms best (141.5 tok/s)
Whole-graph kernel time per step 5291 us 4099 us
Attention dispatches per layer 10 10

Interleaved A/B, three rounds each, same checkpoint and flags, both arms MLXCEL_XLA_QUANT=native: base 110.86 / 117.56 / 113.61 tok/s (median 113.61), this branch 141.48 / 135.73 / 136.42 tok/s (median 136.42). The 7.1 ms step target is met at the top of the observed range (141.5 tok/s = 7.07 ms per step); the median of the three rounds is 7.33 ms. Attention is no longer the constraint at 0.34 ms of the step: whole-graph kernel time is 4.10 ms, and the remaining wall time is host-side per-token work that scales with neither change.

Verification

  • Greedy output byte-identical to the pre-change MLXCEL_XLA_QUANT=native binary and to the f16 path on two prompts (The capital of France is, def fibonacci(n):), and native equals f16 within each binary.
  • xla_batch_bench at B=4 (requests 8, maxcap 24) and B=8 (requests 16, maxcap 48): REFERENCE-EXACT PASS on both.
  • emit_all_reproduces_registered_goldens passes with the goldens untouched, which covers Ragged and Prefill byte-identity.
  • tuning::tests, the new emitter test, cargo fmt --all -- --check, and cargo clippy -p mlxcel-xla --all-targets -- -D warnings pass.

Limitations

  • The tuning match covers only batch 8 (the single-sequence n_kv), group 4, head_dim 64 and keys >= 128, which is the measured single-sequence decode shape for capacity 128 and up. Ragged B=1 has the same collapsed shape and picks up the config; every multi-row graph keeps the compiler default. Other model shapes keep the compiler default until they are profiled.
  • Only capacity 256 was traced end to end; the dispatched shape for other capacities was confirmed by the narrowing of N, not by a per-capacity trace.
  • The KV read convert (103 us/step) and the context matmul (103 us/step) are now the largest attention items. Both are outside the score fix and were not retuned here.
  • On this host the pre-change binary measures 8.80 ms per step wall (5291 us of kernel time), not the 7.1 ms quoted in the issue, so every before/after number above is measured against that same binary on the same host rather than against the issue figure.

Closes #2921

@inureyes inureyes added status:review Under review type:performance Performance improvements priority:high High priority area:inference Generation, sampling, decoding (incl. speculative, DRY) status:done Completed and removed status:review Under review labels Nov 10, 2026
A rocprofv3 trace of one single-sequence decode step on gfx1151 showed the attention score contraction at 1187 us of 1510 us of attention time: 16 dispatches of 74.20 us each for a 256x64 by 64x4 matmul, while the context matmul of the same MAC count ran at 6.42 us per dispatch. The cost was the compiler's default lowering config for that shape (workgroup [32, 1, 1], thread tile [1, 1, 128, 1], 144 VGPRs, grid 2048), not the emitted op sequence.

Emitter-level rewrites of the score path (folding the attention scale into q, transposing K to head-major) were measured and are inert: IREE canonicalizes both before dispatch formation, so the dispatch set and per-call time are unchanged. The fix therefore belongs in the lowering tuning spec, which is the only lever that reaches dispatch formation.

Add a second match to the in-repo ROCm tuning spec for the batched f16 by f16 into f32 contraction with group = 4, head_dim = 64 and at least 128 keys, and give it a 128-thread workgroup with the whole 64-wide reduction in one step. The dispatch drops from 74.20 us to 1.47 us, and attention time per decode step from 1510 us to 338 us. Greedy output stays byte-identical to the pre-change native and f16 paths, and the batch bench stays REFERENCE-EXACT at B=4 and B=8. The match is deliberately narrow: other group sizes and head widths were not measured and keep the compiler default.
@inureyes
inureyes force-pushed the perf/issue-2921-decode-attention branch from d4d035e to 2bea762 Compare November 10, 2026 08:45
#2921)

The GQA score match in the ROCm tuning spec also fired on the ragged and uniform-B score contractions, which issue #2921 keeps out of scope. IREE's StableHLO input lowering collapses the rank-4 `batching_dims = [0, 1]` dot into the same rank-3 contraction as the single-sequence one, with `bsz * n_kv` as the batch, so group, head width, and key count alone cannot distinguish them.

Pin the batch dim to the measured `n_kv = 8`. The single-sequence graph still matches and the compiler default stays on every multi-row graph; verified with the matcher on the lowered goldens: 16 matches on single decode, 0 on prefill, 0 on ragged B=4, 0 on ragged B=8. The tuning test asserts the new predicate.
The report was written against the first version of the tuning match. The implementation review found that the match also fired on the ragged and uniform-B score contractions after StableHLO input lowering collapsed their batch dim, so the report now describes the batch = n_kv = 8 predicate, the matcher evidence (16 single-decode matches, none on prefill or ragged), and the bounded verification that actually ran. English and Korean stay in sync.
@inureyes
inureyes marked this pull request as ready for review November 10, 2026 08:59
@inureyes
inureyes merged commit 0ba1b50 into main Nov 10, 2026
31 checks passed
@inureyes
inureyes deleted the perf/issue-2921-decode-attention branch November 10, 2026 09:00
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:inference Generation, sampling, decoding (incl. speculative, DRY) priority:high High priority status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(xla): reduce decode attention cost in the single-sequence graph

1 participant