Skip to content

perf(xla): compile the ragged native GEMV per batch bucket - #2959

Merged
inureyes merged 1 commit into
mainfrom
perf/issue-2955-b4-ragged-gemv
Nov 11, 2026
Merged

inureyes merged 1 commit into
mainfrom
perf/issue-2955-b4-ragged-gemv

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

The ragged native 4-bit GEMV now compiles one HSACO per batch bucket instead of reusing the widest compile bound for every bucket, so a B=4 decode step no longer carries the B=8 accumulator unroll. At the #2933 acceptance shape a controlled interleaved A/B on one binary measures a median 42.29 to 43.56 tok/s (about 3 percent) at B=4, with the narrow arm ahead in 5 of 5 paired rounds, while B=8 keeps the unchanged default artifact. Every run is REFERENCE-EXACT.

Related issues

Closes #2955

Refs #2933

Type of change

  • feat — new user-visible feature
  • fix — bug fix
  • perf — performance improvement (before/after numbers below)
  • refactor — internal restructuring without behavior change
  • chore — build, CI, dependencies, release infrastructure
  • docs — documentation only
  • test — tests only

What changed

The ragged native GEMV now compiles one HSACO per batch bucket instead of using the widest compile bound for every bucket. csrc/qmv4.hip makes QMV4_MAX_B an overridable default (#ifndef / #define 8 / #endif), native_qmv::hsaco_multi(arch, b_max) compiles the same source with -DQMV4_MAX_B=<bucket> and a cache stem that carries the bound, and IreeRaggedLlama::load_inner calls it instead of hsaco(). The widest bucket (B=8) maps to the default artifact, so it runs the same compile as before; the single-sequence loader still calls hsaco(). The emitted graph, the six export names, the B push constant and the dispatch shape are unchanged.

Root cause

Accumulator width, not predication or dispatch shape. Static evidence from the pinned ROCm tools:

  • hipcc --genco -O3 --offload-arch=gfx1151 -DQMV4_MAX_B={4,8} and /opt/rocm/llvm/bin/llvm-readobj --notes on the extracted code objects: every multi-row export loses VGPR, SGPR and shared memory at bound 4 (qmv4_multi_row_kernel VGPR 19 to 15, SGPR 45 to 34, LDS 1024 to 512 bytes), while the six single-row exports are identical under both bounds.
  • /opt/rocm/llvm/bin/llvm-objdump --mcpu=gfx1151 -d --symbolize-operands: the multi-row exports roughly halve (qmv4_multi_kernel 3017 to 1553 instructions, predication branches 495 to 215; qmv4_multi_row_kernel 1511 to 810, 196 to 88), while the single-row exports are instruction-for-instruction identical (qmv4_kernel 295, qmv4_row_kernel 254).
  • rocprofv3 --kernel-trace on the pre-fix binary at B=4 and B=8 shows the ragged graph dispatching qmv4_multi_kernel, qmv4_multi_row_kernel, qmv4_qkv_multi_kernel and qmv4_gateup_multi_kernel once per layer per step (736 calls at B=4 over 46 pump steps, 1104 at B=8 over 69), with identical runtime resource metadata for both buckets because both embedded the same QMV4_MAX_B=8 artifact.
  • A one-off -DQMV4_MAX_B=4 artifact substituted into the same binary selected the fix: the narrow arm was ahead in all 5 paired rounds at the acceptance shape.

The gain is smaller than the artifact change because predication already suppresses the dead lanes' weight reads and arithmetic; what remains is issue bandwidth, registers and shared memory.

The default-bound artifact is unchanged by this PR: unbundling the artifact built from the new source and comparing normalized disassembly against the pre-fix artifact gives 15,467 instruction lines and 116,072 object bytes on both sides, identical for every export (qmv4_multi_kernel 3017, qmv4_multi_row_kernel 1511, qmv4_qkv_multi_kernel 3001, qmv4_qkv_multi_row_kernel 1573, qmv4_gateup_multi_kernel 3008, qmv4_gateup_multi_row_kernel 1607).

Measurements

gfx1151, llama-3.2-1b-instruct-4bit, n=256, same checkpoint and prompts as #2933, under scripts/rocm_gpu_guard.sh --hold. Controlled interleaved A/B on one binary, swapping only the embedded HSACO:

Shape Default QMV4_MAX_B=8 Narrow QMV4_MAX_B=4 Paired Result
B=4, requests 8, maxcap 24 44.21, 43.81, 35.36, 42.29, 35.23 44.31, 44.47, 43.24, 43.56, 42.45 5 of 5 narrow, median 42.29 to 43.56 tok/s (+3.0 percent) REFERENCE-EXACT PASS
B=4, requests 16, maxcap 48 53.33, 50.61, 50.74 52.05, 52.85, 52.13 2 of 3 narrow, median 50.74 to 52.13 (+2.7 percent) REFERENCE-EXACT PASS
B=8, requests 16, maxcap 48 55.95, 59.24 (pre-fix) same artifact n/a REFERENCE-EXACT PASS

The fixed binary, with a fresh artifact cache, compiles exactly two artifacts and loads them by bucket: qmv4-gfx1151-802a2ac3535eaa05-b4.hsaco (83,816 bytes) for B=4 and the unsuffixed default (120,168 bytes) for B=8. Measured on that binary, native vs f16 interleaved:

Shape Native 4-bit f16 Result
B=4, requests 8, maxcap 24 42.99, 43.65, 43.53 (median 43.53) 22.07, 22.50, 22.45 REFERENCE-EXACT PASS
B=8, requests 16, maxcap 48 60.15, 62.16 29.51, 34.43 REFERENCE-EXACT PASS

The fixed B=4 median reproduces the narrow arm of the controlled A/B (43.56), and a rocprofv3 trace of that run reports LDS_Block_Size=512 and VGPR_Count=16 for qmv4_multi_row_kernel against 1024 and 24 in the pre-fix trace.

The narrow artifact is 83,816 bytes and its hipcc --genco takes 1,032 ms on this host against 2,082 ms for the default bound. A run that loads both buckets pays one extra cached one-second compile per distinct narrow bucket; the B=8 bucket hits the existing cache.

The absolute #2933 thresholds are not reproducible on current HEAD

Two effects outside this change:

  1. The f16 arm of the current binary measures 20.6 to 22.5 tok/s at B=4, not 65.6. MLXCEL_XLA_LOAD_TIMING puts ragged-context-and-upload at about 4.8 s for the f16 graph against about 1.1 s for the native graph, and the pre-perf(xla): run the ragged engine through the native 4-bit GEMV #2933 control binary loads the same checkpoint through the same harness in about 0.7 s. The regression is pre-existing on HEAD and not caused by this change.
  2. The host ran below the perf(xla): run the ragged engine through the native 4-bit GEMV #2933 rates in this window (sequential baselines 34 to 59 tok/s), so even the native absolute numbers are not comparable.

The controlled comparison is therefore the paired one: about +3 percent at B=4, B=8 unchanged by construction, both reference-exact. The f16 ragged-upload cost needs its own issue; it is the reason the #2933 absolute targets cannot be checked here.

Test plan

  • cargo fmt --all -- --check (clean)
  • cargo clippy --workspace --all-targets --features metal,accelerate -- -D warnings (not run: this branch touches the ROCm/XLA path only; scoped clippy below)
  • cargo clippy -p mlxcel-xla --features iree --lib --tests -- -D warnings (clean)
  • cargo test -p mlxcel-xla --lib native_qmv:: (8 passed, including the new kernel_bound_is_overridable_at_compile_time and ragged_buckets_compile_with_their_own_bound)
  • cargo test -p mlxcel-xla --lib native_ragged_decode_emits_multi_row_dispatches (1 passed, unchanged, so the multi-row dispatch shape still holds)
  • cargo test -p mlxcel-xla --lib emitter::builder::tests (15 passed)
  • Validated with a real checkpoint: mlx-community/Llama-3.2-1B-Instruct-4bit on gfx1151, xla_batch_bench at B=4 and B=8, REFERENCE-EXACT PASS on every run above
  • Fallback with HIPCC=/nonexistent/hipcc and a fresh artifact cache (one warning, f16 graph selected, REFERENCE-EXACT PASS)
  • Emission untouched, so the native-absent MLIR goldens are byte-identical

Notes for reviewers

  • The measured gain is a few percent, at the edge of this host's run-to-run noise, not the full ~5 percent gap perf(xla): run the ragged engine through the native 4-bit GEMV #2933 recorded. The window was shared with other GPU work and two default-arm rounds dipped to about 35 tok/s, so the report uses medians and the paired sign test rather than means.
  • max_b enters the cache key through the file stem, not the content hash: a narrow bucket gets qmv4-<arch>-<hash>-b<b>.hsaco, and the default stem keeps its pre-fix form, so an existing warm cache is reused for the widest bucket.
  • Six report_load_timing checkpoints were added to the ragged load for this measurement and stay as diagnostics behind MLXCEL_XLA_LOAD_TIMING.
  • The OpenXLA serve worker at B>1 was not exercised end to end.
  • Detailed analysis: TECHNICAL_REPORTS/2955-b4-ragged-gemv-20261111.en.md (and .ko.md).

Checklist

  • PR title uses a Conventional Commits prefix (perf(xla): ...)
  • One logical change per PR
  • Updated docs/ for the changed cache-key and compile behavior (docs/environment-variables.md, src/lib/mlxcel-xla/README.md)
  • Updated // Used by: ... comments on any shared function I modified
  • No secrets, credentials, or .env files committed

Closes #2955

@inureyes inureyes added type:performance Performance improvements status:done Completed labels Nov 11, 2026
The ragged loader compiled csrc/qmv4.hip once with QMV4_MAX_B=8 and reused that artifact for the B=4 bucket, so a B=4 step carried the B=8 instruction stream, register allocation and 1 KiB shared-memory reduction buffer while half of its accumulator lanes were dead.

Make the bound an overridable default and add native_qmv::hsaco_multi(arch, b_max), which compiles the same source with -DQMV4_MAX_B=<bucket> and a cache stem that carries the bound. The widest bucket maps to the default artifact, so B=8 takes the same compile as before; the single-sequence loader still calls hsaco(). The emitted graph, the six export names and the push-constant ABI are unchanged.

A controlled interleaved A/B on one binary, swapping only the embedded HSACO, measures a median 42.29 to 43.56 tok/s at B=4 (requests 8, maxcap 24) with the narrow artifact ahead in all five paired rounds, and 50.74 to 52.13 at requests 16. Every run is REFERENCE-EXACT. The absolute #2933 targets are not reproducible on current HEAD because the f16 ragged upload takes about 4.8 s; that is pre-existing and out of scope.

Closes #2955
@inureyes
inureyes force-pushed the perf/issue-2955-b4-ragged-gemv branch from 2b17fe8 to 62be0b6 Compare November 11, 2026 04:59
@inureyes
inureyes marked this pull request as ready for review November 11, 2026 05:03
@inureyes
inureyes merged commit 2f912ca into main Nov 11, 2026
31 checks passed
@inureyes
inureyes deleted the perf/issue-2955-b4-ragged-gemv branch November 11, 2026 05:03
@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:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(xla): recover the B=4 ragged decode loss in the multi-row native GEMV

1 participant