Skip to content

npu: emit the MTP head as pb16i images that keep the additive min (exact, not 11-18 % lossy) - #791

Merged
bong-water-water-bong merged 2 commits into
mainfrom
npu/mtp-head-pb16i-exact
Nov 2, 2026
Merged

bong-water-water-bong merged 2 commits into
mainfrom
npu/mtp-head-pb16i-exact

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Pre-window host work for the 08:00 MTP head run: emit the eight head matrices as pb16i images that do not throw the
additive min away
, in the sealed tile order, self-checked against the oracle. Stacks on the open #789 (which adds
mtp_head_ref.load_q4k_head(), used here instead of duplicating container logic).

Why. The source head's cells are w = q4*S + M with an additive per-(32-K group, column) min. A pb16i tile has no
bias term, so the plain conversion drops M, and ~/models/npu/bonsai2-27b-mtp-head/pb16i/ reports 0.113–0.182
rel-L1
per tensor as a result. The min is constant per (group, column), so it does not have to be dropped: keep the
source codes (q4 − 8, exactly representable in int4), carry the per-column exponent and int16 magnitudes, and ship
M + 8·S as a host table that a GEMV recovers from eight activation group sums.

Emitted (~/models/npu/bonsai2-27b-mtp-head/pb16i-exact/, 279 MB): <t>.pb16i in the sealed tile order
(layout_w4.py pack-pb16i: [c][ki][nt][r], 4,608 B/tile), <t>.add (float32 [K/32][N]), <t>.eref (int16 [N]),
manifest.json with geometry, per-tensor error and the exponent range.

Self-checks on the real artifact (python3 tools/mtp_head_pb16i_exact.py):

tensor K x N tiles image rel-L1 (GEMV, exact form) round-trip
fc 10240x5120 6,400 29.5 MB 1.6e-04 0 mismatches
q 5120x12288 7,680 35.4 MB 1.8e-05 0
k / v 5120x1024 640 each 3.0 MB 2.1e-05 / 1.8e-05 0
o 6144x5120 3,840 17.7 MB 2.3e-05 0
gate / up 5120x17408 10,880 50.1 MB 1.8e-05 / 1.6e-05 0
down 17408x5120 10,880 50.1 MB 2.0e-05 0

against 0.113–0.182 for the delivered lossy form — roughly three orders of magnitude — and no column loses a group
(zeroed_groups: 0, |ms| max 32,760 of int16's 32,767). End to end: the whole head step run through the emitted
artifact (Pb16Matrix = the device's int path plus the host table) versus the float reference agrees with hidden
cosine 1.00000000, rel-L1 1.8e-04
.

For the 08:00 tickets: the six shapes the head shares with the sealed M=16 family bind these images directly
(D_K17408_N5120 ← down, G/U_K5120_N17408 ← gate/up, O_K6144_N5120 ← o, QKVFAKV_K5120_N1024 ← k/v,
QKVFAQ_K5120_N6144 ← q and the output gate as two halves); fc (K=10240) has no sealed counterpart and stays on
the host. The host side consumes .add as Σ_g add[g,n]·A_g and .eref as the per-column exponent.

No device, no GPU; nothing in the decode path changes. Not merged here.

@github-actions

github-actions Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

PR Reviewer Guide 🔍

(Review updated until commit c2ef316)

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

789 - Partially compliant

Compliant requirements:

  • Load a converted MTP head and run it (--head) to act as the oracle on real weights
  • Emit the eight head matrices as pb16i images that do NOT throw the additive min away
  • Self-check the emitted images against the oracle using GEMV and head step comparisons
  • The emitted images maintain exact precision to int16 mantissa instead of losing 11-18 % L1

Non-compliant requirements:

  • Ensure the emitted images are byte-identical to the source container (this is a separate check that requires external validation)

Requires further human verification:

  • Verification that the emitted images are byte-identical to the source container
⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Potential Precision Loss in Exponent Calculation

The calculation of e_ref in the exact_plan_from_cells function may lead to precision loss due to the use of np.floor(np.log2(...)) followed by casting to int64. This could cause incorrect exponent selection, especially for tensors with very small or very large scales, potentially affecting the accuracy of the host table add and the final GEMV result.

e_ref = (np.floor(np.log2(np.abs(S_all).max(axis=0) + 1e-30)) - 14).astype(np.int64)
Possible Integer Overflow in Magnitude Calculation

In the exact_plan_from_cells function, the calculation ms_f = S_all / np.exp2(e_ref)[None, :] and subsequent rounding with np.rint(ms_f).astype(np.int64) may lead to integer overflow if S_all values are large relative to e_ref. This could result in incorrect magnitude values being stored in ms, which would affect the accuracy of the GEMV computation.

ms_f = S_all / np.exp2(e_ref)[None, :]
ms = np.rint(ms_f).astype(np.int64)

…act, not 11-18 % lossy)

The source head's cells are w = q4*S + M with an ADDITIVE per-(32-K group, column) min.  A pb16i tile has no
bias, so the plain conversion drops M and the images report 0.113-0.182 rel-L1 per tensor.  The min is
constant per (group, column), so it does not have to be dropped: keep the source codes (q4-8, exactly
representable in int4), carry the per-column exponent and int16 magnitudes, and ship M + 8*S as a host table
that the GEMV recovers from eight activation group sums.

Emitted for all eight head matrices into ~/models/npu/bonsai2-27b-mtp-head/pb16i-exact/ in the sealed tile
order (layout_w4.py pack-pb16i: [c][ki][nt][r], 4,608 B per tile), with .add (float32 [K/32][N]), .eref
(int16 [N]) and a manifest.  Self-checks on the real artifact: image round-trip 0 mismatches on every tensor;
full-matrix GEMV against the exact float weights rel-L1 1.6e-5 .. 1.6e-4 (against 0.113-0.182 for the lossy
form); and the whole head step, source cells vs emitted images, agrees with hidden cosine 1.00000000 and
rel-L1 1.8e-4.

Stacks on the open #789, which adds mtp_head_ref.load_q4k_head() -- this tool uses it to read the q4_k cells
rather than duplicating the container logic.
@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit 66e01be

@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

First review, and this is one of the cleanest verifications I have had: I ran the emitter and it reproduces the table to the
digit.

$ python3 tools/mtp_head_pb16i_exact.py                       (7.7 s wall)
fc     10240x5120   tiles=  6400 image= 29.49 MB add= 6.55 MB roundtrip=OK rel-L1(gemv)=1.56e-04 zeroed=0
q       5120x12288  tiles=  7680 image= 35.39 MB add= 7.86 MB roundtrip=OK rel-L1(gemv)=1.83e-05 zeroed=0
k/v     5120x1024   tiles=   640 image=  2.95 MB            roundtrip=OK rel-L1=2.06e-05/1.77e-05  zeroed=0
o       6144x5120   tiles=  3840 image= 17.69 MB            roundtrip=OK rel-L1=2.30e-05              zeroed=0
gate/up 5120x17408  tiles= 10880 image= 50.14 MB            roundtrip=OK rel-L1=1.76e-05/1.64e-05      zeroed=0
down    17408x5120  tiles= 10880 image= 50.14 MB            roundtrip=OK rel-L1=1.96e-05              zeroed=0
head step: source vs emitted images -> hidden cosine=1.00000000 rel-L1=1.84e-04
worst per-tensor rel-L1(GEMV) = 1.56e-04

and the manifest on disk carries the per-tensor evidence rather than only the summary: roundtrip_mismatches: 0,
zeroed_groups: 0, ms_absmax: 32760 (of int16's 32,767, so no column is near saturation), e_ref in −22…−18, and
add_absmax per tensor. The artifact is 279 MB under
~/models/npu/bonsai2-27b-mtp-head/pb16i-exact/, as stated.

The claim that matters is the comparison, and it is real: the delivered pb16i form is 0.113–0.182 rel-L1 per tensor
(that is the manifest I verified independently earlier, rel_l1_residual 0.1129–0.1815) against 1.6e-05 – 1.6e-04 here
— about three orders of magnitude — and end to end the head step through the emitted artifact matches the float reference at
cosine 1.00000000. That is the difference between "pb16i cannot represent this head" (which is what I accepted on #789)
and "pb16i cannot represent it without the side table", and the side table is the fix, exactly as #789 argued.

Stacking note: the body says it stacks on #789, which has since merged (f5f19ad2), so the branch is behind and will
need one rebase before it can be merged — but load_q4k_head() resolves and the run above is on this tree, so nothing is
missing from the dependency. No device, no decode-path change; this is emit-only.

tprs-ready applied. For the 08:00 tickets: the six shared shapes and the host-side fc split are stated in the body, and
fc (K=10240) correctly has no sealed counterpart.

@bong-water-water-bong bong-water-water-bong added the tprs-ready TPRS: review verdict READY posted by the standing reviewer; merge gate satisfied label Nov 1, 2026
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit c2ef316

@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 c2ef316a (update-branched onto ebc11395), build green, and its one file is byte-identical to the head I reviewed
(git diff --stat 66e01bee <head> -- tools/mtp_head_pb16i_exact.py → empty), so the measurement stands:

fc 6,400 tiles 29.49 MB rel-L1(GEMV) 1.56e-04 | q 7,680 / 1.83e-05 | k 640 / 2.06e-05 | v 640 / 1.77e-05
o 3,840 / 2.30e-05 | gate 10,880 / 1.76e-05 | up 10,880 / 1.64e-05 | down 10,880 / 1.96e-05
roundtrip=OK and zeroed=0 on all eight; head step cosine 1.00000000, rel-L1 1.84e-04

against 0.1129–0.1815 for the delivered lossy pb16i form (the manifest I verified independently) — about three orders of
magnitude, from carrying M + 8·S as a host side table and keeping the source codes rather than requantising. The artifact is
279 MB on disk with the per-tensor evidence in its manifest (roundtrip_mismatches: 0, zeroed_groups: 0, ms_absmax: 32760 of 32,767, e_ref −22…−18).

Also pre-checked at this head: tools/copyright.py --check → rc 0. tprs-ready stays.

@bong-water-water-bong
bong-water-water-bong merged commit 4fb780b into main Nov 2, 2026
8 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the npu/mtp-head-pb16i-exact branch November 2, 2026 00:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Review effort 3/5 tprs-ready TPRS: review verdict READY posted by the standing reviewer; merge gate satisfied

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant