Trace pretraining and model adaptation
Follow a model from pretraining on trillions of tokens to instruction tuning, preference tuning and cheap adapters like LoRA.
- Describe pretraining and how compute, data and model size relate
- Compare supervised fine-tuning, RLHF and DPO
- Explain why LoRA makes fine-tuning cheap
A chat model is built in stages:
- Pretraining - next-token prediction over trillions of tokens of web pages, books, code and more. This is where knowledge and language ability come from, and nearly all of the compute goes. The result is a base model: brilliant at continuing text, not at following instructions.
- Supervised fine-tuning (SFT) - training on curated examples of instructions and good responses, so the model behaves like an assistant.
- Preference tuning - people (or models) compare pairs of responses; the model is pushed toward preferred ones. RLHF (Ouyang et al., 2022) trains a reward model and optimizes against it with reinforcement learning; DPO (Rafailov et al., 2023) learns directly from the preference pairs, more simply.
Many recent models add reinforcement learning on verifiable tasks - math problems and code with checkable answers - which teaches longer step-by-step “reasoning” before answering.
Try it
Up to 15 XPWhich stage?
Match each description to the training stage it belongs to.
“Predict the next token across trillions of tokens of web text and code”
“Train on 50,000 hand-written instruction → ideal response pairs”
“Labelers pick which of two answers is more helpful and honest”
“Produces a base model that continues text rather than answering”
“Directly raise the probability of preferred answers over rejected ones”
Scaling: how big, how much data?
Training compute is roughly floating-point operations, where is the parameter count and the number of training tokens (Kaplan et al., 2020). The Chinchilla study (Hoffmann et al., 2022) found that for a fixed compute budget, the best results come from about 20 tokens per parameter - many earlier models were too big and undertrained. Today many models train far beyond 20× because a smaller model that’s cheaper to run is worth extra training.
Cheap fine-tuning with LoRA
Fine-tuning every weight of a big model is expensive. LoRA (Hu et al., 2021) freezes the original weights and learns a small low-rank update instead: for a weight matrix, it trains two thin matrices of rank , adding only parameters. With that’s well under 1% of the original - small enough to train on one GPU and to keep many adapters for different tasks.
d_in, d_out, rank = 4096, 4096, 8
print(d_in * d_out, rank * (d_in + d_out))16777216 65536
Key takeaways
Pretraining builds knowledge; SFT teaches the assistant role; preference tuning shapes what “good” means.
Compute ≈ 6ND; Chinchilla suggests about 20 training tokens per parameter for compute-optimal training.
LoRA fine-tunes with a tiny fraction of the parameters.
Lesson quiz
7 questions · pass with 5 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write Python
Write Python in the editor and run it against sample inputs. Python runs locally in your browser using a WebAssembly runtime.
Chinchilla-style training budget
Each line is a parameter count in billions. Using about 20 tokens per parameter and training compute , print NB params: TB tokens, F FLOPs with tokens in billions (:g) and FLOPs in scientific notation with 2 decimals (:.2e).
- Three sizes
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Count LoRA parameters
Each line is d_in d_out rank. Print full=F lora=L (P%): the full matrix’s parameter count, LoRA’s rank × (d_in + d_out), and LoRA as a percentage of full (2 decimals).
- Square and wide layers
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…