Repository navigation
perf(xla): tune the GQA decode score matmul on ROCm (#2921) - #2934
Merged
Merged
Conversation
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
force-pushed
the
perf/issue-2921-decode-attention
branch
from
November 10, 2026 08:45
d4d035e to
2bea762
Compare
#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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 1and-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_slicethat IREE lowers toslow_memcpy(grid 512x64, 64 threads per workgroup); the trace does not expose transferred bytes for it.Before
main_dispatch_8_batch_matmul_8x4x256x64_f16xf16xf32elementwise_transpose_256x8x64_f32xf16(2 per layer)main_dispatch_11_batch_matmul_8x4x64x256_f16xf16xf32elementwise_8x2x32_f32,elementwise_32x64_f32xf32xf32xf32xf16slow_memcpy(2 per layer)main_dispatch_14_elementwise_8192_f32xf32xf16main_dispatch_9_softmax_32x256xf32_genericWhole-graph kernel time in the same step: 5291 us.
After
elementwise_transpose_256x8x64_f32xf16(2 per layer)main_dispatch_11_batch_matmul_8x4x64x256_f16xf16xf32slow_memcpy(2 per layer)main_dispatch_8_batch_matmul_8x4x256x64_f16xf16xf32main_dispatch_14_elementwise_8192_f32xf32xf16main_dispatch_9_softmax_32x256xf32_genericWhole-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.mlirgains a second match,match_gqa_score, next to the existingmatch_bigk_vecmatfrom #2873. It matches a batched f16 by f16 into f32 contraction of[group, head_dim]against[head_dim, keys]withbatch = n_kv = 8,group = 4,head_dim = 64and at least 128 keys, and appliesworkgroup = [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 wasworkgroup = [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, withbsz * n_kvas 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 measuredn_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.rsdocuments both matches and adds a test that keeps the narrow match and its entrypoint wiring in place.src/lib/mlxcel-xla/src/emitter/mod.rsadds a focused test that pins the emittedAttnLayout::Singlesequence 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_generalto 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 pinsbatch = 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
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
MLXCEL_XLA_QUANT=nativebinary 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_benchat B=4 (requests 8, maxcap 24) and B=8 (requests 16, maxcap 48): REFERENCE-EXACT PASS on both.emit_all_reproduces_registered_goldenspasses with the goldens untouched, which covers Ragged and Prefill byte-identity.tuning::tests, the new emitter test,cargo fmt --all -- --check, andcargo clippy -p mlxcel-xla --all-targets -- -D warningspass.Limitations
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.N, not by a per-capacity trace.Closes #2921