Implementation sequence
A small GPT,
from scratch.
Text → tokens → attention → training → generation.
Eight stages with related lessons and checks. This is a build roadmap, not a complete runnable course.
Prerequisites: Python, arrays, and basic differentiation.
Explore language modelsStage 00
Make prediction measurable
Create a character-level bigram baseline and calculate negative log-likelihood by hand before using a neural network.
Probability and cross-entropyOutput & verification
- Output
- Baseline notebook + loss table
- Check
- Your held-out loss beats a uniform predictor and the split is reproducible.
Stage 01
Train the tokenizer
Implement byte-pair merges on a small corpus, freeze the vocabulary, then encode and decode real text, code, names, and multilingual samples.
LLM tokenizationOutput & verification
- Output
- Versioned tokenizer + vocabulary
- Check
- Round trips are lossless and fertility is measured on held-out text.
Stage 02
Build the data engine
Clean and deduplicate a bounded corpus, create train and validation partitions, pack fixed-length sequences, and shift inputs into targets.
Data, evidence, and leakageOutput & verification
- Output
- Dataset manifest + batch inspector
- Check
- No document crosses the split and every target is the next input token.
Stage 03
Prove learning works
Write a small neural language model, trace tensor shapes, compare analytical gradients with finite differences, and overfit one tiny batch.
BackpropagationOutput & verification
- Output
- Training loop + gradient checks
- Check
- The tiny batch reaches near-zero loss and a broken gradient test fails loudly.
Stage 04
Implement causal attention
Implement query, key, value projections, scaled dot-product attention, a causal mask, multi-head attention, and one decoder block.
Transformer internalsOutput & verification
- Output
- Attention module + mask tests
- Check
- Changing a future token cannot change an earlier position output.
Stage 05
Assemble a small GPT
Combine token and position representations, repeated decoder blocks, normalization, and a vocabulary projection with tied weights.
GPT architecture and pretrainingOutput & verification
- Output
- Decoder-only model + parameter report
- Check
- Every tensor shape is asserted and parameter count matches the manual estimate.
Stage 06
Pretrain and evaluate
Add AdamW, warmup, decay, clipping, checkpoints, validation loss, perplexity, sample prompts, and controlled ablations.
Training systems and scalingOutput & verification
- Output
- Checkpoint + experiment report
- Check
- A held-out metric improves and the run can resume deterministically from a checkpoint.
Stage 07
Generate, package, and serve
Implement temperature and top-k sampling, protect context limits, measure latency, document limitations, and expose a small inference interface.
Model servingOutput & verification
- Output
- Sampling CLI + model card
- Check
- Fixed-seed output is reproducible and unsafe or unsupported uses are explicit.
Ideas behind GPT
2003Learn distributed word representations
Predict language with learned vectors instead of hand-built symbolic rules.
Bengio et al. ↗2017Replace recurrence with attention
Process token relationships in parallel with attention and feed-forward blocks.
Transformer paper ↗2018Pretrain a decoder, then adapt it
Use generative pretraining to learn transferable representations before supervised adaptation.
GPT-1 paper ↗2020Scale next-token prediction
Larger models and broader data made in-context task behavior much more capable.
GPT-3 paper ↗2022Balance parameters and training tokens
Compute budgets work better when model size and data scale together.
Chinchilla paper ↗2022Post-train for human intent
Demonstrations and preference feedback shape a base model into a more useful assistant.
InstructGPT paper ↗