Skip to content
mentoor

Topic 1 of 6 · 5 min read

RLHF with reward models

Goal: Trace the data and models in an RLHF pipeline and explain what the KL penalty prevents.

RLHF turns human rankings into a score, then trains the model to chase that score without drifting far from the model it started as. It has three stages and four models in memory, and each stage has its own failure.

Check yourself · 1 of 3

What does the KL penalty in RLHF do?

Choose one answer

Why it matters

Supervised fine-tuning (SFT) copies demonstrations. But for many tasks, such as "a helpful, polite reply", people cannot write the perfect answer; they can only say which of two answers is better. RLHF uses that cheaper signal. The InstructGPT paper (2022) used it to make a 1.3B-parameter model that labellers preferred over the much larger 175B GPT-3.

The pipeline

  1. SFT. Fine-tune the base model on demonstrations. This gives the starting policy, also called the reference model.
  2. Preference data and reward model. Sample several answers per prompt, have people rank them, and turn each ranking into pairs. Train a reward model (RM), usually the same architecture with a scalar head, so that it scores the chosen answer above the rejected one.
  3. RL with PPO. The policy writes an answer, the RM scores it, and PPO nudges the policy toward higher scores. A value model estimates how good a state is, to reduce variance.

During step 3 you hold the policy, the frozen reference, the RM and the value model. That is why RLHF is expensive.

What the KL penalty does

The reward model has only seen answers near the SFT model. If the policy wanders far away, the RM is guessing, and the policy can find strange text that scores high but is bad. So the reward is changed to: RM score minus beta times the KL divergence between the policy and the reference. KL measures how far the token probabilities have moved. A larger beta keeps the policy close and safe; a smaller beta allows more gain and more exploitation.

Before and after

Weak plan: "Collect 500 thumbs-up clicks and run PPO." Thumbs-up on single answers is noisy, has no comparison, and has no reference model.

Better plan:

1. SFT on 5k demonstrations -> policy_sft (also the frozen reference)
2. 4 samples per prompt for 20k prompts; ranked by 3 labellers each
3. Train RM on pairs; check accuracy on held-out pairs
4. PPO with reward = RM(x, y) - beta * KL(policy || policy_sft)
5. Watch KL, mean reward and sampled answers every 100 steps

What goes wrong

  • The RM is only as good as its pairs. Labeller disagreement, rushed ranking and prompts unlike real use all leak into the score.
  • With beta too small the policy finds RM blind spots (see the reward hacking topic).
  • Memory and tuning cost: four models, many hyperparameters, and unstable runs. This is why many teams now try DPO or verifiable rewards first.

Examples

Reading a PPO run

A run logs these at step 400:

step  mean_rm_reward  kl_to_ref  mean_len
100   0.8             2.1        180
400   2.9             11.6       410

Reward went up 3.6x, but KL grew 5.5x and answers more than doubled in length. The policy is moving fast and may be paying for score with padding. Raise beta or stop earlier, and read samples at step 400.

Turning a ranking into pairs

Labellers rank four answers best to worst as C > A > D > B. That gives pairs such as (C, A), (C, D), (C, B), (A, D), (A, B) and (D, B). The RM loss only asks that the chosen answer of each pair scores higher than the rejected one. Absolute scores have no meaning.

Key points

  • RLHF has three stages: SFT, a reward model trained on ranked pairs, and RL (usually PPO) against that reward model.
  • The reward model is a learned proxy for human preference, not the preference itself.
  • The KL penalty subtracts a cost for moving away from the reference model, so the policy stays where the reward model is reliable.
  • PPO needs several models at once: policy, reference, reward model and a value model.
  • Log KL, reward and sampled text during the run; a rising reward with rising KL and odd text is a warning.

Common mistakes

  • Running PPO without a frozen reference model, so nothing limits drift.
  • Trusting reward model accuracy on its own training pairs instead of held-out pairs.
  • Collecting thumbs-up clicks on single answers and calling them preference data.
  • Setting beta once and never looking at KL during the run.

Check yourself · 2 of 3

In RLHF, what is the reward model trained on?

Choose one answer

Check yourself · 3 of 3

A PPO run's reward climbs steadily while KL to the reference also climbs sharply. What is the sensible first step?

Choose one answer

Practice items, not counted towards your mastery.

Start here