From d961df9614c1e74a1980270626d43dbaad3208d1 Mon Sep 17 00:00:00 2001 From: t Date: Tue, 10 Nov 2026 06:45:17 -0400 Subject: [PATCH] npu-pack: a pack that DECLARES per-head q/k gains is refused when they are not located (engine#1889) `use_qk_norm` in config.json was read by nothing (`grep -r use_qk_norm npu/ tools/` returned no hits), so the importer decided `qk_norm` from layout arithmetic alone plus a graph check. Neither matched Hunyuan-7B: config use_qk_norm true payload qk_norm false <- silently attention payload 96 tiles against 160 at the same geometry serve 'Trease/ft//////////' 3 of 3 and the import reported `attention 5120/5120 tiles` and `MLP 9216/9216 groups` byte-exact -- a clean-looking result whose per-head q/k gains were never written. The forward then runs without normalising q/k, which is the error class that answers deterministic gibberish instead of refusing, at a geometry four other packs answer correctly on. This is step 1 of the issue (read the key) plus the refusal the issue asks for, and deliberately not step 3 (locating the section on that family), which belongs to the tail/envelope work: * `Dims::use_qk_norm` reads `use_qk_norm` with `opt_field`, so absent means false rather than a parse error; * `qk_norm_refusal(declared, found)` is the whole decision in one testable place: declared-and-NOT-found is a refusal naming the key and what was not found; found-and-not-declared is the Qwen3 shape (the sections are there, the key is not) and must not be refused; neither is the ordinary pack; * `plan()` throws it before the q/k tensors are planned, so a pack in this state cannot import at all. Arms (7) in `tests/npu_pack_import_test.cpp`, beside the other named refusals. The two that matter are the pair that cannot both be satisfied by a no-op or by an always-refusing predicate: `declared+not-found` non-empty and naming the key, and `found+not-declared` empty. Proved they can fail by disabling the predicate (`if (false && declared && !found)`) -- 5 arms report FAIL -- then reverting. **Not claimed:** this does not make Hunyuan-7B work; it makes it refuse by name instead of answering gibberish, which the issue itself calls worth having on its own. Steps 2/3 (locating the gains from the measured section, and `HY-MT1.5-7B` which is likely the same defect) stay on the issue. Suite 174/174; copyright clean. Refs #1889, #1740. --- npu/pack_import.cpp | 18 ++++++++++++++++++ npu/pack_import.h | 15 +++++++++++++++ tests/npu_pack_import_test.cpp | 22 ++++++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/npu/pack_import.cpp b/npu/pack_import.cpp index 400e4fe29..eedfcb3f8 100644 --- a/npu/pack_import.cpp +++ b/npu/pack_import.cpp @@ -1282,6 +1282,9 @@ Dims read_dims(const json& c, const std::string& source = "config.json") { d.rope_theta = opt_field(c, "rope_theta", 10000.0f); d.model_type = opt_field(c, "model_type", ""); d.rms_norm_eps = opt_field(c, "rms_norm_eps", 1e-6f); + // engine#1889: the pack's own statement about per-head q/k gains. `opt_field` because the key is absent on + // every pack that has no such norm, and absent must mean false rather than a parse error. + d.use_qk_norm = opt_field(c, "use_qk_norm", false); d.eps_bits = bf16_bits_of(d.rms_norm_eps); if (c.contains("eos_token_id")) { const auto& e = c["eos_token_id"]; @@ -2473,6 +2476,14 @@ std::string Plan::manifest() const { return j.dump(2); } +std::string qk_norm_refusal(bool declared, bool found) { + if (declared && !found) + return "this pack declares use_qk_norm and its per-head q/k gains are not where the block layout says " + "(engine#1889): importing without them would normalise no q/k, which answers plausibly-wrong text " + "instead of refusing. The section location for this family is not measured yet"; + return std::string(); +} + Plan plan(const std::string& pack_dir) { Plan p; p.dir = pack_dir; @@ -3054,6 +3065,13 @@ Plan plan(const std::string& pack_dir) { // source from the .fconst's two per-head sections but the same tensor in the container. const bool graph_qk_norm = graph_weights && !onnx.index.empty() && onnx.index.count("model.layers.0.attn.q_norm.layernorm.weight"); + /* engine#1889: a pack that DECLARES per-head q/k gains and whose gains this importer did not locate must not + * import -- the forward would run without normalising q/k, which answers deterministic gibberish instead of + * refusing (Hunyuan-7B: `Trease/ft//////////` three of three, at a geometry four other packs answer correctly on). + * The section location for that family is not measured yet, so the honest outcome is a named refusal; the + * layout arithmetic and the graph check stay the two ways the gains ARE found. */ + if (const std::string qk_why = qk_norm_refusal(d.use_qk_norm, p.knorm_off != 0 || graph_qk_norm); !qk_why.empty()) + throw std::runtime_error(pack_dir + ": " + qk_why); if (p.knorm_off || graph_qk_norm) for (int L = 0; L < d.layers; ++L) for (const char* nm : {"self_attn.q_norm.weight", "self_attn.k_norm.weight"}) { diff --git a/npu/pack_import.h b/npu/pack_import.h index 838513fbb..4d7270720 100644 --- a/npu/pack_import.h +++ b/npu/pack_import.h @@ -71,8 +71,23 @@ struct Dims { unsigned short eps_bits = 0x3586; std::string model_type; std::vector eos; + // config.json's `use_qk_norm` (engine#1889). Hunyuan-7B declares it and puts 32x128 + 8x128 per-head q/k + // gains in its block; nothing read this key, so the importer decided `qk_norm` from layout arithmetic alone, + // landed on false, and the pack imported cleanly with the norms silently omitted -- the forward then runs + // without normalising q/k, which produces deterministic gibberish rather than a refusal. Read here so the + // decision has the pack's own statement as one of its inputs. + bool use_qk_norm = false; }; +// Why a pack that DECLARES per-head q/k gains cannot be imported, or "" when it can (engine#1889). +// +// `declared` is config.json's `use_qk_norm`; `found` says the block layout located the two sections (knorm_off / +// qnorm_off) or the graph carries them. The three cases are deliberately separate: declared-and-not-found is a +// REFUSAL (importing without the gains is the silent-garbage path), found-and-not-declared is allowed only with the +// measurement behind it (Qwen3 packs ship the sections without the key), and neither is the ordinary case. Exposed +// so the three can be pinned without a 4 GB pack. +std::string qk_norm_refusal(bool declared, bool found); + // A hybrid pack's embedding table lives in its own graph (`embedding.onnx` + `.data`, gathered with // GatherBlockQuantized). Returns [V, H] f32 row-major, empty when the pack has no such component, with `why` naming the // reason. Exposed so the dequantisation can be tested against known codes and scales instead of only on a 3 GB pack. diff --git a/tests/npu_pack_import_test.cpp b/tests/npu_pack_import_test.cpp index 019347612..8144554c9 100644 --- a/tests/npu_pack_import_test.cpp +++ b/tests/npu_pack_import_test.cpp @@ -3369,7 +3369,29 @@ void test_tail_head_placement() { } +/* engine#1889: a pack that declares per-head q/k gains and whose gains the importer did not locate must be REFUSED, + * not imported without them. Hunyuan-7B is the measured case: `use_qk_norm: true`, 32x128 + 8x128 gains in its block, + * a 96-tile attention payload against the 160 the same geometry gets elsewhere -- and the import reported + * `attention 5120/5120 tiles` byte-exact while the served model answered `Trease/ft//////////` three of three. + * The three cases are separate on purpose: declared-and-not-found is the refusal; found-and-not-declared is the Qwen3 + * shape (the sections are there, the key is not) and must NOT be refused. */ +void test_qk_norm_refusal() { + const std::string a = pi::qk_norm_refusal(true, false); + check(!a.empty(), "qk norm: declared but not found is a refusal (importing without the gains answers gibberish)"); + check(a.find("use_qk_norm") != std::string::npos, "qk norm: the refusal names the key the pack declared"); + check(a.find("not where the block layout says") != std::string::npos, + "qk norm: and says what was not found, rather than failing generically"); + check(a.find("engine#1889") != std::string::npos, "qk norm: the refusal carries its issue, like the others here"); + check(pi::qk_norm_refusal(true, true).empty(), + "qk norm: declared and found is allowed (the arithmetic or the graph located the gains)"); + check(pi::qk_norm_refusal(false, true).empty(), + "qk norm: found and not declared is allowed (Qwen3 ships the sections without the key)"); + check(pi::qk_norm_refusal(false, false).empty(), "qk norm: neither is the ordinary pack"); + std::printf(" qk norm: 7 arms (declared+not-found refuses; found-only and neither are allowed)\n"); +} + int main(int argc, char** argv) { + test_qk_norm_refusal(); test_tail_head_placement(); const std::string golden_dir = argc > 1 ? argv[1] : ""; const std::string pack = argc > 2 ? argv[2] : "";