Repository navigation
tools: moe_expert_chunks gate --proj down builds each expert's own A instead of crashing - #1086
Conversation
…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
left a comment
There was a problem hiding this comment.
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.
tools:
moe_expert_chunks.py gate --proj downbuilds each expert's own A instead of crashingcloses #1083
Root cause
gatebuilt onea8from the layer's MoE input row (hidden 2048 on Qwen3-Coder-30B-A3B) and then didcodes.T @ a8for every selected expert.gateandupcontract over that row, so one A for all experts iscorrect there.
downdoes not: its contraction isK = 768(each expert's SwiGLU width) and its input is eachexpert's own
silu(gate_e·x) * (up_e·x). So the advertised--proj downpath — the one projectiondocs/npu.mdnames as the case where one A cannot be shared — died in numpy after the whole forward hadalready run:
The change
One file,
tools/moe_expert_chunks.py:downcomputesa_row(e) = silu(gate_e @ h) * (up_e @ h)per expert, so it gets a row and a scale perexpert (
ascale_per_expert,A_int8_per_expert); a shape guard refuses loudly if a row does not match thecontraction rather than reaching numpy.
A_int8_M16: theM=16A matrix the chunk-list dispatch actually takes, expertjin rowj(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.
proj_cosine_per_expert: this projection's output for this token's row, int8 x int4against 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/upare untouched:A_f32(the router's input row),ascale,A_int8andexpected_int32keeptheir meaning and their values.
Measured before/after
Reproducer,
origin/mainbd7d451avs this branch, same command, same container(
~/models/npu/qwen3-coder-30b-a3b-moe, 19,528,322,120 B):ValueError: … (size 2048 is different from 768), no artifactK=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 expertsNo 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):
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.shonly.