Skip to content

tools: moe_expert_chunks gate --proj down builds each expert's own A instead of crashing - #1086

Merged
bong-water-water-bong merged 2 commits into
mainfrom
1bit/moe-down-gate
Nov 5, 2026
Merged

bong-water-water-bong merged 2 commits into
mainfrom
1bit/moe-down-gate

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

tools: moe_expert_chunks.py gate --proj down builds each expert's own A instead of crashing

closes #1083

Root cause

gate built one a8 from the layer's MoE input row (hidden 2048 on Qwen3-Coder-30B-A3B) and then did
codes.T @ a8 for every selected expert. gate and up contract over that row, so one A for all experts is
correct there. down does not: its contraction is K = 768 (each expert's SwiGLU width) and its input is each
expert's own silu(gate_e·x) * (up_e·x). So the advertised --proj down path — the one projection
docs/npu.md names as the case where one A cannot be shared — died in numpy after the whole forward had
already run
:

File "tools/moe_expert_chunks.py", line 299, in gate
    exact = codes.astype(np.int32).T @ a8.astype(np.int32)      # [N]
ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0,
            (size 2048 is different from 768)

The change

One file, tools/moe_expert_chunks.py:

  • down computes a_row(e) = silu(gate_e @ h) * (up_e @ h) per expert, so it gets a row and a scale per
    expert (ascale_per_expert, A_int8_per_expert); a shape guard refuses loudly if a row does not match the
    contraction rather than reaching numpy.
  • The artifact also carries A_int8_M16: the M=16 A matrix the chunk-list dispatch actually takes, expert
    j in row j (docs/npu.md, "A chunk-list dispatch shares ONE A across its chunks"), rows past top-k zero.
    That is the layout a down gate needs, instead of the driver re-stacking rows.
  • Every artifact prints proj_cosine_per_expert: this projection's output for this token's row, int8 x int4
    against the container's float weights. The expected int32 is exact for the codes, so it cannot show this; this
    is the number a gate has to be read against.
  • gate/up are untouched: A_f32 (the router's input row), ascale, A_int8 and expected_int32 keep
    their meaning and their values.

Measured before/after

Reproducer, origin/main bd7d451a vs this branch, same command, same container
(~/models/npu/qwen3-coder-30b-a3b-moe, 19,528,322,120 B):

python3 tools/moe_expert_chunks.py gate <model> <out> --prompt "." --layer 0 --proj down --experts 8
arm result
before ValueError: … (size 2048 is different from 768), no artifact
after artifact written: K=768 N=2048, experts [16, 84, 114, 98, 53, 45, 85, 65], expected int32 in [-7413, 8963], output cosine 0.996994-0.999214 over the 8 experts

No regression in gate mode, checked field by field against the frozen oracle
~/evidence/tmoe-npu/gate-L0-gate_proj-pad1024.json (the 2026-11-01 artifact, same prompt, --pad-n 1024,
layer 0):

ok   router_top: [16, 84, 98, 114, 100, 53, 45, 65]
ok   ascale: identical        (0.019788969248761244)
ok   A_int8: identical
ok   expected_int32: 8 experts identical
REGRESSION-CLEAN

The same run gives gate's own output cosine for that row, 0.997246-0.998680 — the symmetric-int4 conversion
read as what it costs a projection's output, not inferred from a weight cosine.

Not done, and said so

No new test file: the artifact comparison above is the known-answer probe, and the alternative (a synthetic
container) would test arithmetic that the frozen oracle already pins. Nothing device-side — no ticket, no ELF,
no npu-hold; this tool reads the container on the CPU under ~/lb/thermal-run.sh only.

…d of crashing

gate built A from the layer's MoE input row (hidden 2048) and then did codes.T @ a8. down's
contraction is K = 768 - each expert's SwiGLU width - and down is the one projection that does not
share one A across experts, so the advertised --proj down path died in numpy after the whole forward
had already run: "size 2048 is different from 768".

down now computes its own row per expert, silu(gate_e @ h) * (up_e @ h), which also means a scale per
expert rather than one; the artifact carries the M=16 A matrix the chunk-list dispatch takes, with
expert j in row j (docs/npu.md, "A chunk-list dispatch shares ONE A"); and every artifact prints the
projection's output cosine for its own activation, so the one number a gate is read against is in the
run rather than inferred later.

gate/up artifacts are unchanged: re-run against the frozen oracle, router_top, ascale, A_int8 and
expected_int32 are field-for-field identical.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: READY at bb7494ea — the arithmetic is right, the guard is where the crash was, and the one format change it makes is contained. I did not re-run the end-to-end row; that needs the 30B container.

The maths, checked rather than assumed. down's contraction is each expert's own SwiGLU activation, and the new a_row(e) computes exactly that — silu(gate_e.T @ h) * (up_e.T @ h) — per expert, with each expert's own row and its own ascale/A_int8. gate/up keep the shared row, which is correct because they contract over the layer's MoE input. So the old failure is a modelling error, not a typo: one A was being handed to a projection whose rows differ per expert, and numpy only noticed at the matmul.

The guard sits at the point of failure, before the matmul rather than after it:

if a.shape[0] != K:
    raise SystemExit(f"{name}: expert {e}'s A row is {a.shape[0]}, the contraction is {K}")

which is the right shape of fix for a tool whose failure mode was a ValueError deep in numpy after the whole forward had run — 2048 from the layer input against 768 for down is now a named refusal at the first expert.

The output change is contained, and I checked that rather than hoping. For --proj down the JSON now carries ascale: null / A_int8: null (there is no single row) alongside ascale_per_expert, A_int8_per_expert and proj_cosine_per_expert. grep across tools/ and tests/ finds no other consumer of ascale/A_int8 — the only reader is tests/npu_moe_test.cpp, which consumes A_f32 and the router's answer, untouched. The tool's own note line says the host needs per-expert A_int8 for down, so a reader of the JSON cannot miss it. py_compile clean.

Also worth keeping: a_rows puts expert j in row j of an M=16 A with the rows past top-k zero — that is the A chunk-list dispatch layout docs/npu.md describes, so the tool now emits something a kernel can take rather than only a number to compare against.

What I did not do: run gate --proj down against a MoE container on this box. There is none here that this tool can read (the MoE GGUF on the box is not a q4nx container), so the before/after row in the body remains the author's. What I can say is that the code path is now shaped correctly, the failure it fixes is unambiguous from the diff, and the guard makes a repeat a named error instead of a traceback.

@bong-water-water-bong
bong-water-water-bong merged commit ade6447 into main Nov 5, 2026
8 of 9 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the 1bit/moe-down-gate branch November 5, 2026 11:24
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.

tools: moe_expert_chunks.py gate --proj down crashes instead of emitting the down gate (it uses the hidden-state row for a 768-wide contraction)

1 participant