From ade0a9e9666f6d3084f592653bad4c1e6aedf558 Mon Sep 17 00:00:00 2001 From: bong-water-water-bong Date: Tue, 10 Nov 2026 03:29:24 -0400 Subject: [PATCH] npu-pack: an arm for the header-over-vote path, and the seam that lets a test build the fixture Review of #1819: the call-order change was unpinned -- `grep locate_fconst_with_header tests/` was 0 -- so reverting it left the suite green. The existing `fconst eps` arms exercise the VOTE, and the case the PR exists for (a header that states the stride while the vote cannot clear its 66 % bar) had no arm at all. The fixture is built, not typed: `fconst19_header(heads)` is exported (and given external linkage by a scoped namespace close) so the test constructs the real marker rather than retyping `00 00 7f ff`. `locate_fconst_with_header` is exported for the same reason. The fixture is a 3-layer file whose payload is eps-DENSE -- bf16 1e-6 followed by zeros at an irregular period -- so the vote has many candidate periods and cannot agree on one. Arms: * the vote CANNOT locate this payload (stride 0) -- the precondition, asserted so the arm cannot silently stop testing what it claims; * the header's stride wins anyway: 1,048,560 = (1 << 20) - 4*4, and no disagreement is reported. Mutant check -- make `locate_fconst_with_header` return the vote's answer when the vote fails, i.e. the pre-#1819 behaviour: FAIL fconst header: the header's stride wins where the vote failed (0 vs 1048560) FAIL fconst header: and it is not reported as a disagreement (the epsilon headers do not repeat with a single period) That is the issue's own message, reproduced by the arm. Verified: full build, `all npu-pack checks passed`; the eps arms still print their votes, so the fallback path is intact. Refs #1819, #1740 --- npu/pack_import.cpp | 2 ++ npu/pack_import.h | 9 +++++++++ tests/npu_pack_import_test.cpp | 35 ++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/npu/pack_import.cpp b/npu/pack_import.cpp index 4ffb990d8..2d28c2bc9 100644 --- a/npu/pack_import.cpp +++ b/npu/pack_import.cpp @@ -491,6 +491,7 @@ const Fconst19Rule* select_rule_in(const Fconst19Rule* rules, int count, int hid // The header a 1.9 block carries: 00 00 7f ff once per head. Its length being the head count is what makes // it a test rather than a magic number. 0x00007fff is the big-endian reading of those four bytes; the bytes // are what is compared, because the little-endian reading of the same bytes is 0xff7f0000. +} // namespace (closed so the marker builder has external linkage: a test builds the fixture with it, engine#1819) std::vector fconst19_header(int heads) { std::vector h(size_t(heads) * 4, 0); for (int i = 0; i < heads; ++i) { @@ -499,6 +500,7 @@ std::vector fconst19_header(int heads) { } return h; } +namespace { // True when `fc` is a 1.9 Token .fconst of this geometry: exactly `layers` headers, evenly spaced. // Fills the first header's offset and the block stride. False means the 1.8 form (or a shape this diff --git a/npu/pack_import.h b/npu/pack_import.h index 838513fbb..7e0ab097e 100644 --- a/npu/pack_import.h +++ b/npu/pack_import.h @@ -440,6 +440,15 @@ int parse_onnx_matmul_census(const std::string& path, int* matmuls, int* without // engine#1638: the geometry as read, so a test can check WHICH source a number came from. nlohmann::json load_geometry_exported(const std::string& pack_dir, std::string* source); FconstGeom locate_fconst(const std::string& path, unsigned short eps_bits = 0x3586); +// The 112-byte-per-head marker a 1.9 Token .fconst repeats once per layer (`heads * 4` bytes, four per head with the +// `7f ff` pair). Exposed for the same reason as `plan_tail_head`: the arm that pins "the header wins over the eps vote" +// needs to BUILD a file whose header states a stride while its payload defeats the vote, and that means constructing the +// marker rather than retyping it in the test (engine#1819 review). +std::vector fconst19_header(int heads); +// engine#1740/#1819: locate with the 1.9 header asked FIRST and the eps vote as the fallback (a payload dense in +// eps-shaped bytes can hold the vote below its 66 % bar while the header states the stride exactly). Exported so the arm +// can hand it a pack directory whose header and whose vote disagree -- the case the call-order change exists for. +FconstGeom locate_fconst_with_header(const std::string& tok, const std::string& pack_dir, const Dims& dims); // One measured Ryzen AI 1.9 Token .fconst block shape (engine#1503). Every field is measured against the 1.8 pack of // the same model by tools/fconst19_measure.py; none of it is derivable from the pack, which is why a pack whose shape diff --git a/tests/npu_pack_import_test.cpp b/tests/npu_pack_import_test.cpp index 25051340b..b8f7ed58e 100644 --- a/tests/npu_pack_import_test.cpp +++ b/tests/npu_pack_import_test.cpp @@ -2879,6 +2879,41 @@ void test_fconst_eps(const std::string& root) { const fs::path dir = fs::path std::to_string(g.h1) + ", period " + std::to_string(g.period)); check(g.blocks == blocks * 2 + 1, "fconst eps: " + label + " found every header including the tail (" + std::to_string(g.blocks) + ")"); + + // engine#1819 review: the case this PR exists for -- a 1.9 header that STATES the stride while the eps vote cannot + // clear its 66 % bar -- had no arm, so reverting the header-first call order left the suite green. Measured on + // Qwen2-7B 1.9 the right stride won only 52.6 % of lag-2 pairs, under the bar, and the pack was refused with "the + // epsilon headers do not repeat with a single period" while its own header carried the stride. + { + const int h_heads = 4, h_layers = 3; + const int64_t stride9 = int64_t(1) << 20; // well above the locator's 1e6 floor and > the marker size + const std::vector pat = pi::fconst19_header(h_heads); + // An eps-DENSE payload: bf16 1e-6 (86 35) followed by zeros, at an irregular period, so the vote has many + // candidate periods and cannot agree on one. That is the payload shape the issue describes, not a tidy one. + std::string bytes(size_t(int64_t(h_layers) * stride9), '\0'); + for (size_t o = 512; o + 12 < bytes.size(); o += 512) + for (int k = 0; k < 12; k += 2) { bytes[o + size_t(k)] = char(0x86); bytes[o + size_t(k) + 1] = char(0x35); } + for (int L = 0; L < h_layers; ++L) + std::memcpy(&bytes[size_t(int64_t(L) * stride9)], pat.data(), pat.size()); + const fs::path hd = fs::path(root) / "fconst19-header-vote"; + fs::remove_all(hd); + fs::create_directories(hd); + const std::string hpath = (hd / "dd_metastate_Llm_Token_x.fconst").string(); + put_file(hpath, bytes); + + const pi::FconstGeom by_vote = pi::locate_fconst(hpath, 0x3586); + check(by_vote.stride == 0, + "fconst header: the eps vote CANNOT locate this payload (got stride " + std::to_string(by_vote.stride) + + "), which is the condition the arm needs"); + pi::Dims d; + d.hidden = 896; d.heads = h_heads; d.layers = h_layers; d.head_dim = 64; d.eps_bits = 0x3586; + const pi::FconstGeom g = pi::locate_fconst_with_header(hpath, hd.string(), d); + const int64_t expect8 = stride9 - int64_t(h_heads) * 4; // 1.9 includes the header inside the block + check(g.stride == expect8, + "fconst header: the header's stride wins where the vote failed (" + std::to_string(g.stride) + " vs " + + std::to_string(expect8) + ")"); + check(g.why.empty(), "fconst header: and it is not reported as a disagreement (" + g.why + ")"); + } const unsigned short other = bits == 0x3586 ? 0x3728 : 0x3586; const pi::FconstGeom w = pi::locate_fconst(path, other); check(w.stride == 0 && !w.why.empty(),