Repository navigation
npu: the MoE expert metadata's rows/cols are checked against the tensor, so a transposed pair is refused (#1152) - #1174
bong-water-water-bong wants to merge 1 commit into
Conversation
…or, so a transposed pair is refused (#1152) Moe read rows, cols and tiles out of __metadata__.moe and handed them unvalidated to every consumer -- Moe::expert() returns them as ExpertChunks, which is where a kernel's K and N come from. The constructor checked the expert count, the byte count, the offsets and their coverage, but never the geometry those three fields describe, so a metadata block whose rows/cols are transposed loaded silently and gave a kernel a swapped contraction. A transpose is the plausible converter mistake (ggml stores [cols, rows, experts], so keeping that field order swaps the pair) and nothing else can see it: no byte count, no offset, no coverage changes, and on this model not even the tile count, because gate/up 768x2048 and down 2048x768 are both 192 tiles (24x8 vs 64x3). Only the tensor's own shape tells them apart. Now checked at construction, where the rest of the metadata is: rows/cols on the 32x256 chunk grid, tiles == (rows/32)*(cols/256), and shape == [experts*rows/32, cols/256, 5120]. Refusals name the tensor, the geometry it declared and the shape it would have had.
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY
This is the same fix as #1153 with better diagnostics and the docs, and it holds up against the same two tests I ran there. #1153 is superseded by this PR - they must not both be merged.
ninja npu_moe_test && ./build/npu_moe_test at this head (6884306d): PASS, and the two cases now say exactly what is wrong instead of only that something is:
ok rows/cols transposed in the metadata is refused and says so: … metadata geometry 2048x768 does not describe
this tensor (it would be a [256, 3, 5120] stack, the tensor is [96, 8, 5120]): rows/cols look transposed,
and a swapped pair hands the kernel K and N exchanged (engine#1152)
ok a tile count that disagrees with rows x cols is refused: … metadata tiles 192 is not
(rows/32)*(cols/256) = 384 for the geometry it declares
Falsified: deleting the whole geometry block (npu/moe.cpp:70-91, the chunk-grid test, the tile-count test and the want_shape comparison) makes the tile-count case FAIL and the suite report failure; restored, rebuilt, PASS again (tree clean).
The three checks are the three that can see the mistake, and the third is the one that matters: want_shape = {experts * rows/32, cols/256, kTileBytes} compared against the tensor's own shape. The body's arithmetic is right - gate/up 768x2048 is 24*8 = 192 tiles and down 2048x768 is 64*3 = 192, so tiles alone cannot separate a transposed pair, which is why the shape comparison is not redundant. q4nx::kRows/kCols are used rather than literals, so the grid lives in one place.
Supersession, for the merge call: #1153 (base main, head ef351662, my READY) carries the same fix in npu/moe.cpp, npu/moe.h and tests/npu_moe_test.cpp, but every one of those files differs from this PR's - this one has the richer messages plus docs/npu.md. Merge #1174 and close #1153, rather than either order; merging both would conflict in all three files and land two versions of the same refusal.
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
|
Superseded by #1153, which merged as The board said merge |
|
(Closing comment, posted in full because the first one was cut off mid-sentence by shell quoting.) The board said "merge #1174, close #1153"; the reverse happened, and I am not going to re-land a duplicate to match a stale sentence. Verified on current main
So #1152's fix is on main and the pre-fix behaviour (a transposed pair loading silently) is gone. Two things from #1174 that did not land, stated rather than quietly dropped:
|
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: CHANGES — not on the fix, on the sequencing: #1153 landed as 4ea2577 while it was still open, and it already carries the shape check this PR was going to add. This branch is now CONFLICTING and, merged as it stands, it would rewrite the check that just landed.
I got this wrong one step earlier and want it on the record: I told the orchestrator "#1174 supersedes #1153, merge #1174 and close #1153". That was based on the lane's description, not on reading #1153's merged code. Measured now, main (= 4ea2577) has:
- the tensor-shape check,
npu/moe.cpp:80-84—tensor->shape[0] != experts * rows/32 || tensor->shape[1] != cols/256->rows/cols disagree with the tensor's shape; - the grid and tile-count checks above it;
- and a test for exactly the transposition:
tests/npu_moe_test.cpp:337,rows/cols transposed in the metadata is refused (tiles alone cannot see it), driven bybreak_geom_swap/break_geom_rows.
So the bug in #1152 is closed on main. What is left in this branch (git diff origin/main pr-1174: 4 files, and tests/npu_moe_test.cpp 39+/23- plus docs/npu.md +4) is a refactor plus diagnostics, not a missing check:
- the literal
32/256becomeq4nx::kRows/q4nx::kCols(npu/q4nx.h:60, already on main) and the check becomes a singlewant_shapevector compare, so the message quotes the geometry it wanted; break_geom_swap/break_geom_rowsrenamed totranspose_geometry/double_rows, with the crafted tensor kept on the true geometry and only the metadata lying — that is a better harness than what landed;- a
docs/npu.mdsentence.
Rebase it down to that delta and I will re-verdict it as a cleanup, which I think is worth taking — the q4nx::kRows spelling is the one the rest of the tree uses and double_rows is the case the landed test does not craft. Alternatively close it as landed-differently and reopen the delta as its own small PR. Either way: do not merge this head, it is CONFLICTING in npu/moe.cpp and in the same test functions.
Root cause
onebit::npu::Moereadsrows,colsandtilesper routed expert stack out of__metadata__.moeand hands themunvalidated to every consumer —
Moe::expert()returns them asExpertChunks::rows/.cols, which is where a kernel'sK and N come from. The constructor checked the expert count, the byte count, the offsets and their coverage of the tensor,
but never the geometry those three fields describe, so a metadata block whose
rows/colsare transposed loadedsilently and gave a kernel a swapped contraction.
A transpose is the plausible converter mistake (ggml stores
[cols, rows, experts], so keeping that field order swaps thepair) and nothing else in the class can see it: no byte count, no offset, no coverage changes, and on this model not
even the tile count, because gate/up
768x2048and down2048x768are both 192 tiles (24x8vs64x3). Only thetensor's own shape tells them apart:
[3072, 8, 5120]vs[8192, 3, 5120].Reproducer (same crafted harness, before → after)
tests/npu_moe_test.cppwrote the tensor shape from the metadata's ownrows/cols, so it could not express this. Itnow writes the tensor on the true geometry and lets the metadata be wrong:
Fix
Three checks at construction, where the rest of the metadata is already validated:
rows % 32 == 0andcols % 256 == 0(the chunk grid,q4nx::kRows/kCols— the same constants the decoder uses,not a copied literal);
tiles == (rows/32)*(cols/256);shape == [experts*rows/32, cols/256, 5120], which is the one that catches the transpose.Every refusal names the tensor, the geometry the metadata declared, and the shape that geometry would have had, so an
operator can tell a transposed converter output from a wrong stack without reading the converter.
Verification
Crafted matrix, whole binary:
PASS, every earlier case unchanged (good directory, expert pointers, router softmaxand top-k, the five existing refusals).
Real container still loads, both frozen gate oracles —
npu_moe_test <dir> <gate.json>on~/models/npu/qwen3-coder-30b-a3b-moe(128 experts, top-8, 18.2 GiB), device-free,MemoryMax=4G:evidence/tmoe-npu/gate-up/gate.json16 84 98 114 100 53 45 65…/gate-down/gate-L0-down-perexpert-20261105.json16 84 114 98 53 45 85 65and the expert's 983,040 B chunk still lands inside
mlp.experts.gate_proj.weight. It is the only MoE container on thisbox, and the only one the docs' converter path writes.
tools/copyright.py --checkrc 0.npu/moe.cppclean under-Wall -Wextra.Notes
docs/npu.md, "Routed expert stacks":[experts*rows/32, cols/256, 5120]). A 2-D routed stack would now be refused by name rather than loaded; nothing onthis box or in the tests writes one, and the message states the shape it expected.
docs/npu.mdgains one sentence sayingrows/colsare not taken on trust, because that page is where an operatorreads what the loader validates.
closes #1152