Skip to content

perf(xla): pack the LM head and retile the native decode GEMV - #2892

Merged
inureyes merged 1 commit into
mainfrom
perf/native-head-tiling
Nov 9, 2026
Merged

inureyes merged 1 commit into
mainfrom
perf/native-head-tiling

Conversation

@inureyes

@inureyes inureyes commented Nov 9, 2026

Copy link
Copy Markdown
Member

Summary

Implements #2891, stacked on #2890 (base branch feat/2889-qmv4-native until that one merges, then retargeted to main). Two changes to the native decode path:

  • The LM head joins the GEMV. When the checkpoint stores the head 4-bit (checked from the tensor dtype, not 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-reads embed's raw parts and an untied one reads lm_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.
  • The kernel is retiled by row width. Below K = 4096 each warp owns one output row, a workgroup covers eight rows, each lane loads 16 bytes (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:

path tok/s
f16-resident (default) 31.3, 32.2
native (#2890) 80.6, 80.1
native (this PR) 109.0, 118.1

rocprofv3 per decode step, before and after:

#2890 this PR
LM head 2.35 ms (525 MB f16) 0.62 ms (131 MB packed)
projections 3.82 ms 3.72 ms (head included)
attention 1.31 ms 1.29 ms
dispatch gaps 2.0 ms 1.6 ms
kernel total 7.95 ms 5.46 ms
step 9.9 ms 7.1 ms

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

  • Greedy output byte-identical to the f16 path on two prompts on the tied llama-3.2-1b-instruct-4bit checkpoint, and on untied Qwen2.5-Coder-7B-Instruct-4bit.
  • Both kernels validated against a double-precision reference through iree-run-module and 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_bench on hip with MLXCEL_XLA_QUANT=native set: REFERENCE-EXACT PASS at B=8 (16 requests), so the batch engine still uses the f16 path unchanged.
  • 396 lib tests pass; new tests cover the packed head's weight-spec triple plus prefill operand (tied and untied), the prefill fallback form, the export selection at the K threshold, both exports in the module prefix, and the kernel source contract.
  • cargo fmt --all -- --check; cargo check --features xla-backend,xla-iree under RUSTFLAGS=-D warnings; check_env_var_docs.py.
  • A head tensor that is not U32 keeps the f16 head path (the loader logs which form it chose).

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

@inureyes inureyes added status:review Under review type:performance Performance improvements labels Nov 9, 2026
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
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