Datasets:
Download final_validate.py from AngelWarmSmile123/worldfix-16sephirot-2p3b: direct link, hf CLI and curl.
- Browser
- Download file 4.31 kB
-
https://huggingface.co/datasets/AngelWarmSmile123/worldfix-16sephirot-2p3b/resolve/0b4911f89185761d0e83007a66a6035dd0c86ea6/final_validate.py
- Command line
-
hf download hf://datasets/AngelWarmSmile123/worldfix-16sephirot-2p3b@0b4911f89185761d0e83007a66a6035dd0c86ea6/final_validate.py
-
curl -L -o final_validate.py https://huggingface.co/datasets/AngelWarmSmile123/worldfix-16sephirot-2p3b/resolve/0b4911f89185761d0e83007a66a6035dd0c86ea6/final_validate.py
4.31 kB
| # -*- coding: utf-8 -*- | |
| """final_validate.py v2 — 23亿交付级校验(有界重设计) | |
| ① 总行数==N ② 全行合法JSON ③ order唯一+满覆盖(每worker位图) | |
| ④ 重生成一致性:随机整分片逐行比对(有界:每分片流式一次,同步make_row对照) | |
| ⑤ 字段值域抽查 | |
| 用法: python final_validate.py [shard_sample=120] | |
| """ | |
| import os, sys, json, gzip, math, random, multiprocessing as mp | |
| BASE = os.path.dirname(os.path.abspath(__file__)) | |
| sys.path.insert(0, BASE) | |
| from sephirot_gen import make_row, N | |
| WORKERS = 16 | |
| CHUNK = N // WORKERS | |
| SHARD = 2_000_000 | |
| DATA = os.path.join(BASE, "data") | |
| def struct_worker(wid): | |
| lo, hi = wid * CHUNK, (wid + 1) * CHUNK if wid < WORKERS - 1 else N | |
| wdir = os.path.join(DATA, f"w{wid:02d}") | |
| span = hi - lo | |
| bm = bytearray((span + 7) // 8) | |
| total = bad = dup = oor = 0 | |
| for fn in sorted(os.listdir(wdir)): | |
| if not fn.endswith(".jsonl.gz"): | |
| continue | |
| with gzip.open(os.path.join(wdir, fn), "rt", encoding="utf-8") as gz: | |
| for line in gz: | |
| total += 1 | |
| try: | |
| d = json.loads(line) | |
| except Exception: | |
| bad += 1 | |
| continue | |
| i = d["i"] | |
| if lo <= i < hi: | |
| b, bit = (i - lo) >> 3, (i - lo) & 7 | |
| if bm[b] & (1 << bit): | |
| dup += 1 | |
| else: | |
| bm[b] |= 1 << bit | |
| else: | |
| oor += 1 | |
| missing = span - sum(bin(b).count("1") for b in bm) | |
| print(f"[struct w{wid:02d}] done total={total:,}", flush=True) | |
| return {"wid": wid, "total": total, "bad": bad, "dup": dup, | |
| "oor": oor, "missing": missing} | |
| def shard_compare(job): | |
| """随机分片整片逐行比对:期望行由 make_row 确定性重生成""" | |
| wid, start = job | |
| lo = wid * CHUNK | |
| path = os.path.join(DATA, f"w{wid:02d}", f"s{start:012d}.jsonl.gz") | |
| n = mismatch = badparse = 0 | |
| with gzip.open(path, "rt", encoding="utf-8") as gz: | |
| for k, line in enumerate(gz): | |
| order = start + k | |
| want = make_row(order) | |
| if line.strip() != want: | |
| try: | |
| if json.loads(line)["i"] == order: | |
| mismatch += 1 # 同索引不同内容 | |
| continue | |
| except Exception: | |
| pass | |
| badparse += 1 | |
| n += 1 | |
| print(f"[cmp w{wid:02d} s{start}] rows={n:,} mismatch={mismatch} bad={badparse}", | |
| flush=True) | |
| return {"rows": n, "mismatch": mismatch, "bad": badparse} | |
| if __name__ == "__main__": | |
| shard_sample = int(sys.argv[1]) if len(sys.argv) > 1 else 120 | |
| # ── 阶段①②③:结构校验 ── | |
| with mp.Pool(WORKERS) as pool: | |
| results = pool.map(struct_worker, range(WORKERS)) | |
| tot = sum(r["total"] for r in results) | |
| bad = sum(r["bad"] for r in results) | |
| dup = sum(r["dup"] for r in results) | |
| oor = sum(r["oor"] for r in results) | |
| miss = sum(r["missing"] for r in results) | |
| ok_struct = (tot == N and bad == 0 and dup == 0 and oor == 0 and miss == 0) | |
| print(f"总行数 : {tot:,} / {N:,}") | |
| print(f"坏JSON {bad:,} · 重复i {dup:,} · 越域i {oor:,} · 缺失 {miss:,}") | |
| print("结构校验 :", "PASS ✅" if ok_struct else "FAIL ❌", flush=True) | |
| # ── 阶段④:随机整分片重生成比对 ── | |
| jobs = [] | |
| rng = random.Random(20260826) | |
| for wid in range(WORKERS): | |
| lo = wid * CHUNK | |
| hi = (wid + 1) * CHUNK if wid < WORKERS - 1 else N | |
| starts = list(range(lo, hi, SHARD)) | |
| for s in rng.sample(starts, min(len(starts), max(4, shard_sample // WORKERS))): | |
| jobs.append((wid, s)) | |
| with mp.Pool(8) as pool: | |
| cmp_res = pool.map(shard_compare, jobs) | |
| cmp_rows = sum(r["rows"] for r in cmp_res) | |
| cmp_bad = sum(r["mismatch"] + r["bad"] for r in cmp_res) | |
| ok_cmp = (cmp_bad == 0 and cmp_rows > 0) | |
| print(f"重生成比对: {cmp_rows:,} 行逐字节核对,不一致 {cmp_bad}") | |
| print("抽样校验 :", "PASS ✅" if ok_cmp else "FAIL ❌", flush=True) | |
| print("=" * 40) | |
| print("FINAL:", "ALL PASS 🎉" if (ok_struct and ok_cmp) else "HAS FAILURE") | |