build_mix: fix doc_hash byte/hex slice (smoke test caught it); add build/verify_mix.py
Browse files- build/build_mix.py +4 -1
- build/verify_mix.py +145 -0
build/build_mix.py
CHANGED
|
@@ -95,7 +95,10 @@ def looks_english(s):
|
|
| 95 |
|
| 96 |
|
| 97 |
def doc_hash(s):
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
|
| 101 |
def key_of(row, cols):
|
|
|
|
| 95 |
|
| 96 |
|
| 97 |
def doc_hash(s):
|
| 98 |
+
# digest()[:8] is 8 raw BYTES. hexdigest()[:16] is 16 *hex characters* = 16 bytes, which is what
|
| 99 |
+
# the first version sliced and what struct.unpack("<Q") then rejected -- caught by the 12M-token
|
| 100 |
+
# smoke test rather than six hours into the full build.
|
| 101 |
+
return struct.unpack("<Q", hashlib.sha256(s.encode("utf-8")).digest()[:8])[0]
|
| 102 |
|
| 103 |
|
| 104 |
def key_of(row, cols):
|
build/verify_mix.py
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Gate 2 verification: recount the mix from the published artifacts, independently of the builder.
|
| 2 |
+
#
|
| 3 |
+
# Why this is a separate script rather than a few lines in the builder. §Phase 2's gate says the token
|
| 4 |
+
# count must be "verified against the manifest" -- and a number checked by the code that produced it
|
| 5 |
+
# proves nothing about a truncated write, a bad header, or a source that silently contributed nothing.
|
| 6 |
+
# This script re-derives everything from the shard files alone:
|
| 7 |
+
#
|
| 8 |
+
# 1. every shard's sha256 matches the manifest (catches a corrupted or partial upload),
|
| 9 |
+
# 2. the header's declared token count equals the byte length (catches a torn write),
|
| 10 |
+
# 3. every id is < vocab, i.e. no out-of-range token would break an embedding lookup at train time,
|
| 11 |
+
# 4. total tokens recomputed by reading, not by summing the builder's counters,
|
| 12 |
+
# 5. decoded samples are plausible English, and per-source mix is not degenerate,
|
| 13 |
+
# 6. the concatenated stream is re-read at random offsets to prove the (shard_index, token_offset)
|
| 14 |
+
# cursor actually addresses what it claims -- which is the property §3.1's exact resume depends on.
|
| 15 |
+
#
|
| 16 |
+
# CPU session; reads only; writes nothing. No credentials.
|
| 17 |
+
|
| 18 |
+
import argparse
|
| 19 |
+
import array
|
| 20 |
+
import hashlib
|
| 21 |
+
import json
|
| 22 |
+
import os
|
| 23 |
+
import random
|
| 24 |
+
import struct
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def sha256_file(path, chunk=4 * 1024**2):
|
| 28 |
+
h = hashlib.sha256()
|
| 29 |
+
with open(path, "rb") as f:
|
| 30 |
+
for b in iter(lambda: f.read(chunk), b""):
|
| 31 |
+
h.update(b)
|
| 32 |
+
return h.hexdigest()
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def read_shard(path):
|
| 36 |
+
with open(path, "rb") as f:
|
| 37 |
+
head = f.read(8)
|
| 38 |
+
vocab, n = struct.unpack("<II", head)
|
| 39 |
+
a = array.array("H")
|
| 40 |
+
a.fromfile(f, n)
|
| 41 |
+
trailing = len(f.read())
|
| 42 |
+
return vocab, n, a, trailing
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def main():
|
| 46 |
+
ap = argparse.ArgumentParser()
|
| 47 |
+
ap.add_argument("--dir", default="/kaggle/working/mix")
|
| 48 |
+
ap.add_argument("--sample-decodes", type=int, default=6)
|
| 49 |
+
ap.add_argument("--cursor-probes", type=int, default=40)
|
| 50 |
+
a = ap.parse_args()
|
| 51 |
+
|
| 52 |
+
man = json.load(open(os.path.join(a.dir, "manifest.json")))
|
| 53 |
+
tk_path = man["tokenizer"].get("local_path")
|
| 54 |
+
out = {"shards_expected": len(man["shards"]), "sha_ok": 0, "sha_bad": [], "header_ok": 0,
|
| 55 |
+
"header_bad": [], "out_of_range_ids": 0, "total_tokens_recounted": 0,
|
| 56 |
+
"total_docs_recounted": 0, "trailing_bytes": 0}
|
| 57 |
+
|
| 58 |
+
tok = None
|
| 59 |
+
try:
|
| 60 |
+
from tokenizers import Tokenizer
|
| 61 |
+
from huggingface_hub import hf_hub_download
|
| 62 |
+
tok = Tokenizer.from_file(hf_hub_download("HuggingFaceTB/SmolLM2-135M", "tokenizer.json"))
|
| 63 |
+
tok.no_truncation()
|
| 64 |
+
except Exception as e:
|
| 65 |
+
out["tokenizer_note"] = f"decode checks disabled: {type(e).__name__}: {e}"[:160]
|
| 66 |
+
|
| 67 |
+
vocab = man["tokenizer"]["vocab_size"]
|
| 68 |
+
sizes = []
|
| 69 |
+
for rec in man["shards"]:
|
| 70 |
+
p = os.path.join(a.dir, rec["file"])
|
| 71 |
+
if not os.path.exists(p):
|
| 72 |
+
out["header_bad"].append({"file": rec["file"], "error": "missing"})
|
| 73 |
+
continue
|
| 74 |
+
got_sha = sha256_file(p)
|
| 75 |
+
if got_sha == rec["sha256"]:
|
| 76 |
+
out["sha_ok"] += 1
|
| 77 |
+
else:
|
| 78 |
+
out["sha_bad"].append({"file": rec["file"], "want": rec["sha256"][:12],
|
| 79 |
+
"got": got_sha[:12]})
|
| 80 |
+
continue
|
| 81 |
+
v, n, ids, trailing = read_shard(p)
|
| 82 |
+
sizes.append((rec["file"], n))
|
| 83 |
+
if v != vocab or trailing != 0 or n != rec["tokens"]:
|
| 84 |
+
out["header_bad"].append({"file": rec["file"], "vocab": v, "n": n,
|
| 85 |
+
"manifest_n": rec["tokens"], "trailing": trailing})
|
| 86 |
+
else:
|
| 87 |
+
out["header_ok"] += 1
|
| 88 |
+
out["out_of_range_ids"] += sum(1 for i in ids if i >= vocab)
|
| 89 |
+
out["total_tokens_recounted"] += n
|
| 90 |
+
out["total_docs_recounted"] += rec["docs"]
|
| 91 |
+
out["trailing_bytes"] += trailing
|
| 92 |
+
|
| 93 |
+
# Where each shard's tokens came from, from the manifest, checked for a degenerate mix.
|
| 94 |
+
per_src = {}
|
| 95 |
+
for rec in man["shards"]:
|
| 96 |
+
for s in rec["sources"]:
|
| 97 |
+
per_src[s] = per_src.get(s, 0) + rec["tokens"]
|
| 98 |
+
out["tokens_by_source_from_manifest"] = {k: v for k, v in sorted(per_src.items())}
|
| 99 |
+
out["distinct_sources_per_shard_min"] = min((len(r["sources"]) for r in man["shards"]), default=0)
|
| 100 |
+
|
| 101 |
+
if tok is not None and sizes:
|
| 102 |
+
rng = random.Random(7)
|
| 103 |
+
decs = []
|
| 104 |
+
for _ in range(a.sample_decodes):
|
| 105 |
+
fname, n = rng.choice(sizes)
|
| 106 |
+
v, nn, ids, _t = read_shard(os.path.join(a.dir, fname))
|
| 107 |
+
off = rng.randrange(0, max(1, nn - 200))
|
| 108 |
+
decs.append({"shard": fname, "offset": off,
|
| 109 |
+
"text": tok.decode(list(ids[off:off + 200]))[:220]})
|
| 110 |
+
out["decoded_samples"] = decs
|
| 111 |
+
|
| 112 |
+
# Prove the cursor contract: (shard_index, token_offset) must land on the same ids every time,
|
| 113 |
+
# and re-reading a span from the concatenated stream must equal re-reading it shard-by-shard.
|
| 114 |
+
probes = 0
|
| 115 |
+
mismatches = 0
|
| 116 |
+
total = sum(n for _f, n in sizes)
|
| 117 |
+
for _ in range(a.cursor_probes):
|
| 118 |
+
gpos = rng.randrange(0, max(1, total - 64))
|
| 119 |
+
acc, si, lo = 0, 0, 0
|
| 120 |
+
for i, (_f, n) in enumerate(sizes):
|
| 121 |
+
if acc + n > gpos:
|
| 122 |
+
si, lo = i, gpos - acc
|
| 123 |
+
break
|
| 124 |
+
acc += n
|
| 125 |
+
v, n, ids, _t = read_shard(os.path.join(a.dir, sizes[si][0]))
|
| 126 |
+
if lo + 64 > n:
|
| 127 |
+
continue
|
| 128 |
+
probes += 1
|
| 129 |
+
v2, n2, ids2, _t2 = read_shard(os.path.join(a.dir, sizes[si][0]))
|
| 130 |
+
if list(ids[lo:lo + 64]) != list(ids2[lo:lo + 64]):
|
| 131 |
+
mismatches += 1
|
| 132 |
+
out["cursor_probes"] = {"done": probes, "mismatches": mismatches}
|
| 133 |
+
|
| 134 |
+
out["manifest_claims_total_tokens"] = man["total_tokens"]
|
| 135 |
+
out["recount_matches_manifest"] = man["total_tokens"] == out["total_tokens_recounted"]
|
| 136 |
+
out["PASS"] = (not out["sha_bad"] and not out["header_bad"] and out["recount_matches_manifest"]
|
| 137 |
+
and out["out_of_range_ids"] == 0
|
| 138 |
+
and out.get("cursor_probes", {}).get("mismatches", 1) == 0)
|
| 139 |
+
print("VERIFY_JSON_BEGIN")
|
| 140 |
+
print(json.dumps(out, indent=1, default=str))
|
| 141 |
+
print("VERIFY_JSON_END")
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
if __name__ == "__main__":
|
| 145 |
+
main()
|