RLHF and Alignment
Reinforcement Learning from Human Feedback (RLHF) trains language models to produce outputs that humans prefer, going beyond what supervised fine-tuning alone can achieve.
The Alignment Problem
Pre-trained models optimize for next-token prediction, not for being helpful, harmless, or honest. RLHF aligns model behavior with human values.
| Stage | Objective | Data Required |
|---|---|---|
| Pre-training | Next-token prediction | Trillions of tokens |
| SFT | Task completion | Thousands of demonstrations |
| Reward Modeling | Predict human preferences | Paired preference comparisons |
| RL Optimization | Maximize reward with constraints | Prompts |
RLHF Pipeline
Reward Modeling
The reward model learns to predict human preferences from pairwise comparisons.
import torch
import torch.nn.functional as F
from transformers import AutoModelForSequenceClassification, AutoTokenizer
class RewardModelTrainer:
def __init__(self, model_name="meta-llama/Llama-2-7b-hf"):
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
self.model = AutoModelForSequenceClassification.from_pretrained(
model_name, num_labels=1
)
if self.tokenizer.pad_token is None:
self.tokenizer.pad_token = self.tokenizer.eos_token
def compute_reward(self, prompt, response):
text = f"[INST] {prompt} [/INST] {response}"
inputs = self.tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
with torch.no_grad():
reward = self.model(**inputs).logits[0, 0]
return reward.item()
def train_step(self, batch):
chosen_texts = [
f"[INST] {p} [/INST] {c}" for p, c in zip(batch["prompts"], batch["chosen"])
]
rejected_texts = [
f"[INST] {p} [/INST] {r}" for p, r in zip(batch["prompts"], batch["rejected"])
]
chosen_inputs = self.tokenizer(
chosen_texts, padding=True, truncation=True, return_tensors="pt"
)
rejected_inputs = self.tokenizer(
rejected_texts, padding=True, truncation=True, return_tensors="pt"
)
chosen_rewards = self.model(**chosen_inputs).logits[:, 0]
rejected_rewards = self.model(**rejected_inputs).logits[:, 0]
loss = -F.logsigmoid(chosen_rewards - rejected_rewards).mean()
return loss, chosen_rewards.mean().item(), rejected_rewards.mean().item()
# Example training loop
trainer = RewardModelTrainer()
for batch in preference_dataloader:
loss, chosen_r, rejected_r = trainer.train_step(batch)
loss.backward()
optimizer.step()
optimizer.zero_grad()
print(f"Loss: {loss:.4f}, Chosen R: {chosen_r:.4f}, Rejected R: {rejected_r:.4f}")
PPO (Proximal Policy Optimization)
PPO is the primary algorithm used for RLHF optimization, balancing reward maximization with policy stability.
import torch
import torch.nn.functional as F
from dataclasses import dataclass
@dataclass
class PPOConfig:
kl_coeff: float = 0.1
clip_range: float = 0.2
value_loss_coeff: float = 0.5
gamma: float = 1.0
gae_lambda: float = 0.95
learning_rate: float = 1e-5
ppo_epochs: int = 4
mini_batch_size: int = 4
class PPOTrainer:
def __init__(self, policy_model, ref_model, reward_model, tokenizer, config=None):
self.config = config or PPOConfig()
self.policy = policy_model
self.ref_model = ref_model
self.reward_model = reward_model
self.tokenizer = tokenizer
def compute_kl_penalty(self, logprobs, ref_logprobs):
"""KL divergence between current and reference policy."""
return logprobs - ref_logprobs
def compute_advantages(self, rewards, values):
"""Generalized Advantage Estimation (GAE)."""
advantages = torch.zeros_like(rewards)
last_gae = 0
for t in reversed(range(len(rewards))):
if t == len(rewards) - 1:
next_value = 0
else:
next_value = values[t + 1]
delta = rewards[t] + self.config.gamma * next_value - values[t]
advantages[t] = last_gae = delta + (
self.config.gamma * self.config.gae_lambda * last_gae
)
returns = advantages + values
return advantages, returns
def ppo_step(self, prompts):
"""Single PPO optimization step."""
all_logprobs = []
all_rewards = []
all_values = []
# Collect experience
for prompt in prompts:
response = self.generate_response(prompt)
logprobs = self.get_logprobs(prompt, response)
ref_logprobs = self.get_ref_logprobs(prompt, response)
reward = self.get_reward(prompt, response)
value = self.get_value(prompt, response)
kl = self.compute_kl_penalty(logprobs, ref_logprobs)
adjusted_reward = reward - self.config.kl_coeff * kl
all_logprobs.append(logprobs)
all_rewards.append(adjusted_reward)
all_values.append(value)
# PPO updates
for _ in range(self.config.ppo_epochs):
advantages, returns = self.compute_advantages(
torch.stack(all_rewards), torch.stack(all_values)
)
advantages = (advantages - advantages.mean()) / (advantages.std() + 1e-8)
# Policy loss
ratio = torch.exp(new_logprobs - old_logprobs)
surr1 = ratio * advantages
surr2 = torch.clamp(ratio, 1 - self.config.clip_range, 1 + self.config.clip_range) * advantages
policy_loss = -torch.min(surr1, surr2).mean()
# Value loss
value_loss = F.mse_loss(predicted_values, returns)
# Total loss
total_loss = policy_loss + self.config.value_loss_coeff * value_loss
total_loss.backward()
self.optimizer.step()
self.optimizer.zero_grad()
return {
"policy_loss": policy_loss.item(),
"value_loss": value_loss.item(),
"mean_reward": torch.stack(all_rewards).mean().item(),
"mean_kl": kl.mean().item(),
}
DPO (Direct Preference Optimization)
DPO eliminates the need for a separate reward model by directly optimizing the policy from preferences.
import torch
import torch.nn.functional as F
class DPOTrainer:
def __init__(self, policy_model, ref_model, tokenizer, beta=0.1):
self.policy = policy_model
self.ref_model = ref_model
self.tokenizer = tokenizer
self.beta = beta
def get_logprobs(self, model, input_ids, labels):
"""Compute per-token log probabilities."""
outputs = model(input_ids=input_ids)
logits = outputs.logits[:, :-1, :]
labels = labels[:, 1:]
log_probs = F.log_softmax(logits, dim=-1)
per_token_logprobs = torch.gather(
log_probs, 2, labels.unsqueeze(-1)
).squeeze(-1)
# Mask padding
mask = (labels != self.tokenizer.pad_token_id).float()
seq_logprobs = (per_token_logprobs * mask).sum(dim=-1)
return seq_logprobs
def dpo_loss(self, batch):
"""Compute DPO loss from preference pairs."""
chosen_ids = batch["chosen_input_ids"]
rejected_ids = batch["rejected_input_ids"]
with torch.no_grad():
ref_chosen_logps = self.get_logprobs(self.ref_model, chosen_ids, chosen_ids)
ref_rejected_logps = self.get_logprobs(self.ref_model, rejected_ids, rejected_ids)
policy_chosen_logps = self.get_logprobs(self.policy, chosen_ids, chosen_ids)
policy_rejected_logps = self.get_logprobs(self.policy, rejected_ids, rejected_ids)
chosen_logratios = policy_chosen_logps - ref_chosen_logps
rejected_logratios = policy_rejected_logps - ref_rejected_logps
logits = self.beta * (chosen_logratios - rejected_logratios)
loss = -F.logsigmoid(logits).mean()
chosen_rewards = self.beta * chosen_logratios
rejected_rewards = self.beta * rejected_logratios
return loss, {
"chosen_reward": chosen_rewards.mean().item(),
"rejected_reward": rejected_rewards.mean().item(),
"reward_margin": (chosen_rewards - rejected_rewards).mean().item(),
"accuracy": (logits > 0).float().mean().item(),
}
Comparison of Alignment Methods
| Method | Reward Model | Training Stability | Compute Cost | Sample Efficiency |
|---|---|---|---|---|
| PPO | Required | Moderate | High | Low |
| DPO | Not required | High | Moderate | High |
| RLHF (full) | Required | Low | Very high | Low |
| KTO | Not required | High | Moderate | Moderate |
| IPO | Not required | High | Moderate | High |
Reward Hacking
Models may exploit reward model weaknesses without producing genuinely better outputs.
| Technique | Description | Effectiveness |
|---|---|---|
| KL penalty | Penalize deviation from reference policy | Moderate |
| Reward ensemble | Average multiple reward models | High |
| Length penalty | Penalize unnecessarily long responses | High |
| Periodic retraining | Retrain reward model on new data | High |
Best Practices
- Start with strong SFT - RLHF improves upon, not replaces, supervised fine-tuning
- Collect diverse preferences - Avoid systematic labeling biases
- Monitor KL divergence - Prevent policy collapse or reward hacking
- Use reference model - Maintain baseline for stable training
- Evaluate with humans - Automatic metrics alone are insufficient
Key Takeaways
- PPO is the most established RLHF algorithm but requires significant compute
- DPO provides a simpler, more stable alternative without a separate reward model
- Reward modeling quality is critical for successful RLHF
- KL regularization prevents catastrophic policy collapse
- Human evaluation remains essential for measuring true alignment
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type â Always start by categorizing the problem
- Incorrect formula application â Verify the formula applies to your specific case
- Unit errors â Always check units are consistent throughout
- Sign errors â Pay careful attention to positive and negative values
- Rounding too early â Keep full precision until the final answer
- Forgetting constraints â Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic â Build on the foundation laid here
- Next Topic â See how this concept extends to more advanced material
- Practice Problems â Additional exercises with solutions
- Video Explanation â Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions