v9.1.1
Menu

Chapter 6: RLHF & Alignment

Week 5-6

Learn how to align LLMs with human values and preferences. Master Reinforcement Learning from Human Feedback (RLHF)---the technique that transformed GPT-3 into ChatGPT---understand reward modeling, policy optimization with PPO, the simpler Direct Preference Optimization (DPO) alternative, Constitutional AI for scalable oversight, and the challenges of collecting human preference data.

Topics
5
Videos
5
Min read
39

Chapter Overview

Instruction tuning teaches a model to follow instructions, but it does not teach it to be helpful, harmless, or honest. A model might follow the instruction "explain how to hack a website" perfectly---and that is exactly the problem. Alignment ensures models behave in ways that are beneficial and safe.

Reinforcement Learning from Human Feedback (RLHF) is the technique that made ChatGPT possible. The key insight: instead of training on (input, correct output) pairs, we train on (output A is better than output B) preferences. Humans rank model outputs, a reward model learns to predict these preferences, and the LLM is optimized to maximize the reward model's score.

However, RLHF is complex and unstable. Recent alternatives like Direct Preference Optimization (DPO) achieve similar results with a simpler training pipeline. Constitutional AI (CAI) reduces the need for human annotation by having the model critique and revise its own outputs.

This chapter covers:

  • Reward Modeling: Training a model to predict human preferences
  • PPO for LLMs: The RL algorithm that optimizes the policy against the reward model
  • DPO: A simpler alternative that eliminates the need for a separate reward model
  • Constitutional AI: Scalable alignment through AI-generated feedback
  • Human Preference Data: How to collect, curate, and validate preference annotations

Chapter Roadmap

Click any topic to jump in

Explainer video
1
Reward Modeling

Training a model to predict human preferences — the bridge between human judgment and optimization.

Bradley-Terry ModelReward Model TrainingReward HackingReward Model Evaluation
2
Preference Data

Collecting comparison data — how to gather and quality-control human preference annotations.

Annotation DesignCommon Annotation BiasesScale and CostPreference Data Formats
Two paths to alignment

RL-based vs direct optimization

3
PPO for LLMs

Proximal Policy Optimization — the RL algorithm that fine-tunes LLMs against the reward model.

The RLHF ObjectivePPO Algorithm for LLMsValue Function BaselineImplementation Challenges
4
DPO

Direct Preference Optimization — skipping the reward model by optimizing preferences directly.

DPO ObjectiveDPO vs RLHF/PPOThe Beta ParameterVariants: IPO, KTO, ORPO
Beyond human feedback
5
Constitutional AI

Self-improvement through AI-generated critiques — reducing reliance on human feedback.

The Constitutional ApproachPrinciples as SupervisionRLAIF: AI as AnnotatorRed Teaming

Topics

5 topics in this chapter

After instruction tuning, a model follows requests, but it cannot tell a great answer from a merely acceptable one. It only imitates its demonstrations. To improve further we need a training signal that scores any response the model writes, and nobody can write down a formula for "helpful" or "harmless". What people can do reliably is compare two answers and say which is better. The challenge is to turn those comparisons into a score a training algorithm can maximise.

The previous chapter, Fine-tuning, ended with supervised instruction tuning. This chapter, RLHF & Alignment, goes beyond imitation to optimising for human preferences, and this first topic builds the scoring model.

We start with the Bradley-Terry model, which turns a reward difference into the probability that one response beats another. Then we cover reward model training, the pairwise loss and how rankings become pairs. Next comes reward hacking, what happens when a policy exploits the reward model's mistakes. We finish with reward model evaluation, including the ceiling set by human agreement.

Definition

A reward model is a network r(x,y)r(x, y) that maps a prompt xx and a response yy to a single scalar score. It is trained on human comparisons so that preferred responses score higher, typically with a Bradley-Terry likelihood. It is usually initialised from the SFT model, with the language-model head replaced by a scalar head, and it supplies the reward for RLHF.

In this topic

  1. 1Bradley-Terry Model
  2. 2Reward Model Training
  3. 3Reward Hacking
  4. 4Reward Model Evaluation
1 of 4
Bradley-Terry Model

P(yw≻yl∣x)=σ(r(x,yw)−r(x,yl))P(y_w \succ y_l \mid x) = \sigma(r(x, y_w) - r(x, y_l))

The Bradley-Terry model, first used to rank sports teams, turns scores into win probabilities. Given a prompt xx, a preferred response ywy_w, and a rejected response yly_l, the formula says ywy_w wins with probability σ\sigma of the reward difference, where σ\sigma is the logistic sigmoid. Only differences matter, so adding a constant to every reward changes nothing, and rewards have no absolute meaning. A gap of 0 gives 50 percent, and larger gaps approach certainty. The reward model r(x,y)r(x, y) is usually the SFT model with its vocabulary head replaced by one scalar output. The model assumes preferences are consistent, which intransitive or split human judgments violate.

Mathematical Intuition

The Bradley-Terry model parameterizes preferences as P(yw≻yl)=σ(rw−rl)P(y_w \succ y_l) = \sigma(r_w - r_l). The sigmoid maps the reward difference to [0,1][0, 1], so a gap of Δr=1.4\Delta r = 1.4 gives σ(1.4)≈0.80\sigma(1.4) \approx 0.80. The loss −log⁡σ(Δr)-\log \sigma(\Delta r) is the negative log-likelihood of the observed preference. Gradient: ∇rL=−(1−σ(Δr))\nabla_r \mathcal{L} = -(1 - \sigma(\Delta r)), meaning the gradient vanishes as the model becomes confident about clear preferences and focuses learning on ambiguous pairs.

Example:

Reward model scores: r(x,yw)=3.2r(x, y_w) = 3.2, r(x,yl)=1.8r(x, y_l) = 1.8. What is the predicted preference probability?

2 of 4
Reward Model Training

LRM=−E(x,yw,yl)[log⁡σ(r(x,yw)−r(x,yl))]\mathcal{L}_{RM} = -\mathbb{E}_{(x, y_w, y_l)} \left[\log \sigma(r(x, y_w) - r(x, y_l))\right]

Training maximises the Bradley-Terry likelihood of the observed human choices. The loss in the formula is the negative log sigmoid of the reward gap, averaged over triples of prompt, chosen response ywy_w, and rejected response yly_l. It is small when the chosen response already scores much higher and large when the order is wrong. Labellers often rank several responses, and each ranking of kk responses gives (k2)\binom{k}{2} pairs. InstructGPT ranked 4 to 9 responses per prompt and kept all pairs from one prompt in the same batch, to avoid overfitting. Llama 2 trained separate helpfulness and safety reward models on over a million comparisons, adding a margin for strongly held preferences.

Mathematical Intuition

The RM loss L=−E[log⁡σ(rw−rl)]\mathcal{L} = -\mathbb{E}[\log \sigma(r_w - r_l)] is a binary cross-entropy where the 'label' is always 1 (preferred wins). With kk ranked responses per prompt, you get (k2)\binom{k}{2} pairs: 4 responses yield 6 pairs, making ranking far more data-efficient than binary comparison. The RM's generalization depends on the ratio of parameters to comparison pairs — overparameterized RMs memorize annotator noise.

Example:

You have 100K prompt-response pairs with rankings. How do you create training data for the RM?

3 of 4
Reward Hacking

A reward model is a learned approximation of human judgment, trained on a limited set of responses. When a policy is optimised hard against it, the policy finds responses that score well for the wrong reasons. It might write longer answers, sound more confident, use phrases the reward model liked, or agree with the user. This is Goodhart's law: a measure that becomes a target stops being a good measure. Gao et al. (2022) measured it with a gold reward model standing in for humans. As optimisation continued, the proxy reward kept rising, but the gold reward first rose and then fell. Larger reward models delayed the fall.

Mathematical Intuition

Reward hacking occurs when the policy finds inputs y∗y^* that maximize r(x,y∗)r(x, y^*) in regions where the RM has poor calibration. Formally, the RM is accurate near the SFT distribution πsft\pi_{\text{sft}} but unreliable far from it. As KL divergence DKL(π∥πsft)D_{KL}(\pi \| \pi_{\text{sft}}) grows, the policy exploits extrapolation errors. The Goodhart curve shows reward proxy r^\hat{r} increasing while true quality r∗r^* peaks and then drops.

Example:

After RLHF, the model starts producing unnecessarily long, repetitive answers that score high on the RM but are unhelpful. What happened?

4 of 4
Reward Model Evaluation

The basic metric is pairwise accuracy on held-out comparisons, the fraction of pairs where the chosen response gets the higher score. Chance is 50 percent. The ceiling is not 100 percent but the rate at which humans agree with each other, about 73 percent between InstructGPT's labellers, because a reward model cannot predict a coin-flip judgment. Calibration matters too: pairs scored at 0.8 should be won about 80 percent of the time. Benchmarks such as RewardBench test hard cases in chat, reasoning, and safety, where one answer has a verifiable flaw. The final test is downstream: does optimising against it improve the policy according to people?

Mathematical Intuition

RM accuracy is bounded by human inter-annotator agreement κ\kappa. If humans agree 76% of the time, a perfect RM can achieve at most 76%. Relative performance (acc−0.5)/(κ−0.5)(\text{acc} - 0.5) / (\kappa - 0.5) normalizes against this ceiling. Calibration measures whether predicted P(yw≻yl)=0.8P(y_w \succ y_l) = 0.8 corresponds to 80% actual agreement in held-out data. Expected Calibration Error (ECE) bins predictions and measures the gap between predicted and actual preference rates.

Example:

Your RM achieves 73% accuracy on held-out preference pairs. Human annotators agree 76% of the time. Is your RM good?

Theory Exercise

Problem:

You are designing a reward model for a coding assistant. What specific signals should the RM learn to detect as 'good' vs 'bad' code responses? How would you structure the preference data collection?

Hints:
  • Think about what makes code responses genuinely helpful
  • Consider objective vs subjective quality signals
  • Think about the expertise needed for annotation