Repository navigation
Description
Context
PR #2890 added MLXCEL_XLA_QUANT=native: the single-sequence decode projections stay 4-bit resident and each one dispatches csrc/qmv4.hip through hal.executable.source @custom_qmv. On gfx1151 (llama-3.2-1b-instruct-4bit, greedy, n=128, capacity 256) decode went from 30.0 to 80.3 tok/s with token-identical output.
A rocprofv3 kernel trace of that path (24 decode steps) shows where the remaining time is:
| per decode step | ms | note |
|---|---|---|
qmv4_kernel (113 dispatches) |
3.82 | k/v 0.19 (90 GB/s), q/o+down 1.55, gate/up 2.05 (about 130 GB/s) |
LM head f16 vecmat 128256x2048 |
2.35 | 525 MB f16 stream per step, 223 GB/s |
attention batch_matmul |
1.31 | 16 layers |
| elementwise / reduce / copy | 0.50 | |
| inter-dispatch gaps | 2.00 | host and launch overhead |
| total | about 9.9 | kernel time 7.95 plus gaps |
So the LM head is now the largest single weight stream, and the native GEMV runs at about 130 GB/s where the f16 head vecmat reaches 223 GB/s on the same device.
Work
- LM head through the native GEMV. In native mode, declare the head as packed tensors instead of the f16
[H, V]operand: packed[V, H/8]i32plus the two[V, H/64]f16 scale/bias tensors, uploaded in the checkpoint layout. Tied checkpoints reuseembed.weight; untied ones useparams['lm_head']. Emit the sameflow.dispatch @custom_qmv::@qmv4_kernelwith N = vocabulary. Expected: 525 MB f16 per step becomes 131 MB 4-bit, about 2.35 ms to 1.0-1.2 ms. - Retile the kernel. One warp per output row with lane-strided
uint4(16-byte) loads and one scale/bias pair per lane iteration, several rows per 256-thread block (blockIdx.x * rows_per_block + warp), warp-shuffle reduction only (no shared memory, no block barrier). The dispatch grid becomesceil(N / rows_per_block); pick the rows per block from N in the emitter if the small shapes need a different value. Expected: gate/up, q/o and down from about 130 GB/s to 200+ GB/s, 3.82 ms to about 2.2-2.5 ms.
Both changes share the same kernel entry point and ABI (constants K and N, five bindings), so they land together in one PR with one verification pass and as few pushes as possible.
Acceptance criteria
MLXCEL_XLA_QUANT=nativedecode output is token-identical to the f16 path on the tied llama-3.2-1b-instruct-4bit checkpoint and on one untied checkpoint (separatelm_head), on at least two prompts; if the head change moves a token anywhere, the issue says how the final gate was resolved.- A checkpoint whose embed is not 4-bit packed keeps the f16 head path (no packed head args, no wrong dequant), decided from the actual tensor dtype at load, not from
config.jsonalone. xla_batch_benchstays REFERENCE-EXACT at B=4 and B=8 withMLXCEL_XLA_QUANT=nativeset (the batch engine keeps the f16 path).- The same
rocprofv3trace after the change is pasted into the PR: per-stepqmv4_kerneltime, head time, and total kernel time, with before/after tok/s at capacity 256. - The f16 path, prefill, and non-HIP devices are unchanged; the committed goldens stay byte-identical.
- Unit tests cover the head packed-argument declaration and the grid rounding, and
cargo check --features xla-backend,xla-ireeunderRUSTFLAGS=-D warningsstays clean.
Out of scope
Fusing q/k/v or gate/up into multi-output dispatches, a multi-row GEMV for the ragged batch engine, and prefill. Those follow once the retiled kernel and the packed head are measured.
Activity
perf(xla): pack the LM head and retile the native decode GEMV
perf(xla): pack the LM head and retile the native decode GEMV (#2892)
inureyes commentedon Nov 9, 2026
Implemented and merged as #2892 (
379a1e29), on top of #2890 (8224641e).[V, H/8]tensor plus its f16 scales/biases and dispatches the same GEMV with N = vocabulary; a tied checkpoint re-readsembed's raw parts and an untied one readslm_head's. Participation is decided from the head tensor's dtype (a safetensors header scan, notconfig.json), so a non-4-bit head keeps the f16 path. 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, and both forms share one argument list across graph kinds.uint4) and the reduction is a warp shuffle (gridceil(N/8)). At K >= 4096 the warp layout underperformed (97 us against 59 on the down projection), 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.xla_batch_benchREFERENCE-EXACT at B=8 with the switch set; both kernels checked against a double-precision reference throughiree-run-module(max relative error 1.3e-5 at K=2048, 2.1e-4 at K=8192); 396 lib tests, fmt clean,-D warningsclean.Still open, outside this issue: multi-output kernels for q/k/v and gate/up (113 launches per step), attention cost, and a multi-row GEMV for the ragged batch engine.
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…