Algorithm Search⌘K

Techniqueresearch

Reinforcement learning from human feedback

Also called RLHF.

Fit a reward model to human comparisons of outputs, then tune the language model by policy gradient to raise reward under a KL penalty.

Reinforcement learning from human feedback (RLHF) tunes a language model so that its responses get a high score from a reward model fitted to human comparisons, while a KL penalty keeps it close to a reference model. It is a pipeline with three steps. First, instruction tuning, which is supervised fine-tuning on written demonstrations, gives the reference model. Second, a preference reward model is fitted to comparisons of that model's responses; this is the model that the pipeline fits, not the pipeline itself. Third, a policy-gradient method, usually PPO, raises the reward minus the penalty. PPO is a general reinforcement learning algorithm and knows nothing of human feedback. Direct preference optimization (DPO) targets the same KL-regularized objective, but with one classification loss on the comparisons, no reward model and no sampling during training.

In plain words

Some goals are hard to write as a formula, such as "a helpful and honest answer". But people can look at two answers and say which one is better.

RLHF uses this. You collect many such choices. You train a second network to guess which answer people will like. Then you let the language model write answers, score them with the second network, and change the model so that it writes answers that score higher.

The scoring network is only a guess. If the model is pushed too hard, it finds answers that fool the scorer but that people do not like. So the training also holds the model near its starting point, and a setting controls how far it may move.

When it applies

  • The goal is a judgment of people, not a label or a formula, and people can compare two outputs more reliably than they can rate one.
  • A pretrained and fine-tuned model already gives outputs that are worth comparing. RLHF moves a good model; it does not build one from scratch.
  • There is a budget for labelers, and compute to sample responses from the policy during training and to hold several large networks in memory.
  • The comparisons follow the Bradley-Terry assumption well enough: preferences are mostly transitive and can be summarized by one number per response.

The template

Let πSFT\pi_{\text{SFT}} be the reference model, πϕ\pi_\phi the policy being trained, and rθr_\theta the reward model. For a prompt xx and a response y=y1…yny = y_1 \ldots y_n of nn tokens over a vocabulary of size ∣V∣|V|, the policy and the reference each give an n×∣V∣n \times |V| matrix of next-token logits, so log⁡π(y∣x)=∑t=1nlog⁡π(yt∣x,y<t)\log \pi(y \mid x) = \sum_{t=1}^{n} \log \pi(y_t \mid x, y_{<t}), as in causal language modeling. The reward model gives one number for the whole response.

  1. Supervised fine-tuning. Fine-tune a pretrained model on demonstrations written by people, with the next-token cross-entropy loss. The result is πSFT\pi_{\text{SFT}}, which stays frozen from now on.
  2. Comparisons. For each prompt, sample KK responses from the model and let a person rank them.
  3. The reward model. Fit rθr_\theta to the comparisons with the loss −log⁡σ(rθ(x,yw)−rθ(x,yl))-\log \sigma(r_\theta(x, y_w) - r_\theta(x, y_l)), where σ\sigma is the logistic sigmoid.
  4. Reinforcement learning. Start πϕ\pi_\phi at πSFT\pi_{\text{SFT}} and maximize
J(ϕ)=Ex,  y∼πϕ(⋅∣x)[rθ(x,y)−βlog⁡πϕ(y∣x)πSFT(y∣x)]+γ Ex∼Dpretrain[log⁡πϕ(x)].J(\phi) = \mathbb{E}_{x,\; y \sim \pi_\phi(\cdot \mid x)} \Big[ r_\theta(x, y) - \beta \log \frac{\pi_\phi(y \mid x)}{\pi_{\text{SFT}}(y \mid x)} \Big] + \gamma\, \mathbb{E}_{x \sim D_{\text{pretrain}}} \big[ \log \pi_\phi(x) \big].

This is equation 2 of Ouyang et al. The second term mixes in the pretraining loss; with γ=0\gamma = 0 it is gone. The expectation of the log ratio over y∼πϕy \sim \pi_\phi is the Kullback-Leibler divergence KL(πϕ ∥ πSFT)\mathrm{KL}(\pi_\phi \,\|\, \pi_{\text{SFT}}), so the first term is the reward minus β\beta times the KL divergence.

One episode. Ouyang et al. (Section 3.5) treat each prompt as a bandit episode: the policy writes a response, the reward model scores it, and the episode ends. The penalty is paid per token. Token tt gets the reward −β(log⁡πϕ(yt∣x,y<t)−log⁡πSFT(yt∣x,y<t))-\beta (\log \pi_\phi(y_t \mid x, y_{<t}) - \log \pi_{\text{SFT}}(y_t \mid x, y_{<t})), and the last token also gets rθ(x,y)r_\theta(x, y). The sum over the tokens is the bracket in JJ. For example, with β=0.1\beta = 0.1, a reward-model score of 1.3 and per-token log ratios (0.2,−0.1,0.4)(0.2, -0.1, 0.4), the return is 1.3−0.1⋅0.5=1.251.3 - 0.1 \cdot 0.5 = 1.25. PPO then uses a value function, which predicts the return at each of the nn positions, and generalized advantage estimation. Ouyang et al. initialize the value function from the reward model and use no discount (Appendix C.4).

The networks in memory. The step uses four networks: the policy (trained), the reference (frozen), the reward model (frozen) and the value function (trained). Ouyang et al. used a 6B reward model and a 6B value function for policies of 1.3B, 6B and 175B parameters.

What the objective has as its optimum (theorem). Hypotheses: a fixed reward rr, β>0\beta > 0, a reference with πref(y∣x)>0\pi_{\text{ref}}(y \mid x) > 0 for every response, and a policy that may be any distribution (no network limits). Then the unique maximizer of Ey∼π[r(x,y)]−β KL(π ∥ πref)\mathbb{E}_{y \sim \pi}[r(x, y)] - \beta\, \mathrm{KL}(\pi \,\|\, \pi_{\text{ref}}) is

π∗(y∣x)=πref(y∣x) er(x,y)/βZ(x),Z(x)=∑yπref(y∣x) er(x,y)/β,\pi^*(y \mid x) = \frac{\pi_{\text{ref}}(y \mid x)\, e^{r(x, y)/\beta}}{Z(x)}, \qquad Z(x) = \sum_y \pi_{\text{ref}}(y \mid x)\, e^{r(x, y)/\beta},

and the maximum is βlog⁡Z(x)\beta \log Z(x) (Rafailov et al., Section 4; the proof is on the DPO page). PPO with a network does not reach this optimum exactly; the theorem says what it aims at. A toy case with three responses, πref=(0.5,0.3,0.2)\pi_{\text{ref}} = (0.5, 0.3, 0.2) and r=(0,1,2)r = (0, 1, 2), so that Eπref[r]=0.7\mathbb{E}_{\pi_{\text{ref}}}[r] = 0.7:

β\beta π∗\pi^* Eπ∗[r]\mathbb{E}_{\pi^*}[r] KL(π∗ ∥ πref)\mathrm{KL}(\pi^* \,\|\, \pi_{\text{ref}}) objective
0.5 (0.03667, 0.16256, 0.80077) 1.76411 0.91548 1.30637
1 (0.17900, 0.29194, 0.52906) 1.35006 0.32283 1.02722
2 (0.32504, 0.32154, 0.35342) 1.02838 0.08353 0.86132

A small β\beta buys reward with a large move away from the reference; a large β\beta keeps the policy near it.

Classic examples

  • Atari and simulated robots. Christiano et al. (2017) trained agents without access to the true reward, from comparisons of short clips. They report that people gave feedback on less than 1% of the interactions of the agent, and that new behaviors could be trained with about an hour of human time (abstract). This is a measurement on their tasks.
  • Summaries. Stiennon et al. (2020) fitted a reward model to comparisons of summaries of Reddit posts and tuned the policy with reinforcement learning. Their models were preferred to the human reference summaries and to much larger models trained with supervised learning only (abstract).
  • Following instructions. Ouyang et al. (2022) applied the three steps to GPT-3, with about 13k prompts for supervised fine-tuning, 33k for the reward model and 31k for PPO. Labelers preferred the outputs of the 1.3B model after RLHF to those of the 175B GPT-3 (Figure 1). The settings were β=0.02\beta = 0.02, γ=27.8\gamma = 27.8, 256k episodes and a PPO clip ratio of 0.2 (Appendix C.4). Training the 175B policy took 60 petaflop/s-days, against 4.9 for supervised fine-tuning and 3,640 for pretraining GPT-3 (Section 5.1).

Variations

  • DPO. It fits the policy to the comparisons directly, with the Bradley-Terry loss of the implicit reward βlog⁡(πϕ/πref)\beta \log (\pi_\phi / \pi_{\text{ref}}). Steps 3 and 4 merge into one supervised step, with no sampling.
  • Best-of-n. Sample nn responses and keep the one with the highest reward-model score. No weights change. For continuous outputs with no ties, the KL divergence of the best-of-n distribution from the base is log⁡n−(n−1)/n\log n - (n - 1)/n (Stiennon et al., Appendix G.3), so 0.6360.636 nats for n=4n = 4 and 1.8351.835 for n=16n = 16.
  • Other policy-gradient methods. Any method that follows the policy gradient theorem can replace PPO, for example REINFORCE with a baseline. PPO is the common choice because its clipped step limits how far one batch can move the policy.
  • The pretraining mix. Ouyang et al. call the version with γ>0\gamma > 0 PPO-ptx. It reduced the drop in scores on public NLP data sets that plain PPO caused.
  • Entropy-regularized control. The KL penalty to a reference is a form of maximum entropy reinforcement learning, with the reference in the place of the uniform policy.

Pitfalls

  • Reward overoptimization. The reward model is a proxy. Gao, Schulman and Hilton (2022) measured that the true score first rises and then falls as the policy moves away from its start, in a synthetic setup where a 6B reward model stood in for people. Watch a held-out judge, not the training reward.
  • The KL penalty is not a cure. In the same study the penalty made the true score level off earlier but did not move the curve of true score against KL distance, so it acted like early stopping (Section 3.6). They also note that this result could be sensitive to the settings.
  • The alignment tax. Ouyang et al. found that plain PPO lowered scores on public data sets such as SQuADv2 and DROP. Raising β\beta to 2.0, 100 times the default, did not fix this on the 1.3B model, but mixing in pretraining gradients did (Appendix E.6).
  • People disagree. Labelers in Ouyang et al. agreed with each other on about 73% to 77% of comparisons. The reward model learns the preferences of the people who labeled, not of everybody.
  • A per-token KL estimate can be negative. The sum of the log ratios over one sampled response is an unbiased estimate of the KL divergence, but one sample can give a negative value. Report the mean over many samples.
  • Shift of the reward. The comparisons fix the reward only up to a constant for each prompt. Ouyang et al. add a bias so that the demonstrations score 0 on average before reinforcement learning (Section 3.5). A constant shift does not change the optimal policy, but it does change the targets that the value function must learn.

Exercises

  1. Prove the theorem of the template: write Eπ[r]−β KL(π ∥ πref)\mathbb{E}_\pi[r] - \beta\, \mathrm{KL}(\pi \,\|\, \pi_{\text{ref}}) as βlog⁡Z−β KL(π ∥ π∗)\beta \log Z - \beta\, \mathrm{KL}(\pi \,\|\, \pi^*). Then check the row β=1\beta = 1 of the table, and show that the objective there equals log⁡Z\log Z with Z=2.793296Z = 2.793296.
  2. Show that for a response sampled from π\pi, the expected sum of the per-token log ratios log⁡π(yt∣x,y<t)−log⁡πref(yt∣x,y<t)\log \pi(y_t \mid x, y_{<t}) - \log \pi_{\text{ref}}(y_t \mid x, y_{<t}) is KL(π(⋅∣x) ∥ πref(⋅∣x))\mathrm{KL}(\pi(\cdot \mid x) \,\|\, \pi_{\text{ref}}(\cdot \mid x)). Give a two-response example in which one sample has a negative sum.
  3. Show that replacing r(x,y)r(x, y) by r(x,y)+c(x)r(x, y) + c(x) leaves π∗\pi^* unchanged. What happens to the maximum value βlog⁡Z(x)\beta \log Z(x)?
  4. Let the base model output a continuous score with no ties, and let best-of-n keep the maximum of nn draws. Show that the density ratio of the best-of-n distribution to the base at quantile uu is nun−1n u^{n-1}, and derive KL=log⁡n−(n−1)/n\mathrm{KL} = \log n - (n - 1)/n.
  5. In the toy table, which values of β\beta keep KL(π∗ ∥ πref)\mathrm{KL}(\pi^* \,\|\, \pi_{\text{ref}}) below 0.1? Find the smallest such β\beta to three digits by bisection.

References

  1. Paul Christiano, Jan Leike, Tom B. Brown, Miljan Martic, Shane Legg and Dario Amodei, Deep reinforcement learning from human preferences, 2017, the abstract (feedback on less than 1% of the interactions of the agent, and new behaviors from about an hour of human time) and Section 2 (the method)
  2. Nisan Stiennon, Long Ouyang, Jeff Wu and others, Learning to summarize from human feedback, NeurIPS 2020, the abstract (summaries of Reddit posts preferred to the human reference summaries) and Appendix G.3 (the KL divergence of best-of-n)
  3. Long Ouyang, Jeff Wu, Xu Jiang and others, Training language models to follow instructions with human feedback, NeurIPS 2022, Figure 1 (the 1.3B model preferred to the 175B GPT-3), Section 3.5 (the three steps and equation 2), Section 5.1 (the compute of each step), Appendix C.4 (beta = 0.02, gamma = 27.8, 256k episodes, no discount, clip 0.2), Appendix E.6 (fixing regressions on public NLP data sets)
  4. John Schulman, Filip Wolski, Prafulla Dhariwal, Alec Radford and Oleg Klimov, Proximal policy optimization algorithms, 2017, the clipped surrogate objective
  5. Leo Gao, John Schulman and Jacob Hilton, Scaling laws for reward model overoptimization, 2022, Section 1 (the gold score as a function of the square root of the KL divergence) and Section 3.6 (the KL penalty acts like early stopping)
  6. Rafael Rafailov, Archit Sharma, Eric Mitchell, Stefano Ermon, Christopher D. Manning and Chelsea Finn, Direct preference optimization: Your language model is secretly a reward model, NeurIPS 2023, Section 4 (the closed-form optimum of the KL-regularized objective)

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • five exercises