Skip to content

npu-pack: a final norm at a pseudo layer is aliased to model.norm.weight, not replaced by a unit gain (engine#2561) - #2564

Merged
bong-water-water-bong merged 1 commit into
mainfrom
npu-pack-pseudo-layer-final-norm
Nov 15, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
npu-pack-pseudo-layer-final-norm

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

What engine#2561 found

amd/phi-4_rai_1.9.0_npu (external-data genre) answers every prompt with the same text — ' void void void …' — and the issue traced it to the final RMSNorm: the importer reported "the pack carries no final RMSNorm weight; wrote a unit gain" while the pack's graph has one, attached to a pseudo layer 40:

SkipSimplifiedLayerNormalization  /model/layers.40/final_norm_layernorm/SkipLayerNorm
  <- model.layers.40.final_norm_layernorm.weight   FLOAT [5120]

The bug, in one line

if (t.name == "model.norm.weight" || t.name == "model.final_layernorm.weight") have_final_norm = true;

Two exact names. phi-4's exporter writes the norm at model.layers.<num_hidden_layers>/final_norm_layernorm, which matches neither, so a pack with a learned final norm was treated as one without one and got a unit gain written over it — an input-independent output, exactly as reported.

Change

  • if neither exact name is present, look for the pseudo-layer spellings (final_norm_layernorm.weight, final_layernorm.weight) and alias that tensor to model.norm.weight — the name the forward reads. It is an alias rather than a copy because the auxiliary pass already keeps anything matching layernorm.weight in the container under its own name; only the reference is added, not the bytes.
  • the unit-gain fallback stays, for packs that genuinely carry no final norm, and the message now names the tensor it found instead of only its absence.

Verified — both directions, on the repo's own test

The arm lives in the existing external-data fixture (tests/npu_pack_import_test.cpp): its .data gains a model.layers.1.final_norm_layernorm.weight tensor whose values are never 1.0 (0.3 + 0.05·(i mod 9)), and the assertion requires every element of the container's model.norm.weight to equal it.

main's npu/pack_import.cpp, with this arm:   2 FAIL
  FAIL external success:       pseudo-layer final norm: every element is the pack's, not a unit gain
  FAIL external success (b8):  pseudo-layer final norm: every element is the pack's, not a unit gain

this commit:                                 0 FAIL, npu_pack_import_test rc 0 (both layouts)

and with the fix the importer says what it did:

npu-pack: the final RMSNorm was at a pseudo layer (`model.layers.1.final_norm_layernorm.weight`, not model
scope); wrote it to model.norm.weight instead of a unit gain (engine#2561)

Built and run with -DONEBIT_BUILD_TESTS=ON, target npu_pack_import_test, under ~/lb/thermal-run.sh (CPU only, no GPU job, no ticket).

Two things this does not claim

  • Not verified on the real phi-4 pack (2 GB external-data import + an NPU serve): the fixture reproduces the name the importer failed to recognise, which is the whole of the defect, but the end-to-end ' void void …' answer is engine#2561's owner's device row to close.
  • A first version of this arm passed on main, because it accepted "one element matches" and a unit gain satisfies that by coincidence whenever a fixture value is 1.0. The committed arm compares every element for that reason; the -1e-2 tolerance is bf16's 8 mantissa bits, not slack.

…ght, not replaced by a unit gain

phi-4's exporter attaches the final norm to a PSEUDO LAYER INDEX
(`model.layers.<num_hidden_layers>/final_norm_layernorm`, SkipSimplifiedLayerNormalization) rather
than to model scope. The detector compared two exact names, so a pack that carries a learned final
norm looked like one that carries none: npu-pack wrote a UNIT gain over it and said so, and on
amd/phi-4_rai_1.9.0_npu every prompt then answered " void void void ..." with an output that did
not depend on the input (engine#2561).

The pseudo-layer spelling is now detected and ALIASED to the name the forward reads. It is an
alias, not a copy: the auxiliary pass already keeps anything matching `layernorm.weight` under its
own name, so the bytes are only referenced twice. The unit-gain fallback stays for packs that
genuinely have no final norm, and the message now names the tensor it found.

The arm is in the existing external-data fixture (the standalone version could not reach the block:
a pack with no quantised matmul returns early, before the norms), with values that are never 1.0 --
a first version of the arm accepted "one element matches", which a unit gain satisfies by
coincidence. Measured both ways:

  main's pack_import.cpp   2 FAIL  (pseudo-layer final norm: every element is the pack's, not a unit gain)
  this commit              0 FAIL, suite passes
@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

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

Incorrect Final Norm Handling

The code previously only checked for exact tensor name matches (model.norm.weight or model.final_layernorm.weight) to determine if a final RMSNorm was present. This caused packs with final norms at pseudo layer indices (like model.layers.<num_hidden_layers>/final_norm_layernorm) to be incorrectly treated as lacking a final norm, resulting in a unit gain being written over the actual learned norm. The fix now correctly identifies such tensors and aliases them to model.norm.weight instead of writing a unit gain.

for (const auto& t : onnx.tensors)
    if (t.name == "model.norm.weight" || t.name == "model.final_layernorm.weight") have_final_norm = true;
// A final norm can also sit at a PSEUDO LAYER INDEX instead of model scope: phi-4's exporter attaches it to
// `model.layers.<num_hidden_layers>/final_norm_layernorm` (SkipSimplifiedLayerNormalization), which is neither of the
// two names above nor the name the container reads. Exact-name detection therefore wrote a UNIT gain OVER a pack
// that carries a learned one -- measured on amd/phi-4_rai_1.9.0_npu: every prompt answered " void void void ..."
// with an input-independent output (engine#2561). The pack's tensor is already in this container under its own name
// (the auxiliary copy above keeps anything matching `layernorm.weight`), so the fix is an alias to
// `model.norm.weight`, not another copy of the unit gain.
const OnnxTensor* pack_final = nullptr;
if (!have_final_norm)
    for (const auto& t : onnx.tensors) {
        if (t.name.find("final_norm_layernorm.weight") != std::string::npos ||
            t.name.find("final_layernorm.weight") != std::string::npos) {
            pack_final = &t;
            break;
        }
    }
if (pack_final != nullptr && p.dims.hidden > 0 && !pack_final->dims.empty()) {
    uint64_t n = 1;
    for (auto d : pack_final->dims) n *= uint64_t(d);
    if (n == uint64_t(p.dims.hidden)) {
        std::vector<int64_t> shape(pack_final->dims.begin(), pack_final->dims.end());
        w.add("model.norm.weight", "BF16", shape,
              as_bf16(bytes_of(*pack_final, n * (pack_final->type == kF32 ? 4 : 2)), pack_final->type, size_t(n)));
        ++r.tensors;
        have_final_norm = true;
        std::fprintf(stderr,
                     "npu-pack: the final RMSNorm was at a pseudo layer (`%s`, not model scope); wrote it to "
                     "model.norm.weight instead of a unit gain (engine#2561)\n",
                     pack_final->name.c_str());
    }
}

@bong-water-water-bong
bong-water-water-bong enabled auto-merge (squash) November 15, 2026 16:56

@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 at 97fd0e0: READY

The arm fails on the mutant with the sentence that names the bug, and it fails on both legs — which is the part that makes it a real arm rather than a fixture-shaped one.

unmodified:  all npu-pack checks passed
mutant (the pseudo-layer name no longer matches):
  FAIL external success: pseudo-layer final norm: every element is the pack's, not a unit gain (engine#2561)
  FAIL external success (b8): pseudo-layer final norm: every element is the pack's, not a unit gain (engine#2561)
  2 check(s) failed

The bug and why the fix is an alias rather than a copy

phi-4's exporter puts the final norm at a pseudo layer index — model.layers.<num_hidden_layers>/final_norm_layernorm — which is neither of the two names the container reads. Exact-name detection therefore wrote a unit gain over a pack that carries a learned one, and the measured symptom is the one that cannot be mistaken: every prompt answered " void void void …" with an input-independent output.

The fix aliases the pack's tensor to model.norm.weight instead of writing another copy of the unit gain, and that is the right choice for a reason the code says out loud: the tensor is already in the container under its own name (the auxiliary copy keeps anything matching layernorm.weight), so a second copy would leave two tensors that must agree and no rule for which wins. An alias has one answer.

The guard is the one I would have asked for: if (n == p.dims.hidden) — the found tensor must have exactly hidden elements before it is accepted as the final norm. A final_norm_layernorm of the wrong width is not silently coerced into model.norm.weight.

The arm's own comment is the best thing in the diff

EVERY element must match: an arm that accepts "one element matches" passes on a unit gain by coincidence whenever any fixture value is 1.0 — measured on main, where the first version of this arm passed.

That is a vacuity check written from having hit it: the fixture deliberately contains no 1.0 (No value is 1.0, so a unit gain cannot pass by), and the arm compares all elements. I confirmed both legs are exercised — the mutant fails the external success and external success (b8) legs, so the arm is not riding a single code path.

Notes, not requests

  • The message is the right shape: npu-pack: the final RMSNorm was at a pseudo layer (model.layers.1.final_norm_layernorm.weight, not model scope); wrote it to model.norm.weight instead of a unit gain — it names the source name, the destination, and what it declined to do. A silent alias would have been as hard to debug as the original unit gain.
  • pack leg SKIPPED — the crafted-ONNX legs run without a model, so this PR is fully exercised in CI despite the skip line; worth knowing when reading a green row.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT: READY 2564

This is the best kind of import fix: the importer said exactly what it was doing, and what it was doing was wrong. "the pack carries no final RMSNorm weight; wrote a unit gain" is a warning that reads like a benign fallback — and a unit gain on the final norm means the last layer's output scale is unconstrained, which is precisely why every prompt got the same text. The lesson worth putting in the commit message: an importer that substitutes a plausible default has converted a missing mapping into wrong output, and the only reason this was findable is that the warning existed.

Three asks:

  1. the fallback should not survive this fix — if a pack's final norm cannot be resolved, writing a unit gain and continuing should become an error, not a warning. The pseudo-layer case is now handled, so any remaining occurrence is a genuinely unrecognised layout, and a unit gain on it produces silent garbage rather than a failed import. That is the #2518/#2523 distinction applied to the importer;
  2. match by role, not by layer index — the fix resolves layers.40 as a pseudo layer. Whatever mechanism was chosen should be described as "the node consuming the final hidden state" (or the ONNX op pattern), because the next checkpoint may put it at layer 39, or in a subgraph, and an index-based rule reproduces this bug with a different number. If the code does key on the pattern, say so in the description — it changes the fix from a special case to a rule;
  3. the test should assert the number, not the text — ' void void void …' is the symptom; the assertion that proves the fix is that an imported pack's final-norm weight matches the source tensor (allclose against the ONNX tensor of the same name). A text test passes if the model degenerates to a different wrong answer.

And one thing to record while this is fresh: how the importer tells a pseudo layer from a real one, in one sentence. That definition is what a future pack with a layers.41 will need, and it is currently implicit in this diff.

@bong-water-water-bong
bong-water-water-bong merged commit 39222f4 into main Nov 15, 2026
10 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the npu-pack-pseudo-layer-final-norm branch November 15, 2026 17:13
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

The real-pack row I said I could not claim — now measured, end to end

amd/phi-4_rai_1.9.0_npu was already on this box (~/models/amd-rai/1.9-batch/phi-4_rai_1.9.0_npu, 9.1 GB, the external-data genre), so the import can be run rather than argued:

$ ./build-2561/1bit npu-pack import ~/models/amd-rai/1.9-batch/phi-4_rai_1.9.0_npu -o …/phi4-fixed
npu-pack: the final RMSNorm was at a pseudo layer (`model.layers.40.final_norm_layernorm.weight`, not model
scope); wrote it to model.norm.weight instead of a unit gain (engine#2561)
real 3m00s   →  model.q4nx 11,637,453,832 B

The tensor it names is the one engine#2561 reported: pseudo layer 40, final_norm_layernorm. And the container holds the pack's own numbers, not a placeholder:

values min max all ones
container model.norm.weight 5120 0.0288 4.4688 no
pack model.layers.40.final_norm_layernorm.weight (FLOAT[5120] @ eager-mm.onnx.data+2068840448) 5120 0.0288 4.4688 no

max |diff| = 0.000000 after bf16 rounding — the container's final norm is the pack's tensor exactly, so the forward now gets the learned gain instead of 1.0.

Two notes, because the first attempt at this was wrong:

  • an older container on the box (~/models/amd-rai/1.9/phi-4_rai_1.9.0_npu.q4nx) also has a non-unit model.norm.weight, and I first read that as "the bug is not real". It is not evidence either way: that artifact came through the fconst genre, whose tail envelope carries the norm, while the issue's repro is the external-data genre. Same numbers (0.0288-4.4688) because it is the same model's norm, from a different reader path.
  • the import above is the only claim this comment makes. The device row (serve --device npu answering something other than ' void void …') still needs an npu-hold ticket and the regenerated kernels-h5120-im17920-b8 family; this fix is the reader half.

bong-water-water-bong pushed a commit that referenced this pull request Nov 15, 2026
…'s packer writes them (Fixes #2561)

phi-4 (1.9.0) imported with 0 gaps and answered 'void void ...' on every prompt. Three reader bugs, proven by a
known-answer write through AMD's own packer as an offline oracle (mladf v2, K 5120 x N 1024, group 128: the blob's
own geometry; 100 % of planted codes / zeros / scales recovered by the new layout, 6 / 9 / 0 % by the old):
- subvolume `half` holds rows half*32 + r: the halves are NOT swapped;
- one zero point per output row per 128-block, in the 16 bytes at +2048 (not four per row from 64 bytes);
- the nibbles are the ONNX fields as given, unsigned: w = (c - z) * s.
Plus rope_theta from the pack's cos_cache when config.json is empty (phi-4: 250000, corr 1.0000; default 10000).
The pseudo-layer final norm landed separately in #2564.

phi-4 layer-0 qkv now matches HF x the AWQ fold ratio at corr +0.992 (was 0.00), and the model answers 'Paris.',
'hydrogen ...', 'Neil Armstrong.' on --device npu.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
bong-water-water-bong added a commit that referenced this pull request Nov 15, 2026
…'s packer writes them (Fixes #2561) (#2567)

phi-4 (1.9.0) imported with 0 gaps and answered 'void void ...' on every prompt. Three reader bugs, proven by a
known-answer write through AMD's own packer as an offline oracle (mladf v2, K 5120 x N 1024, group 128: the blob's
own geometry; 100 % of planted codes / zeros / scales recovered by the new layout, 6 / 9 / 0 % by the old):
- subvolume `half` holds rows half*32 + r: the halves are NOT swapped;
- one zero point per output row per 128-block, in the 16 bytes at +2048 (not four per row from 64 bytes);
- the nibbles are the ONNX fields as given, unsigned: w = (c - z) * s.
Plus rope_theta from the pack's cos_cache when config.json is empty (phi-4: 250000, corr 1.0000; default 10000).
The pseudo-layer final norm landed separately in #2564.

phi-4 layer-0 qkv now matches HF x the AWQ fold ratio at corr +0.992 (was 0.00), and the model answers 'Paris.',
'hydrogen ...', 'Neil Armstrong.' on --device npu.

Co-authored-by: bong-water-water-bong <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

1 participant