File size: 5,126 Bytes
4f168dc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | 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)")
|