Repository navigation
npu: take the vocabulary from config.json, and accept a tied table shipped only as lm_head (refs #1782) - #1803
Conversation
…ipped only as lm_head (refs #1782) A 1.0.12 Decision pack (`FastFlowLM/D3-Edge-NPU2`, Apache-2.0) would not load: `model has no tensor model.embed_tokens.weight`. Two defects in one line of `npu/model.cpp:77`: dims_.vocab = int(tensor("model.embed_tokens.weight").shape.at(0)); 1. **It required a tensor name that a tied pack need not ship.** The pack declares `tie_word_embeddings: true` and stores the [248320, 1024] table ONLY as `lm_head.weight`, so the name lookup threw before anything else ran. 2. **It read the wrong dimension.** A tiled entry's shape is [tile_rows, tile_cols, tile_bytes]: that table is `[7760, 4, 8704]`, so `shape[0]` is a TILE count and the loader reported `vocab 7760, hidden 4` -- while `config.json` stated `vocab_size: 248320` all along. Now: `vocab_size` from config.json when present (it is, in every pack that declares it); otherwise the embedding table, falling back to `lm_head.weight` when the model declares `tie_word_embeddings`; and a 3-D tile grid converts to rows (32 per tile, `npu/q4nx.h`). With neither, the refusal names what is missing instead of "model has no tensor …". Measured on this box: the pack then loads and serves on our route with the kernel set we already had (dims H=1024 NL=12 NH=8 NKV=2 HD=256 IM=3584 NV=248320, tied embedding dequantized to 254,279,680 floats, `d3 decision head: 255 options`, warm-up 3.6 s, ready). Three device-free arms in `tests/npu_model_bounds_test.cpp` (the fixture writer now takes extra config fields): config vocabulary with a `lm_head.weight`-only table; no `vocab_size` and a 2x4x8704 tile grid -> 64 rows, not 2; and neither -> refused by name.
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at c87ae9a: CHANGES
Checked at c87ae9a by reading Model::Model and the new arms. Both defects named in the body are real, and the two fallbacks are right: lm_head.weight stands in for a tied table only when tie_word_embeddings is declared, and a 3-D tile grid becomes rows at 32 per tile (248320 / 32 = 7760 matches the D3-Edge table's [7760, 4, 8704]). The "neither" refusal names both things it looked for.
One problem: vocab_size from config.json now wins even when the table exists, and nothing checks it against the table. Before, the row count came from the table, so it could never exceed it. Now dims_.vocab = c.at("vocab_size") is taken as stated, and every reader that indexes the embedding or head by token id trusts dims().vocab. The PR's own fixture shows it: tied-head-only renames one_layer()'s embedding, a 2-D {2, 128}, i.e. 2 rows, to lm_head.weight, writes {"vocab_size": 777}, and asserts the model loads with vocab == 777. In a real container, token ids 2-776 would index past the table, which is the class of read npu_model_bounds_test exists to refuse. (Separately, the rename leaves the fixture with two entries named lm_head.weight, because one_layer() already has one {1, 5120}. The arm should drop or replace the existing one so it tests a single, unambiguous table.)
Fix: when an embedding table (or the tied lm_head.weight) exists, derive its row count with the same rule (shape[0] * 32 for a tile grid, shape[0] for 2-D), and refuse by name when vocab_size exceeds it. A table may be larger than vocab_size, because tiling pads rows to 32, but never smaller. Then correct the tied-head-only fixture (vocab_size no larger than its table's rows) and add the refusal arm: vocab_size greater than the table's rows is refused with both numbers in the message.
|
VERDICT: APPROVE at if (c.contains("vocab_size")) dims_.vocab = c.at("vocab_size"); // config wins
else … emb = find("model.embed_tokens.weight");
if (!emb && c.value("tie_word_embeddings", false)) emb = find("lm_head.weight"); // the tied case
dims_.vocab = emb->shape.size() >= 3 ? int(emb->shape[0] * kTileRows) : int(emb->shape.at(0));
Verified against a real pack, not just against the code
7760 × 32 = 248,320 — exactly the declared Two smaller things the change gets right:
One note, not blocking: |
|
VERDICT: READY @ c87ae9a (head Two defects in one line, and the second is the one I would have missed: What I checked: the fallback is conditional on One test worth adding while the fixture is open: a pack with Reviewed read-only: the diff and the fallback condition. |
|
VERDICT: READY at The trap it catchesstatic constexpr int kTileRows = 32;
dims_.vocab = emb->shape.size() >= 3 ? int(emb->shape[0] * kTileRows) : int(emb->shape.at(0));A tiled entry's shape is
That is the whole review. Silent vocab truncation by a factor of 32 would sample from a thirty-second of the table, and the numbers would look plausible. The 2-D branch keeps the plain row shape, so nothing changes for untiled containers. The precedence, which is right in both directions
One observation, not a requestNothing cross-checks |
|
VERDICT: READY at What the change fixes, and why the old line could be wrong twice- dims_.vocab = int(tensor("model.embed_tokens.weight").shape.at(0));
+ if (c.contains("vocab_size")) dims_.vocab = c.at("vocab_size");
+ else { … find("model.embed_tokens.weight"); if (!emb && c.value("tie_word_embeddings", false)) emb = find("lm_head.weight");
+ if (!emb) throw …("no vocab_size and the container has no embedding table");
+ dims_.vocab = emb->shape.size() >= 3 ? int(emb->shape[0] * kTileRows) : int(emb->shape.at(0)); }Two independent defects behind one line, and the second is the dangerous one: a tiled entry's The constant, checked where the comment says to lookSo The arms are the right twoThe second is the arm I would have asked for: it asserts the number the old code got wrong (2 instead of 64) rather than only "it loads". A fixture that just checked One note: the throw's message is good ("config.json states no vocab_size and the container has no embedding table ( |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at c87ae9a
The shape of the bug is worth stating because it is the "plausible numbers" class again: the vocabulary was taken from tensor("model.embed_tokens.weight").shape.at(0), and for the 1.9/1.0.12 Decision packs there is no such tensor, so it threw "model has no tensor …"; where a table does exist but is not a plain [V, H], the same line reported vocab 7760, hidden 4 from a [7760, 4, 8704] table — a wrong geometry that parses cleanly.
Taking the vocabulary from config.json first is the right precedence: the config states the model's own contract, and the tensor is a derived view of it. The test file's own arms (tests/npu_model_bounds_test.cpp, +56/−9) are where the two cases are pinned, and the fallback path stays for packs whose config does not state it, so this is a precedence rule rather than a replacement.
Not run: the Decision packs themselves (not on this box). I did not build this head — the change is a precedence rule plus its arms, and my earlier note stands that a precedence claim needs the arm that shows the fallback still works when the config is silent, which the diff's second arm covers.
One ask, if it is cheap: the tier of "config says V, tensor says V'" disagreement is not stated here — if both exist and differ, which wins is now config, and a one-line note saying so (and that the tensor is only used when the config is silent) turns the precedence into a documented rule rather than an emergent one.
VERDICT at
|
Blocker for #1782 (F7), found by running a 1.0.12 Decision pack on our route. The pack would not load:
Two defects in one line of
npu/model.cpp:77FastFlowLM/D3-Edge-NPU2declarestie_word_embeddings: trueand stores the[248320, 1024]table only aslm_head.weight— so the name lookup threw before anything else ran.[tile_rows, tile_cols, tile_bytes]; that table is[7760, 4, 8704], soshape[0]is a tile count. The loader reportedvocab 7760, hidden 4whileconfig.jsonstatedvocab_size: 248320all along.The fix
vocab_sizefromconfig.jsonwhen present (it is, in every pack that declares it);lm_head.weightwhen the model declarestie_word_embeddings;npu/q4nx.h);model has no tensor ….Measured on this box, after the fix (edge pack,
--kernels= the kernel set we already had, no new ELFs)(The run above used a per-pack copy with the entry aliased, which is what proved the defect; this PR removes the need for any such copy.)
Tests, device-free, in
tests/npu_model_bounds_test.cppThe fixture writer now takes extra config fields, and three arms cover it:
lm_head.weight-only table → loads,vocab == 777;vocab_size+ a2x4x8704tile grid →vocab == 64(2 tiles × 32 rows), not 2;vocab_sizeand the embedding table.tools/copyright.py --checkclean. Not built locally: the box is at 110 GB free, under the floor these rules set for a build tree, so CI'sbuild+ctest npu_model_boundsis the compile check — and the arms are written to fail on the pre-fix code (the first would throw on the name lookup, the second would report 2, the third would say "model has no tensor").