Repository navigation
Description
What happens. A Phi-3.5-mini OGA NPU pack (amd/Phi-3.5-mini-instruct_rai_1.8.0_npu_4K) imports strictly, with no --allow-incomplete, and every structural check passes — and the container then produces wrong text. This is the first pack where the import path has no numeric anchor, and the failure is exactly the kind that only an anchor would have caught.
1bit npu-pack import -> imported 291 tensors, 454392 q4_1 tiles
self-check: SKIPPED -- the head is not tied to the embedding, so the pack carries no
independent reference to test the decode against
container: read back through q4nx::dequant_tile rel_err 2.790e-03 max |diff| 8.545e-04
attention: 6144/6144 .fconst tiles survive decode->encode byte for byte
MLP: 5632/5632 .fconst groups survive decode->encode byte for byte
fconst: stride 61923520, second header 20895808; 64/64 RMSNorm anchors found
text: 6 of 8 tokenizer/chat files (absent from the pack: vocab.json, merges.txt)
Host-GEMM output (device-free, no NPU ticket):
'The capital of France is' -> 'Awesome.<0x0A><0x0A><0x0A>####'
'2 + 2 =' -> '+2+2+2+2'
Where the fault is, measured rather than guessed. --dump-logits on the Phi container, with the working Qwen3-1.7B container run through the identical instrument as a control (both host GEMM, same 1-token request, raw fp32 vocabulary logits):
| Qwen3-1.7B (control) | Phi-3.5-mini | |
|---|---|---|
| prompt ids | [785, 6722, 315, 9625, 374] |
[450, 7483, 310, 3444, 338] |
| mean logit | +2.548 | +27.778 |
| logit std | 2.993 | 2.839 |
| top-1 / top-2 | 12095 / 1112 | 29909 / 29954 |
| top-1 margin | +3.250 | +0.032 |
top-1 decoded (pack's own tokenizer.json, via tokenizers) |
' Paris' ✓ |
'A' |
Read it as three separate statements:
- The text path is honest. Phi's served token is its argmax (
'A'), and the argmax decodes to a real token under the pack's own tokenizer read by a different implementation. So the garbage is not a detokeniser artefact — the earlier<0x0A>rendering was genuine byte-fallback tokens. - The distribution is near-flat: margin +0.032 against the control's +3.250. A model that is working does not tie between "A" and "G" on "The capital of France is".
- All logits sit ~25 nats high (mean +27.8 vs +2.5) while the spread is normal (std 2.84 vs 2.99). A uniform scale would move both; a nearly-constant offset with unchanged spread is what a large, near-rank-1 component in the head's input looks like — i.e. the representation entering the head, not the head's weights (the head's dequant is verified to 2.8e-3 by the container round trip and by the four-pack code-convention check).
What is excluded (each checked, not assumed):
attention_bias—config.jsonsaysfalse, so0 bias tensors importedis correct for this pack, not a silently dropped bias. (This was my first suspicion and it was wrong.)longrope— the forward implements it:read_rope_short_factor(q4nx_forward.cpp:229), andq4nx_rope.h:136acceptsropepe[longrope]whenshort_factoris present. The pack'srope_scalingis carried into the container config verbatim. Also, at positions 0-10 the short factors are ≈1.0, so rope cannot produce total garbage.- Tokenizer file count — 6 of 8 (
vocab.json,merges.txtabsent) matches the Llama-3.2-1B pack, which serves fine ontokenizer.jsonalone. - The head's weights — verified by the round trip (2.8e-3) and by the same code convention that scored corr 1.0000 on four packs.
What this costs the loader's credibility: "strict import OK, payloads byte-exact, all norm anchors found" was true and still the model was wrong. That is worth an issue of its own because it is the strongest argument yet for the thing this pack lacks: an anchor that covers the head when the head is untied. Options, smallest first:
- Refuse untied-head packs as "unverifiable" unless
--allow-unverified-headis passed, so the importer never reports a clean import it cannot check. - Take the optional reference from the pack's own
model.onnxwhere available — for this pack the prefill branch carries the same weights, so a prefill-vs-token cross-check may give a second independent path (not yet investigated). - Anchor against the upstream fp16 checkpoint (
microsoft/Phi-3.5-mini-instruct, ~7.7 GB), the method used for Qwen2.5-0.5B in this lane on 2026-10-30 — the only route that gives a real bit-exact target, at the cost of a download.
Reproduce (device-free, ~2 min/arm, ~4 GB scratch for the container):
~/ogaload/oga13/phi/dl_phi35.sh # 5.4 GB pack, verifies sizes against the HF API
~/ogaload/oga13/phi/import_phi35.sh # info -> strict import -> host-GEMM coherence probe
~/ogaload/oga13/phi/logit_probe.sh # logits + argmax decoded by the pack's own tokenizer, with a control arm
Next discriminating experiment (named, not taken): dump the hidden-state RMS entering the head and compare it with the pack's own final-norm weights applied to the last layer's output. If the head input's RMS is far from ~1, the final norm is not being applied on this architecture and that is the bug; if it is ~1, the offset comes from inside the layer stack and the attention payload's interpretation (32/32 heads at head_dim 96) is the suspect — byte-exact round trips prove self-consistency, not meaning.
Found 2026-11-05 while working the pack-import brief (step 6: a second pack of a different family). Pack-side facts and the four-pack comparison are in the npu-re hub, docs/FACTS-oga-pack-weight-layout.md.
Activity
bong-water-water-bong commentedon Nov 5, 2026
Two cheap exclusions, device-free, so the next person does not redo them (both were candidates for the +25-nat offset with normal spread):
1. The head's encoding in the container is the same as every container that works. All four working containers and the Phi one store
lm_head.weightasI8with a fixed 5120-byte row stride (shape is[payload/5120, 5120], i.e. a re-shaped byte blob, so theshapefield carries no meaning):So "the head is stored differently for this pack" is out. (Recorded because the Phi shape
[12024, 5120]looks alarming at first read — 12024 rows is not a divisor of 32064 — and it is not alarming:61562880 / 5120 = 12024exactly, same construction as the others.)2. The final norm is applied, and it is read from the container.
q4nx_forward.cpp:1540loadsfinal_norm_from the container manifest (mw_->norm_weight, BF16[3072]— present and identical in both.fconsttails at import time), andq4nx_forward.cpp:5072runsrmsnorm(x, final_norm_, H, xn)immediately before the head. Alsomodel.norm.weightin Phi's container header isBF16 [3072]at[258957312, 258963456], which is the correct 6,144 bytes.So two of the three "easy" explanations are gone, which leaves the offset arising inside the layer stack or from the head input's content (the norm weight being right does not prove the residual stream it normalises is right). The next discriminating experiment stands as written — dump the hidden-state RMS into the head — and I would now add one cheaper arm ahead of it: run the same container through the prefill branch (a 128-token prompt instead of a 1-token one) and compare the two distributions' margins. The pack ships separate Token and Prefill programs, so if prefill is peaked and token is flat, the fault is in the decode path's layer handling rather than in the architecture at large — and that would point straight at the attention payload's interpretation for 32/32 heads at head_dim 96, which is the one thing byte-exact round trips cannot verify.
bong-water-water-bong commentedon Nov 5, 2026
Branch probe + two more exclusions. The model is not scrambled — it is degraded, and the fault is common to both passes.
~/ogaload/oga13/phi/branch_probe.sh(device-free, host GEMM,--dump-logits, 3 tokens, control arm through the same code):'A'' Paris''w''.''esome'' 'Three things follow:
A+w+esome). That is degraded model behaviour, not scrambled behaviour — word salad from a misordered attention payload would not converge on a single plausible word. So "the attention payload is misinterpreted" is weakened, not excluded: it would have to be a small error, not a permutation.Excluded: the head's zero point is NOT dropped. That was my leading hypothesis, because dropping it predicts exactly this signature — I measured, on the pack's own
lm_head, that a missing zero point adds +0.1133 per weight (row mean goes from −0.0004 to +0.1128, and across 17 sampled vocabulary rows the with-zp means sit in −0.001…+0.001, i.e. genuinely centred), which over K=3072 with a head-input mean of ~0.07 is +24 nats against the observed +25.2. But the container does not have that defect: dequantising itslm_headrow 0 through the engine's own q4_1 tile layout (q4nx_dequant_tile, 32×256 tiles, group-major scale/zero, 5120 B) gives mean −0.0027, std 0.0484 — near-zero mean, matching the pack's with-zp row (−0.0004, 0.0435), not the dropped-zp row (+0.113).(Note for anyone repeating that measurement: the container's
data_offsetsare relative to the data section after the JSON header — for this container the header is 99,712 B and data starts at 99,744. Reading at file-absolute197001216yields a region of zeros and a nonsense scale field; I did that first and it is worth not doing again.)So the offset survives both of the cheap explanations. The next measurement is the one that reads the head input directly, and it turns out no code change is needed:
NPU_INFER_DUMP_HIDDEN=1(q4nx_forward.cpp:5075) already writes the post-final-norm vector for each position plus a stderr line withmean|x|andabsmax. I am running it on the Phi container with the Qwen3-1.7B control now; a sane RMSNorm output is mean|x| ≈ 0.8 / absmax ≈ 3-5, and if Phi's is off that is the fault, while if it is in range the offset must come from the head's own common direction and the only remaining reference is the upstream fp16 checkpoint.bong-water-water-bong commentedon Nov 5, 2026
Head input is sane — so the +28 offset is very likely a red herring, and the real symptom is the wrong prediction.
NPU_INFER_DUMP_HIDDEN=1on both containers (host GEMM, same instrument, same prompt):absmaxRead against what an RMSNorm output should look like (rms ≈ 1, signed mean ≈ 0, roughly half positive), Phi's head input is healthier than the control's — smaller rms, near-zero signed mean, half positive. So the final norm and the residual stream reaching it are behaving; "the representation entering the head is broken" is excluded.
Consequences, in order of how confident I am:
h's affinity with the head's own row distribution changes nothing about the argmax or the sampled tokens. With a saneh, the offset has to come from a common direction in the head's rows — a property of the weights — and it should therefore be dropped from the symptom list. The real symptom is the prediction:'A'/Awesomewhere a working Phi-3.5-mini says' Paris', with a step-0 margin of +0.032.hwhose content is wrong enough to change the answer but not enough to scramble it — consistent with the earlier branch probe (margins grow, tokens compose a real word) and with a small, systematic error rather than a permutation: a head-dim/head-count mix-up that cancels in norm but not in attention, a wrong rotary frequency on the higher dims, or an attention-payload ordering that is close but not exact.lm_headvalues in Python to check the head independently and my tile-order reimplementation overflows on most row-tiles (inf/NaN) while the first row-tile decodes cleanly (mean −0.0027, std 0.0484, matching the pack's with-zp row). The importer's own round trip goes through the engine'sq4nx_dequant_tileand passes at 2.79e-3, so the evidence that the container's head is faithful is the importer's check, not my reimplementation — and my reimplementation is not evidence of anything.The only reference left. With
hsane, the head faithful to round-trip tolerance, the zero point intact, both passes equally affected, and no rope/bias/norm defect found, the remaining way to say "our logits are wrong" rather than "our logits look odd" is to compare against the upstream fp16 checkpoint (microsoft/Phi-3.5-mini-instruct, ~7.7 GB): run it through the CPU path for the same prompt and compare top-1 and margins. That is the method this lane used for Qwen2.5-0.5B on 2026-10-30, and it is a download-sized decision rather than a code one — so I am recording it here rather than starting it unasked.Net for this issue: the loader is exonerated on the structural path (payloads, anchors, head round trip, final norm, head input), and the defect is now confined to numerics inside the layer stack for a 32/32-head, head_dim-96 architecture — the one place where this pack differs from every pack the engine has been validated against.
bong-water-water-bong commentedon Nov 6, 2026
Root cause found: the weights were right all along — the engine's tokenizer fed this model ids it had never seen. Fix is PR #1209 (closes this).
npu/tokenizer.cppdecided "SentencePiece" frommerges.empty() && has_meta. A SentencePiece model exported totokenizer.jsonkeeps its merges ([["▁","t"], …]), so this pack (normalizer{Prepend "▁", Replace " "→"▁"}, 61,249 merges, nopre_tokenizer,byte_fallback: true) fell through to the byte-level GPT-2 path, which byte-maps the metaspace▁(E2 96 81) into pieces the vocabulary does not spell. "The capital of France is" encoded to 11 ids instead of the pack's 5 (450 7483 310 3444 338).Same container, one variable (prompt as text vs prompt as the pack's own five ids, one
forward-serve):'A'29909, margin +0.032'A'→'Awesome''Paris'3681, margin +4.491' Paris''Paris', margin +4.491' Paris', then'.'Two things this issue got wrong, both corrected by measurement:
+25‑nat offset is not a defect. Re-applying the head from the pack's own 4-bitlm_headto the dumped post-final-normhreproduces the engine's step-1 logits to three decimals (mean +28.849, std 3.046, top-1 29893) — the head and its application were fine, and a large mean logit with a healthyhis this pack's normal state. AMD's own stack reports mean +40.518 on the same pack.tokenizer.json.Oracle, same hour (AMD 1.8.0 OGA on this pack, NPU,
npu-holdticket, 12 s,amdxdna ERROR 0 -> 0): first-step distribution.,and\n;(std 3.454, margin +0.500) vs our engine after the fix.,and\n;(std 3.459, +0.540); AMD's textThe capital of France is Paris.— identical top-5 order, std and margin.Regression surface of the fix: new fixture + golden from HF
tokenizerson that same file is 0/10 before / 10/10 after; Phi-3.5 30-case golden 29/30 (the miss is a literal-metaspace round trip HF loses too), Qwen3-1.7B 30/30, Llama-3.2-1B 30/30, Zamba2-1.2B 8/8 (same shape, previously wrong),tokenizer_linearunchanged. On this pack's vocabulary greedy longest-match agrees with the reference on only 14/20 strings, so the merge list is used, not longest-match.Evidence (series):
evidence/oga/phi35-tokenizer-2026-11-05/RESULT.mdin the npu-re hub, FACTS section "resolved".npu-pack: import can declare the device weight layout, so a b8 kernel…