Skip to content

perf(xla): give the narrow decode vecmats one output per thread (#2873) - #3116

Merged
inureyes merged 6 commits into
mainfrom
perf/issue-2873-narrow-vecmat-thread-tile
Nov 18, 2026
Merged

inureyes merged 6 commits into
mainfrom
perf/issue-2873-narrow-vecmat-thread-tile

Conversation

@inureyes

@inureyes inureyes commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Summary

The default f16-resident decode graph ran its per-layer vecmats at 42 GB/s while the 128256-wide LM head reached 153 GB/s in the same trace. IREE gives a rank-1 vecmat thread = [8, 0], workgroup = [256, 0] for every output width at or above 2048, so the projections launched N/256 one-warp workgroups: 8 for the 2048-wide q, o and down, 32 for gate/up. That is under one wave on a 20-WGP gfx1151, and the serial K reduction cannot hide memory latency.

This changes the ROCm tuning spec so every single-row f16-by-f16-to-f32 contraction with 128 <= N <= 8192 gets one output per thread and reduction = [0, 32], whatever K is: 64 warps for q, o and down, 256 for gate/up, 16 for the 512-wide k and v. The tile does not depend on the reduction length, so one matcher covers the hidden sizes that were measured (2048 and 3584). The previous wide tile stays for N >= 8193 with K >= 4096, so the LM head keeps the config it had. No dispatch is added, the weights are not rearranged, and the reduction order is untouched: the disassembly of vecmat_2048x2048 under both specs shows one serial v_fma_mix_f32 chain over ascending K per output in both, and raising the chunk from 4 to 32 changes unrolling, not association.

Measured (gfx1151, capacity 256, MLXCEL_XLA_QUANT unset)

The effect size is the paired interleaved A/B (scripts/xla/ab_decode_pair.sh, llama-3.2-1b-instruct-4bit, n=128, 6 rounds, text EQUAL):

tok/s
b21825cf 41.05 (median)
this branch 73.98 (median)

+80.25%, won in 6 of 6 rounds. A rocprofv3 n=1 vs n=128 per-kernel subtraction gives the attribution: the untraced step falls from 24.06 to 13.54 ms and traced kernel time from 24.44 to 12.48 ms at the same 279 dispatches, with q/o 42 to 219 GB/s, down 104 to 230, gate/up 140 to 196, k/v 44 to 81 (162 counting the activation each thread re-reads). The trace arms are separate runs and the unchanged head moves 153 to 228 GB/s between them, so the per-kernel columns are read through the head as a same-run control: q/o (0.27 to 0.96 of the head) and down (0.68 to 1.01) sit outside that shift, gate/up (0.92 to 0.86) is not separable from it, and the trace's 1.96x total overstates the change, which is why the A/B above is the figure this PR asserts.

On the untied qwen2.5-coder-7b-instruct-4bit (hidden 3584) decode goes from 12.28 to 15.44 to 15.46 tok/s with greedy output unchanged over 48 tokens; gate/up at N = 18944 and the 152064-wide head keep the compiler default there, which is why that checkpoint gains 26% rather than the 1b model's 77%.

What this does not do

  • The argmax item in perf(xla): close the IREE decode-kernel efficiency gap (small-M vecmats and the serial argmax) #2873 was already fixed on main by perf(xla): split the on-device argmax into two parallel stages #2875, whose two-stage reduce measures 0.030 ms per step. This PR does not touch it; the new README section records the measurement.
  • A workgroup-level K split for these rank-1 generics is not expressible in the tuning spec: IREE's split-reduction path requires a matmul-like op with at least two parallel loops, and a lowering config can only tile the reduction inside a workgroup. The occupancy fix is what these shapes allow.
  • k and v remain at 81 GB/s useful because N = 512 caps one output per thread at 16 warps. Raising that needs a concatenated k/v (or q/k/v) upload-time weight layout, which the 0.8 ms per step bounds at roughly 0.5 ms of headroom, and is left as a follow-up.

Changes during review

  • 8beb4208 pins the wide matcher's K >= 4096 gate and its output-width axis on %rhs (a later edit could have put the wide tile on llama's K = 2048 head without failing a test), corrects a 3584/256 arithmetic error in the spec comments, and replaces gate/up's apparent gain with the head-relative ratios above.
  • 1a2814fa records in the README that the two trace arms are not interleaved, so the paired A/B is the authoritative figure.
  • 838c19cf adds an #[ignore]d compiler-backed test: a tuning spec can parse, apply and match nothing, and only throughput changes, which no textual test can see. The test emits the real decode graph, compiles it with the pinned compiler, and asserts every vecmat dispatch with 128 <= N <= 8192 carries the narrow tile while the 128256-wide head does not. Sabotaging umax makes it fail, so it observes the matcher rather than restating it.
  • 0e363583 adds the technical report in English and Korean, the Unreleased changelog entry, and the refreshed f16 baseline in docs/environment-variables.md (the MLXCEL_XLA_QUANT row still presented 31.5 tok/s as the current f16 rate).
  • 32ef48f2 corrects the change statistics in this PR's report and in the already-merged perf(xla): keep the decode KV cache in the contraction precision #3106 report, which both disagreed with their own squash statistics.

Test plan

  • cargo test -p mlxcel-xla --lib tuning:: (4 pass, 1 ignored) and -- --include-ignored (5 pass, 1.39 s) with the pinned 3.12.0rc20260721 compiler
  • cargo test -p mlxcel-xla --lib emitter:: (216 passed, 17 ignored)
  • cargo fmt -p mlxcel-xla -- --check and cargo clippy -p mlxcel-xla --all-targets -- -D warnings
  • scripts/xla/validate_arch.sh --model models/llama-3.2-1b-instruct-4bit --device hip --batch 8 --requests 16 --maxcap 48: structural, token-exact and serve REFERENCE-EXACT all PASS, 16 of 16 requests matching their single-sequence reference
  • Paired A/B above, text EQUAL; greedy output unchanged on llama-3.2-1b and qwen2.5-coder-7b
  • Executable-configuration dump before and after: q, k, v, o and down take reduction = [0, 32], thread = [1, 0], workgroup = [32, 0], the 128256-wide head keeps [0, 4]/[8, 0]/[256, 0], both GQA attention configs and every other dispatch are byte-identical, and both arms hold 52 executables

Closes #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.
@inureyes inureyes added type:performance Performance improvements priority:high High priority area:inference Generation, sampling, decoding (incl. speculative, DRY) status:review Under review labels Nov 16, 2026
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.
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.
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
@inureyes

Copy link
Copy Markdown
Member Author

Security and performance review of 1a2814fa, plus 838c19cf.

No security finding: the spec is a static asset gated on device == "hip" && RuntimeConfig::Dense(_); no model file, request or environment other than device selection reaches it, and a malformed spec fails the compile loudly (compile_one propagates the error), so there is no silent fallback.

Verified with the pinned 3.12.0rc20260721 compiler. The emitted hip single-sequence decode graph (InOut, f16) selects exactly the intended configs: q/o, k/v, gate/up and down take reduction = [0, 32], thread = [1, 0], workgroup = [32, 0]; the 128256-wide head keeps the default [0, 4]/[8, 0]/[256, 0]; the attention matchers and every other dispatch are unchanged; 52 executables in both arms. The bands are disjoint (umax is closed in IREE) and a rank-1 matcher cannot reach the rank-3 attention matmuls. The numerics claim also holds: the generated AMDGPU assembly accumulates each output in one serial FMA chain over ascending K for both tiles, so the chunk change from 4 to 32 does not reorder the sum.

Addressed (MEDIUM, observability): the only guard against a matcher that still parses but stops matching was a textual assertion, which cannot see it. 838c19cf adds an ignored compiler-backed test that emits the decode graph, compiles it with the spec and asserts the dumped configurations; it passes normally and fails when umax is narrowed to 64.

Not changed: the N band, the N >= 128 floor (measured, and the tile cannot be worse than the default there), the spec's arch-independent application (pre-existing policy), and the ROCM_BIGK_VECMAT_TUNING name (renaming would churn the cache name for no behavior change). gate/up at N = 8192 is the one number I could not tighten: its bandwidth relative to the unchanged head fell from 0.92 to 0.86, already recorded as not separable from the clock shift.

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
@inureyes

Copy link
Copy Markdown
Member Author

Finalization pass

Added in 0e363583:

  • TECHNICAL_REPORTS/3116-narrow-vecmat-thread-tile-20261118.{en,ko}.md: the report the recent PR series (#3106, #3065, #3109, #3111) carries and this one lacked. It records the before/after per-kernel table, the paired A/B effect size, the predicate design, and the tests.
  • CHANGELOG.md: an Unreleased Changed entry for the default-path improvement, plus a note on the native GEMV entry that its 31.5 tok/s comparison baseline has moved to about 74.
  • docs/environment-variables.md: the MLXCEL_XLA_QUANT row described 31.5 tok/s as the current f16 decode rate. It now states the introduction-time figure and the current ~74 tok/s baseline.

Judged unnecessary: no test was added. The textual tests pin both predicates and the compiler-backed test observes the emitted decode graph, so the remaining gap (the wide matcher on a K >= 4096 head) would need a second expensive compiler run for a config this PR leaves unchanged. docs/benchmark_results/xla_iree_rocm_qualification.md is a dated 2026-10-14 qualification record with a retention note, not a current figure.

Validation: cargo fmt -p mlxcel-xla -- --check, cargo clippy -p mlxcel-xla --all-targets -- -D warnings, and cargo test -p mlxcel-xla --lib tuning:: (4 passed, 1 ignored as documented).

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.
@inureyes
inureyes merged commit c27b86f into main Nov 18, 2026
32 checks passed
@inureyes
inureyes deleted the perf/issue-2873-narrow-vecmat-thread-tile branch November 18, 2026 00:34
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 18, 2026
@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): close the IREE decode-kernel efficiency gap (small-M vecmats and the serial argmax)

1 participant