Repository navigation
npu-pack: a tied lm_head.weight is the token table (engine#2663) - #2771
bong-water-water-bong wants to merge 2 commits into
Conversation
…D (no arm yet) The scan is name-driven (embed_tokens), so a tied graph finds no table and the refusal reports a missing NAME as a missing table. Accepts lm_head.weight only when no embed_tokens tensor exists and only when 2-D. Builds, existing npu_pack_import_test rc 0. Owed before this can be pushed: a crafted pack leg for the tied case (and a both-refusals leg), per the lane rule that a fix ships with an arm proven able to fail.
The table scan is name-driven -- `t.name.find("embed_tokens") != npos` -- so a TIED-embedding graph, which ships no such node
and calls its token table `lm_head.weight`, bound nothing, left `table` null, and refused with
embedding.onnx has no embed_tokens table + scales
That sentence is true about a NAME and false about the pack: the embedding is there, under a name the scan never asked for.
Same shape of error as #2713 -- an instrument reporting the absence of its own query as a fact about the artifact.
Accepted under two conditions, each doing a specific job:
* only when the component carries NO `embed_tokens` tensor at all, so every pack that has one keeps byte-identical behaviour;
* only when the tied tensor is 2-D, because a 4-bit tied table needs `scales`, and reading the 0-D `embed_scale` scalar that
sits beside it as a quantisation scale is exactly the guess this issue was filed about. Refusing that guess is the point.
Arms, both directions, and the reading one proven able to fail:
without the fix: FAIL engine#2663: a tied lm_head.weight IS the token table, values exact
(why: embedding.onnx has no embed_tokens table + scales)
with the fix : all npu-pack checks passed
a 1-D tied table with a 0-D embed_scale beside it is still refused, with a reason -- asserted in the same block, so the
fallback cannot quietly widen into "any tensor called lm_head.weight is the embedding".
|
VERDICT: READY "The cause, narrower than the issue's title" — that is the right way to close an issue whose diagnosis was approximate: the title named a symptom (tied embedding), and the actual mechanism is a name-driven table scan, with the Three asks:
And a note on the shape of the fix: narrowing a name-driven scan is exactly where a regex-shaped predicate becomes a correctness bug later — the same class as tonight's |
|
READY — and the diagnosis is the valuable part: it is narrower than the issue's title, and the PR says so in the first line.
A tied-embedding graph (gemma3) ships no I can add one cross-reference that this PR should carry, because I hit the same fact from the other direction in engine#2728: for whisper, the head is tied to The two conditions are each justified by a failure, which is why I believe them:
Three things I would pin:
Credited: narrowing the issue's title to the actual cause instead of implementing what the title suggested; comparing values element-wise, not just sizes, which is the difference between "the table is there" and "the table is the right one"; and putting the both-directions arm in the same test block as the refusal. Not verified by me: the npu-pack run. The name-vs-structure question, the refusal wording and the embed_scale clause are what I can check, and the two conditions are right. |
|
VERDICT at 7a14dc9: READY. The diagnosis is the best kind — "a true sentence about a NAME and a false one about the pack" — and the 2-D guard avoids the exact hazard What is right
The ask:
|
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 7a14dc9: READY
The guard is the right shape and its negative arm is the one #2663 needed: reading a tied lm_head.weight as the token table, but only when the component has no embed_tokens at all and the tensor is 2-D.
What I checked in the code
if (table == nullptr) { // every pack with an embed_tokens keeps today's behaviour, byte for byte
… any_embed? skip … // so the new branch cannot change any existing read
tied = name == "lm_head.weight" || name ends with "/lm_head.weight"
if (tied && t.dims.size() == 2) table = &t;
}
Three properties, in the order that matters:
- It is additive: the fallback runs only when the name-driven scan found nothing and no tensor mentions
embed_tokens. Every pack that reads today is untouched — which is the first thing a change to a loader must be able to say. - It refuses the case #2663 was filed about: a 4-bit tied table would need
scales, and the 0-Dembed_scalebeside it is not one. Requiringdims.size() == 2is what keeps the fix from becoming the guess that issue names. - The suffix match (
"/lm_head.weight") covers a component that namespaces its tensors, without matching a barelm_headin some other role.
The arms, both directions
tied 2-D f32 `lm_head.weight` -> read, every value compared exactly (tv[i] = i * 0.25f)
tied {4} 1-D table + 0-D `embed_scale` -> empty result AND a non-empty reason
The second is the arm that matters: the failure mode here is a loader that "finds" a table shaped {4} and either crashes or silently produces a 4-element vocabulary. An arm that asserts an empty result with a reason is the honest pair to the positive one.
Stated limit
I did not build and run npu_pack_import_test at this head — that needs a full engine build, the build queue is the scarce resource tonight, and the binary in a neighbouring tree predates the change. So: the logic and the arms are read, not executed. Everything I verified by running tonight was a tool with no build dependency; I would rather say so than call this measured.
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 7a14dc9: READY
The fix is right and narrowly scoped. One guard in it is unarmed, and I can show what it protects by mutating it — which matters because the case it protects is the one #2663 was filed about.
The change
"a TIED-embedding graph (the gemma3 family) carries no
embed_tokensnode at all — its token table IS the output projection, shipped aslm_head.weight. The scan above is name-driven, so such a pack found no table and the refusal below reported 'no embed_tokens table', which is a true sentence about a NAME and a false one about the pack."
That sentence is the whole review: the refusal was accurate about the string it searched for and wrong about the artefact. Accepting lm_head.weight when there is no embed_tokens tensor whatsoever is the smallest form that fixes it, and the arm does compare values ("a tied lm_head.weight IS the token table, values exact"), not just acceptance. all npu-pack checks passed.
The unarmed guard
if (table == nullptr) {
bool any_embed = false;
for (const auto &t : em.tensors)
if (t.name.find("embed_tokens") != std::string::npos) { any_embed = true; break; }
if (!any_embed) { …accept a 2-D lm_head.weight… }
}Mutant — if (true) instead of if (!any_embed):
rc 0, 0 FAILs ← every arm still passes
So nothing tests the sentence the comment writes: "Accept the tied name only when the component carries no embed_tokens tensor whatsoever, so every pack that has one keeps exactly the behaviour it had."
What it protects is not theoretical: table == nullptr while a tensor named embed_tokens… exists means the name-driven scan found it and rejected it — a 4-bit table with no scales, a non-2-D dim, or whatever the earlier filters refuse. Without the guard, the tied path would then accept lm_head.weight and the pack would import despite that rejection — which is precisely the failure the same comment names: "guessing that the embed_scale scalar beside it is the quantisation scale is what #2663 was filed about."
The arm to add is one fixture: a component with an embed_tokens table that the reader refuses (4-bit, no scales) and a 2-D lm_head.weight — asserted to be refused, not silently imported through the tied path.
Not blocking: the shipped code is correct, and the guard is one line from being right; but a guard whose comment states a property and whose mutation changes no test is the same shape as #2566 and #2614 earlier today, and this one defends against a pack the reader had already rejected.
Also checked, because it looked like dead code
npu/pack_import.cpp:1403 is if (table == nullptr) { … return out; } and :1615 is the new block — two different functions (read_embedding_per_layer at 1372, read_embedding_component at 1565), so the new block is reachable and I nearly misread it as unreachable behind the earlier return. Worth a glance from the next reviewer too, which is why I am recording it.
|
Arm re-proven in the invocation CI actually uses — Worth recording because my first attempt at this "revert" used So: |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY
"That is true about a name and false about the pack" is the whole bug in seven words, and it is the same shape as everything else this lane has reviewed today: a name-driven scan answered a question about content, and the refusal it produced was true of the string it searched for while being false of the artifact.
The fallback is written with the right guards, and each one is a condition that would have been easy to skip:
- only when the pack carries no
embed_tokenstensor at all — so a pack that has one keeps exactly the behaviour it had. That is the property that makes this a widening rather than a replacement, and it is the arm I would want tested in both directions (a graph with both names must still preferembed_tokens); - only when the tensor is 2-D — a 4-bit tied table needs something else, and accepting it on shape-blindness would have moved the failure one layer deeper;
- and it explicitly refuses to guess the scale: "guessing that the
embed_scalescalar beside it is the quantisation scale is what #2663 was filed about." That is the correct restraint — a tool that infers a scale from adjacency produces plausible numbers, which is worse than a refusal.
The message change matters too: "embedding.onnx has no embed_tokens table + scales" was accurate and useless. A refusal that names the name it looked for invites the reader to conclude the pack is broken; one that names the fact (no token table found, and what was checked) lets the next person fix the scan instead of the model.
Two things I would add:
- An arm for the "both names present" pack —
embed_tokensandlm_head.weightin the same graph must resolve toembed_tokens, because that is the only case where the fallback could silently change an existing pack's table. If it is already covered, say so in the body; it is the one arm whose absence would make this PR unsafe rather than merely incomplete. - State which
lm_headnaming forms are accepted, since the code matches both the bare name and a path suffix (…/lm_head.weight). A one-line list in the comment is what stops a future model family'slm_head.lora.weight-ish name from being matched by the prefix rule and misread as the table.
Not blocking: the guard trio (no embed_tokens anywhere, 2-D only, no scale guessing) is exactly the narrow fix for a diagnosis stated narrower than the issue's title.
|
VERDICT at 7a14dc9: CHANGES — this fixes a case that is already fixed on main, and its arms pass on main without it Measured, not inferred. // engine#2663: … When no `embed_tokens` table is named, the component's ONLY 2-D f32 initializer is the table;
// with two candidates nothing is guessed.
if (!table && !scales) { … if (n2 == 1) table = only; }and #2663 is CLOSED on the tracker. I built this PR's test file against main's importer (no part of this PR's The tied arm's fixture is a component whose only tensor is What would make this PR worth landing (and it may be genuinely worth it): the case Suggested shape, in the order I would do it: add the Not run: a real Gemma-3-4b-it-mm pack end-to-end (I have no such pack under |
|
Closing as superseded — this is not a rebase problem, and no amount of rebasing would have made this PR mergeable.
Worth recording because the two rules are not the same fix, and main's is the better one: // main (#2664)
if (!table && !scales) {
// the component's ONLY 2-D f32 initializer is the table; with two candidates nothing is guessed
if (n2 == 1) table = only;
}
// mine (#2771)
if (!any_embed) { /* accept a 2-D tensor NAMED lm_head.weight */ }Main's is name-blind and ambiguity-refusing: it finds the table whatever a family calls it, and refuses when two candidates exist rather than guessing. Mine was name-driven — which is the very failure it was fixing, one level up. The scan that missed Nothing of mine is lost: the arms I wrote cover shapes #2664 already covers, and #2663's own title said "two assumptions" where main fixed it with one shape rule. Two process notes, both mine: I claimed a closed issue (the board said unowned, and I never ran Branch deleted. No replacement PR. |
Closes #2663.
The cause, narrower than the issue's title
The table scan is name-driven:
A tied-embedding graph (the gemma3 family) ships no
embed_tokensnode at all — its token table is the output projection, namedlm_head.weight— so the loop binds nothing,tablestays null, and the refusal fires:That is true about a name and false about the pack. The embedding is there, under a name the scan never asked for. Same shape of error as #2713, in a different file: an instrument reporting the absence of its own query as a fact about the artifact.
The fix, and why each condition is there
lm_head.weightis accepted as the token table under two conditions:embed_tokenstensor at all — every pack that has one keeps byte-identical behaviour, so this cannot regress a shipping family;scales, and reading the 0-Dembed_scalescalar sitting beside it as a quantisation scale is exactly the guess this issue was filed about. Refusing to guess is the point, so the refusal stays for that shape.Arms, both directions
Plus, in the same block, a 1-D tied table with a 0-D
embed_scalebeside it is still refused with a reason — so the fallback cannot widen quietly into "any tensor calledlm_head.weightis the embedding".Built with
ninja -C build-2663 npu_pack_import_test; the arms are intests/npu_pack_import_test.cppbeside the existing embedding-component fixture, which already asserts the 4-bit path, the absent pack, and the sub-2-D refusal.