All courses

Explain how a base model becomes an assistant

Walk through SFT with chat templates, reward modeling, KL-regularized RLHF or DPO, and parameter-efficient variants. Explain which behaviors each stage creates and the side effects it can introduce.

Builds on: Chat templates and special tokens, Direct Preference Optimization, Reward hacking and overoptimization, LoRA: low-rank fine-tuning

▶ Watch this lesson Start the whole course

What the animation says

The document-continuer

Give a base model a question, and it may not answer it. It continues the document, like a forum page, writing more questions underneath. The assistant you use behaves differently. It answers, stops, declines some requests, and keeps one tone. Two training stages produce those behaviors, and each one brings in habits nobody asked for. First question: which stage teaches the model to stop when its answer is done?

SFT: imitate the format

Stopping comes from imitation. That stage is supervised fine-tuning, or SFT. Demonstration chats are flattened into one token stream by the chat template. Cross-entropy is computed only on assistant tokens, including end-of-turn. So the model learns the format: answer, take the assistant role, follow the instruction, stop. It's pure imitation, capped by its demonstrators. It also learns to state guesses as facts, and only in this template. Tap every part of this stream that the loss trains on.

Preferences: compare, then shift

Stage two: people see two answers to one prompt and pick the better one. Comparing is easier than writing, so this can go beyond the demonstrations. Route one: a reward model learns those picks, then PPO maximizes reward, minus a penalty for drifting from the frozen SFT model. Route two, DPO, trains on the pairs directly: no reward model, no sampling. Both chase a proxy, so answers drift longer, more confident, more agreeable, and less varied. Cautious raters add over-refusal.

β at work: one worked case

Here is that penalty at work: reward, minus beta times the log-ratio. The log-ratio says how far the whole answer drifted from the SFT model. Two equally good answers, but the reward model overrates length. Long: reward two point three, log-ratio fifteen. Short: reward one point six, log-ratio two. With beta zero point one, long scores zero point eight, short one point four. Short wins. Now shrink beta to zero point zero one. Which wins?

β is the leash

Long wins, two point one five against one point five eight. The penalty became too small to matter. Beta is the leash to the SFT model. Small beta lets the policy drift to wherever the reward model is fooled. Large beta keeps it near SFT, so the proxy's errors can't be pushed as far. Each stage inherits its signal's flaws. SFT copies confident guessing. Preference training exaggerates whatever the proxy rewards.

LoRA: cheap, but narrow

One cheap tool fits either stage: LoRA. Freeze the big weight matrix. Train two thin matrices whose product is added to it. Their shared inner width, the rank, is tiny: here eight against four thousand ninety-six. So training is cheap and adapters swap in and out. But a low-rank update limits how far behavior can move. Tap every matrix that training updates.

From document-continuer to assistant

A base model only continues documents. Ask it a question and it may write more questions, like a forum page. Yet the assistant you use answers, stops, declines some requests and keeps a consistent tone. **Which training stage produces each of those behaviors? What does each stage optimize? And what unwanted habits does each one bring in along the way?**

Two stages: imitate, then prefer

**SFT**: flatten demonstration conversations with the chat template and train cross-entropy only on assistant tokens. This creates the format: answer instead of continue, take the assistant role, follow instructions, emit the end-of-turn token. It is pure imitation, so it is capped by the demonstrators' quality. **Preferences**: humans pick the better of two answers. There are two routes. Either a reward model learns those judgments and RLHF (PPO) optimizes against it, or DPO trains on the pairs directly. Both shift probability toward preferred answers, which shapes helpfulness, tone and refusals. Comparing is easier than writing, so this can go beyond the demonstrations.

What each stage smuggles in

- **SFT** copies confident demonstrations even where the model lacks the facts, so it learns to state guesses as facts. It also ties behavior to one template. - **RLHF/DPO** optimize a proxy, so outputs drift longer, more confident and more agreeable with the user. Variety shrinks as probability piles onto favored answers. Over-cautious raters produce over-refusal. - **LoRA** (usable at any stage) is cheap and gives swappable adapters. But the small rank $\rho$ of its weight update limits how far behavior can move.

The objectives, stage by stage

SFT, averaged over the set $S$ of positions $t$ whose next token $x_{t+1}$ is in an assistant turn (including end-of-turn): $$\mathcal{L}_{SFT}=-\tfrac{1}{|S|}\textstyle\sum_{t\in S}\log p_\theta(x_{t+1}\mid x_{\le t})$$ Reward model on a pair $(y_w,y_l)$ (Bradley-Terry): $-\log\sigma(r(x,y_w)-r(x,y_l))$. RLHF via PPO, with $\pi_{ref}$ = frozen SFT model, averaged over prompts $x$: $$\max_\theta\ \mathbb{E}_x\big[\mathbb{E}_{y\sim\pi_\theta(\cdot|x)}[r(x,y)]-\beta\,\mathrm{KL}(\pi_\theta(\cdot|x)\|\pi_{ref}(\cdot|x))\big]$$ DPO, with no reward model and no sampling: $-\log\sigma\big(\beta[\log\tfrac{\pi_\theta(y_w|x)}{\pi_{ref}(y_w|x)}-\log\tfrac{\pi_\theta(y_l|x)}{\pi_{ref}(y_l|x)}]\big)$. LoRA: freeze $W\in\mathbb{R}^{d_{in}\times d_{out}}$. Train only $W_a\in\mathbb{R}^{d_{in}\times\rho}$ and $W_b\in\mathbb{R}^{\rho\times d_{out}}$ in $W+W_aW_b$, with $\rho\ll d$.

How β decides whether length wins

Per-sample RLHF score: $r(x,y)-\beta\log\frac{\pi_\theta(y|x)}{\pi_{ref}(y|x)}$. The log-ratio is for the whole response. Suppose both answers are truly equally good, but the reward model overrates length. - **Long answer**: $r=2.3$, log-ratio $15$ (far from SFT). - **Short answer**: $r=1.6$, log-ratio $2$. With $\beta=0.1$: long $2.3-1.5=0.8$; short $1.6-0.2=1.4$. Short wins. With $\beta=0.01$: long $2.3-0.15=2.15$; short $1.6-0.02=1.58$. Long wins. A small $\beta$ lets the policy drift to wherever the reward model is fooled. A large $\beta$ keeps it near SFT.

Remember this

SFT teaches the assistant format by imitation. Preference training (a reward model plus KL-penalized RLHF, or DPO directly on the pairs) teaches which answers people prefer. LoRA makes either stage cheap. Each stage inherits its signal's flaws. SFT copies confident guessing. Preference stages exaggerate whatever the proxy rewards (length, confidence, agreement), held back mainly by $\beta$.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.