Skip to content
Merged
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
15 changes: 10 additions & 5 deletions tests/q4nx_tail_head_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)")

Expand Down
12 changes: 9 additions & 3 deletions tools/q4nx_tail_head.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
Loading