RLHF with a KL penalty
Maximize E[r(x,y)] − β·KL(π_θ ‖ π_ref) with PPO. Explain why the KL term keeps the policy near the SFT model and how β trades reward against drift.
Builds on: PPO: clipped updates and a learned value baseline, Reward models from human comparisons, Cross-entropy and KL divergence, Supervised fine-tuning on demonstrations
What the animation says
Why the policy needs a leash
After fine-tuning, PPO pushes the policy toward whatever the reward model scores highly. But the reward model only learned from comparisons of fine-tuned style answers. Near that kind of text, its scores mean something. Far away, they're guesses. A policy chasing those guesses drifts into odd text that scores high, but that people like less. So we need a leash: room to improve, and a dial for how far it may go.
A per-token fee
Keep a frozen copy of the fine-tuned model. Call it the reference. Every token the policy samples pays a fee: beta times its log-ratio. The log-ratio says how many times more likely the policy made this token than the reference would. Tokens both models agree on cost almost nothing. A token the reference finds near-impossible costs a lot. Now: a token the policy made less likely than the reference would. What does it pay?
On average, the fee is KL
It earns a small credit. Its log-ratio is negative, so the fee flips sign. But averaged over what the policy samples, the fee is the KL divergence, which is never negative. KL is cross-entropy minus the policy's own entropy, so collapsing raises the fee. The policy can't squeeze onto one answer for free. The full goal, for each prompt: expected reward minus beta times KL. Since KL sums over tokens, each token's reward is minus beta times its log-ratio. The last also gets the score.
Per-token rewards in code
Here's one sampled response, four tokens long. These two lines hold each token's log-probability under the policy and under the reference. Beta is zero point one, and the reward model scored the response two. Subtract to get the log-ratios: minus zero point two, zero point four, zero point five, zero. Their sum, zero point seven, estimates the KL. Before we compute the rewards: which token earns a credit? Tap it.
How β reshapes the best policy
These rewards, score included on the last token, feed PPO's usual advantage computation. So what policy does this objective favor? Exactly the reference, reweighted by e to the reward over beta. Three responses. The reference gives them point five, point three, point two. Rewards: zero, one, two. With beta four, the best policy is point four one, point three two, point two seven. Close to the reference. With beta one: point one eight, point two nine, point five three. Now beta is a quarter. Where does the probability go?
The dial, summed up
Beta a quarter: weights point five, sixteen, five hundred ninety-six. About ninety-seven percent on the top response. Small beta: the reward dominates, and the policy chases it. Large beta: the fee weighs more, so the policy stays near the reference. That's RLHF with a KL penalty: reward minus beta times KL, paid token by token. It keeps the policy where the reward model is trustworthy, and keeps it from collapsing.
A per-token fee for leaving the SFT model
The KL-penalized objective
How β moves the best policy
Turning KL into per-token rewards
import numpy as np
logp_pol = np.array([-1.2, -0.5, -2.0, -0.3]) # log pi_theta(y_t|...)
logp_ref = np.array([-1.0, -0.9, -2.5, -0.3]) # log pi_ref(y_t|...)
beta, r = 0.1, 2.0 # r = reward model score
log_ratio = logp_pol - logp_ref
rewards = -beta * log_ratio
rewards[-1] += r # sequence reward lands on last token
print(round(log_ratio.sum(), 3)) # 0.7 (KL estimate)
print(rewards) # [ 0.02 -0.04 -0.05 2. ]
Remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses