Skip to content

perf(xla): raise the K tile for long-K vecmats on ROCm - #2874

Merged
inureyes merged 1 commit into
mainfrom
perf/2873-rocm-vecmat-tuning
Nov 9, 2026
Merged

inureyes merged 1 commit into
mainfrom
perf/2873-rocm-vecmat-tuning

Conversation

@inureyes

@inureyes inureyes commented Nov 6, 2026

Copy link
Copy Markdown
Member

Summary

The pinned IREE gives every single-row contraction reduction = [0, 4]. That tile is fine for short-K shapes but leaves the down projection (K = intermediate, 8192 on llama-3.2-1b) at about 50 GB/s, while the same shape reaches about 110 GB/s at reduction = [0, 32]. This PR adds a user tuning spec that raises the tile for exactly those contractions.

First part of #2873; the argmax (3.87 ms per step, single workgroup of one lane) is the next item and is not addressed here.

Changes

  • assets/rocm/bigk_vecmat_tuning.mlir: a transform-dialect tuning spec that matches rank-1 by rank-2 f16/f16 -> f32 contractions with no batch dims and a reduction axis of at least 4096 elements, and annotates them with reduction = [0, 32] at the compiler's default workgroup and thread tiles.
  • tuning.rs: writes the spec into the compile cache under a content-hashed name and returns --iree-codegen-tuning-spec-path. The hash is in both the file name and the flag, so the vmfb cache key changes with the spec. Kept outside the iree feature gate so its test runs in the default lib suite.
  • compile_bundle: adds the flag for hip targets on dense configs only. Gemma3n keeps the compiler defaults, and CPU, CUDA and Metal are untouched.

Why the match is K-bounded

A standalone sweep on the pinned compiler (same runtime path, one dispatch per run):

shape default [0, 4] [0, 32]
down proj (N=2048, K=8192) 667 us 304 us
gate/up (N=8192, K=2048) 234 us 216 us
qkv/o (N=2048, K=2048) 88 us 155 us (worse)
N=512, K=2048 97 us 139 us (worse)
LM head (K=2048) 2439 us neutral
split-K (partial_reduction) 667 us 5221 us (not used)

Workgroup-size changes did nothing on the down-projection shape (all about 650 us), and split-K was 5-9x slower, so the reduction tile is the lever and the K bound is what keeps it from regressing the short-K shapes.

Validation (gfx1151, hip, llama-3.2-1b-instruct-4bit, release build)

case before after
decode step (profiled) 39.6 ms, 317 dispatches 27.45 ms, 317 dispatches
down projection per step (16 calls) 13.42 ms 4.79 ms
B=1 CLI n=64, capacity 256 21.6-25.1 tok/s 23.1-27.5 tok/s
B=1 CLI n=128, capacity 256 21.3-27.8 tok/s 27.8-28.0 tok/s (32.96 standalone)
batch B=4 N=8 65.4-67.7 tok/s 66.4-67.5 tok/s, REFERENCE-EXACT PASS
batch B=8 N=16 74.3-84.1 tok/s 70.1-75.3 tok/s, REFERENCE-EXACT PASS
  • Greedy output token-identical to the pre-change binary on llama-3.2-1b and qwen3-1.7b (tied) and mistral-7b-instruct-v0.3 (untied).
  • The ragged decode vmfb byte size is unchanged by the spec (534282 bytes before and after), so the batch graph is not modified by the match.
  • cargo test -p mlxcel-xla --lib 388 passed (new: the content-addressed spec flag), committed goldens unchanged.
  • cargo clippy -p mlxcel-xla --all-targets --features iree -- -D warnings and at default features clean; cargo fmt --check clean.

Refs #2873

The pinned IREE gives every single-row contraction `reduction = [0, 4]`. That tile is fine for short-K shapes but leaves the down projection (K = intermediate, 8192 on llama-3.2-1b) at about 50 GB/s, while the same shape reaches about 110 GB/s at `reduction = [0, 32]`. On a standalone trace of `x[8192] x W[8192, 2048] f16 -> f32` the tile moves the kernel from 667 to 304 us; in the model the 16 down projections fall from 13.4 to 4.8 ms per decode step.

A user tuning spec now raises that tile for exactly those contractions and leaves everything else on the compiler default. It matches rank-1 by rank-2 `f16`/`f16` -> `f32` contractions with no batch dims and a reduction axis of at least 4096 elements (`transform.iree.match.dim_bounds`), so the K=2048 shapes keep the default: the same wider tile measured 1.75x slower on qkv/o (N=2048, K=2048), 1.43x slower on N=512, and neutral on the head, while split-K (`partial_reduction`) was 5-9x slower and is not used.

The spec is a plain MLIR asset (`assets/rocm/bigk_vecmat_tuning.mlir`). `tuning.rs` writes it to the compile cache under a content-hashed name and returns `--iree-codegen-tuning-spec-path`, so changing the spec changes the vmfb cache key instead of reusing a stale build. `compile_bundle` adds the flag for hip targets on dense configs only; Gemma3n keeps the compiler defaults, and CPU, CUDA and Metal are untouched.

Validation on gfx1151 (hip, llama-3.2-1b-instruct-4bit, release build): the decode step drops from 39.6 to 27.45 ms, the down projection from 13.4 to 4.8 ms, and dispatches stay at 317. B=1 CLI at capacity 256: n=64 23.1-27.5 tok/s and n=128 27.8-28.0 tok/s (32.96 in a standalone profiled run), against 21.6-25.1 and 21.3-27.8 before. 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 66.4-67.5 and B=8 N=16 70.1-75.3 tok/s, both REFERENCE-EXACT, and the ragged vmfb byte size is unchanged by the spec. `cargo test -p mlxcel-xla --lib` 388 passed with unchanged goldens; clippy clean at default and `iree` features.

The argmax (3.87 ms per step, single workgroup of one lane) is the next item in #2873 and is not addressed here.

Refs #2873
@inureyes inureyes added type:performance Performance improvements status:review Under review labels Nov 6, 2026
@inureyes
inureyes merged commit d7022d5 into main Nov 9, 2026
31 checks passed
@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

status:review Under review type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant