diff --git a/npu/pack_import.cpp b/npu/pack_import.cpp index 19c171787..a14c00c1b 100644 --- a/npu/pack_import.cpp +++ b/npu/pack_import.cpp @@ -1320,6 +1320,19 @@ int fill_from_model_config(const std::string& pack_dir, nlohmann::json& cfg) { // engine#734: exported so the epsilon anchor itself can be tested against a crafted .fconst, without a pack. // The two bytes a bf16 epsilon is stored as (little-endian): round the f32 to nearest-even bf16. +// engine#1740: is the .fconst tail LONGER than the head needs by whole tile-slots? A vendor pads the head to a whole number of +// 64-row tiles -- Hunyuan-7B-Instruct 1.9 carries 2004 slots for a vocabulary of 128,167 (2003 slots needed = 89 padding rows, +// 143,616 B) -- and the exact-length check refused the whole head over it. Accepting is safe only while the padding is whole +// SLOTS (the head's own offsets stay the ones this arithmetic derives) and small (<= 2 slots): anything else means the tail is +// not this envelope plus this head and the offsets are not established. Exported so the decision is testable without a pack. +bool fconst19_tail_padded(int64_t env, int64_t rows, int64_t tile, int64_t have) { + if (tile <= 0 || rows <= 0 || have <= 0) return false; + const int64_t body = have - env; + if (body <= 0 || body % tile != 0) return false; + const int64_t slots = body / tile - rows; + return slots > 0 && slots <= 2; +} + unsigned short bf16_bits_of(float v) { uint32_t u = 0; std::memcpy(&u, &v, 4); @@ -3506,7 +3519,12 @@ Report import_pack(const std::string& pack_dir, const std::string& out_dir, cons const int64_t env = attn_payload_off(H); const int64_t want = env + rows * tile; const int64_t have = int64_t(fc.size) - tail_off; - if (want != have) { + // A vendor may pad the head to a whole number of tiles: Hunyuan-7B-Instruct 1.9 carries 2004 tile-slots for a + // vocab of 128,167 (2003 of 64 rows needed), i.e. 89 padding rows and 143,616 B of trailing padding. That is + // a tail LARGER than the head needs, which used to refuse the whole head. Accept the extra only when the + // remainder is a whole number of tile-slots (so the head's own offsets are still the ones this arithmetic + // derives) and read the tiles the vocab needs, leaving the padding unread. + if (want != have && !fconst19_tail_padded(env, rows, tile, have)) { late.push_back("lm_head.weight: the .fconst tail is " + std::to_string(have) + " B, not the " + std::to_string(want) + " B its envelope and head tiles would take (" + std::to_string(env) + " + " + std::to_string(rows) + " x " + diff --git a/npu/pack_import.h b/npu/pack_import.h index 1a4f96da4..b0643cb49 100644 --- a/npu/pack_import.h +++ b/npu/pack_import.h @@ -389,6 +389,11 @@ struct FconstGeom { int64_t post_attn_hdr() const { return hdr.empty() ? h1 : hdr.back(); } std::string why; // non-empty when the geometry could not be established }; +// Whether a 1.9 pack's .fconst tail is LONGER than its head needs by whole tile-slots, which is a vendor padding the head to +// a multiple of 64 rows rather than a tail whose offsets are not established (engine#1740: Hunyuan-7B-Instruct carries 2004 +// slots for a 128,167-token vocabulary). Exported so the decision has a test that needs no pack. +bool fconst19_tail_padded(int64_t env, int64_t rows, int64_t tile, int64_t have); + unsigned short bf16_bits_of(float v); // engine#1650: config.json may declare an OPTIONAL field as JSON null (AMD's 1.9 diff --git a/tests/npu_pack_import_test.cpp b/tests/npu_pack_import_test.cpp index a639cc241..f30efe046 100644 --- a/tests/npu_pack_import_test.cpp +++ b/tests/npu_pack_import_test.cpp @@ -2950,6 +2950,24 @@ int main(int argc, char** argv) { test_embedding_f32_component(golden_dir.empty() ? "." : golden_dir); test_qwen3_attn_sections(); test_post_attn_norm_header(golden_dir.empty() ? "." : golden_dir); + // engine#1740: the tail may be padded by whole tile-slots; anything else means the head's offsets are not established. + { + const int64_t env = 8256, rows = 2003, tile = 143616; // Hunyuan-7B-Instruct 1.9's own numbers + // The predicate answers "is it LONGER by whole slots", never "is this length fine" -- the caller's exact-equality + // arm accepts the exact length, and conflating the two is how an accept-everything 0-slot case would slip in. + check(!pi::fconst19_tail_padded(env, rows, tile, env + rows * tile), + "tail padding: the exact length is not \"padded\" (the caller accepts it separately)"); + check(pi::fconst19_tail_padded(env, rows, tile, env + (rows + 1) * tile), + "tail padding: one padded slot is accepted (Hunyuan-7B 1.9 ships exactly this)"); + check(pi::fconst19_tail_padded(env, rows, tile, env + (rows + 2) * tile), + "tail padding: two padded slots are accepted"); + check(!pi::fconst19_tail_padded(env, rows, tile, env + (rows + 3) * tile), + "tail padding: three slots is not padding, the offsets are not established"); + check(!pi::fconst19_tail_padded(env, rows, tile, env + rows * tile + tile / 2), + "tail padding: a half slot is not padding"); + check(!pi::fconst19_tail_padded(env, rows, tile, env + rows * tile - 1), + "tail padding: a SHORT tail is never accepted"); + } test_final_norm_envelope(golden_dir.empty() ? "." : golden_dir); test_import_fp32_params(golden_dir.empty() ? "." : golden_dir); test_text_artifacts(golden_dir.empty() ? "." : golden_dir);