Loading
0x30Lesson 4 of 16

Trace pretraining and model adaptation

Follow a model from pretraining on trillions of tokens to instruction tuning, preference tuning and cheap adapters like LoRA.

24 min 7-question quiz 2 code exercises
By the end of this lesson you can
  • 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:

  1. 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.
  2. Supervised fine-tuning (SFT) - training on curated examples of instructions and good responses, so the model behaves like an assistant.
  3. 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 XP

Which stage?

Match each description to the training stage it belongs to.

0 of 5 sortedScore 0/0
  • “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 6ND6ND floating-point operations, where NN is the parameter count and DD 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 din×doutd_{in} \times d_{out} weight matrix, it trains two thin matrices of rank rr, adding only r(din+dout)r(d_{in} + d_{out}) parameters. With r=8r = 8 that’s well under 1% of the original - small enough to train on one GPU and to keep many adapters for different tasks.

lora.py
d_in, d_out, rank = 4096, 4096, 8
print(d_in * d_out, rank * (d_in + d_out))
Output
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.

Exercise 1

Chinchilla-style training budget

+25 XP

Each line is a parameter count in billions. Using about 20 tokens per parameter and training compute C≈6NDC \approx 6ND, print NB params: TB tokens, F FLOPs with tokens in billions (:g) and FLOPs in scientific notation with 2 decimals (:.2e).

  • Three sizes
main.py
Loading editor…

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.

Exercise 2

Count LoRA parameters

+25 XP

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
main.py
Loading editor…

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…