import os import time import psutil import numpy as np import tiktoken from datasets import load_dataset, disable_caching # Prevents the datasets library from writing arrow cache shards to disk as # it streams — keeps disk usage down too, not just RAM. disable_caching() enc = tiktoken.get_encoding("gpt2") EOT = enc.eot_token os.makedirs("data", exist_ok=True) train_f = open("data/train.bin", "wb") val_f = open("data/val.bin", "wb") total_train_tokens = 0 total_val_tokens = 0 docs_processed = 0 t0 = time.time() process = psutil.Process(os.getpid()) def write_tokens(text, f): ids = enc.encode_ordinary(text) ids.append(EOT) arr = np.array(ids, dtype=np.uint16) arr.tofile(f) return len(ids) def progress(): global docs_processed, total_train_tokens if docs_processed % 5000 == 0: elapsed = time.time() - t0 ram_mb = process.memory_info().rss / (1024 * 1024) # live RSS, not peak print(f"...{docs_processed:,} docs, {total_train_tokens:,} tokens, " f"{elapsed:.1f}s elapsed, RAM: {ram_mb:.1f} MB") # Flush periodically so writes actually hit disk rather than # sitting in an OS buffer indefinitely train_f.flush() # --------------------------------------------------------------------------- # 1. WikiText-103 # --------------------------------------------------------------------------- print("=== WikiText-103 ===") wikitext = load_dataset("Salesforce/wikitext", "wikitext-103-raw-v1", split="train", streaming=True) for row in wikitext: text = row["text"].strip() if not text: continue total_train_tokens += write_tokens(text, train_f) docs_processed += 1 progress() wikitext_val = load_dataset("Salesforce/wikitext", "wikitext-103-raw-v1", split="validation", streaming=True) for row in wikitext_val: text = row["text"].strip() if not text: continue total_val_tokens += write_tokens(text, val_f) print(f"WikiText done. train_tokens: {total_train_tokens:,}, val_tokens: {total_val_tokens:,}") # --------------------------------------------------------------------------- # 2. OpenWebText — 5.5M docs # --------------------------------------------------------------------------- print("=== OpenWebText ===") OWT_DOC_LIMIT = 100 owt = load_dataset("Skylion007/openwebtext", split="train", streaming=True) owt_count = 0 for row in owt: text = row["text"].strip() if not text: continue total_train_tokens += write_tokens(text, train_f) docs_processed += 1 owt_count += 1 progress() if owt_count >= OWT_DOC_LIMIT: break print(f"OpenWebText done ({owt_count:,} docs). train_tokens: {total_train_tokens:,}") # --------------------------------------------------------------------------- # 3. the-stack-dedup (v1) — Python only, capped by token budget # --------------------------------------------------------------------------- print("=== the-stack-dedup (Python) ===") STACK_TOKEN_BUDGET = 200_000 stack = load_dataset( "bigcode/the-stack-dedup", data_dir="data/python", split="train", streaming=True, ) stack_tokens = 0 for row in stack: text = row["content"].strip() if not text: continue n = write_tokens(text, train_f) total_train_tokens += n stack_tokens += n docs_processed += 1 progress() if stack_tokens >= STACK_TOKEN_BUDGET: break print(f"Stack done ({stack_tokens:,} code tokens). train_tokens: {total_train_tokens:,}") # --------------------------------------------------------------------------- # 4. Chat seed exposure — OpenHermes 2.5 + UltraChat samples # --------------------------------------------------------------------------- print("=== OpenHermes 2.5 (sample) ===") hermes = load_dataset("teknium/OpenHermes-2.5", split="train", streaming=True) hermes_count = 0 HERMES_LIMIT = 50 for row in hermes: convo = row.get("conversations", []) text = "\n".join(f"{turn.get('from','')}: {turn.get('value','')}" for turn in convo) if not text.strip(): continue total_train_tokens += write_tokens(text, train_f) docs_processed += 1 hermes_count += 1 progress() if hermes_count >= HERMES_LIMIT: break print(f"OpenHermes done ({hermes_count:,} convos).") print("=== UltraChat (sample) ===") ultrachat = load_dataset("stingning/ultrachat", split="train", streaming=True) ultra_count = 0 ULTRA_LIMIT = 50 for row in ultrachat: turns = row.get("data", []) text = "\n".join(turns) if isinstance(turns, list) else str(turns) if not text.strip(): continue total_train_tokens += write_tokens(text, train_f) docs_processed += 1 ultra_count += 1 progress() if ultra_count >= ULTRA_LIMIT: break print(f"UltraChat done ({ultra_count:,} convos).") train_f.close() val_f.close() elapsed_total = time.time() - t0 print("=== DONE ===") print(f"Total docs processed: {docs_processed:,}") print(f"Total train tokens: {total_train_tokens:,}") print(f"Total val tokens: {total_val_tokens:,}") print(f"Total time: {elapsed_total:.1f}s ({elapsed_total/3600:.2f}h)")