Skip to content

tools: moe_expert_chunks verify --pad-n reads the chunks chunks --pad-n writes (it refused them as corrupt) - #1136

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

bong-water-water-bong merged 2 commits into
mainfrom
1bit/moe-verify-pad

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Root cause

chunks --pad-n 1024 writes each expert's chunk at N_padded (gate/up 768 -> 1024; down's 2048 stays 2048),
while verify took K, N from model.q4nx's __metadata__.moe and called read_chunk(..., K, N). On a padded
dir the byte-count check therefore fired on a correct file, with the one message in this tool that reads like
file corruption:

$ python3 tools/moe_expert_chunks.py chunks ~/models/npu/qwen3-coder-30b-a3b-moe /tmp/moe-pad --layers 1 --experts 2 --pad-n 1024
$ python3 tools/moe_expert_chunks.py verify ~/models/npu/qwen3-coder-30b-a3b-moe /tmp/moe-pad --layer 0 --experts 2
/tmp/moe-pad/L0_gate_e0.tf16: 2129920 B, want 1597440 B      # 2048x1024 codes + 16x1024 d = 2129920 B, i.e. right

This is not cosmetic: NW=32 needs N % 1024 == 0 (768 does not), and docs/npu.md sizes this model's decode stream
with --pad-n 1024 (1,930,346,496 B/token vs 1,703,854,080 unpadded), so the padded chunk is the one a dispatch
would eat and it was the one shape verify refused to read.

The fix

verify gains --pad-n, and prefers the per-projection N_padded that chunks() already writes into
<out>/manifest.json; without either it keeps the container's N (old behaviour). The readback is compared in the
container's own N columns, since the padding columns are zero.

Measured, same container (19,528,322,120 B), origin/main 6211c2fe -> 7e1c71e6, host only, no ticket

arm before after
padded chunks + manifest (reproducer) ...: 2129920 B, want 1597440 B, rc 1 ok gate 2048 x 768: rel rms 0.1309, cos 0.991547, max |dw| 0.0143 (written padded to 1024, compared in the container's 768), rc 0
same, up / down — 0.1288 / 0.991808 / 0.0131; 0.1399 / 0.990344 / 0.0287
unpadded chunks (regression, 2 experts) 0.1309 / 0.991547, 0.1288 / 0.991808, 0.1399 / 0.990344 identical, byte for byte on stdout
padded dir, no manifest, --pad-n omitted — still refuses (width unknowable)
padded dir, no manifest, --pad-n 1024 — ok, rc 0
expert 1's chunk copied over expert 0's — BAD ... rel rms 1.1899, cos 0.000460, rc 1

The padded readback reporting the same loss as the unpadded one is the known-answer check: zero padding cannot
change the container's own 768 columns.

tools/copyright.py --check rc 0 (no new file); one file changed, tools/moe_expert_chunks.py; gate untouched
(its --proj down fix from #1086 is in this base).

closes #1135

…wrote

chunks --pad-n writes N_padded columns; verify opened the chunk at the
container's own N and died with read_chunk's corruption message on a
perfectly good file. It now takes --pad-n and prefers the N_padded the
manifest beside the chunks already records, and compares the container's
own N columns (padding is zero). Padded and unpadded readbacks are
byte-identical in the error they report, which is the known answer.

closes #1135
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

#1136 ready for review at 7e1c71e

@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 — the reported failure reproduced exactly, and the fix reproduces the unpadded answer

Ran it on the container from the issue (~/models/npu/qwen3-coder-30b-a3b-moe), padded and unpadded, with this head and with main's tool over the same chunks.

Before, on this head's chunks but main's code — byte-identical to the issue's report:

$ python3 tools/moe_expert_chunks.py chunks <model> /tmp/moe-pad --layers 1 --experts 2 --pad-n 1024
$ (main's tool) verify <model> /tmp/moe-pad --layer 0 --experts 2
/tmp/moe-pad/L0_gate_e0.tf16: 2129920 B, want 1597440 B          rc=1

2,129,920 = 2048×1024 codes + 16×1024 fp16 d, i.e. exactly what chunks() wrote. And --pad-n did not exist on main at all (error: unrecognized arguments).

After, at this head, on the same padded chunks:

ok   gate  2048 x 768: worst of 2 experts rel rms 0.1309, cos 0.991547, max |dw| 0.0143 (written padded to 1024, compared in the container's 768)
ok   up    2048 x 768: worst of 2 experts rel rms 0.1288, cos 0.991808, max |dw| 0.0131 (written padded to 1024, compared in the container's 768)
ok   down  768 x 2048: worst of 2 experts rel rms 0.1399, cos 0.990344, max |dw| 0.0287
rc=0

The control that makes those numbers mean something: unpadded chunks (chunks with no --pad-n) verified on the same head give the same three rows to the last digit — 0.1309/0.991547/0.0143, 0.1288/0.991808/0.0131, 0.1399/0.990344/0.0287. So the padding really is zero, the [:, :N] slice takes the container's own columns, and the padded readback is not a different measurement — which is what the commit claims ("padded and unpadded readbacks are byte-identical in the error they report").

I also checked the axis rather than assuming it: read_chunk() returns codes [K][N], dequantise() keeps that shape, container_expert() is [K][N], so [:, :N] is the column plane and not the row plane. And chunks() pads the whole expert (pad_n(container_expert(...), N_padded)) before quantise(), so both the code plane and the fp16 d plane are N_padded wide — which is why read_chunk(..., K, n_out) computes the right want.

manifest_padding() reads geometry[<proj>].N_padded from the same manifest.json chunks() writes, with --pad-n as the override for a bare directory; a missing/garbled manifest falls back to the container's N instead of raising. Nothing else in the tool changed.

@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

PR Reviewer Guide 🔍

(Review updated until commit 0055495)

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

1086 - Partially compliant

Compliant requirements:

  • Fix the moe_expert_chunks.py gate --proj down to build each expert's own A instead of crashing
  • Ensure that the down projection computes a_row(e) = silu(gate_e @ h) * (up_e @ h) per expert
  • Ensure that the artifact carries A_int8_M16 and proj_cosine_per_expert
  • Verify that the fix does not introduce regression in gate mode

Non-compliant requirements:

  • Measure the before/after behavior with the specified command and container (this is a code review, not a runtime test)

Requires further human verification:

  • The before/after behavior measurement needs to be verified on the actual hardware

1135 - Partially compliant

Compliant requirements:

  • Fix moe_expert_chunks verify to refuse the very chunks chunks --pad-n writes
  • Make verify read the padding from the manifest file
  • Ensure that verify can handle padded chunks correctly
  • Ensure that verify still works with unpadded chunks

Non-compliant requirements:

  • None

Requires further human verification:

  • None
⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Padding Logic Inconsistency

The verify function introduces a new --pad-n argument to handle padded chunks, but it relies on reading from a manifest file. If the manifest file is missing or malformed, it defaults to using the container's N, which could lead to incorrect behavior if the chunk was actually padded. This could cause verification to fail on valid padded chunks.

written = manifest_padding(args.out)
bad = 0
for proj, suffix in PROJ.items():
    name = q.layer_name(args.layer, suffix)
    info = q.moe_meta["tensors"][name]
    K, N = info["cols"], info["rows"]
    n_out = max(N, args.pad_n or written.get(proj, N))
Manifest Parsing Robustness

The manifest_padding function uses a broad exception handling pattern that catches FileNotFoundError, KeyError, TypeError, and ValueError. While this prevents crashes, it might mask underlying issues with manifest files, making debugging harder when the manifest is corrupted or improperly formatted.

try:
    with open(os.path.join(out, "manifest.json")) as f:
        geo = json.load(f)["geometry"]
    return {p: int(v["N_padded"]) for p, v in geo.items()}
except (FileNotFoundError, KeyError, TypeError, ValueError):
    return {}

@bong-water-water-bong
bong-water-water-bong merged commit 5623482 into main Nov 5, 2026
9 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the 1bit/moe-verify-pad branch November 5, 2026 20:37
@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit 0055495

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.

tools: moe_expert_chunks verify refuses the very chunks chunks --pad-n writes, with a byte-count that reads like corruption

1 participant