Session S21

Alignment

A pretrained model completes text. How do demonstrations and a reward model learned from comparisons start turning it into an assistant, how do reinforcement learning from human feedback (RLHF) and Direct Preference Optimization (DPO) optimize those preferences while staying near the fine-tuned model, and what does preference optimization not buy?

Deep Learning · CS 4715/5715 · University of Idaho

The second deck of today's class, after S20's.

S20 read the model's distribution; alignment changes it

S20 took the model's distribution as given and chose how to read it; alignment changes the distribution itself.

How do I write aresignation letter?pretrained modelan answerthree more questionsa forum post where someoneelse asks the same thingall likelycontinuations of thepretrainingdistributionHow do I write aresignation letter?aligned modelan answerAlignment makes the assistant'sanswer the default.
  • In S20, samplers chose tokens from the model's distribution and prompts conditioned it, with every weight fixed.
  • Alignment (in the engineering sense): tune the model so its default behavior is a helpful, harmless assistant, not a text predictor. InstructGPT (Ouyang et al., 2022) assembled the canonical pipeline: SFT, then a reward model and RL; DPO (2023) later became an alternative to its last two steps.

Today follows the pipeline's steps, then asks what they do not buy

Pretrained → SFT → reward model → RL with KL → aligned; DPO replaces the last two steps.

Part 1 · Demonstrations and comparisonsstep 1, SFT, and step 2, the reward model
Part 2 · Optimizing the preferencesstep 3, RL with a KL penalty, then DPO, which replaces steps 2 and 3
Part 3 · What it does not buyreward hacking and evaluation

InstructGPT's method has three steps

Each step trains one model on its own data, with its own objective:

dataobjectivemodelStep 1 · SFThuman demonstrationsnext-token losson the responseSFT modelStep 2 · reward modelhuman rankings ofsampled responsesBradley–Terry lossreward model rϕStep 3 · RL with PPOprompts onlymaximize rewardminus β·KLpolicy π
  • Christiano et al. (2017) learned a reward model from human comparisons and optimized it with RL, on Atari and simulated robots.
  • Stiennon et al. (2020) ran all three steps for summarization: SFT, a reward model from comparisons, then PPO (proximal policy optimization) with a KL penalty. InstructGPT (Ouyang et al., 2022) applied them to general instructions on GPT-3.
  • The reading groups steps 2 and 3 as its Stage 2.

Step 1, SFT, applies the next-token loss to the response tokens

Part 1 · Demonstrations and comparisons. Supervised fine-tuning (SFT): hire people to write good responses to prompts, and train on them with S18's loss, restricted to the response:

\[L_{\text{SFT}}=-\sum\nolimits_{t\,\in\,\text{response}}\log\pi_\theta\big(y_t\mid x,\,y_{<t}\big)\]

\(x\) is the prompt and \(y\) the response; \(\pi_\theta\), the model being tuned, is called the policy.

thecatsatonthematpretraining(S18)every token after the first is a targetHowdoIwritearesignationletter?SFTresponse, written by a hired personprompt: not in the losssame loss;only the datachangedA marked token is a target of the next-token loss.
  • This alone turns a text predictor into an instruction follower. Limits: writing demonstrations is costly, and only demonstrated behavior is directly supervised.

Step 2 learns a reward model from comparisons

A reward model \(r_\phi(x,y)\) reads a prompt and a response and returns one number: how strongly people would prefer that response. Step 3 queries it in place of a person, since no one can rate every sampled response. It is trained on comparisons, because comparing two responses is easier than writing one.

what it isprompt xresponse yLLM layersh, (d,)last tokenlanguage model× W (d×V) → V logitsreward model× w (d×1) → one score rϕ(x, y)how it istrainedprompt xABClabelers rank them:B > A > Cscores after trainingBACpreferred responsesscore higher
\[P\big(y_w\succ y_l\mid x\big)=\sigma\big(r_\phi(x,y_w)-r_\phi(x,y_l)\big)\]
  • The Bradley–Terry model: \(y_w\) is the preferred response, \(y_l\) the other, \(\sigma\) the sigmoid. Training minimizes \(-\log\) of this probability, a binary cross-entropy with the score difference as the logit.

Self-check on SFT targets, ranked pairs and preference probabilities

Three questions on Part 1.

(a) SFT on a 9-token prompt and a 5-token response: how many positions are in the loss?

(b) Labelers rank three responses B, A, C, best first. Which preference pairs follow?

(c) The score difference is 0: \(P(y_w\succ y_l\mid x)\), and the loss? For a difference of 2?

(a) 5: only the response tokens are targets.

(b) Three: B over A, B over C, A over C.

(c) \(\sigma(0)=0.5\), loss \(\log2\approx0.693\). For 2: \(\sigma(2)\approx0.881\), loss about 0.127.

That settles Part 1. Next comes Part 2, optimizing the model against the learned reward.

Step 3, RL, maximizes the reward minus a KL penalty

Part 2 · Optimizing the preferences. RLHF optimizes the SFT model against \(r_\phi\) with reinforcement learning (RL); InstructGPT used proximal policy optimization (PPO):

\[\max_{\pi}\ \mathbb{E}_{y\sim\pi(\cdot\mid x)}\big[r_\phi(x,y)\big]-\beta\,\mathrm{KL}\big(\pi(\cdot\mid x)\,\Vert\,\pi_{\text{ref}}(\cdot\mid x)\big)\] \[\mathrm{KL}(\pi\,\Vert\,\pi_{\text{ref}})=\mathbb{E}_{y\sim\pi}\big[\log\pi(y\mid x)-\log\pi_{\text{ref}}(y\mid x)\big]\ge0\]
  • Reward term: pushes \(\pi\) toward responses the reward model scores high, that is, toward what labelers preferred.
  • KL term: \(\pi_{\text{ref}}\) is the SFT model, \(\beta\) its weight. The KL divergence (Kullback–Leibler), the expected log-ratio of the policy's probabilities to the SFT model's, is 0 only when they match; it keeps \(\pi\) near the SFT model, so the policy cannot drift into degenerate text that games the reward model.

One RL step samples, scores, compares with the SFT model and updates

PPO maximizes slide 8's objective one step at a time, and each step uses one sampled response \(y\) for both terms:

prompt xpolicy πsamples yresponse yreward modelrϕ(x, y)the same ypolicy πSFT model πreflog π(y | x) − log πref(y | x)its expectation is the KLrϕ(x, y) − β [log π − log πref]PPO update1234

The reward pulls toward what labelers preferred, the log-ratio pulls back toward the SFT model, and \(\beta\) sets the balance.

InstructGPT's 1.3B model was preferred over a model 100× larger

Ouyang et al. (2022), in their human evaluations:

GPT-3InstructGPT175B parameters1.3B; its outputs were preferred in their human evaluationsover 100× larger
  • Outputs of the 1.3B InstructGPT model were preferred to outputs of the 175B GPT-3. Also reported: improvements in truthfulness and reductions in toxicity, on their measures.
  • On this evidence, alignment training changed perceived usefulness far more than scale did.
  • Reported costs: an "alignment tax" on some academic benchmarks (mitigated by mixing in pretraining data), and a dependency on a specific pool of labelers.

RLHF needs a separate reward model, an RL loop and careful stabilization; DPO needs neither.

DPO replaces steps 2 and 3

DPO uses the same preference pairs as step 2 and trains the policy on them directly:

RLHF (steps 2 and 3)DPOpreference pairsreward modelPPO loopwith a KL penaltypolicythe same preference pairsone classification-style losson the policy,with the SFT model as referencepolicyno reward model, no RL

DPO is an alternative to steps 2 and 3, not a step after them.

DPO starts from the optimum of slide 8's objective

DPO (Rafailov et al., 2023). RLHF fits a reward model, then searches for the policy. DPO asks what that policy would be, and trains it directly.

We use slide 8's objective \(J(\pi)\) for one prompt \(x\) and a given reward \(r\); \(\pi^*\) maximizes it.

  1. One sum over \(y\): \(J(\pi)=\sum_y\pi(y\mid x)\big[r(x,y)-\beta\log\big(\pi(y\mid x)/\pi_{\text{ref}}(y\mid x)\big)\big]\).
  2. Put \(r\) into the log as \(\beta\log e^{r/\beta}\). With \(Z(x)=\sum_y\pi_{\text{ref}}(y\mid x)\,e^{r(x,y)/\beta}\), \(\pi_{\text{ref}}\,e^{r/\beta}/Z(x)\) sums to 1, and \(J(\pi)=-\beta\,\mathrm{KL}\big(\pi\,\Vert\,\pi_{\text{ref}}\,e^{r/\beta}/Z(x)\big)+\beta\log Z(x)\).
  3. \(\beta\log Z(x)\) does not depend on \(\pi\); the KL is \(\ge0\), and 0 only when the two match: \[\pi^*(y\mid x)=\frac{1}{Z(x)}\,\pi_{\text{ref}}(y\mid x)\,e^{r(x,y)/\beta}\] \(Z(x)\) depends only on \(x\) and sums over all responses: it cannot be computed.

The optimum reweights \(\pi_{\text{ref}}\), and the reward can be read back from it

Slide 12's optimum multiplies each response's probability under \(\pi_{\text{ref}}\) by \(e^{r/\beta}\), then divides by the sum \(Z(x)\): the higher the reward, the larger the weight. Three responses with rewards 0, 1 and 2:

πref0.5r = 00.3r = 10.2r = 2π*, β = 100.4648r = 00.3082r = 10.2271r = 2π*, β = 10.1790r = 00.2919r = 10.5291r = 2π*, β = 0.10.0000r = 00.0001r = 10.9999r = 2
  • \(\beta\) divides the reward as S20's temperature divides the logits: small \(\beta\) moves nearly all the probability to the highest reward, and large \(\beta\) keeps \(\pi^*\) near \(\pi_{\text{ref}}\).
  • Read back the reward: take logs and solve, \(r(x,y)=\beta\log\big(\pi^*(y\mid x)/\pi_{\text{ref}}(y\mid x)\big)+\beta\log Z(x)\).

Every reward corresponds to a policy, and back. The subtitle names the move: "Your Language Model Is Secretly a Reward Model".

Substituting into Bradley–Terry cancels \(\log Z\), leaving a loss on the policy

Slide 13's rewards for \(y_w\) and \(y_l\) share \(\beta\log Z(x)\), because both answer the same prompt \(x\):

\[r(x,y_w)=\beta\log\tfrac{\pi^*(y_w\mid x)}{\pi_{\text{ref}}(y_w\mid x)}+\cancel{\beta\log Z(x)}\]
\[r(x,y_l)=\beta\log\tfrac{\pi^*(y_l\mid x)}{\pi_{\text{ref}}(y_l\mid x)}+\cancel{\beta\log Z(x)}\]

Train a policy \(\pi_\theta\) in the place of \(\pi^*\): it becomes the optimal policy for a reward that agrees with the preference pairs. With slide 6's loss on the difference:

\[L_{\text{DPO}}=-\mathbb{E}\Big[\log\sigma\Big(\beta\log\frac{\pi_\theta(y_w\mid x)}{\pi_{\text{ref}}(y_w\mid x)}-\beta\log\frac{\pi_\theta(y_l\mid x)}{\pi_{\text{ref}}(y_l\mid x)}\Big)\Big]\]

\(\log\pi_\theta(y\mid x)\) sums the log-probabilities at the response's positions. Each response goes once through the policy and once through the reference: no reward model, no sampling.

Under its assumptions (Bradley–Terry preferences, slide 8's objective), DPO's optimum is the policy RLHF aims for; the paper reports DPO matching or exceeding PPO-based RLHF.

Optimize a learned reward too hard, and true quality falls

Part 3 · What it does not buy. Preference optimization aligns to what labelers preferred, which is not identical to "true" or "good". Reward models can be over-optimized: reward hacking.

scorehow hard the policy is optimized against the reward model →peakreward hackingreward model's score(the proxy)gold reward (stands infor human preference)The shape Gao, Schulman and Hilton (2023) report in a synthetic setup where a large goldreward model stands in for human preference (reading 3.7); schematic, with no numbers.
  • Classic failure: outputs get longer and more sycophantic but not better; length and agreeableness correlate with labeler preference in the training data. DPO has no separate reward model, but its implicit reward, \(\beta\log(\pi_\theta/\pi_{\text{ref}})\) (the reward read back from the policy), is fit to the same preference data and learns the same correlations.

Never optimize a proxy metric you have not stress-tested; inspect outputs directly. The KL penalty exists to limit this drift.

Aligned models are compared by preference, and judges are biased

Perplexity, the exponential of S18's next-token loss on held-out text, measures the pretraining objective, which post-training moves away from: an aligned model can have worse perplexity and be more useful.

  • Pairwise win rates: a judge picks the better of two models' responses, over many prompts. The judge is a person, as in Chatbot Arena's millions of crowd-sourced votes, or an LLM, which is cheap and fast.
  • Strong LLM judges agree with human majority preferences about as often as humans agree with each other, on Zheng et al.'s (2023) setup, with three biases:
biasthe judge favorsmitigation
positionwhichever answer is shown firstswap the order and average
verbositylonger answerscontrol for length
self-enhancementoutputs resembling its own styleuse a judge from a different model family

Report win rates with their uncertainty, over seeds and prompts, decoding fixed (S20).

Self-check on win rates, perplexity and what preferences align to

Three questions on Part 3.

(a) After preference tuning, your model wins 70% of comparisons against the base under an LLM judge, and its answers are twice as long. What do you check first?

(b) Held-out perplexity rose after RLHF. Has the run failed?

(c) What does preference optimization align a model to, and what does that not guarantee?

(a) Length: the judge's verbosity bias, or a hacked reward. Control for length, swap the order, and read the outputs.

(b) Not by that measure: perplexity measures the pretraining objective, which post-training moves away from. Compare by preference and instruction following.

(c) To what its labelers preferred; that is no guarantee of "true" or "good".

That settles what it does not buy. Next come four mistakes, today's answer and next time.

Four mistakes with alignment, with their causes and fixes

Part 4 · Close. Reading 3.7's three pitfalls, and S20's lesson about settings:

symptomcausefix
after preference tuning, outputs get longer and more sycophantic but not betterthe reward is gamed: length and agreeableness correlate with labeler preferenceinspect outputs directly; never optimize a proxy you have not stress-tested
benchmark scores dip after RLHF, and the run is declared a failurethe alignment tax: helpfulness tuning can trade off narrow benchmark accuracyevaluate what alignment targets (win rate, instruction following), not only what pretraining targets
DPO training looks stable, but answers drift toward terse, generic textover-training on a narrow offline preference set collapses diversityhold out preference pairs, stop early on win rate against the reference, and read samples
an alignment claim reported without its decoding settingsevaluation results depend on the sampler (S20)fix and report the decoding settings with every alignment claim

Demonstrations, a KL penalty and preferences answer today's question

Back to the title page's question, clause by clause.

  • Demonstrations and comparisons: SFT trains S18's next-token loss on the response tokens of human-written demonstrations; the reward model, the LLM with a scalar output, learns ranked comparisons through Bradley–Terry.
  • Optimizing the preferences: RLHF maximizes \(r_\phi\) minus \(\beta\) times the KL divergence to the SFT model (PPO in InstructGPT). DPO solves that objective's optimum for the reward; \(\log Z\) cancels, and one classification-style loss on preference pairs trains the model directly, with no reward model and no RL.
  • What it does not buy: it aligns to what labelers preferred, not to truth; a learned reward can be hacked (length, sycophancy); aligned models are judged by preference, with the judge's biases controlled.

For next time

  • Next session (S22), Monday: Vision Transformers and multimodal models (ViT and CLIP), the final session of Module 3.
  • Read: 3.8 Vision Transformers and Multimodal Models.
  • HW3 (Transformer from scratch) is due next session (S22) at 11:59 PM Pacific; the 12-hour grace period applies.
  • Quiz 3 (Module 3) is at S23, in the final 30 minutes, in Canvas. It covers S13–S22, both of today's sessions included.
  • Project: the proposal is due at the end of the S23 week.