Offline RL & RLHF
- Offline RL (batch RL) trains from a fixed dataset of past interactions, without live environment access
- RLHF (Christiano et al. 2017) uses human preference labels to train reward models that guide RL fine-tuning of LLMs
- InstructGPT (2022) and subsequent ChatGPT showed RLHF is the key to instruction-following LLMs
Offline RL trains a policy from a fixed historical dataset without further environment interaction, enabling RL on data collected by other agents. RLHF applies offline RL to LLMs: human preferences train a reward model, which then guides PPO fine-tuning of the language model.
- Connects directly to fine-tuning and PEFT - RLHF is the fine-tuning method that creates chat-aligned LLMs
- Constitutional AI (Anthropic) and DPO (Direct Preference Optimization) are alternatives to the full RLHF pipeline
- Dataset shift and distributional issues in offline RL connect to data leakage and covariate shift concepts
- Explain the offline RL problem: why distribution shift and extrapolation error make off-policy learning from a static dataset fundamentally harder than online RL.
- Describe how Conservative Q-Learning (CQL) penalises out-of-distribution actions and how IQL avoids querying OOD actions entirely.
- Explain the Decision Transformer framing: RL as sequence modeling conditioned on desired return.
- Derive the DPO objective from the KL-regularised RLHF objective, showing how the partition function cancels and why no separate reward model is needed.
- Explain Constitutional AI and RLAIF as mechanisms for scalable alignment that replace human labellers with model-generated feedback.
TL;DR
Offline RL learns from a fixed dataset without environment interaction, but distribution shift causes Q-values to extrapolate dangerously for unseen actions — CQL and IQL constrain this conservatively. RLHF trains a reward model from human preferences and fine-tunes with PPO; DPO eliminates the separate reward model by directly expressing the RLHF objective as a classification loss over the policy itself, which is why it is simpler and more stable.
Intuition
Standard RL assumes the agent can explore: try an action, observe the outcome, update. Offline RL removes this privilege — you have a dataset of tuples collected by some behaviour policy, and the goal is to extract the best possible policy from that fixed data. This sounds like supervised learning, but the distribution shift problem makes it far harder: a learned Q-function may assign high values to (state, action) pairs that look good on paper but were never tried — and acting on those inflated values during deployment leads to catastrophic failures.
The core offline RL problem: the Q-function is only accurate near the support of the dataset. Any action outside that support can be assigned arbitrarily high Q-values by function approximation, and greedy policies will exploit these hallucinated values.
The Offline RL Problem: Distribution Shift and Extrapolation Error
Let the behaviour policy that collected the dataset be and the target policy be . The Bellman backup includes a max over actions:
When selects actions outside , the TD target bootstraps from values never trained on real data. Bellman residual minimisation will try to reduce error in regions where data exist — but the max operation systematically selects the worst offenders (highest but least accurate values). Levine et al. [9] call this extrapolation error.
Conservative Q-Learning (CQL)
Kumar et al. (2020) [1] propose adding a penalty to the standard Bellman objective that lowers values for out-of-distribution (OOD) actions and raises them for in-distribution actions:
The first term is a soft maximum that assigns high values globally to all actions, then subtracts back the expectation under the data distribution — the net effect is to push below the data values for unseen pairs. The hyperparameter controls conservatism. CQL provides theoretical guarantees that the learned is a lower bound on the true , making the greedy policy pessimistically safe.
Implicit Q-Learning (IQL)
Kostrikov et al. (2021) [2] avoid the problem of ever querying OOD actions by reformulating the Bellman update to use only in-dataset transitions. The key trick is expectile regression for the value function:
where and is a quantile parameter. With close to 1, approximates the maximum in-dataset Q-value without ever evaluating the policy at novel states. Policy extraction then uses advantage-weighted regression:
IQL achieves state-of-the-art on D4RL benchmarks and is simpler to tune than CQL.
Decision Transformer: RL as Sequence Modeling
Chen et al. (2021) [3] reframe offline RL entirely: instead of learning a Q-function, train a causal Transformer to predict actions conditioned on past returns, states, and actions. At test time, condition on a desired high return and let the model produce the actions:
where is the “return-to-go” (desired future return, decremented after each step). This approach avoids Bellman backups entirely, treating the whole trajectory as a language-model-style prediction problem. It matches or exceeds CQL on several D4RL tasks and scales gracefully with model size, making it a precursor to the trajectory-level reasoning seen in LLM-based agents.
RLHF: The Three-Stage Pipeline
Reinforcement Learning from Human Feedback [4] [7] aligns language models through three stages:
Stage 1 — Supervised Fine-Tuning (SFT): Fine-tune the base LLM on high-quality human-written demonstrations to produce .
Stage 2 — Reward Modeling (RM): Collect pairs of completions for the same prompt with human labels indicating which is preferred. Train a scalar reward model via the Bradley-Terry log-likelihood:
Stage 3 — PPO Fine-Tuning: Maximise the reward model signal while penalising KL divergence from the SFT reference policy to prevent reward hacking:
Reward hacking: the policy may learn to game the reward model — for example, producing extremely long outputs if the RM spuriously scores length highly, or generating confident-sounding nonsense. The KL penalty limits how far the policy can drift, but tuning it is critical.
DPO: Direct Preference Optimization
Rafailov et al. (2023) [5] show that the RLHF objective can be solved in closed form, eliminating the need for a separate reward model and PPO training loop.
Deriving the DPO objective from the RLHF KL-regularised objective
Step 1 — The KL-regularised RLHF objective.
Step 2 — Optimal policy in closed form.
This is a classic KL-regularised optimisation. The functional derivative with respect to yields the optimal policy:
where is the (intractable) partition function.
Step 3 — Invert to express reward in terms of optimal policy.
Taking the log and rearranging:
Step 4 — Substitute into the Bradley-Terry preference model.
The Bradley-Terry probability that is preferred to is:
Substituting the expression for and noting that appears in both terms and cancels:
Step 5 — The DPO training loss.
Replacing with the trainable policy and taking the negative log-likelihood:
This is a binary cross-entropy loss over log-likelihood ratios of the trainable policy relative to the reference policy. No reward model, no PPO, no RL training loop. The policy is simultaneously the language model and the implicit reward model.
Constitutional AI and RLAIF
Constitutional AI (CAI) [6] (Anthropic, 2022) scales alignment by replacing expensive human preference labels with model-generated feedback:
- Generate responses with the SFT model (including some harmful ones).
- Ask the model to critique each response against a set of principles (the “constitution”), then revise it.
- Use the original vs revised pairs to train a Preference Model (PM) — the equivalent of the RLHF reward model, but trained on AI-generated labels.
- Fine-tune with RL (RL-CAI) or with DPO against the AI-labeled preferences (RLAIF).
RLAIF is the generalisation: any pipeline where the preference signal comes from a model rather than humans. Constitutional AI is Anthropic’s specific instantiation with an explicit constitution of principles.
Method Comparison
| Method | Data needed | Requires RM? | Online/Offline | Compute | Stability | Reward hacking risk |
|---|---|---|---|---|---|---|
| SFT only | Demonstrations | No | Offline | Low | High | None |
| RLHF (PPO) | Preferences + demonstrations | Yes (explicit) | Online | High (4 models) | Low (PPO instability) | High if KL not tuned |
| DPO | Preferences | No (implicit in pi) | Offline | Medium (SFT-level) | High | Lower (no separate RM) |
| IPO (Azar 2023) | Preferences | No | Offline | Medium | High | Lower (direct preference target) |
| KTO (Ethayarajh 2024) | Single-label (good/bad) | No | Offline | Medium | High | Lower |
| Constitutional AI / RLAIF | AI-generated preferences | Yes (AI-labeled) | Either | Medium | Medium | Medium |
Real-World: ChatGPT, Claude, Llama 2
- ChatGPT (InstructGPT) [7]: classic three-stage RLHF with PPO. Human labellers rank completions; RM trained; PPO optimises with KL penalty.
- Claude [6]: Constitutional AI reduces reliance on human preference labels. The constitution specifies principles; the model self-critiques and revises.
- Llama 2-Chat [10]: two reward models (helpfulness and safety) trained separately; PPO optimises a linear combination with a margin-based reward.
- DPO in practice: many open-source fine-tunes (Zephyr, OpenHermes) use DPO because it requires no separate RM training and is as simple as running SFT on preference-weighted data.
Length bias in preference datasets: humans often prefer longer outputs even when the content is equivalent. This causes reward models to spuriously score length, and DPO to increase output length. Explicitly controlling length in the preference collection phase or adding length penalties helps.
Pop Quiz
- 1. What is the primary challenge that makes offline RL harder than online RL?
- 2. How does CQL prevent extrapolation error?
- 3. In DPO, what mathematical quantity cancels that allows the derivation to work?
- 4. What does RLAIF replace compared to standard RLHF?
- 5. The Decision Transformer frames offline RL as:
Interview Q&A
Derive the DPO objective from the RLHF objective, explaining each step.staff
The full derivation is a 5-step argument:
- Start from the KL-regularised RLHF objective: .
- Solve the functional optimisation via calculus of variations. Setting the derivative to zero gives .
- Invert this to express the reward: .
- Substitute into the Bradley-Terry preference model . The terms cancel because they appear in both and .
- Replace with the trainable and take the negative log-likelihood to get the DPO cross-entropy loss over log-ratio differences.
The result is a supervised binary classification loss requiring no reward model and no RL. DPO works because the optimal RLHF policy already implicitly encodes a reward model; DPO just directly optimises that implicit encoding.
Why is offline RL harder than online RL? What is extrapolation error and how does CQL address it?onsite
In online RL, every action the policy takes generates a new tuple, so the Q-function is trained on its own distribution. In offline RL, the dataset was generated by a fixed behaviour policy . When we run policy improvement (take the max-Q action), we select actions that may be outside the support of . The Q-function was never trained on those pairs, so function approximation can assign them arbitrarily high values — a phenomenon called extrapolation error. The policy then exploits these spurious maxima and fails in deployment.
CQL addresses this by adding a pessimistic regulariser to the Bellman objective: a term that simultaneously maximises the log-sum-exp of Q over all actions (encourages high values globally) and subtracts the in-data expectation (anchors values down in regions where data exist). The net effect is that OOD actions have lower Q-values than in-distribution actions, making the greedy policy pessimistically conservative.
What is reward hacking, and how does the KL penalty in RLHF mitigate it?phone
Reward hacking is Goodhart’s Law applied to RL: “When a measure becomes a target, it ceases to be a good measure.” The reward model is a neural network trained on a finite preference dataset; it approximates human preferences but is not identical to them. A powerful policy optimised against can find inputs that score highly on the learned reward but are actually poor outputs — for example, extremely long responses, repetitive confident-sounding text, or adversarial patterns that exploit reward-model weaknesses.
The KL penalty prevents the policy from drifting too far from the SFT reference. A large keeps the policy conservative (close to SFT, less likely to hack), while a small allows more reward-driven optimisation. In practice is treated as a hyperparameter that trades off alignment-to-reward-model vs divergence-from-human-intent.
References
- Kumar, A., Zhou, A., Tucker, G., and Levine, S. (2020). Conservative Q-Learning for Offline Reinforcement Learning. NeurIPS 2020 (arXiv preprint). arXiv:2006.04779
- Kostrikov, I., Nair, A., and Levine, S. (2021). Offline Reinforcement Learning with Implicit Q-Learning. ICLR 2022 (arXiv preprint 2021). arXiv:2110.06169
- Chen, L., Lu, K., Rajeswaran, A., Lee, K., Grover, A., Laskin, M., Abbeel, P., Srinivas, A., and Mordatch, I. (2021). Decision Transformer: Reinforcement Learning via Sequence Modeling. NeurIPS 2021 (arXiv preprint). arXiv:2106.01345
- Christiano, P. F., Leike, J., Brown, T., Martic, M., Legg, S., and Amodei, D. (2017). Deep Reinforcement Learning from Human Preferences. NeurIPS 2017 (arXiv preprint). arXiv:1706.03741
- Rafailov, R., Sharma, A., Mitchell, E., Ermon, S., Manning, C. D., and Finn, C. (2023). Direct Preference Optimization: Your Language Model is Secretly a Reward Model. NeurIPS 2023 (arXiv preprint). arXiv:2305.18290
- Bai, Y., Jones, A., Ndousse, K., Askell, A., Chen, A., DasSarma, N., Drain, D., Fort, S., Ganguli, D., Henighan, T., et al. (2022). Constitutional AI: Harmlessness from AI Feedback. arXiv preprint. arXiv:2212.08073
- Ouyang, L., Wu, J., Jiang, X., Almeida, D., Wainwright, C. L., Mishkin, P., Zhang, C., Agarwal, S., Slama, K., Ray, A., et al. (2022). Training language models to follow instructions with human feedback. NeurIPS 2022 (arXiv preprint). arXiv:2203.02155
- Schulman, J., Wolski, F., Dhariwal, P., Radford, A., and Klimov, O. (2017). Proximal Policy Optimization Algorithms. arXiv preprint. arXiv:1707.06347
- Levine, S., Kumar, A., Tucker, G., and Fu, J. (2020). Offline Reinforcement Learning: Tutorial, Review, and Perspectives on Open Problems. arXiv preprint. arXiv:2005.01643
- Touvron, H., Martin, L., Stone, K., Albert, P., Almahairi, A., Babaei, Y., Bashlykov, N., Batra, S., Bhargava, P., Bhosale, S., et al. (2023). Llama 2: Open Foundation and Fine-Tuned Chat Models. arXiv preprint. arXiv:2307.09288