Skip to content

npu: the w4g32 M=16 decode family under NPU_W4_PREFILL (int8 role images no longer packed; decode GEMMs 11-12x) - #2633

Merged
bong-water-water-bong merged 2 commits into
mainfrom
cl-game/w4g32-decode
Nov 16, 2026
Merged

bong-water-water-bong merged 2 commits into
mainfrom
cl-game/w4g32-decode

Conversation

@bong-water-water-bong

@bong-water-water-bong bong-water-water-bong commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

What

The w4g32 decode family. With NPU_W4_PREFILL=1, decode-width (≤ 16-row) layer GEMMs run int4-weight M=16 kernels, and the int8 role images are no longer packed at all. Follows #2615, the M=512 prefill tier.

Why

#2615 kept both images resident: int8 for the _m16 decode twins, plus w4g32. On bonsai2-27b-NPU that is about 1.5× the int8 weight footprint. On 2026-11-15 a 27B run with both was killed by the global OOM killer (shmem-rss 38.6 GB), so the tier was not safe to serve beside the box's other services.

How

  • Kernels: npu-kernels W4-g128 G32H built at M=16 (one core row, 16 rows per A tile). All six 27B roles are bit-exact ×3 on signed-seed and edge sets. Medians against the int8 _m16 kernels, measured in the same ticket:
role w4 M=16 int8 _m16
U/G 2.38 ms 27.3 ms
D 2.29 ms 26.9 ms
O 0.87 ms 9.79 ms
QKV 10240 1.41 ms 17.1 ms
QKV 14336 1.95 ms 24.2 ms
Z 0.90 ms 11.1 ms

That is 11.3–12.4×.

The int8 column is bonsai2-27b-NPU/kernels/full_i8_<ROLE>_K*_N*_m16.elf, the pack's shipped decode twins and the genuine 16-row design (md5-identical to its "prefill" ELFs, #2599). The gap is about 2× from bytes and about 6× from achieved bandwidth: U is 19.9 against 3.3 GB/s. On this box the pb16i M=16 kernel reaches 50.8 GB/s on D, so the int8 _m16 design is latency-bound there.

Tile heights: the w4 M=16 design is one core row with 16-row A tiles. The M=512 prefill design is four core rows with 32-row A tiles. a_tile_rows() encodes both.

  • q4nx_w4g32.h: the A tile height follows the design (a_tile_rows: 32 for M % 128 == 0, 16 for M == 16). init_elf accepts M == 16.
  • XdnaTarget:
    • Opening: every role opens <prefill stem>_w4g32_m16.elf, all or none. ctx_for routes decode-width layer GEMMs there. A cache fill builds only the w4g32 image.
    • The hardware-context limit: a process gets 16 hardware contexts on this driver. 6 prefill + 7 int8 twins + 6 decode = 19, and the 17th init_elf failed (device error (code 102), aie2_xrs_load: create context failed, ret -22). So with the family, only the LM head's int8 twin opens, since the head's tile images are int8. That gives 6 + 6 + 1 = 13 contexts.
    • The head's context: an int8_only request from a role without a twin gets a same-K int8 twin.
    • Fallback: if the family cannot open, decode-width GEMMs run the role's w4g32 prefill context (correct and logged).
  • Tests: the layout ctest checks pack_a at M=16/128/512 against the restated formula. Its device-vector leg reproduces C_ref on M=16 sets (U seed, D edge). The gate ctest accepts M=16. NPU_M16_CHECK skips mixed-layout pairs.

Gate (bonsai2-27b-NPU, ticket cl-game-w4dec2)

NPU_W4_PREFILL=1 NPU_BATCH_CHECK=1 1bit npu-run … --ids <32>:

  • w4g32 decode family: every decode role at M=16 (NPU_W4_PREFILL); int8 role images are not packed
  • rows M=32 identical=32 differing=0, argmax MATCH

The sequential replay runs the M=16 kernels and the batched pass runs M=512. Both are integer-exact on the same image, so every row is bitwise equal, which checks the routing and the A pack end to end.

Measured (bonsai2-27b-NPU; same snapshot binary 1bit-5bdd23c; npu-hold tickets; 0 amdxdna lines in the timing runs)

Decode, per token (median [ph] step wall of NPU_BATCH_CHECK's sequential replay; the first step, which includes the one-time weight fill, is excluded from the median):

decode path median per token steps ticket
int8 _m16 twins (main) 13.1 s/token 16 cl-game-i8dec
w4g32 M=16 (this PR) 1.36 s/token 32 cl-game-w4dec2

That is about 9.7× end to end, against 11–12× on the isolated decode GEMMs. From the isolated kernel times, the decode GEMMs are about 7.6 s of the int8 step and about 0.65 s of the w4 step. I have not split the rest of each step, so I am not claiming what it is.

  • Memory: 68 → 67 G MemAvailable across the run, where the dual-image tier OOMed.

Device note: a TDR and NPU power-cycle happened at 01:17:10 local inside this ticket's window, on PID 789671. That is not this run's process as far as the evidence goes: the run started at 01:17:15, took the exclusive device lock after the power-cycle, and completed rc 0. It is reported to the orchestrator, and no further device runs are made until it is cleared.

🤖 Generated with Claude Code

bong-water-water-bong and others added 2 commits November 16, 2026 01:20
…packed

With NPU_W4_PREFILL, each role that has an int8 _m16 twin also opens
<prefill stem>_w4g32_m16.elf, all roles or none. ctx_for() routes
decode-width layer GEMMs to those contexts. A weight-cache fill then builds
only the w4g32 image: the int8 role images, about 26 G on bonsai2-27b, are
never packed. Both images together OOM-killed a 27B run on 2026-11-15.

The int8 twins stay as the LM head's context (pick_ctx int8_only), because
the head's own tile images are int8.

- q4nx_w4g32.h: the A tile height follows the design (a_tile_rows: 32 for
  M % 128 == 0, 16 for M == 16); init_elf accepts M == 16.
- The layout ctest checks pack_a at M=16/128/512 against the restated
  formula. Its device-vector leg reproduces C_ref on M=16 sets (U seed,
  D edge). The gate ctest accepts M=16.
- NPU_M16_CHECK skips a mixed-layout pair.

Kernels: npu-kernels W4-g128 G32H built at M=16 (one core row). All six 27B
roles are bit-exact x3 on signed-seed and edge sets, 11.3-12.4x the int8
_m16 kernels (U 2.38 vs 27.3 ms).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s per process

The first device run of the family failed on its 17th distinct hw_context
(D's _w4g32_m16):
  npu runtime: device error (code 102): Invalid argument
  amdxdna: aie2_xrs_load: create context failed, ret -22
The 16 already open were 6 w4 prefill + 7 int8 _m16 twins (G and U are
separate files) + 3 w4 m16.

With the family, the int8 twins serve only the LM head, whose tile images
are int8. So when every role has its _w4g32_m16.elf, open_decode_family
opens only the head role's int8 twin (the widest K = hidden design):
6 + 6 + 1 = 13 contexts. ctx_for serves an int8_only (head) request on a
role without a twin from any int8 twin of the same K. design_index knows
the w4 decode contexts. If the family still fails to open, decode-width
GEMMs run the role's w4g32 prefill context: correct, slower, logged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

PR Reviewer Guide 🔍

(Review updated until commit 3f5e93b)

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

2599 - Partially compliant

Compliant requirements:

  • The w4g32 decode family is implemented with proper context selection logic
  • The int8 role images are no longer packed when using NPU_W4_PREFILL
  • The forward function now properly handles context selection for decode operations

Non-compliant requirements:

  • The PR does not explicitly state that it measures the performance improvements on the hardware as claimed in the description

Requires further human verification:

  • Verification that the performance improvements (11-12x speedup) are actually achieved on the hardware
  • Confirmation that the fix resolves the specific issue described in the ticket where only 16 rows out of 128 are computed
⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Incorrect Context Selection Logic

The logic for selecting contexts in q4nx_forward.cpp when NPU_W4_PREFILL is enabled may be flawed. The code attempts to select an int8 context for the head, but the conditions for when to use ctx16_[role] or xd_->head_ctx() may not correctly handle all cases, particularly when the head's role is not the same as the prefill role. This could lead to incorrect context usage for the LM head computation.

if (hctx && hctx->w4g32) hctx = xd_->pick_ctx(i_wide, M, /*decode_twin=*/true, /*int8_only=*/true);
if (hctx && hctx->w4g32) hctx = nullptr;
// A verify batch may be wider than the twin's row capacity (mtp_acceptance measures up to 128 rows).
Incorrect Context Selection Logic

The ctx_for function in XdnaTarget has a logic issue in how it selects contexts for decode operations when NPU_W4_PREFILL is enabled. Specifically, when int8_only is true, the function attempts to find an int8 twin context that matches the prefill context's KD value. However, this logic does not correctly handle cases where the head's role is not the same as the prefill role, potentially leading to incorrect context selection for the LM head. This could result in incorrect tensor layouts or memory access patterns during the LM head computation.

I8Ctx* XdnaTarget::ctx_for(I8Ctx* prefill, int rows, bool decode_twin, bool int8_only) {
    // Rows that fit one M=16 row-block (decode's 1 row, or a short prompt's
    // prefill chunk of <= 16) run the cheap twin: the M=128 kernel streams the
    // same weights but walks 128 baked MAC rows to deliver <= 16, and a request
    // pays one full weight pass in prefill whatever its prompt length.  Chunks
    // wider than 16 keep M=128 (one launch per chunk), so long prefill is
    // unchanged.
    // decode_twin forces the narrow kernel even for a wide launch: the pb16i prefill path uses it for a
    // projection that has no M=128 kernel at all.
    if (!m16_) return prefill;
    if (!decode_twin && (rows < 1 || rows > 16)) return prefill;
    for (int i = 0; i < ndesigns_ && i < Q4NX_MAX_DESIGNS; i++) {
        if (ctx_[i].get() != prefill) continue;
        // NPU_W4_PREFILL: the role's decode-width layer GEMMs run the w4g32 M=16 context.
        if (!int8_only && w4_decode_ && ctx16w_[i]) return ctx16w_[i].get();
        if (ctx16_[i]) return ctx16_[i].get();
        // The LM head (int8_only) on a role whose int8 twin was not opened (the w4g32 decode family keeps only the head
        // role's): any int8 twin with the same K serves it -- the head launches its own tile images, never this role's.
        if (int8_only)
            for (int j = 0; j < ndesigns_ && j < Q4NX_MAX_DESIGNS; j++)
                if (ctx16_[j] && !ctx16_[j]->w4g32 && ctx16_[j]->KD == prefill->KD) return ctx16_[j].get();
        return prefill;
    }
    return prefill;
}

Potential Incorrect Context Usage
In xdna_target.cpp, the code modifies the hctx pointer when hctx->w4g32 is true, setting it to nullptr after potentially setting it to a decode twin context. This could lead to the head context being incorrectly skipped or used inappropriately, especially when NPU_W4_PREFILL is enabled and the head should use an int8 context instead of a w4g32 one.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT at 3f5e93b: READY on the design — and one question about the 11.5× that decides how the table should be read.

The motivation is a hard constraint, not a preference

"a process gets 16 hardware contexts on this driver. 6 prefill + 7 int8 twins + 6 decode = 19"

That is the reason this PR exists: #2615 kept the int8 image resident for the decode twins alongside w4g32, the count exceeds the driver's limit, and one image has to go. Choosing the w4 image for decode — so int8 role images are not packed — is the right resolution, because the only remaining int8 consumer is kctx itself and the head's twin. "The limit forced a choice, and here is why this side of it is right" is a much better argument than "faster".

The correctness evidence is the same shape as #2615, which is why I trust it

  • All six 27B roles bit-exact ×3 on identical inputs, plus rows M=32 identical=32 differing=0, argmax MATCH — and the two paths a prefill-then-decode pipeline actually uses: "the sequential replay runs the M=16 kernels and the batched pass runs M=512. Both are integer-exact on the same image". The M=16 and M=512 designs agreeing on the same image is the assertion that makes the tier safe to mix.
  • a_tile_rows follows the design (32 for M % 128 == 0, 16 for M == 16, 0 for anything else), and init_elf refuses the shapes the layout cannot carry (K % 128, N % 1024, other M) with a named reason — so an unsupported geometry is a stop, not a silent fallback.
  • Fallback is explicit: if the family cannot open, decode-width GEMMs run the role's w4g32 prefill context, correct and logged. Correct-and-logged beats fast-and-silent.

The question: 11.5× needs a mechanism, and 2× weight bytes is not it

role w4 M=16 int8 _m16
U/G 2.38 ms 27.3 ms
QKV 14336 1.95 ms 24.2 ms

Both arms are 16-row designs over the same K and N, so the weight traffic differs by ~2× (4-bit vs 8-bit) and the activation traffic is identical. 11.3–12.4× is not explained by 2× fewer weight bytes, and a number that large invites the question: is the int8 arm the right arm?

Two possibilities, and the table should say which:

  1. A real kernel difference — the W4-g128 G32H design at M=16 has a different core-row/scheduling structure that the int8 16-row kernel lacks, and the int8 path is latency-bound on this geometry. Then the 11.5× is genuine and the table is the headline.
  2. The int8 arm is #2611's twin case — the int8 "decode" ELF on this pack is the prefill design launched at 16 rows (which is exactly why #2611 exists), so part of the 11.5× is a mismatched kernel rather than a better one. Then the comparison overstates the win and a genuine int8 16-row kernel would narrow it.

One line in the body naming the int8 ELF the column measures (and whether it is the twin-substituted one) settles it; the design decision does not depend on the answer, but the claim does.

Small note

npu-kernels W4-g128 G32H at M=16 is described as "one core row, 16 rows per A tile" — worth naming in the table's caption, since a reader comparing it with the M=128 design will otherwise wonder why the same kernel family shows two tile heights.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

On the 11.5× question: it is possibility 1, a real kernel difference. The int8 arm is the pack's own decode kernel, not a mismatch.

The int8 column is bonsai2-27b-NPU/kernels/full_i8_<ROLE>_K*_N*_m16.elf, the pack's shipped decode twins. Per #2599 they are md5-identical to the pack's "prefill" ELFs, and both are the genuine 16-row design. So both columns are 16-row designs over the same K and N; nothing was substituted. Each pair was timed in the same ticket (cl-game-w4m16all), with 20 launches per median.

The mechanism: about 2× from bytes and about 6× from achieved bandwidth.

role w4 M=16 weights / time GB/s int8 _m16 weights / time GB/s
U 47.3 MB / 2.38 ms 19.9 89.1 MB / 27.3 ms 3.3
O 16.7 MB / 0.90 ms 18.6 31.5 MB / 9.79 ms 3.2

The int8 _m16 design is far below what this box streams. The pack's pb16i M=16 decode kernel reached 50.8 GB/s on D (17408×5120) here on 11-15. So the int8 16-row kernel is latency-bound on this geometry, and G32H's coalesced per-group DMA (one task per channel per output group) is not.

A better int8 16-row kernel would narrow the gap. The ceiling at the same ~20 GB/s would be about 2×, from bytes alone. That is a statement about the int8 kernel this pack ships, not a mis-measurement. Both points are now in the body: the ELF names, and why the family shows two tile heights.

@bong-water-water-bong
bong-water-water-bong marked this pull request as ready for review November 16, 2026 07:03
@bong-water-water-bong
bong-water-water-bong merged commit d5ed6d9 into main Nov 16, 2026
12 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the cl-game/w4g32-decode branch November 16, 2026 07:04
@github-actions

Copy link
Copy Markdown

Persistent review updated to latest commit 3f5e93b

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

A measurement after merge, because it changes how #2615's prefill number should be read.

The warm 512-row prefill pass on bonsai2-27b with this decode family (only the w4g32 image resident) takes 43.3 s, with the layers at 30.2 s: 0.085 s/token. The same prompt and pass under #2615's dual-image build took 110.5 s, with the layers at 96.8 s. Most of the host time that looked "unattributed" under #2615 came from carrying both images, not from the forward itself.

Run details: 1,024-token prompt, the second [bs] pass, 1bit at this PR's head, ticket cl-game-w4perf2, 61–65 G MemAvailable, no amdxdna or OOM lines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant