Skip to content

perf(xla): fuse q/k/v and gate/up into multi-output native GEMV dispatches #2895

Closed

Description

@inureyes

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

  1. 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) and qmv4_gateup_kernel (x, two sets, two outputs; constants K, N0, N1). Each keeps the warp-per-row layout of qmv4_kernel (these projections have K = hidden < 4096) and maps a global output row to its matrix by cumulative N. Both can live in one .hip and be embedded through two additional hal.executable.source blocks, because the pipeline layout (binding count) differs per export; the same HSACO bytes are referenced from each.
  2. 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.
  3. 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.
  • rocprofv3 before/after pasted into the PR: dispatches per step, gap time, per-kernel time, and tok/s at MLXCEL_XLA_CONTEXT_CAPACITY=256, n=128.
  • xla_batch_bench REFERENCE-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-iree under RUSTFLAGS=-D warnings stays 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.

Refs #2889, #2891

Activity

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

    @inureyes
    MemberAuthor

    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:

    • GEMV launches per step: 113 to 65
    • Per-step kernel time: 5.46 to 5.30 ms
    • Step time: 7.10 to 7.11 ms, unchanged because the dispatch gaps did not move
    • Paired A/B on one binary: 117.0 tok/s per-projection against 122.7 fused, about 5 percent, every interleaved round positive

    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).

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