Skip to content

perf(xla): pack the LM head and retile the native decode GEMV on ROCm #2891

Closed

Description

@inureyes

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

  1. 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] i32 plus the two [V, H/64] f16 scale/bias tensors, uploaded in the checkpoint layout. Tied checkpoints reuse embed.weight; untied ones use params['lm_head']. Emit the same flow.dispatch @custom_qmv::@qmv4_kernel with N = vocabulary. Expected: 525 MB f16 per step becomes 131 MB 4-bit, about 2.35 ms to 1.0-1.2 ms.
  2. 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 becomes ceil(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=native decode output is token-identical to the f16 path on the tied llama-3.2-1b-instruct-4bit checkpoint and on one untied checkpoint (separate lm_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.json alone.
  • xla_batch_bench stays REFERENCE-EXACT at B=4 and B=8 with MLXCEL_XLA_QUANT=native set (the batch engine keeps the f16 path).
  • The same rocprofv3 trace after the change is pasted into the PR: per-step qmv4_kernel time, 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-iree under RUSTFLAGS=-D warnings stays 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

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

    @inureyes
    MemberAuthor

    Implemented and merged as #2892 (379a1e29), on top of #2890 (8224641e).

    • LM head: the decode graph declares the packed [V, H/8] tensor plus its f16 scales/biases and dispatches the same GEMV with N = vocabulary; a tied checkpoint re-reads embed's raw parts and an untied one reads lm_head's. Participation is decided from the head tensor's dtype (a safetensors header scan, not config.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.
    • Retiling: below K = 4096 one warp owns an output row, a workgroup covers eight rows, lanes load 16 bytes (uint4) and the reduction is a warp shuffle (grid ceil(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.
    • Results on gfx1151 with llama-3.2-1b-instruct-4bit (greedy, n=128, capacity 256, warm vmfb): 80.3 to 113 tok/s (109 to 118 across rounds) against 31.5 for the f16 path, and the decode step from 9.9 to 7.1 ms. The head drops from 2.35 ms to 0.62 (131 MB packed instead of a 525 MB f16 stream); per-shape medians are k/v 4.8 us, q/o 14.3, gate/up 43.4, head 620, down 62.1.
    • Gates: greedy output byte-identical to the f16 path on tied llama-3.2-1b and untied qwen2.5-coder-7b; xla_batch_bench REFERENCE-EXACT at B=8 with the switch set; both kernels checked against a double-precision reference through iree-run-module (max relative error 1.3e-5 at K=2048, 2.1e-4 at K=8192); 396 lib tests, fmt clean, -D warnings clean.

    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.

  3. added
    status:done
    and removed
    status:ready
    on Nov 9, 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

    priority:highstatus:donetype:performance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions