Recommended resources
The freely available resources this lesson will direct you to.
- Group Relative Policy Optimization (GRPO) - Formula and Codewww.youtube.com
- The ONLY DeepSeek GRPO/PPO video you'll EVER need (with examples and exercises) | RL Foundationswww.youtube.com
- Comparative Analysis and Parametric Tuning of PPO, GRPO ... - arXivarxiv.org
- Group Relative Policy Optimization (GRPO) - Matthew Landersmattlanders.net
Welcome to the next lesson in our exploration of Group Relative Policy Optimization. In our previous discussion, we derived the group-relative advantage estimator, establishing how GRPO creates a powerful, critic-free performance signal by normalizing rewards within a sampled group.
Today, we will assemble the complete GRPO objective function. You'll see how this advantage estimator integrates with two other crucial components inherited and adapted from modern policy optimization techniques: the PPO-style clipped policy ratio and a KL divergence penalty. By the end of this lesson, you will be able to interpret the full objective function and understand the specific role each term plays in balancing performance improvement with training stability.
The Full GRPO Objective Function
Let's begin by looking at the complete objective function. At first glance, it appears complex, but we will find it is composed of three main, interpretable parts that we will deconstruct piece by piece.
The formal GRPO objective, which we aim to maximize, is defined in the paper "Comparative Analysis and Parametric Tuning of PPO, GRPO..." as follows:
This section of the paper presents the formal mathematical definition of the GRPO surrogate objective (Equation 9) and lists its components.
Focus on Equation (9). Don't worry about understanding every symbol immediately. The goal here is to see the overall structure we'll be dissecting: an expectation over a term involving a min function and a term involving D_KL.
As you saw in the paper, the objective function is:
Let's break this down. The expectations and sums indicate that we are averaging this objective over many prompts , groups of outputs, and tokens within each output. The core of the function lies inside the parentheses, which contains two primary components:
- The clipped advantage term:
- The KL penalty:
We will now analyze each of these in detail.
1. The Clipped Advantage Term: Stabilizing Updates
This part of the objective is adapted directly from Proximal Policy Optimization (PPO). Its purpose is to encourage policy updates that improve performance without letting the policy change too drastically in a single step, which can lead to instability.
Let's unpack the terms within the function:
- is the group-relative advantage for token of output , which we derived in our last lesson.
- is the probability ratio. It measures how much more (or less) likely the current policy is to generate token compared to the policy before the update, .
The min function takes the minimum of two values:
- Unclipped Objective:
- Clipped Objective:
The clip function constrains the probability ratio to stay within the range , where is a small hyperparameter (e.g., 0.1 or 0.2).
The following video provides an excellent, detailed walkthrough of how these components work together.
The ONLY DeepSeek GRPO/PPO video you'll EVER need (with examples and exercises) | RL Foundations
This video explains the mechanics of the probability ratio, the clipping mechanism, and the min function with clear numerical examples.
Please watch two segments: First, watch the explanation of the probability ratio and clipping. Focus on understanding why clipping prevents the new policy from straying too far from the old one. Next, watch the segment on the min function. This will clarify how the choice between the clipped and unclipped values penalizes overly optimistic policy updates.
Let's summarize the logic:
- If Advantage is positive: The model is incentivized to increase the probability of this action, which means increasing the ratio . However, the
minfunction ensures that once the ratio exceeds , the objective value is capped by the clipped term. This removes the incentive to make the ratio arbitrarily large, promoting a more conservative update. - If Advantage is negative: The model is incentivized to decrease the probability of this action (decrease the ratio). Here, the logic is slightly different from PPO, but the clipping mechanism in the full PPO objective (which GRPO is based on) similarly prevents overly pessimistic updates. The overall effect is to gently discourage bad actions rather than making drastic, potentially harmful changes.
A Note on Conservatism
You might wonder why such complex machinery is needed for stabilization, especially compared to the more straightforward loss functions in supervised learning. Your background in stochastic systems provides a good intuition here. The feedback in RL (a single reward for a long sequence of actions) is far sparser and noisier than the per-token ground-truth labels in supervised learning. This inherent uncertainty makes RL updates prone to error.
The following short segment gives a brilliant justification for this conservative design philosophy.
The ONLY DeepSeek GRPO/PPO video you'll EVER need (with examples and exercises) | RL Foundations
This part of the video contrasts the noisy, sparse feedback signal in RL with the dense, high-quality signal in supervised learning, motivating the need for conservative update mechanisms like clipping.
Watch the segment from "Why do we need all this?". The speaker's analogy to correcting a small mistake in a long math proof is particularly insightful.
2. The KL Divergence Penalty: Staying Grounded
The second major component of the GRPO objective is the KL divergence penalty: .
- is a fixed reference policy, typically the supervised fine-tuned (SFT) model that we started with before RL training.
- measures the "distance" or divergence between our current, learning policy and the reference policy. It's always a non-negative value.
- is a hyperparameter that controls the strength of this penalty.
Because this term is subtracted from the objective, maximizing the overall objective requires minimizing the KL divergence. In essence, this term acts as a regularizer, pulling the policy back towards the reference model .
This serves a crucial purpose: it prevents the policy from "reward hacking"—finding an esoteric strategy to maximize reward that results in nonsensical or un-fluent text. By staying close to the high-quality SFT model, the policy retains its general language capabilities while specializing on the task defined by the reward function.
The ONLY DeepSeek GRPO/PPO video you'll EVER need (with examples and exercises) | RL Foundations
This segment explains the role of the KL divergence penalty and the trade-off it creates between improving task performance and remaining similar to the original base model.
Watch the explanation from the final term. Pay attention to the role of the \beta coefficient in managing this trade-off.
Synthesis: The Complete Picture
Now we can see how the full objective function works as a cohesive whole.

The training process for each batch involves:
- Sampling a group of responses for a given prompt.
- Calculating the group-relative advantage for each token based on the group's rewards (as covered in the last lesson).
- Calculating the clipped advantage term to determine the primary policy update signal, ensuring stability.
- Calculating the KL divergence penalty to ensure the policy doesn't drift too far from its original, fluent state.
- Combining these to get the final objective value, which is then used to compute gradients and update the model's parameters .
This elegant combination allows GRPO to effectively fine-tune a language model using a simple, empirical reward signal while maintaining the stability and safety features developed in more complex algorithms like PPO.
Conclusion
In this lesson, we constructed the complete GRPO objective function by integrating three key ideas:
- Group Normalization: The advantage is calculated not against a learned critic, but relative to the performance of a peer group of responses. This is the "GR" in GRPO.
- Clipped Advantage: The core update mechanism is a PPO-style clipped objective that multiplies the advantage by a constrained probability ratio. This prevents destructive policy updates and stabilizes training.
- KL Penalty: An explicit KL divergence term is subtracted from the objective to regularize the policy, ensuring it improves on the task without forgetting its fundamental language capabilities learned during pre-training and SFT.
We now have a complete theoretical understanding of the GRPO objective. In the next module, we will transition from theory to practice and begin implementing this objective function from scratch in Python, starting with a function to compute the group-relative advantage.