#!/usr/bin/env python3 """merge_append.py — APPEND fineweb-dedup + openstax×N to EXISTING train.bin IN-PLACE. For disk-constrained pods (no room for a separate 16.6GB output alongside 10.8GB corpus). train.bin (corpus, 5,396,605,407 tok) -> expanded (8,293,595,760 tok) in-place. Original corpus recoverable from HF SlayerLab/minimal-en-corpus-5b. Idempotency-guard: refuses to append unless target is EXACTLY the pristine corpus size. Dataloader = random-offset (train_gpt_ref.py torch.randint) -> append-order irrelevant, NO shuffle. uint16-LE EOS=12285. NIE dotyka val.bin. NIE GPU. """ import argparse, json, os, time BPT = 2 CORPUS_TOK = 5396605407 OVERLAP_TOK = 57241804 FINAL_TOK = 8293595760 # corpus + fineweb-dedup(2,756,248,149) + openstax x4(140,742,204) def log(m): print(f"[{time.strftime('%H:%M:%S')}] {m}", flush=True) def load_overlap_ranges(jsonl): ranges = [] with open(jsonl, encoding="utf-8") as f: for line in f: line = line.strip() if not line: continue d = json.loads(line) if "_meta" in d or "bin_byte_start" not in d: continue ranges.append((int(d["bin_byte_start"]), int(d["bin_byte_end"]))) ranges.sort() merged = [] for s, e in ranges: if merged and s <= merged[-1][1]: merged[-1] = (merged[-1][0], max(merged[-1][1], e)) else: merged.append((s, e)) return merged def copy_whole(src, out, buf=64 * 1024 * 1024): n = 0 with open(src, "rb") as f: while True: b = f.read(buf) if not b: break out.write(b); n += len(b) return n def copy_skip(src, out, skip_ranges, buf=64 * 1024 * 1024): written = 0; pos = 0 size = os.path.getsize(src) with open(src, "rb") as f: for s, e in skip_ranges: while pos < s: b = f.read(min(buf, s - pos)) if not b: break out.write(b); written += len(b); pos += len(b) f.seek(e); pos = e while pos < size: b = f.read(buf) if not b: break out.write(b); written += len(b); pos += len(b) return written def main(): ap = argparse.ArgumentParser() ap.add_argument("--target", required=True, help="train.bin appended IN-PLACE") ap.add_argument("--fineweb", required=True) ap.add_argument("--openstax", required=True) ap.add_argument("--overlap", required=True) ap.add_argument("--os-repeat", type=int, default=4) a = ap.parse_args() cur = os.path.getsize(a.target) log(f"target current: {cur:,}B ({cur // BPT:,} tok)") if cur // BPT == FINAL_TOK: log("target ALREADY expanded (== FINAL_TOK). Nothing to do. EXIT ok.") return assert cur // BPT == CORPUS_TOK, ( f"target {cur // BPT:,} != pristine corpus {CORPUS_TOK:,} — appended/wrong file? ABORT (re-pull corpus from HF)") skip = load_overlap_ranges(a.overlap) skip_bytes = sum(e - s for s, e in skip) assert skip_bytes // BPT == OVERLAP_TOK, f"overlap {skip_bytes // BPT} != {OVERLAP_TOK}" log(f"overlap ranges={len(skip)} skip={skip_bytes // BPT:,} tok") t0 = time.time() with open(a.target, "ab", buffering=64 * 1024 * 1024) as out: n_fw = copy_skip(a.fineweb, out, skip) log(f"appended fineweb-dedup {n_fw // BPT:,} tok {time.time() - t0:.0f}s") n_os = 0 for r in range(a.os_repeat): n = copy_whole(a.openstax, out); n_os += n log(f"appended openstax {r + 1}/{a.os_repeat} {time.time() - t0:.0f}s") final = os.path.getsize(a.target) exp_fw = os.path.getsize(a.fineweb) - skip_bytes assert n_fw == exp_fw, f"fineweb mismatch {n_fw} != {exp_fw}" assert final // BPT == FINAL_TOK, f"FINAL {final // BPT:,} != {FINAL_TOK:,}" log(f"VALIDATION OK: target now {final // BPT:,} tok ({final:,}B) " f"= corpus {CORPUS_TOK:,} + fineweb {n_fw // BPT:,} + openstax_x{a.os_repeat} {n_os // BPT:,}") log("DONE (in-place expanded)") if __name__ == "__main__": main()