gollem-v5-expand / merge_expanded.py
Maggio33's picture
Upload merge_expanded.py with huggingface_hub
39e7619 verified
Raw History Blame Contribute Delete
4.12 kB
#!/usr/bin/env python3
"""merge_expanded.py — GoLLeM-v5 expanded-corpus merge (crown-run data, 16M@expanded).
Concat (canonical uint16-LE, EOS=12285):
corpus train.bin (5,396,605,407 tok)
+ fineweb_edu_clean.bin MINUS 42,846 overlap-docs (byte-range-skip) = 2,756,248,149 tok
+ openstax_clean.bin x OS_REPEAT (35,185,551 tok x4 = 140,742,204 tok)
= expanded-train.bin ~8,293,595,760 tok (16.59 GB)
Dataloader = random-offset (train_gpt_ref.py get_batch: torch.randint over whole train.bin)
-> concat-order IRRELEVANT, NO doc-shuffle needed. Verified 2026-09-22.
Overlap byte-ranges from fineweb_overlap_docs.jsonl (bin_byte_start/end, token-aligned;
embedded-EOS doc fineweb-edu:1662841 already merged into one range).
NIE dotyka val.bin (held-out, unchanged). NIE GPU.
"""
import argparse, json, os, time
BPT = 2 # bytes/token (uint16)
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("--corpus", required=True)
ap.add_argument("--fineweb", required=True)
ap.add_argument("--openstax", required=True)
ap.add_argument("--overlap", required=True)
ap.add_argument("--out", required=True)
ap.add_argument("--os-repeat", type=int, default=4)
a = ap.parse_args()
skip = load_overlap_ranges(a.overlap)
skip_bytes = sum(e - s for s, e in skip)
log(f"overlap ranges={len(skip)} skip_bytes={skip_bytes:,} (={skip_bytes // BPT:,} tok)")
assert skip_bytes // BPT == 57241804, f"overlap tokens {skip_bytes // BPT} != 57,241,804 expected"
t0 = time.time()
with open(a.out, "wb", buffering=64 * 1024 * 1024) as out:
n_corpus = copy_whole(a.corpus, out)
log(f"corpus {n_corpus:,}B ({n_corpus // BPT:,} tok) {time.time() - t0:.0f}s")
n_fw = copy_skip(a.fineweb, out, skip)
log(f"fineweb-dedup {n_fw:,}B ({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"openstax {r + 1}/{a.os_repeat} {n:,}B {time.time() - t0:.0f}s")
total = n_corpus + n_fw + n_os
exp_fw = os.path.getsize(a.fineweb) - skip_bytes
assert n_fw == exp_fw, f"fineweb mismatch {n_fw} != {exp_fw}"
assert n_corpus // BPT == 5396605407, f"corpus tok {n_corpus // BPT} != 5,396,605,407"
assert n_fw // BPT == 2756248149, f"fineweb-dedup tok {n_fw // BPT} != 2,756,248,149"
log(f"VALIDATION OK: corpus={n_corpus // BPT:,} fineweb_dedup={n_fw // BPT:,} "
f"openstax_x{a.os_repeat}={n_os // BPT:,} TOTAL={total // BPT:,} tok ({total:,}B)")
log(f"DONE -> {a.out}")
if __name__ == "__main__":
main()