tte-cmix-residual-blocks / smoke_test.py
dfreelan's picture
Upload smoke_test.py with huggingface_hub
279d211 verified
Raw
History Blame
2.88 kB
#!/usr/bin/env python3
"""
SMOKE TEST -- run this on every box after pulling, BEFORE burning GPU.
Reproduces cmix's bits/byte on one block from your downloaded shards and compares
it to the anchor recorded in the manifest (which was cross-checked against the
trace's own authoritative per-byte cost column at build time).
python3 smoke_test.py <root> [block_id] e.g. python3 smoke_test.py . b005
Exits nonzero on any mismatch.
"""
import sys, json, hashlib, numpy as np
root = sys.argv[1] if len(sys.argv) > 1 else "."
bid = sys.argv[2] if len(sys.argv) > 2 else "b005"
M = json.load(open(f"{root}/manifest.json"))
B = next(b for b in M["blocks"] if b["id"] == bid)
n, BB, d = M["block_bits"], M["block_bytes"], f"{root}/blocks/{bid}"
fail = []
# 1. sha256 of every shard we actually have
for fn, exp in B["files"].items():
try:
h = hashlib.sha256()
with open(f"{d}/{fn}", "rb") as f:
for c in iter(lambda: f.read(1 << 22), b""):
h.update(c)
got = h.hexdigest()
except FileNotFoundError:
print(f" - {fn}: not downloaded (ok if your arm doesn't need it)")
continue
ok = got == exp["sha256"]
print(f" {'OK ' if ok else 'BAD'} {fn} {exp['bytes']:>11} B sha256 {got[:16]}…")
if not ok:
fail.append(f"{fn} sha256 mismatch -- CORRUPT DOWNLOAD, re-pull")
# 2. reproduce cmix's bits/byte from final_p + labels
fp = np.fromfile(f"{d}/final_p_u16.bin", np.uint16).astype(np.float64)
fl = np.fromfile(f"{d}/labels_u8.bin", np.uint8)
y = (fl & 1).astype(np.float64)
p = np.clip(fp / 65536.0, 1e-9, 1 - 1e-9)
bpb = float(np.sum(-(y*np.log2(p) + (1-y)*np.log2(1-p))) / BB)
exp = B["cmix_bits_per_byte"]
print(f"\ncmix bits/byte on {bid}: got {bpb:.6f} manifest anchor {exp:.6f} (full corpus: 1.484123)")
if abs(bpb - exp) > 1e-3:
fail.append(f"bits/byte {bpb:.6f} != anchor {exp:.6f}")
# 3. labels must be the MSB-first bits of bytes_ctx's block half
try:
bc = np.fromfile(f"{d}/bytes_ctx.bin", np.uint8)
if np.array_equal(np.unpackbits(bc[M["ctx_bytes"]:]), (fl & 1)):
print("labels == MSB-first bits of bytes_ctx block half: OK")
else:
fail.append("labels do not match bytes_ctx (bit order / alignment wrong)")
except FileNotFoundError:
pass
# 4. int8 experts sane
try:
X = np.fromfile(f"{d}/experts_i8.bin", np.int8)
assert X.size == n * 25, f"experts_i8 size {X.size} != {n*25}"
S = np.array(M["int8_scales"], np.float32)
Xs = X.reshape(n, 25)[:100000] * S
print(f"experts stretch range on 100k rows: [{Xs.min():.3f}, {Xs.max():.3f}] (expect within +/-16)")
except FileNotFoundError:
print(" - experts_i8.bin not downloaded (ok for byte-LM arms)")
print()
if fail:
print("SMOKE TEST FAILED:")
[print(" !!", f) for f in fail]
sys.exit(1)
print("SMOKE TEST PASSED -- your pull is intact.")