RLHF: Reinforcement Learning from Human Feedback
The full RLHF pipeline: reward modeling from comparisons, PPO optimization, KL-constrained policy updates, the Bradley-Terry model, and InstructGPT methodology.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- The Alignment Problem
- Stage 1: Supervised Fine-Tuning (SFT)
- Stage 2: Reward Modeling
- Stage 3: PPO Optimization
- The KL Penalty
- Connection to DPO
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Describe the three-stage RLHF pipeline (SFT → RM → PPO).
- Derive the Bradley-Terry model for preference learning.
- Explain the PPO objective with KL constraint.
- Prove the connection between reward maximization and DPO.
- Analyze failure modes: reward hacking, mode collapse.
Notation
- — policy (language model)
- — reference policy (SFT model)
- — reward model
- — KL penalty coefficient
Core Intuition
Pre-trained LLMs predict likely text, not helpful text. A model trained on the internet will happily generate toxic, incorrect, or harmful content if it's statistically likely. RLHF aligns the model with human preferences: first teach it what "good" outputs look like (reward model), then optimize it to produce those outputs (RL), while staying close to the pre-trained model (KL penalty) to preserve capabilities.
RLHF Pipeline
The Alignment Problem
Pre-training objective: Maximize — predict likely text.
Desired behavior: Produce helpful, harmless, honest responses.
Gap: Likely ≠ helpful. The most probable completion of "How do I hack..." might be instructions for hacking, not a refusal.
Solution: RLHF adds a layer of preference optimization on top of the pre-trained model.
Stage 1: Supervised Fine-Tuning (SFT)
Fine-tune the pre-trained model on high-quality demonstrations:
Data: Human-written (prompt, response) pairs demonstrating ideal behavior.
Purpose: Shift the model's distribution toward helpful response format. Creates the reference policy and starting point for RL.
Stage 2: Reward Modeling
Data collection: For each prompt , generate two responses and have humans label which is better: .
Bradley-Terry model: The probability of preferring over :
where is the sigmoid function.
Training objective:
Architecture: Same as the language model but with the last token's hidden state projected to a scalar reward. Typically initialized from the SFT model.
Stage 3: PPO Optimization
Objective: Maximize reward while staying close to the reference policy:
PPO implementation:
- Generate responses for a batch of prompts.
- Score with reward model: .
- Compute advantages using a value function.
- Update policy with clipped PPO objective:
The KL Penalty
Purpose: Prevent the policy from diverging too far from the pre-trained model.
Without KL penalty: The model "reward hacks" — finds degenerate outputs that exploit reward model weaknesses (e.g., repeating a praised phrase endlessly).
Effect: The KL term penalizes outputs that would be very unlikely under the reference model.
Optimal solution (analytically):
The optimal policy is the reference policy reweighted by exponentiated reward.
Connection to DPO
DPO derives from RLHF: Substituting the optimal policy (equation 6) into the Bradley-Terry model:
Plugging into the preference loss eliminates the reward model entirely:
DPO = RLHF without explicit reward modeling or RL. Same objective, simpler optimization.
Common Pitfalls
Pitfall 1. Reward hacking. The model finds outputs that score highly on the reward model but are low quality (e.g., excessive hedging, sycophancy). Mitigation: KL penalty, reward model ensembles.
Pitfall 2. Mode collapse. The policy collapses to a narrow set of "safe" responses, losing diversity. Mitigation: entropy bonus, temperature sampling during RL.
Pitfall 3. Reward model overoptimization. As the policy optimizes harder, reward model score increases but true quality decreases (Goodhart's law). Mitigation: early stopping, conservative .
Summary
- RLHF pipeline: SFT → Reward Model → PPO optimization.
- Reward model: Bradley-Terry preference model from human comparisons.
- PPO: Maximize reward subject to KL constraint from reference policy.
- KL penalty prevents reward hacking and preserves capabilities.
- DPO is a simplified equivalent: skips RM and RL entirely.
Exercises
Exercise 1. Derive the optimal policy (equation 6) by solving the KL-constrained optimization problem.
Exercise 2. Show that the Bradley-Terry model is equivalent to logistic regression on reward differences.
Exercise 3. For and reward difference 2.0: compute the optimal policy's probability ratio .
Exercise 4. Explain why reward model quality degrades as the policy moves far from the training distribution.
Exercise 5. Derive DPO (equation 8) from the RLHF objective by substituting the optimal policy into the Bradley-Terry preference model.