nanoglimmer / training /data /extend_data_v2.py
apullin's picture
nanoGlimmer: checkpoint, tokenizer, model card and training code
6b665d1 verified
Raw History Blame Contribute Delete
2.8 kB
"""Append TinyStoriesV2 (GPT-4-only regeneration) to the 8k training bin.
V2 is a fresh generation, disjoint text from V1, so the frozen val set
(carved from V1's slice) stays clean by construction -- no boundary
gymnastics needed, unlike extend_data.py. Output: train_v1v2_v8192.bin =
train_full_v8192.bin (all of V1) + every V2 story, tokenized with the same
bpe8192. One epoch of it is ~2x the unique tokens of the properly-fed runs.
"""
import os
import numpy as np
import requests
from tokenizers import Tokenizer
HERE = os.path.dirname(os.path.abspath(__file__))
URL = ("https://huggingface.co/datasets/roneneldan/TinyStories/resolve/main/"
"TinyStoriesV2-GPT4-train.txt")
V2 = os.path.join(HERE, "tinystories_v2.txt")
BASE = os.path.join(HERE, "train_full_v8192.bin")
OUT = os.path.join(HERE, "train_v1v2_v8192.bin")
EOT = "<|endoftext|>"
def download():
if os.path.exists(V2) and os.path.getsize(V2) > 2_000_000_000:
print(f"already have {V2} ({os.path.getsize(V2) / 1e9:.2f}GB)")
return
print("downloading TinyStoriesV2-GPT4-train.txt...")
got = 0
with requests.get(URL, stream=True, timeout=60) as r:
r.raise_for_status()
with open(V2, "wb") as f:
for chunk in r.iter_content(chunk_size=1 << 20):
f.write(chunk)
got += len(chunk)
if got % (200 << 20) < (1 << 20):
print(f" {got / 1e9:.2f}GB", flush=True)
print(f"done, {got / 1e9:.2f}GB")
def main():
if os.path.exists(OUT) and os.path.getsize(OUT) > os.path.getsize(BASE):
print(f"already have {OUT}")
return
download()
with open(V2, "r", encoding="utf-8", errors="ignore") as f:
text = f.read()
text = text[: text.rfind(EOT) + len(EOT)]
print(f"V2 text: {len(text) / 1e9:.2f}GB")
tok = Tokenizer.from_file(os.path.join(HERE, "bpe8192.json"))
eot = tok.token_to_id(EOT)
base = np.fromfile(BASE, dtype=np.uint16)
n_new = 0
with open(OUT, "wb") as out:
base.tofile(out)
docs = text.split(EOT)
for i in range(0, len(docs), 20000):
batch = [d for d in docs[i : i + 20000] if d.strip()]
ids = []
for enc in tok.encode_batch(batch):
ids.extend(enc.ids)
ids.append(eot)
arr = np.array(ids, dtype=np.uint16)
assert arr.max() < 8192
arr.tofile(out)
n_new += len(arr)
print(f" {i + len(batch)}/{len(docs)} docs, +{n_new / 1e6:.1f}M tokens",
flush=True)
total = len(base) + n_new
print(f"train_v1v2: {len(base):,} V1 + {n_new:,} V2 = {total:,} tokens")
print(f"one epoch at bs16 sl256 = {total // 4096:,} steps")
if __name__ == "__main__":
main()