Introduction
Welcome to our deep dive into the mathematical core of Group Relative Policy Optimization (GRPO). In the last lesson, we outlined the four main steps of the GRPO algorithm: group sampling, reward computation, advantage normalization, and the final policy update. We established that GRPO's key innovation is replacing the complex critic network with a simple, empirical baseline derived from a group of generated responses.
Today, we will focus exclusively on that final, crucial step: the policy update. Our learning outcome is to write the GRPO objective function and identify the clipped importance ratio and KL divergence terms. This objective function is the mathematical heart of the algorithm; it dictates precisely how the model learns from the rewards it receives and, just as importantly, ensures the training process remains stable. By the end of this lesson, you will be able to deconstruct this formula and explain the purpose of each of its components.
The GRPO Objective Function: An Overview
The goal of our reinforcement learning process is to update the policy's parameters, , to maximize a certain objective. The GRPO objective function, , defines what we are maximizing.
Let's look at the full equation as presented in the official DeepSeek-R1 paper. Don't worry about understanding every symbol immediately; we will break it down piece by piece.
DeepSeek-R1: Incentivizing Reasoning Capability in LLMs via Reinforcement Learning
The DeepSeek-R1 paper provides the canonical definition of the GRPO objective function. We will use this as our starting point.
Focus on Equation (1). This is the complete GRPO objective function. We will spend the rest of the lesson dissecting this formula.
The objective function presented in the paper is:
This formula can be broken into two principal components, which are illustrated in the diagram below:
- The Clipped Surrogate Objective: This is the part with the
minandclipfunctions. It's responsible for driving the policy to produce higher-reward outputs. - The KL Divergence Penalty: This is the term. It acts as a regularizer to keep the training process stable.

Let's now investigate each of these components in detail.
Part 1: The Clipped Surrogate Objective
This part of the objective function is inherited directly from Proximal Policy Optimization (PPO). It's a clever mechanism designed to encourage beneficial updates while preventing the policy from changing too drastically in a single step. It consists of two key pieces: the importance sampling ratio and the clipping mechanism.
The Importance Sampling Ratio
The core of the update is the ratio:
- is the probability of generating output given the query with the new policy parameters that we are optimizing.
- is the probability of generating the same output with the policy parameters before the update.
This ratio measures how the policy is changing. A ratio > 1 means the new policy makes the output more likely; a ratio < 1 means it makes it less likely. This ratio is then multiplied by the advantage (the normalized, group-relative reward we calculated in the previous step). The product tells us whether the policy change is aligned with the reward signal.
The Clipping Mechanism
The problem with just using the ratio is that it can become very large or very small, leading to unstable, oversized updates. To solve this, PPO and GRPO introduce clipping.
The full term is:
The clip function constrains the ratio to stay within the range , where is a small hyperparameter (e.g., 0.2).
By taking the min of the unclipped and clipped versions, the objective function creates a pessimistic bound.
- If the advantage is positive (a good output), we want to increase the ratio. However, the clipping prevents the update from being too large, promoting stability.
- If the advantage is negative (a bad output), the update will seek to decrease the ratio. The
minfunction ensures that even if the gradient suggests a very large, potentially incorrect update, we take the more conservative step.
To solidify your understanding of these components, please consult the following resources.
Advanced Understanding of Group Relative Policy Optimization
This Hugging Face article provides a clear, step-by-step explanation of the GRPO objective function's components.
Please read sections 'Step 3: Policy Update', '1. Probability Ratio', and '2. Clip Function'. Focus on the interpretation of the ratio and how the clipping mechanism promotes training stability.
DeepSeek R1 Theory Tutorial – Architecture, GRPO, KL Divergence
This segment from a freeCodeCamp.org tutorial visually explains the role of clipping in the PPO/GRPO objective.
Watch from 08:50 to 11:23. Pay close attention to how the speaker describes the 'confidence' (log probability ratio) and the 'clipping' as a way to avoid taking steps that are 'too big'.
Part 2: The KL Divergence Penalty
The second major part of the GRPO objective is the KL divergence penalty:
- stands for Kullback-Leibler (KL) divergence. Conceptually, it's a measure of how different one probability distribution is from a second, reference probability distribution. A KL divergence of 0 means the distributions are identical.
- is our current, learning policy.
- is a fixed reference policy. This is typically the supervised fine-tuned (SFT) model before the RL training began.
- is a hyperparameter that controls the strength of this penalty.
The purpose of this term is to act as a regularizer. While the clipped surrogate objective pushes the model to explore better reasoning paths, the KL penalty acts as a "leash," preventing the policy from diverging too far from the sensible, coherent behavior of the initial reference model. This helps prevent the model from learning to "cheat" the reward function by generating nonsensical but high-scoring outputs, a common failure mode in RL known as reward hacking.
The following resources explain this concept in more detail.
Advanced Understanding of Group Relative Policy Optimization
The same Hugging Face article also has an excellent section on the KL divergence term.
Please read the section '3. KL Divergence'. Focus on what the reference policy (π_ref) is and why penalizing divergence from it is important for stability. Note the explanation of the β parameter.
DeepSeek's GRPO (Group Relative Policy Optimization) | Reinforcement Learning for LLMs
This clip from Julia Turc's video provides an excellent intuitive explanation of the KL divergence penalty and contrasts how it's used in GRPO versus PPO.
Watch from 20:00 to 21:23. The key insight here is understanding why GRPO adds the KL term to the loss directly, acting as a constraint on the distance from the original reference model.
Tying It All Together
We have now examined the two main parts of the GRPO objective function:
- A clipped surrogate objective that encourages the policy to increase the probability of high-advantage outputs in a stable way.
- A KL divergence penalty that regularizes the policy, ensuring it doesn't stray too far from a trusted reference model.
The final objective, , is maximized with respect to the policy parameters using gradient ascent. The expectation and the summation indicate that this calculation is averaged over all queries in a batch and all outputs within each group.
Since you have a background in PyTorch, it might be helpful to see how this mathematical formula translates into code. The final section of the Hugging Face article provides a practical implementation example. You don't need to code this yourself, but reviewing it can help connect the abstract formula to a concrete implementation.
Advanced Understanding of Group Relative Policy Optimization
Let's look at a practical implementation to see how the math translates to code.
Briefly scan through the 'Implementation Example' section, particularly '3. Updating the Policy'. Notice how the ratio, advantages, clipping, and per_token_kl from the formula are all calculated and combined in the final loss.
Conclusion
In this lesson, we have thoroughly analyzed the GRPO objective function. You can now not only write the formula but also identify and explain the purpose of its critical components.
Key Takeaways:
- The GRPO objective function is:
- The clipped importance ratio term, inherited from PPO, uses the group-relative advantage to guide the policy update while using clipping to ensure stability and prevent destructive, large updates.
- The KL divergence term acts as a regularizer, penalizing the policy if it deviates too much from a stable reference policy , which helps maintain coherence and prevent reward hacking.
Preview of the Next Lesson:
We have now completed our study of the GRPO algorithm's mechanics. The next logical question is: what is the result of applying this specific training process? In our next lesson, we will explore how reasoning behaviors emerge during GRPO training. We will examine the full DeepSeek-R1 training pipeline, including the "cold start" and "rejection sampling" phases, to understand how the model learns to self-verify, backtrack, and generate the long, structured chains of thought that are the hallmark of advanced reasoning.