Skip to content

npu forward: read the engine converter's MoE container, so a converted Qwen3-30B-A3B runs on the device (refs #1035) - #1266

Merged
bong-water-water-bong merged 2 commits into
mainfrom
1bit/npu-moe-container
Nov 6, 2026
Merged

bong-water-water-bong merged 2 commits into
mainfrom
1bit/npu-moe-container

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

A converted MoE container loaded as a DENSE model, and would have permuted its experts if the names had been the only problem. Both are fixed; a Qwen3-Coder-30B-A3B Q4_K_M container converted by tools/gguf_to_q4nx.py now loads and answers on the NPU. The throughput row is measured below, with the next blockers named.

Root cause (measured, not inferred)

tools/gguf_to_q4nx.py writes the engine's own MoE convention:

  • config.json carries HF keys num_experts/num_experts_per_tok/moe_intermediate_size;
  • the router is model.layers.N.mlp.gate.weight, the routed stacks are model.layers.N.mlp.experts.{gate,up,down}_proj.weight, each a plain [N][K] per-expert tile grid.

npu/forward/src/q4nx_forward.cpp read the FastFlowLM-style convention instead: n_expert/top_k, moe_router.weight, mlp.*_exps_proj.weight, and decoded an expert slice on a [K][N] grid. So n_expert_ stayed 0, layer 0 took the dense path, asked for a mlp.gate_proj.weight the container does not carry, and the first token died with

MLP dequant : data_size 0 at geometry 768x2048 = 192 tiles (chunk 0 B is not a kind ...)

The grid half is silent: on Qwen3-30B expert 0 the converter's bytes correlate 0.99999 with the GGUF on [N][K] and 0.00855 on [K][N].

Also fixed on the way: with a fused-GU plan (what a converted qwen3moe plan is: QKV, O, GU, D, GB, UB) moe_mlp refused with "MoE designs missing" because it required the split G/U designs; it now accepts the batched GB/UB designs, which those containers use. And a failed moe_mlp keeps its reason instead of collapsing to "moe failed" (that cost a ticket).

Change

  • npu/forward/include/moe_container.h (new): the two conventions in one table — tensor suffix names, optional router bias, HF vs llama.cpp config keys, and the decode geometry for an expert slice.
  • npu/forward/src/q4nx_forward.cpp: the convention is detected from layer 0's gate tensor (both spellings exist on disk; neither means the old FLM assumption), then used by the standard and GDN-MoE load paths and by mla_expert; config keys fall back from n_expert/top_k to num_experts/num_experts_per_tok and moe_intermediate_size.
  • tests/npu_moe_container_test.cpp + .sh (CTest npu_moe_container, self-compiling, no NPU runtime): a known-answer test — both spellings, the config fallbacks, the geometry each convention selects, and a q4_1 slice carrying a known [N][K] weight decoded through the shipped q4nx_dequant.c; the selected grid recovers the weights exactly, the other grid does not.

CI (default build, current main): ctest -R 'npu_moe_container|npu_forward_loader|npu_plan|npu_padded_grid|npu_t2_layout|safetensors_to_q4nx' 8/8 pass; tools/copyright.py --check rc 0.

On the device (strixhalo, npu-hold, current main a6c9bf5 + this patch)

forward-serve -m ~/models/npu/qwen3-coder-30b-a3b-moe/model.q4nx --kernels ~/models/npu/qwen3-coder-kernels, under one npu-hold ticket (queued 11:16:45, token after 0 s, held 196 s, released 11:20:01, amdxdna errors 0):

[moe] container layout: engine-converter names, per-expert [N][K] tiles
[moe] MoE: NH=32 n_expert=128 top8 im=768 gating=softmax
[forward] batched expert gate/up: N=6144 (top_k=8 IM=768) via K2048_N8192 kernels
[forward] derived 6 designs (fused_gu=1, H=2048 ...); decode family: 4 role(s) at M=16, 2 on M=128
device fds: 1   -> every request HTTP 200

Row (same prompts as the leaderboard instrument, greedy): 5-token prompt + 1 token = 50.3 s cold / 23.5 s warm → prefill 0.21-0.23 tok/s; 8 tokens decoded in 58.4 s → 0.137 tok/s decode (7.3 s/token). In-tree GGUF Q4_K_M baseline on the same box: bench/npu-leaderboard/coder30b-hrx-solo-20261030.json = 84.33 tok/s (HRX/GPU, parallel=1). CPU llama.cpp on the same GGUF and box measures 29.2 tok/s decode, 49.4 tok/s prefill, for scale.

What remains (named, not papered over)

  1. Throughput: 7.3 s/token is the host-side per-token dequant of the top-8 experts — about 1.1 GB of q4_1 tiles per token (8/128 x 3 projections x 48 layers), decoded and packed per layer. A winner needs the expert weights resident/streamed on the device (the chunk-list dispatch direction in docs/npu.md, "Routed expert stacks"), not this path.
  2. Fidelity: on the same prompt the engine's short completion differs from llama.cpp's, but the container is a requantisation of that GGUF and docs/npu.md already measured that 32/48 layers route a different top-8 for a 0.5 % hidden-state difference — single-token comparisons are not the acceptance gate there. The gate is aggregate KLD/perplexity against the container's own host reference (tools/moe_q4nx_ref.py), which is the next measurement this row needs.

No pin or submodule change.

…ig keys and the per-expert tile grid (refs #1035)

A container built by tools/gguf_to_q4nx.py from a qwen3moe GGUF loaded as DENSE: it carries num_experts/num_experts_per_tok (HF names) and mlp.experts.*_proj.weight tensors, while the forward read n_expert/top_k and the FastFlowLM-style *_exps_proj names. n_expert_ stayed 0, so layer 0 asked for a mlp.gate_proj.weight the container does not have and the first token died with 'MLP dequant : data_size 0 at geometry 768x2048' (engine#1035). Even with the names fixed the expert slices would have been silently permuted: the converter writes a plain [N][K] tile grid, the forward decoded [K][N] (same bytes, corr 0.99999 vs the GGUF on [N][K] and 0.00855 on [K][N] on Qwen3-30B expert 0).

npu/forward/include/moe_container.h holds the two conventions in one table (names, router bias, HF vs llama.cpp config keys, and the per-expert decode geometry); q4nx_forward detects the convention from layer 0's gate tensor and uses it in the standard and GDN-MoE load paths and in mla_expert. mla_expert's design check now accepts a fused-GU plan's batched GB/UB designs (a converted qwen3moe plan is fused and has no G/U), and a failed moe_mlp keeps its reason instead of collapsing to 'moe failed'.

Test: tests/npu_moe_container_test.cpp (+ .sh self-compile, CTest npu_moe_container) is a known-answer check: both spellings, the config fallbacks, the geometry each convention selects, and a q4_1 slice built with a known [N][K] weight decoded through the shipped q4nx_dequant.c -- the selected grid recovers it exactly, the other grid does not.
@bong-water-water-bong
bong-water-water-bong merged commit 34c899a into main Nov 6, 2026
8 of 9 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the 1bit/npu-moe-container branch November 6, 2026 15:38
bong-water-water-bong added a commit that referenced this pull request Nov 6, 2026
…ixes #803)

Same content as the 13-commit stack on npu/plan-p4-target, re-applied onto
origin/main 7153bed as ONE commit: the branch had accumulated fifteen "merge
origin/main" commits, one per re-gate, and the repo squashes on merge anyway.

  npu(p4): extract the XDNA launch layer behind NpuTarget (engine#799 P4 / #803)
  docs(npu-p4): the Apache-2.0 notice tools/copyright.py requires
  npu(p4): consume P1's plan loader in NpuTarget::open instead of duplicating the derivation
  npu(p4): the LM head falls back through ctx_for(), so a set with no M=16 twins still runs
  npu(p4): the pb16i role split is the target's state, so setup asks it instead of assigning
  npu(p4): the kernel family is the forward's to name, so open() takes it
  npu(p4): the rotation is one implementation, so the target calls hadamard.h too
  npu(p4): the head's ternary flag is head_pb16i_, not a self-reference
  npu(p4): the rebase put a_k in the wkey/sc slots, so pass all three as main does
  docs(npu-p4): name the gemv family's boundary in A1, now that #885 is in the rebase
  npu(p4): the m16 debug comparison takes its difference at int64 width
  npu(p4): the target headers take npu-relative includes, so CI's syntax target compiles them
  chore: re-trigger CI

Rebased rather than merged this time, because the merge caravan is what has
been voiding the gate: each "merge origin/main" moved the head, so fourteen
pairings were spent keeping a table honest against a tree that kept moving.
The split's own files are byte-identical to the reviewed ones (blob check in
the PR): npu/target/target.h, npu/target/xdna/*, npu/forward/include/q4nx_lh.h,
docs/npu-p4-target-split.md. The three that differ are the three main changed:
CMakeLists.txt (main's test registrations), and q4nx_forward.{h,cpp}, which
now carry main's newer forward work (#1243's AVX2/FMA attention, #1266's MoE
container) *on top of* the split -- verified by grepping the moved-in symbols
in the rebased file, not by trusting the clean merge.

Nothing in here changes numerics: the device and launch layer moved, the 38
launch sites became role-based calls, and the model math stayed where it was.
bong-water-water-bong added a commit that referenced this pull request Nov 6, 2026
…ixes #803)

Same content as the 13-commit stack on npu/plan-p4-target, re-applied onto
origin/main 7153bed as ONE commit: the branch had accumulated fifteen "merge
origin/main" commits, one per re-gate, and the repo squashes on merge anyway.

  npu(p4): extract the XDNA launch layer behind NpuTarget (engine#799 P4 / #803)
  docs(npu-p4): the Apache-2.0 notice tools/copyright.py requires
  npu(p4): consume P1's plan loader in NpuTarget::open instead of duplicating the derivation
  npu(p4): the LM head falls back through ctx_for(), so a set with no M=16 twins still runs
  npu(p4): the pb16i role split is the target's state, so setup asks it instead of assigning
  npu(p4): the kernel family is the forward's to name, so open() takes it
  npu(p4): the rotation is one implementation, so the target calls hadamard.h too
  npu(p4): the head's ternary flag is head_pb16i_, not a self-reference
  npu(p4): the rebase put a_k in the wkey/sc slots, so pass all three as main does
  docs(npu-p4): name the gemv family's boundary in A1, now that #885 is in the rebase
  npu(p4): the m16 debug comparison takes its difference at int64 width
  npu(p4): the target headers take npu-relative includes, so CI's syntax target compiles them
  chore: re-trigger CI

Rebased rather than merged this time, because the merge caravan is what has
been voiding the gate: each "merge origin/main" moved the head, so fourteen
pairings were spent keeping a table honest against a tree that kept moving.
The split's own files are byte-identical to the reviewed ones (blob check in
the PR): npu/target/target.h, npu/target/xdna/*, npu/forward/include/q4nx_lh.h,
docs/npu-p4-target-split.md. The three that differ are the three main changed:
CMakeLists.txt (main's test registrations), and q4nx_forward.{h,cpp}, which
now carry main's newer forward work (#1243's AVX2/FMA attention, #1266's MoE
container) *on top of* the split -- verified by grepping the moved-in symbols
in the rebased file, not by trusting the clean merge.

Nothing in here changes numerics: the device and launch layer moved, the 38
launch sites became role-based calls, and the model math stayed where it was.

One row of the gate was a citation rather than a check, and is now a check: §4 item 4 asks for
`--npu-target xdna` as the default with an unknown name refused by name, and A1 had no such
option at all -- the name appeared only in a comment in npu/target/target.h and in the design
doc, so the earlier "PASS: from the A1 design row" was a claim about the document. The flag now
exists: `forward-serve --npu-target <name>` (defaulting to XdnaTarget::kName), `serve` carries
it through to that child, and an unknown name is refused **before the device is opened**, which
is what makes the row measurable without a ticket.
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

Failed to review PR

bong-water-water-bong added a commit that referenced this pull request Nov 6, 2026
…ixes #803)

Same content as the 13-commit stack on npu/plan-p4-target, re-applied onto
origin/main 7153bed as ONE commit: the branch had accumulated fifteen "merge
origin/main" commits, one per re-gate, and the repo squashes on merge anyway.

  npu(p4): extract the XDNA launch layer behind NpuTarget (engine#799 P4 / #803)
  docs(npu-p4): the Apache-2.0 notice tools/copyright.py requires
  npu(p4): consume P1's plan loader in NpuTarget::open instead of duplicating the derivation
  npu(p4): the LM head falls back through ctx_for(), so a set with no M=16 twins still runs
  npu(p4): the pb16i role split is the target's state, so setup asks it instead of assigning
  npu(p4): the kernel family is the forward's to name, so open() takes it
  npu(p4): the rotation is one implementation, so the target calls hadamard.h too
  npu(p4): the head's ternary flag is head_pb16i_, not a self-reference
  npu(p4): the rebase put a_k in the wkey/sc slots, so pass all three as main does
  docs(npu-p4): name the gemv family's boundary in A1, now that #885 is in the rebase
  npu(p4): the m16 debug comparison takes its difference at int64 width
  npu(p4): the target headers take npu-relative includes, so CI's syntax target compiles them
  chore: re-trigger CI

Rebased rather than merged this time, because the merge caravan is what has
been voiding the gate: each "merge origin/main" moved the head, so fourteen
pairings were spent keeping a table honest against a tree that kept moving.
The split's own files are byte-identical to the reviewed ones (blob check in
the PR): npu/target/target.h, npu/target/xdna/*, npu/forward/include/q4nx_lh.h,
docs/npu-p4-target-split.md. The three that differ are the three main changed:
CMakeLists.txt (main's test registrations), and q4nx_forward.{h,cpp}, which
now carry main's newer forward work (#1243's AVX2/FMA attention, #1266's MoE
container) *on top of* the split -- verified by grepping the moved-in symbols
in the rebased file, not by trusting the clean merge.

Nothing in here changes numerics: the device and launch layer moved, the 38
launch sites became role-based calls, and the model math stayed where it was.

One row of the gate was a citation rather than a check, and is now a check: §4 item 4 asks for
`--npu-target xdna` as the default with an unknown name refused by name, and A1 had no such
option at all -- the name appeared only in a comment in npu/target/target.h and in the design
doc, so the earlier "PASS: from the A1 design row" was a claim about the document. The flag now
exists: `forward-serve --npu-target <name>` (defaulting to XdnaTarget::kName), `serve` carries
it through to that child, and an unknown name is refused **before the device is opened**, which
is what makes the row measurable without a ticket.
bong-water-water-bong added a commit that referenced this pull request Nov 6, 2026
…ixes #803)

Same content as the 13-commit stack on npu/plan-p4-target, re-applied onto
origin/main 7153bed as ONE commit: the branch had accumulated fifteen "merge
origin/main" commits, one per re-gate, and the repo squashes on merge anyway.

  npu(p4): extract the XDNA launch layer behind NpuTarget (engine#799 P4 / #803)
  docs(npu-p4): the Apache-2.0 notice tools/copyright.py requires
  npu(p4): consume P1's plan loader in NpuTarget::open instead of duplicating the derivation
  npu(p4): the LM head falls back through ctx_for(), so a set with no M=16 twins still runs
  npu(p4): the pb16i role split is the target's state, so setup asks it instead of assigning
  npu(p4): the kernel family is the forward's to name, so open() takes it
  npu(p4): the rotation is one implementation, so the target calls hadamard.h too
  npu(p4): the head's ternary flag is head_pb16i_, not a self-reference
  npu(p4): the rebase put a_k in the wkey/sc slots, so pass all three as main does
  docs(npu-p4): name the gemv family's boundary in A1, now that #885 is in the rebase
  npu(p4): the m16 debug comparison takes its difference at int64 width
  npu(p4): the target headers take npu-relative includes, so CI's syntax target compiles them
  chore: re-trigger CI

Rebased rather than merged this time, because the merge caravan is what has
been voiding the gate: each "merge origin/main" moved the head, so fourteen
pairings were spent keeping a table honest against a tree that kept moving.
The split's own files are byte-identical to the reviewed ones (blob check in
the PR): npu/target/target.h, npu/target/xdna/*, npu/forward/include/q4nx_lh.h,
docs/npu-p4-target-split.md. The three that differ are the three main changed:
CMakeLists.txt (main's test registrations), and q4nx_forward.{h,cpp}, which
now carry main's newer forward work (#1243's AVX2/FMA attention, #1266's MoE
container) *on top of* the split -- verified by grepping the moved-in symbols
in the rebased file, not by trusting the clean merge.

Nothing in here changes numerics: the device and launch layer moved, the 38
launch sites became role-based calls, and the model math stayed where it was.

One row of the gate was a citation rather than a check, and is now a check: §4 item 4 asks for
`--npu-target xdna` as the default with an unknown name refused by name, and A1 had no such
option at all -- the name appeared only in a comment in npu/target/target.h and in the design
doc, so the earlier "PASS: from the A1 design row" was a claim about the document. The flag now
exists: `forward-serve --npu-target <name>` (defaulting to XdnaTarget::kName), `serve` carries
it through to that child, and an unknown name is refused **before the device is opened**, which
is what makes the row measurable without a ticket.
bong-water-water-bong added a commit that referenced this pull request Nov 6, 2026
…ixes #803)

Same content as the 13-commit stack on npu/plan-p4-target, re-applied onto
origin/main 7153bed as ONE commit: the branch had accumulated fifteen "merge
origin/main" commits, one per re-gate, and the repo squashes on merge anyway.

  npu(p4): extract the XDNA launch layer behind NpuTarget (engine#799 P4 / #803)
  docs(npu-p4): the Apache-2.0 notice tools/copyright.py requires
  npu(p4): consume P1's plan loader in NpuTarget::open instead of duplicating the derivation
  npu(p4): the LM head falls back through ctx_for(), so a set with no M=16 twins still runs
  npu(p4): the pb16i role split is the target's state, so setup asks it instead of assigning
  npu(p4): the kernel family is the forward's to name, so open() takes it
  npu(p4): the rotation is one implementation, so the target calls hadamard.h too
  npu(p4): the head's ternary flag is head_pb16i_, not a self-reference
  npu(p4): the rebase put a_k in the wkey/sc slots, so pass all three as main does
  docs(npu-p4): name the gemv family's boundary in A1, now that #885 is in the rebase
  npu(p4): the m16 debug comparison takes its difference at int64 width
  npu(p4): the target headers take npu-relative includes, so CI's syntax target compiles them
  chore: re-trigger CI

Rebased rather than merged this time, because the merge caravan is what has
been voiding the gate: each "merge origin/main" moved the head, so fourteen
pairings were spent keeping a table honest against a tree that kept moving.
The split's own files are byte-identical to the reviewed ones (blob check in
the PR): npu/target/target.h, npu/target/xdna/*, npu/forward/include/q4nx_lh.h,
docs/npu-p4-target-split.md. The three that differ are the three main changed:
CMakeLists.txt (main's test registrations), and q4nx_forward.{h,cpp}, which
now carry main's newer forward work (#1243's AVX2/FMA attention, #1266's MoE
container) *on top of* the split -- verified by grepping the moved-in symbols
in the rebased file, not by trusting the clean merge.

Nothing in here changes numerics: the device and launch layer moved, the 38
launch sites became role-based calls, and the model math stayed where it was.

One row of the gate was a citation rather than a check, and is now a check: §4 item 4 asks for
`--npu-target xdna` as the default with an unknown name refused by name, and A1 had no such
option at all -- the name appeared only in a comment in npu/target/target.h and in the design
doc, so the earlier "PASS: from the A1 design row" was a claim about the document. The flag now
exists: `forward-serve --npu-target <name>` (defaulting to XdnaTarget::kName), `serve` carries
it through to that child, and an unknown name is refused **before the device is opened**, which
is what makes the row measurable without a ticket.
bong-water-water-bong added a commit that referenced this pull request Nov 7, 2026
…808)

* npu: extract the XDNA launch layer behind NpuTarget (engine#799 P4, fixes #803)

Same content as the 13-commit stack on npu/plan-p4-target, re-applied onto
origin/main 7153bed as ONE commit: the branch had accumulated fifteen "merge
origin/main" commits, one per re-gate, and the repo squashes on merge anyway.

  npu(p4): extract the XDNA launch layer behind NpuTarget (engine#799 P4 / #803)
  docs(npu-p4): the Apache-2.0 notice tools/copyright.py requires
  npu(p4): consume P1's plan loader in NpuTarget::open instead of duplicating the derivation
  npu(p4): the LM head falls back through ctx_for(), so a set with no M=16 twins still runs
  npu(p4): the pb16i role split is the target's state, so setup asks it instead of assigning
  npu(p4): the kernel family is the forward's to name, so open() takes it
  npu(p4): the rotation is one implementation, so the target calls hadamard.h too
  npu(p4): the head's ternary flag is head_pb16i_, not a self-reference
  npu(p4): the rebase put a_k in the wkey/sc slots, so pass all three as main does
  docs(npu-p4): name the gemv family's boundary in A1, now that #885 is in the rebase
  npu(p4): the m16 debug comparison takes its difference at int64 width
  npu(p4): the target headers take npu-relative includes, so CI's syntax target compiles them
  chore: re-trigger CI

Rebased rather than merged this time, because the merge caravan is what has
been voiding the gate: each "merge origin/main" moved the head, so fourteen
pairings were spent keeping a table honest against a tree that kept moving.
The split's own files are byte-identical to the reviewed ones (blob check in
the PR): npu/target/target.h, npu/target/xdna/*, npu/forward/include/q4nx_lh.h,
docs/npu-p4-target-split.md. The three that differ are the three main changed:
CMakeLists.txt (main's test registrations), and q4nx_forward.{h,cpp}, which
now carry main's newer forward work (#1243's AVX2/FMA attention, #1266's MoE
container) *on top of* the split -- verified by grepping the moved-in symbols
in the rebased file, not by trusting the clean merge.

Nothing in here changes numerics: the device and launch layer moved, the 38
launch sites became role-based calls, and the model math stayed where it was.

One row of the gate was a citation rather than a check, and is now a check: §4 item 4 asks for
`--npu-target xdna` as the default with an unknown name refused by name, and A1 had no such
option at all -- the name appeared only in a comment in npu/target/target.h and in the design
doc, so the earlier "PASS: from the A1 design row" was a claim about the document. The flag now
exists: `forward-serve --npu-target <name>` (defaulting to XdnaTarget::kName), `serve` carries
it through to that child, and an unknown name is refused **before the device is opened**, which
is what makes the row measurable without a ticket.

* npu(p4): the host fallback pads its activations too, so a padded design cannot be read past its buffer

Review of #808 found the regression this move introduced: main pads A from the design's real_k
up to K before branching, and the split moved that pad into XdnaTarget while leaving the host
branch of npu_gemm()/npu_gemm_rows() on the raw A. At a padded design (0 < real_k < K, e.g.
hidden 896 stored at K=1024) the host matvec then read A[real_k..K) past the caller's buffer,
and the rows form read at stride K from a buffer laid out at stride real_k -- every row after
the first was the wrong row as well as an overrun. It fires under NPU_INFER_HOST_GEMM=1/init_host,
which is #796 rung 1's reference path.

The pad and the multiply are one decision, so they now live in one function:
q4nx_host_gemm_f32_padded() (q4nx_pack.{h,c}) zero-fills the tail into scratch and runs the loop
at stride K; both host branches are one call to it, so no caller can take the read without the pad.
a_k <= 0 (the MLA designs) and a_k >= K keep their old path exactly.

tests/host_gemm_pad_test.c (ctest host_gemm_pad) pins it with a NaN guard tail after the activation
buffer, so an implementation that reads past the buffer fails even when its numbers look plausible.
Checked by breaking the code rather than by watching it pass: with the pad disabled it reports
'worst |d| 60' and 'got 0 want 1' on the guard for the 896-of-1024 case, and passes once restored.

Also: the design doc now records that the engine#666 canary retry is conditional on !ctx.gemv
because readback() only verifies for a non-gemv context -- not a dropped safety, and worth
saying so where a reader will look.

* ci: bind host_gemm_pad_test to a target CI already builds, so ctest runs it

ci.yml names its build targets explicitly and needs the `workflow` scope to extend, which this lane's
token does not have. Without the binding the new row reports 'Not Run' and the job fails after 106 tests
have passed -- which is exactly what happened on f605dbe. Same mechanism as hadamard_rotate_test and
attn_math_test on the line above.

---------

Co-authored-by: bong-water-water-bong <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant