Skip to content

perf(xla): close the remaining decode gap by running projections from narrow weights instead of f16-resident copies #2886

Closed

Description

@inureyes

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:

  1. 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.
  2. 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

  1. Measure the existing packed path first: MLXCEL_XLA_QUANT=packed already loads 4-bit weights and emits dequant_affine into the graph (issue feat: int8 weight quantization with quantized matmul on the OpenXLA path (#449) #516). Determine from a rocprofv3 trace 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.
  2. If the unpack materializes, find a form the pinned compiler can lower into one fused dispatch for sub-byte weights: a linalg.generic contraction whose operand is packed ui32 with in-loop unpack, an iree_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.
  3. If no single-dispatch form exists on this pin, evaluate a native HIP GEMV (the MLX qmv_wide shape) 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.
  4. 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=1 vs n=128 trace 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-EXACT gate, 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 --lib and clippy at default and iree features stay green.

Out of scope

Prefill throughput, the batch/serving engine beyond a no-regression check, and activation/precision changes. Vision and audio encoders.

Refs #2859, #2863, #2873

Activity

  1. added
    type:performance
    status:investigation
    area:inference
    priority:high
    on Nov 9, 2026
  2. inureyes commented on Nov 9, 2026

    @inureyes
    MemberAuthor

    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:

    run n result
    default (f16-resident) 32 30.60 tok/s
    MLXCEL_XLA_QUANT=packed 32 0.07 tok/s
    MLXCEL_XLA_QUANT=packed 2 26.33 s for 2 tokens

    Packed run, device time by kernel (prefill plus 2 decode steps):

    kernel time calls
    elementwise_transpose_256x8x8192_i32xi32xf32xf32xf16 18.15 s 64
    elementwise_transpose_1024x8x2048_i32xi32xf32xf32xf16 7.01 s 32
    elementwise_transpose_256x8x2048_... 0.79 s 64
    elementwise_broadcast_262144x64_f16xf32 0.12 s 288
    the actual vecmats (vecmat_8192x2048, vecmat_2048x8192) about 0.5 ms/call 32 each

    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 the InOut single-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: QuantizedMatmulToMatmul covers 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:

    1. hal.executable.source private @custom_qmv custom 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 MLX qmv_wide one: one workgroup per output tile, groups of 64 scales in the loop.
    2. Upstream an i4 GEMV ukernel or an iree_gpu matvec 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.

  3. added
    status:in-progress
    status:review
    and removed
    status:investigation
    status:in-progress
    on Nov 16, 2026
  4. inureyes commented on Nov 16, 2026

    @inureyes
    MemberAuthor

    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=128 rocprofv3 subtraction (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_import keeps 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).

  5. added
    status:done
    and removed
    status:review
    on Nov 16, 2026
  6. added a commit that references this issue on Nov 16, 2026
    b21825c
  7. added this to the 0.9 milestone on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:inferencepriority:highstatus:donetype:performance

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions