File size: 12,404 Bytes
a8f07a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
"""Download and prepare English text data for training.

Datasets (via HF datasets):
  - wikitext2:      ~2M tokens (smoke test)
  - wikitext103:    ~103M tokens (Wikipedia articles)
  - mixture250m:    ~255M tokens β€” FineWeb-Edu + Cosmopedia + WikiText-103

All text is English only. Tokenized with tiktoken GPT-2 BPE (50257 vocab).
Cached as .pt files for fast loading.
"""
import os
import torch
import tiktoken

CACHE_DIR = "data"


def _tokenize_stream(rows, target, enc, name):
    """Tokenize text rows until target tokens collected. Returns [N] tensor."""
    chunks = []
    total = 0
    batch = []
    for t in rows:
        if not t or not t.strip():
            continue
        batch.append(t)
        if len(batch) >= 256:
            ids = enc.encode_ordinary("\n".join(batch))
            chunks.append(torch.tensor(ids, dtype=torch.long))
            total += len(ids)
            if total % 20_000_000 < 3_000_000:
                print(f"  {name}: {total/1e6:.0f}M tokens...", flush=True)
            if total >= target:
                break
            batch = []
    if batch and total < target:
        ids = enc.encode_ordinary("\n".join(batch))
        chunks.append(torch.tensor(ids, dtype=torch.long))
        total += len(ids)
    print(f"  {name}: done β€” {total:,} tokens")
    return torch.cat(chunks)[:target] if chunks else torch.empty(0, dtype=torch.long)


def prepare_wikitext103(target_tokens: int = 80_000_000) -> str:
    """Download wikitext-103 via HF datasets, tokenize, cache. Returns .pt path."""
    cache_path = os.path.join(CACHE_DIR, "wikitext103_tokens.pt")
    if os.path.exists(cache_path):
        tokens = torch.load(cache_path)
        print(f"wikitext103: cached {tokens.numel():,} tokens")
        return cache_path

    from datasets import load_dataset
    print("wikitext103: downloading via HF datasets...")
    ds = load_dataset("Salesforce/wikitext", "wikitext-103-raw-v1", split="train")
    print(f"  {len(ds):,} rows")

    enc = tiktoken.get_encoding("gpt2")
    os.makedirs(CACHE_DIR, exist_ok=True)
    tokens = _tokenize_stream((r["text"] for r in ds), target_tokens, enc, "wikitext103")
    torch.save(tokens, cache_path)
    print(f"  saved to {cache_path}")
    return cache_path


def prepare_wikitext2() -> str:
    """Download wikitext-2 (small smoke test corpus)."""
    cache_path = os.path.join(CACHE_DIR, "wikitext2_tokens.pt")
    if os.path.exists(cache_path):
        tokens = torch.load(cache_path)
        print(f"wikitext2: cached {tokens.numel():,} tokens")
        return cache_path

    import subprocess
    os.makedirs(CACHE_DIR, exist_ok=True)
    txt_path = os.path.join(CACHE_DIR, "wikitext2.txt")
    if not os.path.exists(txt_path):
        url = ("https://raw.githubusercontent.com/pytorch/examples/main/"
               "word_language_model/data/wikitext-2/train.txt")
        subprocess.run(["curl", "-sL", "-o", txt_path, url], check=True)
    with open(txt_path, "r", encoding="utf-8", errors="ignore") as f:
        text = f.read()
    enc = tiktoken.get_encoding("gpt2")
    tokens = torch.tensor(enc.encode_ordinary(text), dtype=torch.long)
    print(f"wikitext2: {tokens.numel():,} tokens")
    torch.save(tokens, cache_path)
    return cache_path


def _stream_hf(repo, config, split, text_field, target, enc, name,
               skip_docs=0):
    """Stream an HF dataset, return token tensor (empty on failure).
    skip_docs: drop the first N documents (fresh data past what the
    model already trained on)."""
    from datasets import load_dataset
    try:
        print(f"{name}: streaming {repo} / {config} (skip {skip_docs:,})...")
        ds = load_dataset(repo, config, split=split, streaming=True)
        it = (r.get(text_field, "") for r in ds)
        if skip_docs:
            import itertools
            it = itertools.islice(it, skip_docs, None)
        return _tokenize_stream(it, target, enc, name)
    except Exception as e:
        print(f"  {name} FAILED: {type(e).__name__}: {e}")
        return torch.empty(0, dtype=torch.long)


def prepare_mixture250m() -> str:
    """~255M token English mixture: FineWeb-Edu + Cosmopedia + WikiText-103.

    FineWeb-Edu: educationally filtered web text β€” highest quality per token.
    Cosmopedia: synthetic textbooks β€” clean structured English.
    WikiText-103: Wikipedia articles (reuses existing cache).
    """
    cache_path = os.path.join(CACHE_DIR, "mixture250m_tokens.pt")
    if os.path.exists(cache_path):
        tokens = torch.load(cache_path)
        print(f"mixture250m: cached {tokens.numel():,} tokens")
        return cache_path

    enc = tiktoken.get_encoding("gpt2")
    os.makedirs(CACHE_DIR, exist_ok=True)
    parts = []

    # 1. FineWeb-Edu β€” educational web text (primary source)
    t = _stream_hf("HuggingFaceTB/fineweb-edu", "sample-10BT", "train",
                   "text", 110_000_000, enc, "fineweb-edu")
    if t.numel() > 0:
        parts.append(t)

    # 2. Cosmopedia β€” synthetic textbooks (multiple configs for diversity)
    cosmo_budget = 65_000_000
    for cfg in ["openstax", "stanford", "wikihow", "stories", "khanacademy"]:
        if cosmo_budget <= 0:
            break
        t = _stream_hf("HuggingFaceTB/cosmopedia", cfg, "train",
                       "text", cosmo_budget, enc, f"cosmopedia-{cfg}")
        if t.numel() > 0:
            parts.append(t)
            cosmo_budget -= t.numel()

    # 3. WikiText-103 β€” Wikipedia (reuse cache if present)
    wt_path = os.path.join(CACHE_DIR, "wikitext103_tokens.pt")
    if os.path.exists(wt_path):
        t = torch.load(wt_path)
        print(f"wikitext103: reusing cache β€” {t.numel():,} tokens")
    else:
        t = _stream_hf("Salesforce/wikitext", "wikitext-103-raw-v1", "train",
                       "text", 80_000_000, enc, "wikitext103")
    if t.numel() > 0:
        parts.append(t)

    # Fallback: if fineweb/cosmopedia both failed, top up with openwebtext
    total = sum(t.numel() for t in parts)
    if total < 200_000_000:
        need = 255_000_000 - total
        print(f"mixture short ({total/1e6:.0f}M) β€” topping up with openwebtext ({need/1e6:.0f}M)...")
        t = _stream_hf("Skylion007/openwebtext", None, "train",
                       "text", need, enc, "openwebtext")
        if t.numel() > 0:
            parts.append(t)

    tokens = torch.cat(parts)
    print(f"mixture250m: {tokens.numel():,} tokens total "
          f"({', '.join(f'{t.numel()//1_000_000}M' for t in parts)})")
    torch.save(tokens, cache_path)
    print(f"  saved to {cache_path}")
    return cache_path


def prepare_mixture500m() -> str:
    """~510M token English mixture β€” SmolLM2-style recipe for max quality jump.

    fineweb-edu-dedup: 200M β€” educationally-filtered web (accuracy)
    cosmopedia-v2:     100M β€” synthetic textbooks (coherent exposition)
    openwebtext:       100M β€” diverse general web (narrative English)
    wikitext103:        80M β€” Wikipedia (encyclopedic, cached)
    finepdfs:           30M β€” long-form books/papers (topic coherence)
    """
    cache_path = os.path.join(CACHE_DIR, "mixture500m_tokens.pt")
    if os.path.exists(cache_path):
        tokens = torch.load(cache_path)
        print(f"mixture500m: cached {tokens.numel():,} tokens")
        return cache_path

    enc = tiktoken.get_encoding("gpt2")
    os.makedirs(CACHE_DIR, exist_ok=True)
    parts = []

    # 1. FineWeb-Edu dedup (via cosmopedia-v2 repo β€” same data, not gated)
    t = _stream_hf("HuggingFaceTB/cosmopedia-v2", "fineweb-edu-dedup", "train",
                   "text", 200_000_000, enc, "fineweb-edu-dedup")
    if t.numel() > 0:
        parts.append(t)
    if t.numel() < 100_000_000:  # fallback: smollm-corpus mirror
        t2 = _stream_hf("HuggingFaceTB/smollm-corpus", "fineweb-edu-dedup", "train",
                        "text", 200_000_000 - t.numel(), enc, "fineweb-edu-dedup-b")
        if t2.numel() > 0:
            parts.append(t2)

    # 2. Cosmopedia v2 β€” synthetic textbooks
    t = _stream_hf("HuggingFaceTB/cosmopedia-v2", "cosmopedia-v2", "train",
                   "text", 100_000_000, enc, "cosmopedia-v2")
    if t.numel() > 0:
        parts.append(t)

    # 3. OpenWebText β€” diverse general web
    t = _stream_hf("Skylion007/openwebtext", None, "train",
                   "text", 100_000_000, enc, "openwebtext")
    if t.numel() > 0:
        parts.append(t)

    # 4. WikiText-103 β€” cached Wikipedia
    wt_path = os.path.join(CACHE_DIR, "wikitext103_tokens.pt")
    if os.path.exists(wt_path):
        t = torch.load(wt_path)
        print(f"wikitext103: reusing cache β€” {t.numel():,} tokens")
    else:
        t = _stream_hf("Salesforce/wikitext", "wikitext-103-raw-v1", "train",
                       "text", 80_000_000, enc, "wikitext103")
    if t.numel() > 0:
        parts.append(t)

    # 5. FinePDFs β€” long-form books/papers (small slice, fights topic drift)
    t = _stream_hf("HuggingFaceFW/finepdfs", "eng_Latn", "train",
                   "text", 30_000_000, enc, "finepdfs")
    if t.numel() > 0:
        parts.append(t)

    # Top-up with c4 if any source failed badly
    total = sum(t.numel() for t in parts)
    if total < 450_000_000:
        need = 510_000_000 - total
        print(f"mixture short ({total/1e6:.0f}M) β€” topping up with c4 ({need/1e6:.0f}M)...")
        t = _stream_hf("allenai/c4", "en", "train", "text", need, enc, "c4")
        if t.numel() > 0:
            parts.append(t)

    tokens = torch.cat(parts)
    print(f"mixture500m: {tokens.numel():,} tokens total "
          f"({', '.join(f'{t.numel()//1_000_000}M' for t in parts)})")
    torch.save(tokens, cache_path)
    print(f"  saved to {cache_path}")
    return cache_path


def prepare_mixture1b() -> str:
    """~1.3B FRESH tokens β€” skips past docs the 500m mixture already used.
    fineweb-edu:   700M β€” primary (skip ~800K docs)
    cosmopedia-v2: 250M β€” textbooks/lessons (skip ~150K docs)
    c4:            250M β€” common-crawl diversity (unused before)
    openwebtext:   150M β€” skip ~50K docs
    """
    cache_path = os.path.join(CACHE_DIR, "mixture1b_tokens.pt")
    if os.path.exists(cache_path):
        tokens = torch.load(cache_path)
        print(f"mixture1b: cached {tokens.numel():,} tokens")
        return cache_path
    enc = tiktoken.get_encoding("gpt2")
    parts = []
    total = 0
    t = _stream_hf("HuggingFaceTB/fineweb-edu", "sample-10BT", "train",
                   "text", 700_000_000, enc, "fineweb-edu",
                   skip_docs=800_000)
    if t.numel() < 400_000_000:
        t2 = _stream_hf("HuggingFaceTB/smollm-corpus", "fineweb-edu-dedup",
                        "train", "text", 700_000_000 - t.numel(), enc,
                        "smollm-fineweb", skip_docs=500_000)
        t = torch.cat([t, t2])
    parts.append(t); total += t.numel()
    t = _stream_hf("HuggingFaceTB/cosmopedia-v2", "cosmopedia-v2", "train",
                   "text", 250_000_000, enc, "cosmopedia-v2",
                   skip_docs=150_000)
    parts.append(t); total += t.numel()
    t = _stream_hf("allenai/c4", "en", "train", "text", 250_000_000, enc,
                   "c4")
    parts.append(t); total += t.numel()
    t = _stream_hf("Skylion007/openwebtext", None, "train", "text",
                   150_000_000, enc, "openwebtext", skip_docs=50_000)
    parts.append(t); total += t.numel()
    tokens = torch.cat(parts)
    print(f"mixture1b: {tokens.numel():,} tokens total "
          f"({total/1e6:.0f}M collected)")
    torch.save(tokens, cache_path)
    print(f"  saved to {cache_path}")
    return cache_path


def load_tokens(name: str) -> torch.Tensor:
    if name == "mixture1b":
        return torch.load(prepare_mixture1b())
    if name == "mixture500m":
        return torch.load(prepare_mixture500m())
    if name == "mixture250m":
        return torch.load(prepare_mixture250m())
    if name == "wikitext103":
        return torch.load(prepare_wikitext103())
    return torch.load(prepare_wikitext2())


if __name__ == "__main__":
    import sys
    name = sys.argv[1] if len(sys.argv) > 1 else "wikitext2"
    if name == "mixture1b":
        prepare_mixture1b()
    elif name == "mixture500m":
        prepare_mixture500m()
    elif name == "mixture250m":
        prepare_mixture250m()
    elif name == "wikitext103":
        prepare_wikitext103()
    else:
        prepare_wikitext2()