The previous chapter’s agents learned from a reward signal in a game. This one turns the same machinery on a language model: a large teacher (an API model) transfers competence into a small student (Qwen2.5-1.5B in the playground) through the standard modern post-training recipe — supervised fine-tuning, then preference optimization against the teacher’s judgements. That second stage is reinforcement learning from AI feedback, and by the end of the chapter you will have derived, line by line, why the playground can do it with a classification loss (DPO) instead of running PPO.
Stage 0 — what a language model is optimizing
An autoregressive LM defines , trained by next-token cross-entropy. Post-training never changes this machinery — it changes which distribution the model is pulled toward. Keeping that frame prevents the two chronic confusions of this area: fine-tuning does not “add a database” (it shifts a distribution), and RLHF-style methods do not “train on answers” (they reshape relative preferences between answers the model can already produce).
Stage 1 — SFT: distillation as supervised learning
Collect prompts with teacher (or curated) responses , and minimize
— cross-entropy on the target tokens only (the prompt is context, not target; the playground masks it accordingly). This is behavioral cloning, the same “copy the expert” move that imitation learning makes, with the same characteristic strengths and failure. It transfers style, format and procedure extremely efficiently — a few hundred examples visibly reshape how the student answers. It transfers facts and competence only to the extent the student’s base distribution can support them: token-forcing a 1.5B model through statements it has no representation for yields memorization at best and confident hallucination at worst. The honest framing for small-student distillation: SFT mostly selects and sharpens behaviours that the pretrained model already latently contains.
LoRA: why 3 GB of VRAM is enough
Full fine-tuning updates every weight matrix. Low-Rank Adaptation freezes the pretrained and learns only a low-rank correction:
For a 4096×4096 attention projection at : 16.8M frozen parameters, 131K trainable — 0.8% — and the optimizer states (Adam keeps two moments per parameter, the real memory hog) shrink by the same factor. is initialized to zero so training starts exactly at the base model; decouples the update’s scale from the chosen rank. The bets LoRA makes are worth stating: that the change needed for a task is low-rank (empirically true for style/format adaptation — which is precisely what SFT transfers best; less true for injecting broad new knowledge), and — recall the factorization caveat from the very first Foundations lesson — the objective is non-convex in even where it was convex in , which in practice bothers no one. Adapters also compose: keep one per skill and swap or merge them — the mechanism the University chapter builds its skill registry on.
Stage 2 — from RLHF to DPO, the actual derivation
SFT clones; it cannot express “this answer is better than that one.” Preference data can: for a prompt , two responses with a judgement (in the playground the student samples both and the teacher judges — RLAIF: same mathematics as RLHF, an AI where the human panel sat).
The preference model. Bradley–Terry (1952) posits a latent reward:
The RLHF objective. Classic RLHF fits a reward model to preferences, then optimizes the policy with PPO (last chapter’s engine), leashed to a reference policy (the SFT model) by a KL penalty so it can’t wander into reward-hacked gibberish:
Step 1 — this objective has a closed-form optimum. For each , maximizing over distributions is solved by the Gibbs distribution (complete the KL and normalize):
Step 2 — invert it. Solve for the reward that makes any given optimal:
Step 3 — substitute into Bradley–Terry. The intractable is the same for and under one prompt, so it cancels in the difference, and the preference probability becomes a function of the policy alone. Fitting it by maximum likelihood gives Direct Preference Optimization:
Read what happened: the reward model, the sampling loop and PPO disappeared — not approximated away, but derived away. Your language model is secretly its own reward model (the paper’s slogan, now earned), and preference optimization became logistic regression (lesson 3 of Supervised Learning) on a margin built from four log-probabilities — two forward passes each on policy and frozen reference.
The loss is logistic in the margin; the dashed curve is the gradient weight σ(−βm) each pair contributes — the derivative’s scaling factor. Pairs the model already ranks correctly by a wide margin (m large) contribute almost nothing; pairs it ranks wrongly (m < 0) dominate the update — DPO automatically concentrates on its mistakes. Now sweep β: small β makes the loss nearly flat (weak preference pressure, strong KL leash — updates stay close to the reference); large β sharpens it, trading reference-anchoring for preference-fitting. β is the same dial as RLHF’s KL coefficient, surviving the derivation intact.
The trade-offs, honestly: DPO is offline (it fits the pairs it was given; PPO-style online RL can explore beyond them), it can lower the absolute likelihood of both responses while widening their gap (a known quirk — the objective only sees differences), and its strength depends on pair quality — which in RLAIF means judge quality, a dependency the University chapter quantifies with a widget.
Stage 3 — did it work? Evaluation without self-deception
Post-training evaluation is where wishful thinking goes to thrive; the playground ships the standard battery, and each metric answers a different question:
| Metric | Needs judge? | Question it answers | Blind spot |
|---|---|---|---|
| Win-rate (pairwise, position-swapped) | yes | “preferred over the base, head-to-head?” | judge biases (length, style) |
| Rubric score (1–5, absolute) | yes | “how good on defined criteria?” | scale drift between runs |
| Perplexity on held-out text | no | “does it model the domain’s distribution?” | fluency ≠ usefulness |
| ROUGE-L vs references | no | “lexical overlap with a known-good answer” | paraphrase-blind |
Three disciplines carry over from the Supervised Learning evaluation lesson with extra force here. Held-out means held out — the eval prompts must never appear in SFT or preference data, and with a teacher generating both training data and judgements, contamination is one lazy prompt-template away. Judges are biased instruments — LLM judges systematically favour longer answers, confident tone, and the first position shown (hence the playground’s position-swapped pairs at temperature 0). Report the spread — SFT and DPO runs vary across seeds and data orderings; a single win-rate without repetition is an anecdote.
And the capability ceiling, stated plainly since students will hit it in the lab: a 1.5B student can be made noticeably more useful in format and focus with a few hundred good pairs, and it will still hallucinate at the edge of its knowledge — post-training reshapes the distribution the pretraining built; it does not extend its support. Fine-tuning also bakes in a timestamp: for knowledge that moves, the architecture answer is retrieval (the LLM track’s territory), not more epochs.
Exercises
Work these before the next chapter
- Derive Step 1: show that is solved by the Gibbs form (write the Lagrangian for the normalization constraint, or complete the KL). Where exactly does β end up in the exponent?
Solution
Worked solutions are part of Premium — unlock all of them for £5/month →
- 5 more exercises — each with a worked solution — are part of Premium. Unlock everything for £5/month →
References
- R. Rafailov et al., “Direct Preference Optimization: Your Language Model is Secretly a Reward Model”, NeurIPS 2023 — the derivation of Steps 1–3.
- L. Ouyang et al., “Training language models to follow instructions with human feedback”, 2022 — InstructGPT: the RLHF pipeline DPO simplifies.
- E. Hu et al., “LoRA: Low-Rank Adaptation of Large Language Models”, ICLR 2022.
- Y. Bai et al., “Constitutional AI: Harmlessness from AI Feedback”, 2022 — RLAIF at scale.
- R. A. Bradley & M. E. Terry, “Rank Analysis of Incomplete Block Designs”, Biometrika 1952.
- L. Zheng et al., “Judging LLM-as-a-Judge with MT-Bench and Chatbot Arena”, NeurIPS 2023 — judge biases, position swapping.