diff --git a/tests/q4nx_tail_head_test.py b/tests/q4nx_tail_head_test.py index 787c5bd58..0b6f79d0c 100755 --- a/tests/q4nx_tail_head_test.py +++ b/tests/q4nx_tail_head_test.py @@ -253,6 +253,21 @@ def main(): check(T.head_offset(291655296, 2374, 34816) == 209000256 + T.attn_payload_off(896), "it equals the importer's layers*stride + env (209000256 is this pack's 24 layer blocks)") + # ── placement when the tail is padded to whole tile slots (engine#1852) ── + ENV, TILE, N = T.attn_payload_off(896), T.attn_tile_bytes(896), 2374 + unpadded = ENV + N * TILE + check(T.slot_candidates(unpadded, ENV, N, TILE) == [T.head_offset(unpadded, N, TILE)], + "an unpadded tail has exactly one placement, and it is head_offset's") + byte_padded = unpadded + 4096 + check(T.slot_candidates(byte_padded, ENV, N, TILE, zeros=4096) == [ENV + 0 * TILE], + "bytes padded after the grid still give the envelope's end (engine#1789's case)") + slot_padded = ENV + (N + 1) * TILE # Hunyuan-7B: 2004 slots for a 2003-tile head + c = T.slot_candidates(slot_padded, ENV, N, TILE) + check(len(c) == 2 and c[1] == ENV, + "a whole-slot pad gives two placements and the head is the SECOND one (env), which is the #1789 rule landing a tile early") + check(all(off >= ENV for off in T.slot_candidates(ENV + (N + 9) * TILE, ENV, N, TILE)), + "no candidate starts before the envelope, however much padding the file holds") + # ── the envelope decoder, against the vendor tile ── with open(GOLDEN + ".json") as f: meta = json.load(f) diff --git a/tools/q4nx_tail_head.py b/tools/q4nx_tail_head.py index 56bdeea7a..3e9fe9dcf 100755 --- a/tools/q4nx_tail_head.py +++ b/tools/q4nx_tail_head.py @@ -97,6 +97,26 @@ def attn_payload_off(hidden): return K_NORM_OFF + hidden * 2 +def slot_candidates(fconst_bytes, env, ntiles, tile, zeros=0, slack=8): + """Where the head's grid can start when the tail is padded to WHOLE tile slots. + + engine#1852: the head occupies `ntiles` tiles, but a 1.9 pack may carry more slots -- `Hunyuan-7B` needs 2003 for a + 128,167-row vocabulary and ships 2004 (89 padding rows, 143,616 B) -- so the #1789 rule (`size - zeros - ntiles*tile`) + lands a whole tile early on such a pack. The grid still ENDS at `size - zeros`, so the candidates grow backwards from + there: `size - zeros - (ntiles + k)*tile` for k = 0..slack, keeping only those at or after the envelope (nothing before + the tail can be the head). `slack` is bounded like the importer's own search window -- the padding seen in practice is + one slot, and an unbounded search would walk into the per-layer payload. + """ + end = fconst_bytes - zeros + out = [] + for k in range(0, slack + 1): + off = end - (ntiles + k) * tile + if off < env or off + ntiles * tile > end: + break + out.append(off) + return out + + def head_offset(fconst_bytes, ntiles, tile, zeros=0): """Where the tied head's grid starts: the last `ntiles * tile` bytes of the `.fconst`. @@ -318,7 +338,38 @@ def measure(pack, tiles_wanted, ref_path=None, ref_dtype="bf16"): # reaches the head as `layers*stride + env`, which equals this subtraction whenever its size check passes # (engine#1693: at `env` the decode correlates at chance, at the real offset 1.0000). zeros = trailing_zeros(path) - base = head_offset(size, ntiles, tile, zeros) + # engine#1852: the grid may be padded to whole tile slots, so there can be more than one placement. The candidates grow + # backwards from the end of the payload; which one is the head can only be settled by the oracle, so without one this + # refuses rather than picking the #1789 rule's answer and calling it measured. + cands = slot_candidates(size, env, ntiles, tile, zeros) + placement = 0 + base = cands[0] if cands else -1 + if len(cands) > 1: + if ref_all is None: + return None, 7, ("the payload holds %d more tile-slot(s) than the head needs (%d tiles), so %d placements are " + "possible; pass --pack with model.onnx (the oracle) or --ref, or the answer is a guess" + % (len(cands) - 1, ntiles, len(cands))) + best_i, best_c, second_c = 0, -2.0, -2.0 + with open(path, "rb") as f: + for i, off in enumerate(cands): + f.seek(off) + buf = f.read(tile) + if len(buf) != tile: + continue + w0, _ = decode_tile(buf, hidden, K_TILE_ROWS) + with np.errstate(all="ignore"): + c = float(np.corrcoef(w0.ravel().astype(np.float64), + ref_all[0:K_TILE_ROWS].ravel().astype(np.float64))[0, 1]) + if c > best_c: + second_c, best_i, best_c = best_c, i, c + elif c > second_c: + second_c = c + if not (best_c > 0.5) or second_c > 0.5: + return None, 7, ("no placement in the tail correlates with the packet's embedding (%d candidates, best %.4f, " + "second %.4f at %d tiles needed against a padded grid)" + % (len(cands), best_c, second_c, ntiles)) + placement = best_i + base = cands[best_i] rows = [] status = 0 if base >= 0 else 3 if base >= 0 and oracle is None: @@ -356,7 +407,7 @@ def measure(pack, tiles_wanted, ref_path=None, ref_dtype="bf16"): "pack": os.path.basename(os.path.abspath(pack)), "vocab": vocab, "hidden": hidden, "tied": tied, "head": decl, "oracle": oracle, "block_checked": decl is not None, "block_unchecked_why": unchecked, - "geometry": {"env": env, "head_off": base, "tile_bytes": tile, "tiles": ntiles, "tail_bytes": env + ntiles * tile, + "geometry": {"env": env, "head_off": base, "placement": placement, "placements": len(cands), "tile_bytes": tile, "tiles": ntiles, "tail_bytes": env + ntiles * tile, "fconst_bytes": size, "trailing_zero_bytes": zeros, "tail_fits": base >= 0, "tiles_wanted": tiles_wanted, "ref_rows": None if ref_all is None else int(ref_all.shape[0]), "ref_tiles": ref_tiles},