RLHF Pipeline › Worked Example: Tracing a Single RLHF Training Step
Language AI HandbookAll chapters RLHF Pipeline › Worked Example: Tracing a Single RLHF Training StepOne section · Sections in this chapter6 / 11 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
To make the pipeline concrete, let's trace a single complete training step from prompt to policy update, using specific numerical values at each stage. This worked example connects all the abstract components into a coherent sequence.
Setup: Suppose we have already completed SFT and trained a reward model. The policy starts from the SFT model checkpoint. We use a KL coefficient and a PPO clip epsilon of .
Step 1: Sample a prompt from the training distribution. We draw the prompt "What is gradient descent?" from our prompt library.
Step 2: Generate a response from the current policy. The policy samples token by token, creating the response: "Gradient descent is an optimization algorithm that iteratively adjusts parameters in the direction that minimizes a loss function." Alongside the response, we record the log-probabilities assigned by the policy to each token, which we'll call for each token position .
Step 3: Score the response using the reward model. The reward model receives the full prompt-response pair and outputs a raw score. Suppose it outputs .
Step 4: Compute the KL penalty. We also pass the prompt-response pair through the frozen reference model (the SFT model) and compute token-level log-probabilities. The KL divergence at each token position is:
Summing across all token positions gives the total sequence-level KL divergence. Suppose this comes out to .
Step 5: Compute the total reward. Combining the reward model score with the KL penalty:
where:
- : the raw reward model score for this response
- : the KL penalty coefficient
- : the total sequence-level KL divergence
At this early stage of training, the KL penalty is small because the policy has barely moved from its SFT initialization. The total reward is almost entirely the raw reward model score.
Step 6: Estimate advantages. The advantage tells us how much better this response was than the average response to this prompt. Using a value function baseline with estimated value , the advantage is approximately:
A positive advantage means this response was better than expected. The policy update will increase the probability of generating similar responses.
Step 7: Compute the PPO ratio. For each token, we compute the ratio of the current policy's probability to the old policy's probability (from before this update):
Suppose the mean ratio across tokens is , meaning the policy has moved slightly toward creating this response.
Step 8: Apply the clipped objective. Since , the ratio is within the clipping bounds. The surrogate objective for this response is . The gradient update increases the probability of generating this response, pushing the policy in a direction that the reward model rates more highly.
After this single step, the policy is infinitesimally more likely to generate responses similar to "Gradient descent is an optimization algorithm..." when asked about gradient descent. Repeating this process across thousands of prompts and responses gradually sculpts the policy's behavior across the entire prompt distribution.