Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions npu/pack_import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1835,9 +1835,15 @@ FconstGeom locate_fconst_with_header(const std::string& tok, const std::string&
return g;
}
const std::vector<int64_t> pos = eps_header_positions(tok, size_t(1) << 30, dims.eps_bits);
// The scan window is the first GiB (about 8 blocks at h3584), so a real section boundary repeats ~8 times against
// 1-2 for a payload coincidence; 3 separates them (requiring `layers/2` was too strict and dropped the real one).
const std::vector<int64_t> inner = fconst_block_headers(pos, stride8, 3);
// engine#1985: a constant cut of `3` was calibrated on h3584, where the first-GiB window holds about 8 blocks. At a
// smaller stride the window holds more blocks, so a payload coincidence accrues more chances to reach 3 while a real
// boundary stays at one per block -- Qwen3-1.7B (hidden 2048) came back with four in-block offsets, three real plus a
// coincidence, and `fconst_set_block_headers` accepts only 1 or 3, so the whole layout was dropped and the pack was
// refused. The floor is taken from the observed distribution (half the most frequent offset, never below 3) rather than
// from the window: `pos.size()` counts anchors, not blocks, and in a mixed attention/GDN pack a real header appears in
// only about half the blocks, so neither `pos.size()/4` nor a fixed fraction of the window separates the two cases
// (review of engine#2021, gaps 1 and 2).
const std::vector<int64_t> inner = fconst_block_headers_observed(pos, stride8);
if (!fconst_set_block_headers(g, inner)) {
g.why = "the 1.9 header gives stride " + std::to_string(stride8) + ", but the epsilon anchors repeat at " +
std::to_string(inner.size()) + " in-block offset(s), and a layer block has 1 (2-header) or 3 (4-header); "
Expand All @@ -1851,6 +1857,34 @@ FconstGeom locate_fconst_with_header(const std::string& tok, const std::string&
// bytes lands at `r` modulo `stride` in every block, so the offsets that recur `min_repeat` or more times are the block's
// section boundaries; offset 0 is the block start, not an inner header. ALL of them are kept rather than the most common
// one: a Qwen3 block has three equally frequent inner headers, and keeping only the first maximum picked k_norm's.
std::vector<int64_t> fconst_block_headers_observed(const std::vector<int64_t>& pos, int64_t stride) {
std::vector<int64_t> out;
if (stride <= 0) return out;
std::map<int64_t, int> count;
int most = 0;
for (const int64_t q : pos) {
const int64_t r = q % stride;
if (r > 0) most = std::max(most, ++count[r]);
}
// Never below 3: that is the value measured on a h3584 window of ~8 blocks, and a smaller floor would keep payload
// coincidences on a short pack. Half the mode covers the mixed-layer case without a per-architecture constant.
const int floor_count = std::max(3, most / 2);
for (const auto& [r, n] : count)
if (n >= floor_count) out.push_back(r);
// A floor taken from the mode is only meaningful if the mode is a real header. `fconst_set_block_headers` accepts ONE
// offset as a 2-header block, so when the cut leaves one, a dominant payload coincidence would become a silent layout
// instead of a refusal (review of #2021: 3 real headers x6 blocks + one residue x20 -> floor 10 keeps only the
// residue). A real 2-header block has exactly one recurring inner offset, so every other residue must stay below the
// constant floor 3 that main used; if one does not, return nothing and the caller refuses. This is never more
// permissive than the constant: under it a second residue at >= 3 already gave two offsets, which is refused too.
// (A ">= 2x the next residue" test does not catch the review's case: 20 >= 2 x 6.)
if (out.size() == 1) {
for (const auto& [r, n] : count)
if (r != out.front() && n >= 3) return {};
}
return out;
}

std::vector<int64_t> fconst_block_headers(const std::vector<int64_t>& pos, int64_t stride, int min_repeat) {
std::vector<int64_t> out;
if (stride <= 0) return out;
Expand Down
7 changes: 7 additions & 0 deletions npu/pack_import.h
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,13 @@ FconstGeom locate_fconst(const std::string& path, unsigned short eps_bits = 0x35
// recurs `min_repeat` or more times (offset 0, the block start, excluded). Kept as a list, not a mode: a Qwen3 block has
// three equally frequent inner headers.
std::vector<int64_t> fconst_block_headers(const std::vector<int64_t>& pos, int64_t stride, int min_repeat);
// engine#1985: the same list with the count floor taken from the *observed* distribution -- half the most frequent offset's
// count, never below 3 -- instead of a constant or a fraction of the scan window. A real inner header lands at the same
// offset in most of the blocks (all of them in a uniform pack, about half in a mixed attention/GDN pack, where a
// q_norm-style header exists only in the attention layers), while a payload coincidence is a handful of hits by
// construction and does not grow with the block count. Counting offsets rather than anchors is deliberate: `pos` may carry
// more than one anchor per block, so its length is not the block count (review of engine#2021, gap 1).
std::vector<int64_t> fconst_block_headers_observed(const std::vector<int64_t>& pos, int64_t stride);
// engine#1831: set `hdr`/`h1`/`period` from that list the way locate_fconst() does -- one inner header is a 2-header block,
// three is a 4-header block. Returns false, and changes nothing, for any other count.
bool fconst_set_block_headers(FconstGeom& g, const std::vector<int64_t>& inner);
Expand Down
60 changes: 60 additions & 0 deletions tests/npu_pack_import_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4307,6 +4307,66 @@ void test_fconst_block_headers() {
g.post_attn_hdr() == 340,
"block headers: and is set as a 4-header block whose post-attention header is the last one, not k_norm's");
}
// engine#1985: the cut has to come from the observed distribution, not from a constant or from a fraction of the window.
// 16 blocks of a uniform 4-header pack: the three real offsets appear 16 times each, a coincidence appears 3 times.
// The constant keeps the coincidence -- four offsets, which `fconst_set_block_headers` refuses -- and the observed floor
// (half the mode) drops it. This is the arm the fix was missing: the direct call cannot see the caller's choice.
{
const auto pos = blocks({300, 320, 340}, 16, {1234, 1234, 1234});
check(pi::fconst_block_headers(pos, stride, 3).size() == 4,
"block headers: a constant 3 keeps a coincidence in a wider window -- the defect engine#1985 reports");
const auto inner = pi::fconst_block_headers_observed(pos, stride);
check(inner.size() == 3 && inner[0] == 300 && inner[1] == 320 && inner[2] == 340,
"block headers: the observed floor drops the coincidence and keeps the three real ones");
pi::FconstGeom g;
g.stride = stride;
check(pi::fconst_set_block_headers(g, inner) && g.period == 4,
"block headers: so a wider window imports as a 4-header block instead of being refused");
}
// The same window with the coincidence at 2: the rule keys on the count, not on the pack's geometry.
{
const auto inner = pi::fconst_block_headers_observed(blocks({300, 320, 340}, 16, {1234, 1234}), stride);
check(inner.size() == 3, "block headers: a coincidence at 2 is dropped as before -- the rule keys on the count");
}
// engine#2021 gap 2: a mixed attention/GDN pack carries a q_norm-style header in only HALF the blocks (12 layers, 6 of
// each). It is a real header and must survive; `pos.size()/4` or a fixed fraction of the window would drop it.
{
std::vector<int64_t> pos;
for (int b = 0; b < 16; ++b) {
pos.push_back(int64_t(b) * stride); // the block's own header, offset 0
pos.push_back(int64_t(b) * stride + 300); // carried by every block
if (b % 2 == 0) pos.push_back(int64_t(b) * stride + 900); // only by the attention layers: 8 of 16
}
pos.push_back(5 * stride + 1234); // a payload coincidence: 3 hits, once each
pos.push_back(9 * stride + 1234);
pos.push_back(13 * stride + 1234);
std::sort(pos.begin(), pos.end());
const auto inner = pi::fconst_block_headers_observed(pos, stride);
check(inner.size() == 2 && inner[0] == 300 && inner[1] == 900,
"block headers: a header in half the blocks still clears the floor (mixed-layer pack)");
}
// Review of #2021: when the mode is a payload coincidence, half of it can exceed the real headers. Three real headers in
// 6 blocks (6 hits each) and one residue at 20 hits: the floor is 10 and keeps only the residue, which
// `fconst_set_block_headers` would accept as a 2-header block. It must refuse instead.
{
std::vector<int64_t> pos = blocks({300, 320, 340}, 6, {});
for (int i = 0; i < 20; ++i) pos.push_back(7 * stride + 1234 + int64_t(i) * stride); // residue 234, 20 hits
std::sort(pos.begin(), pos.end());
const auto inner = pi::fconst_block_headers_observed(pos, stride);
pi::FconstGeom g;
g.stride = stride;
check(inner.empty() && !pi::fconst_set_block_headers(g, inner) && g.hdr.empty() && g.period == 0,
"block headers: a dominant coincidence (reals x6, noise x20) is refused, not read as a 2-header block");
}
// ...and a real 2-header block through the same path still imports: one inner header, stray residues at 1-2 hits.
{
const auto inner = pi::fconst_block_headers_observed(blocks({400}, 8, {1234, 5678, 5678}), stride);
pi::FconstGeom g;
g.stride = stride;
check(inner.size() == 1 && inner[0] == 400 && pi::fconst_set_block_headers(g, inner) && g.period == 2,
"block headers: a real 2-header block with stray coincidences still imports through the observed floor");
}

// coincidences alone are not a header, and a count that is neither 1 nor 3 sets nothing
{
check(pi::fconst_block_headers(blocks({}, 8, {111, 222, 222}), stride, 3).empty(),
Expand Down
Loading