PPO, KL Penalty, and Reward Hacking

PPO (Proximal Policy Optimization) is a common way to run the RL part of RLHF. The practical story: sample answers with a policy, score them, update carefully for a few epochs, and keep the new model close to a reference model.

The last lesson said: raise probability when advantage is positive. PPO is a careful way to do that without the policy leaping into strange wording overnight.

Intuition

Updating every step from fresh samples is expensive. A common PPO-style pattern:

  1. An older policy generates trajectories (answers)
  2. Those cached trajectories are reused for several update epochs
  3. A KL penalty acts like a leash so the aligned model does not drift into strange wording just to chase reward

Dog-on-a-leash picture:

How it works

PPO training idea (plain English)

Step What happens
Sample Generate responses with the current / old policy
Score Get rewards / advantages for those responses
Update Improve the policy for a few epochs on that cached experience
Constrain Keep behavior near the reference model (KL leash)

You do not always need two live full copies updating at once — caching trajectories and reusing them is part of the efficiency story.

flowchart TB Old[Old policy samples answers] --> Cache[Cache those trajectories] Cache --> Score[Score / advantage] Score --> Upd[Update policy a few epochs] Ref[Reference model] --> KL[KL leash] Upd --> KL KL --> New[New policy, still nearby]

Tiny sketch (concept only):

# 1) sample once, reuse a few times
answers = old_policy.sample(prompts)
advantages = score_and_advantage(answers)

for epoch in range(few_epochs):
    pg_loss = ppo_policy_loss(policy, answers, advantages)
    kl = kl_divergence(policy, reference, answers)   # how far from home
    loss = pg_loss + kl_coef * kl                    # leash
    loss.backward()
    optimizer.step()

few_epochs on cached answers is the efficiency move: you do not resample a brand-new batch for every tiny update.

KL divergence as a leash

KL divergence measures how far one probability distribution has moved from another. In alignment:

If KL is over-tight, the model barely moves — almost no alignment progress. If KL is missing, fluent nonsense that “scores well” can win.

The reference is usually the SFT model you started from: the intern who already writes decent answers, before preference optimization.

Reward hacking

Reward hacking means the model finds shortcuts that raise the score without truly solving the user’s need — for example, overly flattering text, empty verbosity, or patterns the reward model wrongly likes.

If the reward signal is imperfect, RL will exploit the cracks.

Same prompt, two “high score” answers:

The training loop only sees the number. If verbosity or sycophancy inflates the score, the policy learns that habit.

That is why later lessons still check human preference, not only the automatic reward.

What goes wrong

One-line summary

PPO improves the policy on sampled answers with careful updates; KL limits drift; reward hacking is cheating the score instead of helping the user.

Key terms