Skip to content
Closed
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
2 changes: 2 additions & 0 deletions npu/pack_import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t> fconst19_header(int heads) {
std::vector<uint8_t> h(size_t(heads) * 4, 0);
for (int i = 0; i < heads; ++i) {
Expand All @@ -499,6 +500,7 @@ std::vector<uint8_t> 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
Expand Down
9 changes: 9 additions & 0 deletions npu/pack_import.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t> 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
Expand Down
35 changes: 35 additions & 0 deletions tests/npu_pack_import_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3009,6 +3009,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<uint8_t> 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(),
Expand Down
Loading