From 4de4ac169f7bff583d52041d050b6cdaab003639 Mon Sep 17 00:00:00 2001 From: t Date: Tue, 10 Nov 2026 04:55:28 -0400 Subject: [PATCH 1/2] npu-pack: the tied block-32 head synthesis and its untied refusal get the two crafted arms they never had (fixes #1850) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #1633's synthesis — a 1.9 pack that quantises its HEAD at a K block other than 128 and declares tied embeddings gets `lm_head` from the embedding, with `q4nx_head_note` in the container and the self-check measuring the synthesis — was exercised **only by hand on one real pack**. `git grep -E 'head_synthesized|selfcheck_tied|q4nx_head_note' -- tests` was empty, and `q4nx_tail_head_test.py`'s "block-32 arm" covers the *Python tool's* exit code (and skips in CI without `onnx`). Two arms, one fixture, no pack and no device: * **tied** (`tie_word_embeddings: true`): the import succeeds, `head_synthesized_note` names block 32, `selfcheck_tied` is set with **corr > 0.999** against the embedding, `config.json` carries `q4nx_head_note`, and the container carries the synthesised `lm_head.weight`; * **untied** twin: refused, `refused` carries `lm_head.weight: this pack quantises its head at block 32 … does not declare tied embeddings …`, and the container carries **no** head — nothing stands in for an untied one. Three things the fixture had to be told, each of which cost a build to find and is the part worth keeping: 1. **`p.head_block` comes from the pack's `cache/*_meta.json` (`read_declared`), not from the graph** — and the synthesis branch also requires `onnx.matmuls.empty()`, so the head cannot be a node the walker keeps. Between the two, the branch looked unreachable from `import_pack` until this fixture put the declaration in `cache/` and left the graph with no matmul at all, which is the shape the real 1.9 packs have. 2. **The embedding must vary.** A constant fill quantises to one code with a near-zero scale, so the self-check computes 0/0 and the arm fails for the fixture's sake rather than the code's. 3. **The untied arm runs with `--allow-incomplete`.** A strict import bails out at the FIRST refusal, and this crafted pack has 80 of them from its empty layer payload, so it never reaches the head gate — asserting there would test the bail-out, not the head. Mutation-checked: inverting the guard (`if (tied)`) fails all four tied arms; dropping only the `tie_declared` term changes nothing, which says that term is redundant (`tied` already defaults false when the key is absent) — noted rather than changed, since that is #1633's code and not this test's subject. Verified: `ninja npu_pack_import_test 1bit` rc 0; unit-only run `all npu-pack checks passed`; the reference 1.9 pack leg `all npu-pack checks passed (including the pack leg)`; `tools/copyright.py --check` rc 0 with the tree staged; `git add ` only. --- tests/npu_pack_import_test.cpp | 147 +++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) diff --git a/tests/npu_pack_import_test.cpp b/tests/npu_pack_import_test.cpp index 25051340b..fcd6655e9 100644 --- a/tests/npu_pack_import_test.cpp +++ b/tests/npu_pack_import_test.cpp @@ -1361,6 +1361,152 @@ void test_mlp_gu(const std::string& golden_dir) { tiled.size() / onebit::npu::kTileBytes); } +// ── engine#1850: the tied block-32 head synthesis, and the untied refusal, on a crafted pack ── +// #1633 makes `import_pack()` synthesise `lm_head` from the embedding when a 1.9 pack quantises its HEAD at a K block other than +// 128 and declares tied embeddings, refuses an untied pack by name, records `q4nx_head_note`, and self-checks the synthesis +// against the embedding. None of that had an arm: it was exercised only by hand on the real Coder-1.5B pack, and the Python +// tail-head test's "block-32 arm" covers the tool's exit code, not this code. One fixture, two arms, no pack and no device. +void test_tied_block32_head(const std::string& root) { + const auto build = [&](bool tied, const char* tag) { + const fs::path dir = fs::path(root) / tag; + fs::remove_all(dir); + fs::create_directories(dir / "cache"); + const int H = 128, L = 8, V = 64; + const int64_t stride = 1 << 20, second = 262144; + std::string fc(size_t(int64_t(L) * stride + 64 + size_t(H) * 2), '\0'); + auto put_eps = [&](int64_t off) { + fc[size_t(off)] = char(0x86); + fc[size_t(off) + 1] = char(0x35); + }; + for (int l = 0; l < L; ++l) { + put_eps(int64_t(l) * stride); + put_eps(int64_t(l) * stride + second); + } + put_eps(int64_t(L) * stride); + for (int i = 0; i < H; ++i) { + const uint16_t bits = pi::bf16_bits_of(float(i + 1)); + fc[size_t(int64_t(L) * stride + 64 + 2 * i)] = char(bits & 0xFF); + fc[size_t(int64_t(L) * stride + 64 + 2 * i) + 1] = char(bits >> 8); + } + put_file((dir / "dd_metastate_Llm_Token_Token_crafted.fconst").string(), fc); + const std::string cfg = + std::string(R"({"model_type":"qwen2","hidden_size":128,"intermediate_size":256,"num_hidden_layers":8,)" + R"("num_attention_heads":2,"num_key_value_heads":1,"head_dim":64,"vocab_size":64,)" + R"("rope_theta":10000.0,"tie_word_embeddings":)") + + (tied ? "true" : "false") + R"(,"rms_norm_eps":1e-06})"; + put_file((dir / "config.json").string(), cfg); + // The head's declared geometry (N=V=64, K=H=128, block=32) has to be carried by the bytes: the gate checks + // qweight >= N x (K/block) x (block/2) = 4,096 B, f32 scales >= N x (K/block) x 4 = 1,024 B, zeros >= 128 B. + const int64_t kblocks = H / 32; + const uint64_t embed_bytes = uint64_t(V) * uint64_t(H) * 2; + const uint64_t qw_off = embed_bytes, qw_len = uint64_t(V) * uint64_t(kblocks) * 16; + const uint64_t sc_off = qw_off + qw_len, sc_len = uint64_t(V) * uint64_t(kblocks) * 4; + const uint64_t z_off = sc_off + sc_len, z_len = uint64_t(V) * ((kblocks + 1) / 2); + // A VARIED embedding on purpose: a constant fill quantises to one code with a near-zero scale, so the self-check + // would compute 0/0 and the arm would fail for the fixture's sake rather than the code's. + std::string data(size_t(z_off + z_len), '\x11'); + for (int r = 0; r < V; ++r) + for (int c = 0; c < H; ++c) { + const uint16_t bits = pi::bf16_bits_of(0.05f * float((r * 7 + c * 13) % 97) - 2.4f); + const size_t at = (size_t(r) * size_t(H) + size_t(c)) * 2; + data[at] = char(bits & 0xFF); + data[at + 1] = char(bits >> 8); + } + put_file((dir / "model.onnx.data").string(), data); + // engine#1850: `p.head_block` comes from the pack's `cache/*_meta.json` (read_declared), NOT from the graph -- which + // is what makes the synthesis reachable at all: the branch needs `onnx.matmuls.empty()`, so the head cannot be a node + // the walker keeps. Writing the declaration into cache/ is the shape the real 1.9 packs have. + put_file((dir / "cache" / "lm_head_meta.json").string(), + R"({"op_list":[{"type":"MladfMatMul","name":"lm_head.MatMulNBits",)" + R"("attrs":{"N":{"value":[64]},"K":{"value":[128]},"block_size":{"value":[32]},"bits":{"value":[4]}}}],)" + R"("tensor_map":{}})"); + put_file((dir / "model.onnx").string(), + pb::model({pb::tensor("model.embed_tokens.weight", {V, H}, "model.onnx.data", 0, + uint64_t(V) * uint64_t(H) * 2, 10), + // PLACEHOLDERS, not weights (engine#1850): the synthesis path runs only when the walk ends + // with NO matmuls, which is the shape a real pack has when its head is a placeholder with no + // `*.packed` sibling -- the node is dropped from `matmuls` while its `block_size` is still + // counted by the census that sets `p.head_block`. A tensor that carries bytes here would be + // read by the graph-head loop instead and refused for its declared length. + pb::placeholder("lm_head.weight.MatMulNBits.qweight"), + pb::placeholder("lm_head.weight.MatMulNBits.scales"), + pb::placeholder("lm_head.weight.MatMulNBits.zeros")}, + {})); + return dir; + }; + + pi::Options allow; + allow.allow_incomplete = true; + { // arm 1: declared tied -> synthesised from the embedding, and the note says so + const fs::path dir = build(true, "tied-block32"); + const std::string out = (fs::temp_directory_path() / + ("oga_pack_import_tied32." + std::to_string(::getpid()))).string(); + fs::remove_all(out); + bool threw = false; + std::string why; + pi::Report r; + try { + r = pi::import_pack(dir.string(), out, allow); + } catch (const std::exception& e) { + threw = true; + why = e.what(); + } + check(!threw, "tied block-32: a tied pack imports (" + (why.empty() ? std::string("ok") : why.substr(0, 140)) + ")"); + check(!r.head_synthesized_note.empty() && r.head_synthesized_note.find("block 32") != std::string::npos, + "tied block-32: the synthesis is recorded and names the block (" + r.head_synthesized_note + ")"); + check(r.selfcheck_tied && r.lmhead_corr > 0.999, + "tied block-32: the self-check measures the SYNTHESIS against the embedding (corr " + + std::to_string(r.lmhead_corr) + ")"); + if (fs::exists(out + "/model.q4nx")) { + // `q4nx_head_note` is written into the container's config.json (that is what the forward reads), not into the + // q4nx tensor table's metadata. + std::ifstream cf(out + "/config.json"); + check(cf.good(), "tied block-32: the container carries a config.json"); + if (cf.good()) { + const json cfg = json::parse(cf); + check(cfg.contains("q4nx_head_note") && cfg["q4nx_head_note"].get().find("block 32") != + std::string::npos, + "tied block-32: config.json carries q4nx_head_note and names the block"); + } + check(container_table(out + "/model.q4nx").contains("lm_head.weight"), + "tied block-32: the container carries the synthesised head"); + } + fs::remove_all(out); + fs::remove_all(dir); + } + { // arm 2: the same pack without the declaration -> refused by name, nothing written + const fs::path dir = build(false, "untied-block32"); + const std::string out = (fs::temp_directory_path() / + ("oga_pack_import_untied32." + std::to_string(::getpid()))).string(); + fs::remove_all(out); + bool threw = false; + std::string why; + pi::Report r; + try { + // allow-incomplete ON purpose: a strict import bails out at the FIRST refusal (this fixture's layer payload is + // empty, so it has 80 of them) and never reaches the head gate -- asserting on it there would test the + // bail-out, not the head. With it on, the run reaches the gate and the refusal lands in `refused`. + allow.allow_incomplete = true; + r = pi::import_pack(dir.string(), out, allow); + } catch (const std::exception& e) { + threw = true; + why = e.what(); + } + std::string said = why; + for (const auto& w : r.refused) + if (w.rfind("lm_head.weight", 0) == 0) said += " | " + w; + check(threw || !r.refused.empty(), + "untied block-32: an untied pack is refused rather than synthesised (" + said.substr(0, 140) + ")"); + check(said.find("block 32") != std::string::npos && said.find("does not declare tied") != std::string::npos, + "untied block-32: the refusal names the block and the missing declaration (" + said.substr(0, 160) + ")"); + if (fs::exists(out + "/model.q4nx")) + check(!container_table(out + "/model.q4nx").contains("lm_head.weight"), + "untied block-32: the container carries no head (nothing stands in for an untied one)"); + fs::remove_all(out); + fs::remove_all(dir); + } +} + // ── the final norm out of the tail envelope, on a crafted pack (engine#844) ────────────────── // The real-pack leg only covers this where a pack is supplied, and three of the four packs on the // box ship no prefill program, so the import's tail-envelope read had no coverage in the default @@ -3286,6 +3432,7 @@ int main(int argc, char** argv) { test_embedding_f32_component(golden_dir.empty() ? "." : golden_dir); test_qwen3_attn_sections(); test_post_attn_norm_header(golden_dir.empty() ? "." : golden_dir); + test_tied_block32_head(golden_dir.empty() ? "." : golden_dir); test_final_norm_envelope(golden_dir.empty() ? "." : golden_dir); test_import_fp32_params(golden_dir.empty() ? "." : golden_dir); test_text_artifacts(golden_dir.empty() ? "." : golden_dir); From b9d5a363238837e306d2694e6b45cb4e2a9e222a Mon Sep 17 00:00:00 2001 From: t Date: Tue, 10 Nov 2026 05:36:24 -0400 Subject: [PATCH 2/2] npu-pack test: decode the container's own head bytes, so an all-zero written head can no longer pass (review of #1882) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The review found the gap this arm had: `r.lmhead_corr` measures `q` **before** `tile_q4_1_f32` turns it into the bytes `w.add("lm_head.weight", …)` writes, and the arm only checked the name was present. Its mutant — `std::fill(tiled, 0)` after tiling — came back **rc 0, 0 FAIL**, i.e. a container whose synthesised head is garbage passed every arm. That is the state a serve reads. Now the arm reads `lm_head.weight` back out of `out/model.q4nx` (payload after the 8-byte length + JSON header, `data_offsets` from the table), decodes its first tile with `q4nx::dequant_tile(Chunk::Q4_1F, …)` — the variant `tile_q4_1_f32` writes — and correlates it against **the embedding the container carries**, not against this test's recollection of what it wrote. Both sides now come from the file, which is the synthesis's actual claim. Two things this took a build each to get right, both worth the note: * the chunk is **`Q4_1F`** (float scales), not `Q4_1`: decoding as `Q4_1` gives corr 0.003 and looks exactly like a real defect in the synthesis; * comparing against the fixture's *assumed* ramp (rather than the container's embedding) gives **corr 0.909**, also indistinguishable from a partial misalignment — the fixture's expected values were not where this test believed. Reading both sides from the container makes the arm independent of how the fixture lays its tensors out. Mutation-checked with the reviewer's own mutant, in the position that matters: `std::fill(tiled, 0)` **after** `tile_q4_1_f32` → `FAIL … corr 0, n 4096`. The same fill **before** the call is a no-op (the call overwrites `tiled`), which is why the first attempt at this mutation proved nothing. Verified: unit suite `all npu-pack checks passed`; the reference 1.9 pack leg `all npu-pack checks passed (including the pack leg)`; copyright rc 0 with the tree staged. --- tests/npu_pack_import_test.cpp | 47 ++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/tests/npu_pack_import_test.cpp b/tests/npu_pack_import_test.cpp index fcd6655e9..512b49a7d 100644 --- a/tests/npu_pack_import_test.cpp +++ b/tests/npu_pack_import_test.cpp @@ -220,6 +220,15 @@ json container_table(const std::string& path) { return json::parse(header); } +// The container's payload, i.e. what `data_offsets` are measured from: after the 8-byte header length and the JSON. +std::vector container_payload(const std::string& path) { + std::ifstream f(path, std::ios::binary); + uint64_t hlen = 0; + f.read(reinterpret_cast(&hlen), 8); + f.seekg(std::streamoff(8 + hlen)); + return std::vector(std::istreambuf_iterator(f), std::istreambuf_iterator()); +} + // How many I8 tensors of a container are the imported 6,144-byte q4_1f form -- exactly the // trigger of the forward loader's refusal (npu/forward/src/model.c, engine#1185). int q4_1f_tensors(const json& table) { @@ -1470,6 +1479,44 @@ void test_tied_block32_head(const std::string& root) { } check(container_table(out + "/model.q4nx").contains("lm_head.weight"), "tied block-32: the container carries the synthesised head"); + // engine#1882 review: presence is not the assertion. The self-check above measures `q` BEFORE `tile_q4_1_f32` + // turns it into the bytes the container carries, so an all-zero written head passed every arm -- and that is + // exactly the state a serve reads. Decode the container's own bytes with the decoder the forward uses. + const json tbl = container_table(out + "/model.q4nx"); + if (tbl.contains("lm_head.weight")) { + const std::vector blob = container_payload(out + "/model.q4nx"); + const auto offs = tbl["lm_head.weight"]["data_offsets"].get>(); + std::vector w(size_t(onebit::npu::q4nx::kRows) * onebit::npu::q4nx::kCols, 0.f); + onebit::npu::q4nx::dequant_tile(onebit::npu::q4nx::Chunk::Q4_1F, blob.data() + offs[0], w.data(), + onebit::npu::q4nx::kCols); + // Compare against the embedding THE CONTAINER CARRIES, not against this test's recollection of what it + // wrote: the synthesis's claim is that the head IS the embedding, so both sides must come from the file. + std::ifstream cfin(out + "/config.json"); + const json cf = json::parse(cfin); + const int V = cf.at("vocab_size").get(), H = cf.at("hidden_size").get(); + const auto eoffs = tbl["model.embed_tokens.weight"]["data_offsets"].get>(); + std::vector emb(size_t(V) * size_t(H), 0.f); + for (size_t i = 0; i < emb.size(); ++i) { + const uint16_t b = uint16_t(blob[size_t(eoffs[0]) + 2 * i]) | + (uint16_t(blob[size_t(eoffs[0]) + 2 * i + 1]) << 8); + const uint32_t u = uint32_t(b) << 16; + float v; + std::memcpy(&v, &u, 4); + emb[i] = v; + } + double num = 0, da = 0, db = 0; + int n = 0; + for (int r = 0; r < onebit::npu::q4nx::kRows && r < V; ++r) + for (int c = 0; c < H; ++c) { + const double want = double(emb[size_t(r) * size_t(H) + size_t(c)]); + const double got = double(w[size_t(r) * onebit::npu::q4nx::kCols + size_t(c)]); + num += want * got, da += want * want, db += got * got, ++n; + } + const double corr = (da > 0 && db > 0) ? num / std::sqrt(da * db) : 0.0; + check(n > 0 && corr > 0.999, + "tied block-32: the container's head, decoded from its own bytes, is the embedding (corr " + + std::to_string(corr) + ", n " + std::to_string(n) + ")"); + } } fs::remove_all(out); fs::remove_all(dir);