Repository navigation
perf(xla): give the narrow decode vecmats one output per thread (#2873) - #3116
Conversation
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.
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
|
Security and performance review of No security finding: the spec is a static asset gated on Verified with the pinned Addressed (MEDIUM, observability): the only guard against a matcher that still parses but stops matching was a textual assertion, which cannot see it. Not changed: the N band, the |
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
Finalization passAdded in
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. Validation: |
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.
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 <= 8192gets one output per thread andreduction = [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 forN >= 8193withK >= 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 ofvecmat_2048x2048under both specs shows one serialv_fma_mix_f32chain 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_QUANTunset)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):b21825cf+80.25%, won in 6 of 6 rounds. A rocprofv3
n=1vsn=128per-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
Changes during review
8beb4208pins the wide matcher'sK >= 4096gate 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.1a2814farecords in the README that the two trace arms are not interleaved, so the paired A/B is the authoritative figure.838c19cfadds 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 with128 <= N <= 8192carries the narrow tile while the 128256-wide head does not. Sabotagingumaxmakes it fail, so it observes the matcher rather than restating it.0e363583adds the technical report in English and Korean, the Unreleased changelog entry, and the refreshed f16 baseline indocs/environment-variables.md(theMLXCEL_XLA_QUANTrow still presented 31.5 tok/s as the current f16 rate).32ef48f2corrects 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 pinned3.12.0rc20260721compilercargo test -p mlxcel-xla --lib emitter::(216 passed, 17 ignored)cargo fmt -p mlxcel-xla -- --checkandcargo clippy -p mlxcel-xla --all-targets -- -D warningsscripts/xla/validate_arch.sh --model models/llama-3.2-1b-instruct-4bit --device hip --batch 8 --requests 16 --maxcap 48: structural, token-exact and serveREFERENCE-EXACTall PASS, 16 of 16 requests matching their single-sequence referencetext EQUAL; greedy output unchanged on llama-3.2-1b and qwen2.5-coder-7breduction = [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 executablesCloses #2873