#!/usr/bin/env python3 """Integrity checks for each stage of the pipeline. python verify.py u16 data_u16 # offsets contiguous, sizes match, sample replay legal python verify.py factorized data_3 # BOS..EOS per game, sidecar aligned, piece/replay consistent python verify.py packed data_3p # record/sidecar counts, BOS/EOS balance, game count python verify.py roundtrip data_u16 original_2b # byte-identical moves.u16 vs. an original tier-2b copy Each prints one line per month/shard and exits non-zero on the first failure. """ import argparse import filecmp import json import os import sys from pathlib import Path import numpy as np import pyarrow.parquet as pq from chsm8_format import KIND_BOS, KIND_EOS, KIND_MOVE, KIND_PAD, LEGAL_DTYPE, code_to_uci def months(root): return sorted(m for m in os.listdir(root) if os.path.isfile(os.path.join(root, m, "meta.parquet"))) def fail(msg): print("FAIL:", msg) sys.exit(1) def check_u16(root, sample): import chess for m in months(root): meta = pq.read_table(os.path.join(root, m, "meta.parquet"), columns=["move_off", "n_plies"]) off, n = meta["move_off"].to_numpy().astype(np.int64), meta["n_plies"].to_numpy().astype(np.int64) size = os.path.getsize(os.path.join(root, m, "moves.u16")) // 2 if len(off) and (off[0] != 0 or (off[1:] != off[:-1] + n[:-1]).any() or off[-1] + n[-1] != size): fail(f"{m}: offsets not contiguous / size mismatch") mv = np.memmap(os.path.join(root, m, "moves.u16"), dtype=" last_eos[:, None] if (k[tail] != KIND_PAD).any(): fail(f"{p}: game split across records") records += len(r); games += nb if records != info["n_rows"] or games != info["n_games"]: fail(f"{split}: counted {records:,} records/{games:,} games, _SUCCESS says " f"{info['n_rows']:,}/{info['n_games']:,}") print(f"[{split}] ok: {records:,} records, {games:,} games, seq_len={info['seq_len']}, legal={info['has_legal']}") def check_roundtrip(a, b): for m in months(a): if not filecmp.cmp(os.path.join(a, m, "moves.u16"), os.path.join(b, m, "moves.u16"), shallow=False): fail(f"{m}: moves.u16 differs") print(f"[{m}] moves.u16 identical") def main(): ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) ap.add_argument("stage", choices=["u16", "factorized", "packed", "roundtrip"]) ap.add_argument("path") ap.add_argument("other", nargs="?") ap.add_argument("--sample", type=int, default=200, help="games replayed with python-chess per month (u16)") a = ap.parse_args() {"u16": lambda: check_u16(a.path, a.sample), "factorized": lambda: check_factorized(a.path), "packed": lambda: check_packed(a.path), "roundtrip": lambda: check_roundtrip(a.path, a.other)}[a.stage]() print("ALL OK") if __name__ == "__main__": main()