From b0e4e4460c193fc5df97c95b87663efe61e51a1b Mon Sep 17 00:00:00 2001 From: t Date: Tue, 10 Nov 2026 03:24:23 -0400 Subject: [PATCH] npu-pack: a tail padded by whole tile-slots is not a head whose offsets are unknown (refs #1740) `import_pack` required the 1.9 pack's `.fconst` tail to be exactly `envelope + rows x tile`, and refused the head by name otherwise. Hunyuan-7B-Instruct 1.9 pads that tail to a whole number of 64-row tiles: a vocabulary of 128,167 needs 2003 slots and the pack carries **2004** -- 89 padding rows, 143,616 B -- so the head was refused on a pack whose offsets are exactly right: lm_head.weight: the .fconst tail is 287,814,720 B, not the 287,671,104 B its envelope and head tiles would take (8256 + 2003 x 143616) -> incomplete: 1 with this change -> incomplete: 0 Measured on `amd/Hunyuan-7B-Instruct_rai_1.9.0_npu` (attention 5120/5120 tiles and MLP 9216/9216 groups byte-exact either way). The accept is deliberately narrow, and the predicate is 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); * the excess must be a **whole number of tile-slots** -- that is what keeps the head's own offsets the ones this arithmetic derives; a partial slot means the tail is not this envelope plus this head; * at most **two** slots of it, because a vendor padding for alignment pads to the tile, not by an arbitrary amount; * a tail **shorter** than the head needs is never accepted. Six arms pin exactly those edges (`env 8256, rows 2003, tile 143616` -- the pack's own numbers): exact length is not "padded" (the caller accepts it separately, and conflating the two would let a zero-slot case accept everything), +1 slot and +2 slots accept, +3 slots refuse, a half slot refuses, a short tail refuses. Mutation-checked: widening the bound to `slots > 0` fails the +3 arm. Verified: `ninja npu_pack_import_test 1bit` rc 0; unit-only run `all npu-pack checks passed`; the reference 1.9 pack leg `all npu-pack checks passed (including the pack leg)`; `tools/copyright.py --check` rc 0 with the tree staged; `git add ` only. --- npu/pack_import.cpp | 20 +++++++++++++++++++- npu/pack_import.h | 5 +++++ tests/npu_pack_import_test.cpp | 18 ++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) 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);