File size: 2,878 Bytes
279d211
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/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.")