Repository navigation
Description
Context
PRs #2890 and #2892 brought MLXCEL_XLA_QUANT=native up on ROCm: the packed 4-bit projections and LM head dispatch csrc/qmv4.hip through hal.executable.source @custom_qmv, and llama-3.2-1b greedy decode now runs at 113 tok/s with a 7.1 ms decode step on gfx1151 (kernel time 5.46 ms, inter-dispatch gaps 1.6 ms).
The step still issues 113 GEMV dispatches (seven per layer for q, k, v, o, gate, up, down across 16 layers, plus the head). q, k and v read the same activation row and differ only in the output width, and so do gate and up. Per-shape medians from the trace: k/v 4.8 us, q/o 14.3 us, gate/up 43.4 us, down 62.1 us (block-per-row export), head 620 us.
Work
- Add two multi-output kernel exports to
csrc/qmv4.hip, reusing the same HSACO:qmv4_qkv_kernel(bindings: x, three packed/scales/biases sets, three outputs; constants K, N0, N1, N2) andqmv4_gateup_kernel(x, two sets, two outputs; constants K, N0, N1). Each keeps the warp-per-row layout ofqmv4_kernel(these projections have K = hidden < 4096) and maps a global output row to its matrix by cumulative N. Both can live in one.hipand be embedded through two additionalhal.executable.sourceblocks, because the pipeline layout (binding count) differs per export; the same HSACO bytes are referenced from each. - Emitter: when every weight in a group is a registered packed operand and native mode is on, emit one dispatch with the fused entry point instead of one per projection; otherwise fall back to the per-projection path unchanged. The workgroup count is
ceil((N0 + N1 + ...) / ROWS_PER_BLOCK). Wire the standard dense q/k/v and gate/up sites through a pair of helpers so the fused and unfused forms produce identical tokens. - Keep the existing single-output exports for o, down, the head, and any site that is not fused.
Expected
About 65 dispatches per step (four per layer plus the head) against 113 today, with the launch gaps dropping from about 1.6 ms toward 0.9 ms and the three (respectively two) reads of the activation row becoming one. A step-time gain of roughly 8 to 12 percent is the bar, measured with the same trace protocol.
Acceptance criteria
- Greedy output token-identical to the f16 path and to the pre-fusion native path on the tied llama-3.2-1b-instruct-4bit checkpoint and on one untied checkpoint, on at least two prompts.
rocprofv3before/after pasted into the PR: dispatches per step, gap time, per-kernel time, and tok/s atMLXCEL_XLA_CONTEXT_CAPACITY=256,n=128.xla_batch_benchREFERENCE-EXACT at B=4 and B=8 with the switch set; the f16 path, prefill and the committed goldens unchanged.- Unit tests cover the fused dispatch emission (binding order, cumulative grid, fallback when one weight is not packed) and the kernel source contract;
cargo check --features xla-backend,xla-ireeunderRUSTFLAGS=-D warningsstays clean.
Out of scope
The ragged batch engine's weight path (a multi-row GEMV is its own follow-up), attention dispatch cost, and prefill.
Activity
perf(xla): fuse q/k/v and gate/up into multi-output native GEMV launches
perf(xla): fuse q/k/v and gate/up into multi-output native GEMV launc…
inureyes commentedon Nov 10, 2026
Implemented and merged as f083bea (PR #2907).
What changed: q, k and v are multiplied in one launch and gate and up in another, each reading the activation row once per group. Decode now issues 65 GEMV launches per step instead of 113.
Measured on gfx1151, llama-3.2-1b-instruct-4bit, n=256, warm vmfb:
The 5 percent is below the 8-12 percent bar written into the issue. The change was kept anyway because it never regresses and reduces the launch count for hosts where launch overhead is larger; the measured number, not the expected one, is what the commit and PR body state.
Validation: the fused q/k/v kernel against a double-precision reference through iree-run-module with three weight sets of width 2048/512/512 (max absolute error 8.6e-6, and the two 512-wide sets return distinct correct results, so the row-to-set mapping is exercised), greedy output byte-identical to the f16 path on two prompts, xla_batch_bench REFERENCE-EXACT at B=8, 397 lib tests, fmt and -D warnings clean. The seq layouts keep the original op order, so the committed goldens are byte-identical; the first attempt hoisted the up projection and broke three golden tests without changing the math.
Remaining levers are tracked separately: #2921 (attention, 1.31 ms per step) and #2922 (multi-row GEMV for the ragged batch engine).
perf(xla): port the native 4-bit GEMV to the CUDA device (#2984)