From b861d9a624ad2a6f0f59c30a94521f373f04ad8f Mon Sep 17 00:00:00 2001 From: bong-water-water-bong Date: Tue, 10 Nov 2026 06:31:59 -0400 Subject: [PATCH 1/2] npu-pack: assert that a non-default weight layout lands in the container it builds (engine#1840) The merged fix (#1793) made import_external write q4nx_weight_layout exactly as the .fconst path does, and the CHANGES it was merged over asked for the arm that would have caught it -- 'the fixture already exists'. That arm was never added: grep -c q4nx_weight_layout tests/npu_pack_import_test.cpp was 0, so the regression (a report printing declares q4nx_weight_layout "b8" while the writer omitted the key) can return silently. Two assertions on a fixture this file already builds and imports, in test_final_norm_envelope: - the DEFAULT import's container carries no q4nx_weight_layout -- the key is written only for a non-default layout, and every container the importer wrote before the option existed depends on it being absent; - a second import with Options::weight_layout = "b8" writes q4nx_weight_layout = "b8" into the header of the container it builds (read back through the same 8-byte-length + JSON-header path the other arms use). Rationale for the pair rather than the positive alone: the writer's contract is 'absent means default', so an arm that only checks the b8 key would still pass if the writer started emitting the key for everyone. Verified: g++-16 -fsyntax-only against the real headers (nlohmann json fetched to /tmp, since this box does not have it installed and CI installs it); the arm runs in CI because it sits inside a ctest row ci.yml already builds (add_test(NAME npu_pack_import ...)), so no new target and no workflow edit. Not run end to end locally: that needs the full onebit_npu link, and / is under this lane's build gate (173G). --- tests/npu_pack_import_test.cpp | 40 ++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/npu_pack_import_test.cpp b/tests/npu_pack_import_test.cpp index 019347612..38ba52488 100644 --- a/tests/npu_pack_import_test.cpp +++ b/tests/npu_pack_import_test.cpp @@ -1413,6 +1413,46 @@ void test_final_norm_envelope(const std::string& root) { ("oga_pack_import_norm." + std::to_string(::getpid()))).string(); fs::remove_all(out); const pi::Report r = pi::import_pack(dir.string(), out, allow); + // engine#1840: a container built with a NON-default weight layout must carry `q4nx_weight_layout`, and a + // default-layout import must NOT -- every container the importer wrote before the option existed depends on the + // key being absent. Without this pair, the regression #1793 fixed (a report printing `declares q4nx_weight_layout + // "b8"` while the writer omitted the key) can return silently: nothing else asserts the writer's half. + { + const auto header_of = [](const std::string& file) -> json { + std::ifstream cf(file, std::ios::binary); + uint64_t hlen = 0; + cf.read(reinterpret_cast(&hlen), 8); + std::string hdr(size_t(hlen), '\0'); + cf.read(hdr.data(), std::streamsize(hlen)); + while (!hdr.empty() && (hdr.back() == ' ' || hdr.back() == '\0')) hdr.pop_back(); + return json::parse(hdr); + }; + const auto default_doc = header_of(out + "/model.q4nx"); + check(!default_doc.contains("q4nx_weight_layout"), + "weight layout: a default-layout container carries no q4nx_weight_layout (the key is only for a non-default one)"); + const std::string out_b8 = out + "_b8"; + fs::remove_all(out_b8); + pi::Options b8 = allow; + b8.weight_layout = "b8"; + bool threw_b8 = false; + try { + (void)pi::import_pack(dir.string(), out_b8, b8); + } catch (const std::exception& e) { + // an exception here would take the rest of this function with it, and the arm's job is to report, not to abort + threw_b8 = true; + std::printf(" weight layout: the b8 import threw: %s\n", e.what()); + } + check(!threw_b8, "weight layout: a b8 import completes on a pack the default layout imports"); + if (fs::exists(out_b8 + "/model.q4nx")) { + const auto b8_doc = header_of(out_b8 + "/model.q4nx"); + check(b8_doc.contains("q4nx_weight_layout") && b8_doc["q4nx_weight_layout"].is_string() && + b8_doc["q4nx_weight_layout"].get() == "b8", + "weight layout: --weight-layout b8 writes q4nx_weight_layout=\"b8\" into the container it builds"); + } else { + check(false, "weight layout: a b8 import wrote a container"); + } + fs::remove_all(out_b8); + } check(r.final_norm_imported, "final norm: the crafted tail envelope yields model.norm.weight"); check(!r.prefill_tail_seen && !r.final_norm_crosscheck, "final norm: with no prefill program the cross-check is unavailable, not failed"); From d7ba11c160aaff7ebf1b81c2e9d7943a2a3fd58d Mon Sep 17 00:00:00 2001 From: bong-water-water-bong Date: Tue, 10 Nov 2026 06:51:01 -0400 Subject: [PATCH 2/2] npu-pack: read the container's config.json, not the model.q4nx header (engine#1840) CI caught the first version of this arm, which is the best thing that could have happened to it: the positive arm failed because q4nx_weight_layout is written into the container's config.json (pack_import.cpp:3385, alongside model_type and hidden_size), not into the model.q4nx JSON header. Reading the header made the negative pass trivially -- no key in either place -- and the positive find nothing. Now both assertions parse the config.json the importer writes into the out dir, via json::parse(std::ifstream&), so the arm compares the file the option actually lands in. Re-type-checked with g++-16 -fsyntax-only against the real headers. --- tests/npu_pack_import_test.cpp | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/tests/npu_pack_import_test.cpp b/tests/npu_pack_import_test.cpp index 38ba52488..e1cc31580 100644 --- a/tests/npu_pack_import_test.cpp +++ b/tests/npu_pack_import_test.cpp @@ -1418,16 +1418,14 @@ void test_final_norm_envelope(const std::string& root) { // key being absent. Without this pair, the regression #1793 fixed (a report printing `declares q4nx_weight_layout // "b8"` while the writer omitted the key) can return silently: nothing else asserts the writer's half. { - const auto header_of = [](const std::string& file) -> json { + // the key lives in the container's config.json (`c["q4nx_weight_layout"]`, pack_import.cpp:3385), NOT in the + // model.q4nx header -- reading the header is what my first version of this arm did, and CI caught it: the + // negative passed trivially and the positive found nothing. + const auto config_of = [](const std::string& file) -> json { std::ifstream cf(file, std::ios::binary); - uint64_t hlen = 0; - cf.read(reinterpret_cast(&hlen), 8); - std::string hdr(size_t(hlen), '\0'); - cf.read(hdr.data(), std::streamsize(hlen)); - while (!hdr.empty() && (hdr.back() == ' ' || hdr.back() == '\0')) hdr.pop_back(); - return json::parse(hdr); + return json::parse(cf); }; - const auto default_doc = header_of(out + "/model.q4nx"); + const auto default_doc = config_of(out + "/config.json"); check(!default_doc.contains("q4nx_weight_layout"), "weight layout: a default-layout container carries no q4nx_weight_layout (the key is only for a non-default one)"); const std::string out_b8 = out + "_b8"; @@ -1444,7 +1442,7 @@ void test_final_norm_envelope(const std::string& root) { } check(!threw_b8, "weight layout: a b8 import completes on a pack the default layout imports"); if (fs::exists(out_b8 + "/model.q4nx")) { - const auto b8_doc = header_of(out_b8 + "/model.q4nx"); + const auto b8_doc = config_of(out_b8 + "/config.json"); check(b8_doc.contains("q4nx_weight_layout") && b8_doc["q4nx_weight_layout"].is_string() && b8_doc["q4nx_weight_layout"].get() == "b8", "weight layout: --weight-layout b8 writes q4nx_weight_layout=\"b8\" into the container it builds");