Repository navigation
perf(xla): split the on-device argmax into two parallel stages - #2875
Merged
Merged
Conversation
IREE lowers a rank-1 reduction to one workgroup of one lane, so the sampling argmax ran as a serial scan of the whole vocabulary: 3.87 ms of a 27.5 ms decode step on gfx1151 for llama-3.2-1b (128256 logits). The standalone trace confirms it: `reduction_128256` with grid and workgroup both `(1,1,1)`. `Builder::argmax` now reshapes `[V]` to `[C, W]` (W the largest power of two up to 512 that divides V), argmaxes each row with an in-row iota, maps the row indices back to the vocabulary (`chunk * W + index`), and argmaxes the C partials. Both stages use the same NaN-propagating, lowest-index tie-break reducer as before, so the composite is the same first-max argmax. The standalone measurement of the same two stages is 41.7 us against 4519 us for the single reduce (108x). Vocabularies below 4096, odd vocabularies, and widths below 32 keep the unsplit reduce. The batched (ragged) argmax is a separate op with the batch axis as an outer parallel dimension and is unchanged. Validation on gfx1151 (hip, llama-3.2-1b-instruct-4bit, release build): the decode step drops from 27.45 to 24.63 ms and dispatches go from 317 to 318 (the extra stage); the argmax no longer appears among the top kernels. B=1 CLI at capacity 256: n=64 29.5-32.3 tok/s and n=128 30.1-33.8 tok/s (36.45 in a standalone profiled run), against 23.1-27.5 and 27.8-28.0. Greedy output is token-identical on llama-3.2-1b and qwen3-1.7b (tied) and mistral-7b-instruct-v0.3 (untied). Batch B=4 N=8 and B=8 N=16 still pass REFERENCE-EXACT at 66.6 and 69.3 tok/s. The committed llama-3.2-1b decode and prefill goldens are re-frozen for the new tail; the diff is the argmax region only, and both graphs compile with the pinned compiler. Small-vocabulary fixtures keep the unsplit form, so their goldens are unchanged. `cargo test -p mlxcel-xla --lib` 390 passed (two new: the split emits two reduces and the index remap, small and odd vocabularies keep one). Refs #2873
inureyes
added a commit
that referenced
this pull request
Nov 9, 2026
`make verify-clippy` fails on a local Metal build with `clippy::manual_is_multiple_of` at `argmax_chunk_width` (added in #2875). No behaviour change: `vocab` is a `usize` and the divisor is non-zero. Workspace clippy with `metal,accelerate` and `cargo fmt --check` pass after the change.
6 tasks done
inureyes
added a commit
that referenced
this pull request
Nov 18, 2026
… (#3116) * perf(xla): give the narrow decode vecmats one output per thread (#2873) The compiler default for a rank-1 vecmat is `thread = [8, 0]`, `workgroup = [256, 0]` for every output width at or above 2048, so the decode projections launched N/256 one-warp workgroups: 8 for the 2048-wide q, o and down projections and 32 for gate/up. That is under one wave on a 20-WGP gfx1151 and the serial K reduction (default chunk 4) cannot hide memory latency, so q/o ran at 42 GB/s where the 128256-wide LM head, which launches 501 workgroups, reached 153 GB/s in the same trace. The 512-wide k and v, where the compiler already picks one output per thread, sat at 44 GB/s on the 4-wide chunk. The tuning spec now gives every single-row vecmat whose output width is between 128 and 8192 one output per thread with `reduction = [0, 32]`, whatever K is. The tile does not depend on the reduction length (each output still walks K serially; only the output count sets the occupancy), so one matcher covers both hidden sizes that were measured. The LM head and wider long-K shapes keep the previous config because their activation re-read grows with N. On llama-3.2-1b-instruct-4bit at capacity 256 the untraced decode step drops from 24.06 to 13.54 ms (41.6 to 73.5-74.5 tok/s) and traced kernel time from 24.44 to 12.48 ms at the same 279 dispatches: q/o 42 -> 219 GB/s, down 104 -> 230, gate/up 140 -> 196. On the untied qwen2.5-coder-7b-instruct-4bit the step goes from 12.28 to 15.44-15.46 tok/s; the first version of the matcher bounded K at the 1b model's 2048 and reached only that checkpoint's down projection, which made 12.94. No dispatch is added and no weight layout changes. IREE's split-reduction path cannot express a workgroup-level K split for these rank-1 generics (it needs a matmul-like op with at least two parallel loops), and a lowering config can only tile the reduction inside a workgroup. The k/v projections stay at 81 GB/s useful, 162 counting the re-read, because N = 512 caps one output per thread at 16 warps; a concatenated k/v upload-time layout is left to a follow-up. The issue's argmax item was already fixed on this main by #2875: the traced two-stage reduce measures 0.030 ms per step, well under the 0.2 ms target, so this change does not touch it. Greedy output is unchanged on llama-3.2-1b-instruct-4bit and qwen2.5-coder-7b-instruct-4bit. * test(xla): pin the wide vecmat predicates and correct the tuning notes Review of the #2873 split found the wide fallback's K >= 4096 gate and its output-width axis unasserted, so a later edit could put the wide tile on every LM head (including llama's K = 2048 one that is deliberately left on the compiler default) without failing a test. The test now pins both bounds on %rhs with their axes, next to the narrow matcher's assertions. The docs said a 3584-wide projection launches 8 workgroups where 3584/256 is 14, and presented gate/up's 140 to 196 GB/s as a gain although the unchanged head moves 153 to 228 GB/s between the same two traces. The head-relative ratios are now recorded, the 7b paragraph notes its 48-token greedy output is unchanged, and the stale long-K comments in iree.rs and the spec header name all four matchers. Validation: cargo test -p mlxcel-xla --lib tuning:: (4 pass) and --lib emitter:: (216 pass) under scripts/rocm_gpu_guard.sh --hold. No behavior change: the spec's matchers and lowering configs are untouched. * docs(xla): name the authoritative figure for the #2873 breakdown The trace pair behind the per-kernel table is not interleaved, and the unchanged LM head moves 1.49x between the two runs on its own. The README now says which reading it asserts: the trace total overstates the change, and the paired interleaved A/B is the figure that carries it, at 41.05 to 73.98 tok/s over 6 rounds with identical greedy text. * test(xla): observe the vecmat tuning spec on the emitted decode graph The tuning-spec assertions are textual, and the failure this asset can produce silently is a matcher that still parses but stops matching: the graph compiles on the compiler default and only throughput changes, so those assertions stay green. This adds an ignored test that emits the single-sequence decode graph the hip backend compiles (InOut layout, f16 projections), runs the content-addressed spec through the version-matched iree-compile with --iree-hal-dump-executable-configurations-to, and asserts every vecmat dispatch with 128 <= N <= 8192 carries reduction = [0, 32], thread = [1, 0], workgroup = [32, 0] while the 128256-wide LM head does not. It cross-compiles for gfx1151 unless MLXCEL_XLA_ROCM_TARGET names another arch. Verified: cargo test -p mlxcel-xla --lib tuning:: (4 pass, 1 ignored); the ignored test passes against the pinned 3.12.0rc20260721 compiler and fails with "lost the narrow tile" when umax is temporarily narrowed to 64, so it observes the matcher rather than restating it. Refs #2873 * docs(xla): record the #2873 decode tuning and refresh the f16 baseline The crate README and the tuning asset describe the new narrow single-row tile, but the change had no technical report and the environment-variable reference still presented 31.5 tok/s as the current f16 decode rate. Add the report in English and Korean following the recent PR convention, and state the measured 74 tok/s baseline where `MLXCEL_XLA_QUANT` is documented. Add the Unreleased changelog entry for the default-path improvement and note in the native GEMV entry that its comparison baseline moved. No test or source change: the tuning, emitter and lint gates are unchanged from 838c19c. Validation: - cargo fmt -p mlxcel-xla -- --check (clean) - cargo clippy -p mlxcel-xla --all-targets -- -D warnings (clean) - cargo test -p mlxcel-xla --lib tuning:: (4 passed, 1 ignored as documented) Refs #3116, #2873 * docs(reports): state the change statistics the PRs actually merged The #3116 report counted the pre-report surface (4 files, +305/-27) and the already-merged #3106 report said 6 files, +156/-24 where its code-only surface is 5 files, +342/-12; both disagreed with the squash statistics of their own PRs. Each now states the surface it covers and excludes the report files explicitly.
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
IREE lowers a rank-1 reduction to one workgroup of one lane, so the sampling argmax ran as a serial scan of the whole vocabulary: 3.87 ms of a 27.5 ms decode step on gfx1151 for llama-3.2-1b (128256 logits). The standalone trace confirms the shape:
reduction_128256with grid and workgroup both(1,1,1).This PR splits the argmax into two stages so the vocabulary scan is parallel, and keeps the exact same sampling semantics.
Changes
Builder::argmaxreshapes[V]to[C, W](W the largest power of two up to 512 that divides V), argmaxes each row with an in-row iota, maps the row indices back to the vocabulary (chunk * W + index), and argmaxes the C partials. Both stages use the same NaN-propagating, lowest-index tie-break reducer, so the composite result is the same first-max argmax as the unsplit form.argmax_unsplit).Effect
Standalone
[128256] f32argmax on gfx1151, same reducer, one run each underrocprofv3:Validation (gfx1151, hip, llama-3.2-1b-instruct-4bit, release build)
iree-compileforgfx1151. Small-vocabulary fixtures keep the unsplit form, so their goldens are unchanged.cargo test -p mlxcel-xla --lib390 passed (two new: the split emits two reduces plus the index remap; small and odd vocabularies keep one reduce).Note for review
The golden suite checks byte-exactness, not validity: the first re-freeze recorded a
stablehlo.reshapetype annotation the compiler rejects, which the unit test did not catch. The graph was compiled with the pinned compiler before the release build, and the final goldens are the corrected form.Refs #2873