Repository navigation
perf(xla): compile the ragged native GEMV per batch bucket - #2959
Merged
Merged
Conversation
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
force-pushed
the
perf/issue-2955-b4-ragged-gemv
branch
from
November 11, 2026 04:59
2b17fe8 to
62be0b6
Compare
inureyes
marked this pull request as ready for review
November 11, 2026 05:03
5 tasks done
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
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=4decode step no longer carries theB=8accumulator unroll. At the #2933 acceptance shape a controlled interleaved A/B on one binary measures a median42.29to43.56 tok/s(about 3 percent) atB=4, with the narrow arm ahead in 5 of 5 paired rounds, whileB=8keeps the unchanged default artifact. Every run is REFERENCE-EXACT.Related issues
Closes #2955
Refs #2933
Type of change
feat— new user-visible featurefix— bug fixperf— performance improvement (before/after numbers below)refactor— internal restructuring without behavior changechore— build, CI, dependencies, release infrastructuredocs— documentation onlytest— tests onlyWhat changed
The ragged native GEMV now compiles one HSACO per batch bucket instead of using the widest compile bound for every bucket.
csrc/qmv4.hipmakesQMV4_MAX_Ban 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, andIreeRaggedLlama::load_innercalls it instead ofhsaco(). The widest bucket (B=8) maps to the default artifact, so it runs the same compile as before; the single-sequence loader still callshsaco(). The emitted graph, the six export names, theBpush 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 --noteson the extracted code objects: every multi-row export loses VGPR, SGPR and shared memory at bound 4 (qmv4_multi_row_kernelVGPR 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_kernel3017 to 1553 instructions, predication branches 495 to 215;qmv4_multi_row_kernel1511 to 810, 196 to 88), while the single-row exports are instruction-for-instruction identical (qmv4_kernel295,qmv4_row_kernel254).rocprofv3 --kernel-traceon the pre-fix binary atB=4andB=8shows the ragged graph dispatchingqmv4_multi_kernel,qmv4_multi_row_kernel,qmv4_qkv_multi_kernelandqmv4_gateup_multi_kernelonce per layer per step (736 calls atB=4over 46 pump steps, 1104 atB=8over 69), with identical runtime resource metadata for both buckets because both embedded the sameQMV4_MAX_B=8artifact.-DQMV4_MAX_B=4artifact 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_kernel3017,qmv4_multi_row_kernel1511,qmv4_qkv_multi_kernel3001,qmv4_qkv_multi_row_kernel1573,qmv4_gateup_multi_kernel3008,qmv4_gateup_multi_row_kernel1607).Measurements
gfx1151, llama-3.2-1b-instruct-4bit, n=256, same checkpoint and prompts as #2933, underscripts/rocm_gpu_guard.sh --hold. Controlled interleaved A/B on one binary, swapping only the embedded HSACO:QMV4_MAX_B=8QMV4_MAX_B=4The fixed binary, with a fresh artifact cache, compiles exactly two artifacts and loads them by bucket:
qmv4-gfx1151-802a2ac3535eaa05-b4.hsaco(83,816 bytes) forB=4and the unsuffixed default (120,168 bytes) forB=8. Measured on that binary, native vs f16 interleaved:The fixed
B=4median reproduces the narrow arm of the controlled A/B (43.56), and arocprofv3trace of that run reportsLDS_Block_Size=512andVGPR_Count=16forqmv4_multi_row_kernelagainst 1024 and 24 in the pre-fix trace.The narrow artifact is 83,816 bytes and its
hipcc --gencotakes 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; theB=8bucket hits the existing cache.The absolute #2933 thresholds are not reproducible on current HEAD
Two effects outside this change:
B=4, not 65.6.MLXCEL_XLA_LOAD_TIMINGputsragged-context-and-uploadat 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.The controlled comparison is therefore the paired one: about +3 percent at
B=4,B=8unchanged 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 newkernel_bound_is_overridable_at_compile_timeandragged_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)mlx-community/Llama-3.2-1B-Instruct-4bitongfx1151,xla_batch_benchatB=4andB=8, REFERENCE-EXACT PASS on every run aboveHIPCC=/nonexistent/hipccand a fresh artifact cache (one warning, f16 graph selected, REFERENCE-EXACT PASS)Notes for reviewers
max_benters the cache key through the file stem, not the content hash: a narrow bucket getsqmv4-<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.report_load_timingcheckpoints were added to the ragged load for this measurement and stay as diagnostics behindMLXCEL_XLA_LOAD_TIMING.B>1was not exercised end to end.TECHNICAL_REPORTS/2955-b4-ragged-gemv-20261111.en.md(and.ko.md).Checklist
perf(xla): ...)docs/for the changed cache-key and compile behavior (docs/environment-variables.md,src/lib/mlxcel-xla/README.md)// Used by: ...comments on any shared function I modified.envfiles committedCloses #2955