Repository navigation
Closed
Labels
area:inferenceGeneration, sampling, decoding (incl. speculative, DRY)priority:highHigh prioritystatus:doneCompletedtype:performancePerformance improvements
Milestone
Description
Current state
After #2859, #2863, #2865 and #2873, a llama-3.2-1b decode step on gfx1151 (hip) is 24.63 ms with 318 dispatches. The sampling tail is now cheap; the step is almost entirely weight streaming for the projections:
| kernel | per step | calls | per call |
|---|---|---|---|
vecmat_2048x8192 (down) |
4.88 ms | 16 | 0.305 ms |
vecmat_8192x2048 (gate + up) |
3.59 + 3.57 ms | 16 + 16 | 0.224 ms |
vecmat_128256x2048 (LM head) |
3.33 ms | 1 | 3.33 ms |
vecmat_2048x2048 (q, o) |
3.00 + 2.86 ms | 16 + 15 | 0.19 ms |
vecmat_512x2048 (k, v) |
1.49 ms | 32 | 0.047 ms |
batch_matmul_8x4x256x64 (attention) |
1.20 ms | 16 | 0.075 ms |
Against MLX ROCm at 4.2 ms per step (235-238 tok/s versus 30-36 tok/s), the gap is 5.9x, and it decomposes almost exactly into two factors:
- Bytes. The XLA path keeps every projection f16-resident: 974M projection parameters plus a 263M-parameter head is about 2.47 GB streamed per step. The MLX path reads the same checkpoint's 4-bit weights, about 618 MB. That is a 4x difference in bytes.
- Bandwidth utilization. 2.47 GB in 24.63 ms is about 100 GB/s; MLX's 618 MB in 4.2 ms is about 167 GB/s. That is a 1.67x difference.
4x times 1.67x is the observed 5.9x, so the dominant remaining lever is the weight format, not another tiling tweak.
Work to do
- Measure the existing packed path first:
MLXCEL_XLA_QUANT=packedalready loads 4-bit weights and emitsdequant_affineinto the graph (issue feat: int8 weight quantization with quantized matmul on the OpenXLA path (#449) #516). Determine from arocprofv3trace whether IREE's tile-and-fuse pipeline fuses that unpack into the vecmat operand load, or materializes an f16 copy per step (the current documentation implies the latter). Record the measured per-step time and the dispatch list either way; that single number decides whether this issue is a tuning problem or a new lowering. - If the unpack materializes, find a form the pinned compiler can lower into one fused dispatch for sub-byte weights: a
linalg.genericcontraction whose operand is packedui32with in-loop unpack, aniree_gpu/linalg.quantized_matmul-style op if this pin supports mixed 4-bit/f16, or a ukernel (--iree-rocm-enable-ukernels). The packed format is MLX affine group-64 4-bit, so the scale and bias read must stay in the loop. - If no single-dispatch form exists on this pin, evaluate a native HIP GEMV (the MLX
qmv_wideshape) as a custom dispatch for the projection shapes, and record what the graph/ABI change would cost. The repo already carries a native shim for Gemma3n's qmv, so the mechanism exists. - Only after the format question is settled, consider dispatch-count work (fusing q/k/v and gate/up into wider contractions), since that trades bytes neutral for fewer, larger kernels.
Acceptance criteria
- A measured per-step breakdown before and after on gfx1151 at B=1, n=128, capacity 256, with the same
n=1vsn=128trace subtraction used in perf(xla): close the IREE decode-kernel efficiency gap (small-M vecmats and the serial argmax) #2873. - Either a working narrow-weight path with token-identical greedy output on llama-3.2-1b (tied) and one untied checkpoint and the batch
REFERENCE-EXACTgate, or a written record of why this pin cannot do it and what kernel work would be needed. - The f16 path stays available and unchanged for devices or checkpoints where narrow weights are not supported; committed goldens and the ragged batch graph are not modified unless the change is engine-specific and re-frozen deliberately.
cargo test -p mlxcel-xla --liband clippy at default andireefeatures stay green.
Out of scope
Prefill throughput, the batch/serving engine beyond a no-regression check, and activation/precision changes. Vision and audio encoders.
Metadata
Metadata
Assignees
Labels
area:inferenceGeneration, sampling, decoding (incl. speculative, DRY)priority:highHigh prioritystatus:doneCompletedtype:performancePerformance improvements
Activity
inureyes commentedon Nov 9, 2026
First experiment: the existing packed path on the current main (
b2bea7a8), llama-3.2-1b, gfx1151,MLXCEL_XLA_QUANT=packed,MLXCEL_XLA_CAPACITY=256.It is decisively worse, and the trace says why:
MLXCEL_XLA_QUANT=packedMLXCEL_XLA_QUANT=packedPacked run, device time by kernel (prefill plus 2 decode steps):
elementwise_transpose_256x8x8192_i32xi32xf32xf32xf16elementwise_transpose_1024x8x2048_i32xi32xf32xf32xf16elementwise_transpose_256x8x2048_...elementwise_broadcast_262144x64_f16xf32vecmat_8192x2048,vecmat_2048x8192)So IREE does not fuse the unpack into the vecmat: it materializes a full dequantized-and-transposed f16 operand per weight per step, as its own dispatch. The vecmats that consume those operands are cheap; the dequant materialization dominates by 20x. The current packed path also dequantizes into
[out, in]and then transposes in-graph for theInOutsingle-sequence layout (the note in #2861), which is part of why the dispatch is a combined transpose-and-convert.Consequence for this issue: the packed path cannot be fixed by a flag or a layout tweak. A narrow-weight kernel has to consume packed 4-bit directly, which this pin offers no reachable lowering for:
QuantizedMatmulToMatmulcovers the int8 path, the ROCm ukernel catalog in this tree ships only argmax and one int8 MFMA micro-kernel (no mmt4d or narrow GEMV), and nothing in the vector-distribute or tile-and-fuse pipelines consumes sub-byte weights inside a contraction.The two remaining paths, with what each needs:
hal.executable.source private @custom_qmvcustom dispatch, the mechanism the Gemma3n CUDA qmv already uses (flow.dispatch @custom_qmv::@gemma3n_qmv). A ROCm sibling needs the GEMV in HIP C, compiled to HSACO at build time, uploaded as the executable object, plus a weight-upload form that keeps the packed/scales/bias tensors. The kernel shape is the MLXqmv_wideone: one workgroup per output tile, groups of 64 scales in the loop.iree_gpumatvec op for this pin, which is a compiler change outside this repository.Recording this so the next step is an explicit choice rather than another attempt at the packed path.
feat(xla): run 4-bit decode projections through a native HIP GEMV
feat(xla): run 4-bit decode projections through a native HIP GEMV (#2890
inureyes commentedon Nov 16, 2026
Recorded the per-step breakdown and the one reachable per-step cost in PR #3106.
Measured on gfx1151, llama-3.2-1b-4bit,
MLXCEL_XLA_QUANT=native, capacity 512, greedy, with the n=1 / n=128rocprofv3subtraction (the shared prefill cancels, 127 steps remain). Before the change the traced step was 5.054 ms: 3.976 ms of GPU kernel time and 1.077 ms of dispatch gaps over 281 dispatches. The GEMV stream is 695 MB per step: gate/up 1.312 ms at 230 GB/s, down 0.998 ms at 151, the head 0.619 ms at 239, o 0.231 ms at 163, q/k/v 0.329 ms at 172. Gate/up and the head are at the 240 GB/s practical roofline; down, q/k/v and o leave about 0.49 ms of headroom, 0.35 ms of it in down. The rest of the kernel time is attention, norms, softmax, argmax (already a two-stage reduce at 0.033 ms) and 32 whole-cache convert-and-transposes.What landed: the decode KV globals now hold the contraction precision (f16 on hip, bf16 under f16/bf16 precision selection, f32 on CPU), the new K/V row is narrowed at the update, and
kv_importkeeps its f32 signature and converts once per request. The cache operands are bit-identical and each cache dispatch drops from 5.78 to 3.90 us. Traced step 5.054 to 4.895 ms (204.3 tok/s); untraced 196.1 against 190.2 tok/s (median of three). Dispatches 281 to 263.Left to follow-up: vectorizing the down projection (it changes the f32 reduction grouping, so it needs the token-identity gate on a tied and an untied checkpoint), and further graph fusion for the ~1 ms of launch gaps. #2873's small-M vecmats and serial argmax do not exist on the native path; they still apply to the default f16 graph (42.0 tok/s, 23.8 ms per step).
fix(xla): narrow the MLA single-decode cache row too
test(xla): pin the KV globals test to f32
test(xla): cover the narrowed decode cache on every graph kind
docs: add technical report for PR #3106
perf(xla): keep the decode KV cache in the contraction precision (lab…