GPT-2 small, trained from scratch on FineWeb-Edu
A 124M-parameter GPT-2 small, trained from random weights on 9.85 billion tokens of FineWeb-Edu, with a readable GPT-2 written from scratch in PyTorch.
Code: github.com/JohnMachado11/GPT-2 -- the model, the training loop and the scripts that trained this one, all hand-written, with every weight and step named.
The whole story of this run, from the data to the overnight training and what it cost, is in the blog post How I trained GPT-2 small from scratch.
On the same 99.6M held-out FineWeb-Edu tokens it reaches a lower validation loss than OpenAI's original GPT-2 small from 2019:
| model | validation loss (lower is better) | perplexity |
|---|---|---|
| this model | 3.0548 | 21.2 |
OpenAI's GPT-2 small (gpt2) | 3.2975 | 27.0 |
Both models were scored on exactly the same 99,631,104 tokens: FineWeb-Edu's held-out 1% (every 100th web page), never seen in training. FineWeb-Edu is home turf for this model -- OpenAI's was trained on WebText -- so this compares the two on modern educational web text, not on every kind of text.
Use it
from transformers import AutoModelForCausalLM, AutoTokenizer
model = AutoModelForCausalLM.from_pretrained("John-Machado/gpt2-small-fineweb-edu")
tokenizer = AutoTokenizer.from_pretrained("John-Machado/gpt2-small-fineweb-edu")
ids = tokenizer("The water cycle is", return_tensors="pt").input_ids
out = model.generate(ids, max_new_tokens=40, do_sample=True, top_k=50, pad_token_id=tokenizer.eos_token_id)
print(tokenizer.decode(out[0]))
Or load it into the readable model from the GitHub repo, where every weight has a plain name:
from gpt2.from_hf import load_gpt2
model = load_gpt2("John-Machado/gpt2-small-fineweb-edu")
Samples
Sampled with top_k=50 (seed 0); the prompt is in bold:
The water cycle is a fundamental system of our planet that is crucial for life on the planet. The Water Cycle, while essential for life in general, can't be changed. Water is essential for life on Earth
The French Revolution began in 1789, with the establishment of the "New State of the People" at Versailles. Napoleon III and Napoleon Bonaparte, a French Revolutionary party led by Giacomo Meyerbeer,
Fluent, on-topic, textbook-style English -- and, like any 124M model, confident inventions (1789 and Versailles are right; Napoleon III and the composer Meyerbeer are not).
Reproduce it
Every step of this run -- preparing the data, a smoke test, training, and the comparison with OpenAI's model -- is written out in the repo: Train GPT-2 small from scratch.
Training
| Architecture | GPT-2 small: 12 layers, 768 wide, 12 heads, context 1024, tied embeddings -- 124,439,808 parameters |
| Data | FineWeb-Edu sample-10BT (revision 87f09149ef4734204d70ed1d046ddc9ca3f2b8f9), GPT-2 tokenizer; 9,854,302,149 training tokens, read once, in order |
| Steps | 18,794 steps of 524,288 tokens (micro-batch 16 × 32 accumulation × 1024) |
| Optimizer | AdamW, betas (0.9, 0.95), weight decay 0.1 on the 2-D weights, gradient norm capped at 1.0 |
| Learning rate | linear warmup over 715 steps to 6e-4, then cosine down to 6e-5 |
| Dropout | 0.0 |
| Precision and speed | bf16 autocast, TF32, fused attention (FlashAttention via PyTorch SDPA), torch.compile |
| Hardware | one NVIDIA A100 40GB on Google Colab Pro+: 15.6 hours at ~176,000 tokens/s |
| Final loss | train ~3.0; validation 3.0113 on the in-training check (the first 327,680 held-out tokens) |
The learning rates and batch size follow the GPT-3 paper's settings for its 125M model.
Limitations
- A base model. It continues text; it isn't instruction-tuned and doesn't answer questions reliably.
- Not a source of facts. It invents details, and often names a country's most famous city as its capital (Montreal for Canada, Sydney for Australia) -- as OpenAI's GPT-2 small does.
- English, in the style of educational web pages, with a 1024-token context.
- Trained on web text, so it can reflect the biases and errors of that text.
License and credits
The weights are released under the MIT license, like the code. The training data is FineWeb-Edu by Hugging Face (ODC-By 1.0; its use is also subject to Common Crawl's terms of use). The tokenizer is OpenAI's GPT-2 tokenizer. Architecture: OpenAI's GPT-2 (Radford et al., 2019).