Repository navigation
perf(xla): pack the LM head and retile the native decode GEMV - #2892
Merged
Merged
Conversation
MLXCEL_XLA_QUANT=native now runs the LM head through the same HIP GEMV as the projections, when the checkpoint stores it 4-bit (decided from the head tensor's dtype, not config.json). A tied checkpoint re-reads embed's raw parts; an untied one reads lm_head's. Prefill keeps a loader-dequantized [H, V] operand declared alongside the packed triple, because reconstructing the vocabulary matrix in the graph cost about 12 s per request; both forms sit in the shared weight list so every graph kind keeps one argument order. The kernel splits by row width. Below K = 4096 each warp owns an output row, a workgroup covers eight rows, lanes load 16 bytes (uint4) at a time, and the reduction is a warp shuffle with no shared memory. At K >= 4096 (the down projection) that layout halved the memory parallelism in flight and measured 97 us against 59 for the block-per-row form, so a second export keeps the whole workgroup on one row and the emitter picks it by K. The module now carries both exports in one HSACO. On gfx1151 with llama-3.2-1b-instruct-4bit (greedy, n=128, capacity 256, warm vmfb) decode rises from 80.3 to 113 tok/s (109 to 118 across rounds) against 31.5 for the f16 path, 3.6x. A rocprofv3 trace puts the step at about 7.1 ms against 9.9 before: the LM head drops from 2.35 ms to 0.62 (131 MB packed instead of a 525 MB f16 stream), the projections total 3.7 ms, attention 1.3 and dispatch gaps 1.6. Greedy output stays byte-identical to the f16 path on two prompts, on the tied llama checkpoint and on untied qwen2.5-coder-7b; xla_batch_bench stays REFERENCE-EXACT at B=8 with the switch set. The kernels were validated against a double-precision reference through iree-run-module (max relative error 1.3e-5 at K=2048 and 2.1e-4 at K=8192), and a checkpoint whose head is not U32 keeps the f16 head path. Refs #2891, #2889
inureyes
force-pushed
the
perf/native-head-tiling
branch
from
November 9, 2026 10:40
8361ff8 to
9766283
Compare
This was referenced Nov 9, 2026
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
Implements #2891, stacked on #2890 (base branch
feat/2889-qmv4-nativeuntil that one merges, then retargeted tomain). Two changes to the native decode path:config.json), the decode graph declares the packed[V, H/8]tensor plus its f16 scales/biases and dispatches the same kernel with N = vocabulary. A tied checkpoint re-readsembed's raw parts and an untied one readslm_head's. Prefill keeps a loader-dequantized[H, V]operand declared next to the packed triple: reconstructing the vocabulary matrix in-graph cost about 12 s per request, so both forms sit in the shared weight list and every graph kind keeps one argument order.uint4) per iteration and the reduction is a warp shuffle with no shared memory or barrier. At K >= 4096 (the down projection) that layout underperformed, so a second export keeps the whole workgroup on one row and the emitter picks it by K; both exports live in the one HSACO and the module carries both.Measurements
gfx1151, llama-3.2-1b-instruct-4bit, greedy,n=128,MLXCEL_XLA_CONTEXT_CAPACITY=256, warm vmfb, two rounds each:rocprofv3per decode step, before and after:Per-shape medians in the new kernel: k/v 4.8 us (was 5.7), q/o 14.3 (16.5), gate/up 43.4 (64.7), head 620 us for 131 MB (211 GB/s), down 62.1 us through the block-per-row export (59 before, 97 with the warp layout).
Validation
Qwen2.5-Coder-7B-Instruct-4bit.iree-run-moduleand the real IREE runtime: max relative error 1.3e-5 at K=2048 (warp export) and 2.1e-4 at K=8192 (row export, error dominated by cancellation on a small value).xla_batch_benchon hip withMLXCEL_XLA_QUANT=nativeset:REFERENCE-EXACT PASSat B=8 (16 requests), so the batch engine still uses the f16 path unchanged.cargo fmt --all -- --check;cargo check --features xla-backend,xla-ireeunderRUSTFLAGS=-D warnings;check_env_var_docs.py.Follow-ups
Still out of scope: fusing q/k/v and gate/up into multi-output dispatches (dispatch gaps and 113 launches per step), a multi-row GEMV for the ragged batch engine, and prefill.
Refs #2891, #2889