diff --git a/tests/q4nx_tail_head_test.py b/tests/q4nx_tail_head_test.py index eaf22b4d0..787c5bd58 100755 --- a/tests/q4nx_tail_head_test.py +++ b/tests/q4nx_tail_head_test.py @@ -179,16 +179,17 @@ def synthetic(): check(all(r["finite"] == 1.0 for r in out["tiles_measured"]) and len(out["tiles_measured"]) == 2, "and its decode is reported as finite on both tiles") - # the end-anchored offset on a file that ends in padding (the 1.5B pack's .fconst ends in 110 MB of zeros): - # the count is reported, and the shifted read shows up as a non-finite decode instead of a quiet number + # a file that ends in padding (the 1.5B packs' .fconst ends in 110 MB of zeros): the count is reported AND used + # to place the grid. It used to be reported only, so the read landed inside the padding and came out non-finite -- + # the symptom engine#1693 started from, on a pack where nothing is wrong (engine#1740) pz = os.path.join(work, "padded") make_pack(pz, tied=False, head_block=T.K_KBLOCK, zeros=4096) out, status, _ = T.measure(pz, 2) check(out["geometry"]["trailing_zero_bytes"] == 4096, "trailing zero bytes are counted (%d)" % out["geometry"]["trailing_zero_bytes"]) - check(min(r["finite"] for r in out["tiles_measured"]) < 1.0, - "and a read shifted by them is visible as a non-finite decode (min finite %.3f)" - % min(r["finite"] for r in out["tiles_measured"])) + check(min(r["finite"] for r in out["tiles_measured"]) == 1.0, + "and the grid is placed BEFORE them, so a padded pack decodes finitely instead of reading its own padding " + "(min finite %.3f)" % min(r["finite"] for r in out["tiles_measured"])) # --ref as the oracle: the decode itself, written as f32 rows, must correlate 1.0 with itself with open(T.fconst(pu), "rb") as f: @@ -245,6 +246,10 @@ def main(): check(T.head_offset(291655296, 2374, 34816) + 2374 * 34816 == 291655296, "the head grid ends exactly at EOF") check(T.head_offset(291655296, 2374, 34816) != T.attn_payload_off(896), "attn_payload_off(H) alone is not the head's offset -- the mistake engine#1693 records") + check(T.head_offset(291655296, 2374, 34816, 0) == T.head_offset(291655296, 2374, 34816), + "no padding leaves the offset unchanged (the case engine#1693 measured)") + check(T.head_offset(291655296, 2374, 34816, 110208000) == T.head_offset(291655296, 2374, 34816) - 110208000, + "a trailing zero run moves the grid back by exactly its length (engine#1740's padded packs)") 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)") diff --git a/tools/q4nx_tail_head.py b/tools/q4nx_tail_head.py index 16fd1b354..56bdeea7a 100755 --- a/tools/q4nx_tail_head.py +++ b/tools/q4nx_tail_head.py @@ -97,7 +97,7 @@ def attn_payload_off(hidden): return K_NORM_OFF + hidden * 2 -def head_offset(fconst_bytes, ntiles, tile): +def head_offset(fconst_bytes, ntiles, tile, zeros=0): """Where the tied head's grid starts: the last `ntiles * tile` bytes of the `.fconst`. The importer derives the same number as `layers * stride + attn_payload_off(H)` -- past the per-layer payload, then @@ -114,7 +114,13 @@ def head_offset(fconst_bytes, ntiles, tile): correlates 1.0000 with the pack's own fp16 embedding, magnitude ratio 1.000, rel_err 0.0007 over four tiles on `Qwen2.5-0.5B-Instruct_rai_1.9.0_npu`. """ - return fconst_bytes - ntiles * tile + # engine#1740: the tail is [envelope][rows x tile][PADDING], and 12+ of the 42 1.9 packs end in a multi-hundred-MB + # all-zero run (110,208,000 B on the hidden-1536 family). Subtracting only the grid lands *inside that padding* and + # reports corr ~ 0 with a zero variance -- the symptom engine#1693 started from, on a pack where nothing is wrong. + # So the grid ends where the zero run begins; `zeros` is measured, not assumed. This is the zero-run-implied candidate + # and not a proof of placement (the head need not end exactly there): the printed correlation is what decides, and the + # importer's `plan_tail_head()` walks the neighbouring tile-aligned candidates for the cases where it does not. + return fconst_bytes - zeros - ntiles * tile def bf16_to_f32(codes): @@ -311,8 +317,8 @@ def measure(pack, tiles_wanted, ref_path=None, ref_dtype="bf16"): # WHERE the head is: the LAST `ntiles * tile` bytes. `env` on its own is only the tail's envelope -- the importer # 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). - base = head_offset(size, ntiles, tile) zeros = trailing_zeros(path) + base = head_offset(size, ntiles, tile, zeros) rows = [] status = 0 if base >= 0 else 3 if base >= 0 and oracle is None: