#!/usr/bin/env python3 """Optional: globally shuffle packed training records (tier 3p -> 3p-shuffled). ``pack.py`` output is in chronological order (2013 -> 2026). Reading it sequentially is an unintended old-to-new curriculum; reading it randomly is slow on network filesystems. This external bucket shuffle gives both: it scans the source sequentially, hashes every record into one of ``--buckets`` files, then permutes each bucket in memory. Every output shard is a uniform random sample of the whole corpus, so a trainer can read shards sequentially. Records and their occ/pmask sidecars always move together. Validation is not shuffled; it is copied (or symlinked with --link-val) unchanged. python shuffle.py data_3p -o data_3p_shuffled --buckets 128 --seed 42 python shuffle.py data_3p -o data_3p_shuffled --stage /fast/tmp/stage # staging on a faster disk Disk: needs one extra copy of the training split for staging (deleted at the end unless --keep-stage). """ import argparse import json import os import shutil from pathlib import Path import numpy as np def splitmix64(x): x = x.astype(np.uint64, copy=False) + np.uint64(0x9E3779B97F4A7C15) x = (x ^ (x >> np.uint64(30))) * np.uint64(0xBF58476D1CE4E5B9) x = (x ^ (x >> np.uint64(27))) * np.uint64(0x94D049BB133111EB) return x ^ (x >> np.uint64(31)) def sidecars(path: Path): return path.with_name(path.stem + "_occ.bin"), path.with_name(path.stem + "_pmask.bin") def main(): ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) ap.add_argument("indir", help="packed directory from pack.py (has train/ and val/)") ap.add_argument("-o", "--outdir", required=True) ap.add_argument("--buckets", type=int, default=128, help="number of output shards") ap.add_argument("--seed", type=int, default=42) ap.add_argument("--stage", help="staging directory (default: /.stage)") ap.add_argument("--chunk-records", type=int, default=16384) ap.add_argument("--keep-stage", action="store_true") ap.add_argument("--link-val", action="store_true", help="symlink val/ instead of copying it") a = ap.parse_args() src = Path(a.indir) info = json.loads((src / "train" / "_SUCCESS").read_text()) cap, legal = info["seq_len"] + 1, info["has_legal"] rec_bytes = [cap * 5 * 2] + ([cap * 8, cap] if legal else []) dtypes = [("