GRPO Algorithm Formula
Group Relative Policy Optimization (GRPO) is a reinforcement learning algorithm designed to optimize language models by comparing groups of generated responses, normalizing their rewards, and updating the policy while constraining divergence from a reference policy. Below is the canonical formula and its components, as described in the literature and technical documentation.
1. Core Objective Function
The GRPO objective for updating the policy (\pi_\theta) is:
[
\max_{\pi_\theta} ; \mathbb{E}{x \sim \mathcal{D}, ; y{1:k} \sim \pi_{\text{old}}(\cdot|x)} \left[ \frac{1}{k} \sum_{i=1}^k \min \left( r_\theta(y_i|x), ; \text{clip}(r_\theta(y_i|x), 1-\epsilon, 1+\epsilon) \right) \cdot \hat{A}i \right] - \beta \cdot \text{KL}(\pi\theta(\cdot|x) | \pi_{\text{ref}}(\cdot|x))
]
Where:
- (x) is a prompt/context sampled from the dataset (\mathcal{D})
- (y_{1:k}) are (k) responses sampled from the old policy (\pi_{\text{old}})
- (r_\theta(y_i|x) = \frac{\pi_\theta(y_i|x)}{\pi_{\text{old}}(y_i|x)}) is the policy ratio
- (\epsilon) is the clipping parameter (as in PPO)
- (\hat{A}_i) is the normalized advantage for response (i)
- (\beta) is the KL penalty coefficient
- (\text{KL}(\cdot)) is the Kullback-Leibler divergence between the new policy and the reference policy[3][2][7][1]
2. Advantage Normalization
The advantage for each response (i) within a group is normalized as:
[
\hat{A}_i = \frac{R_i - \bar{R}}{\sigma_R}
]
Where:
- (R_i) is the reward assigned to response (i)
- (\bar{R} = \frac{1}{k} \sum_{j=1}^k R_j) is the mean reward for the group
- (\sigma_R) is the standard deviation of the rewards in the group
This normalization ensures that the advantage reflects how much better or worse a response is relative to its peers, regardless of absolute reward scale or question difficulty[6][3][2].
3. KL Divergence Penalty
The KL penalty term ensures the updated policy does not drift too far from a reference policy (often the initial or base model):
[
\text{KL}(\pi_\theta(\cdot|x) | \pi_{\text{ref}}(\cdot|x))
]
This term is weighted by (\beta) and is subtracted from the main objective, penalizing large deviations[3][2][7].
4. Clipped Surrogate Objective (Optional)
To prevent excessively large policy updates, the policy ratio is clipped:
[
\min \left( r_\theta(y_i|x) \cdot \hat{A}i, ; \text{clip}(r\theta(y_i|x), 1-\epsilon, 1+\epsilon) \cdot \hat{A}_i \right)
]
This mechanism, borrowed from PPO, ensures stable and conservative updates[1][2][3].
Summary Table
| Component | Formula/Description |
|---|
| Policy Ratio | ( r_\theta(y_i |
| Advantage | ( \hat{A}_i = \frac{R_i - \bar{R}}{\sigma_R} ) |
| Clipping | ( \text{clip}(r_\theta(y_i |
| KL Penalty | ( \beta \cdot \text{KL}(\pi_\theta(\cdot |
| Objective | See full formula above |
References to Key Sources
- The formula and its components are detailed in the DeepSeek Math paper, HuggingFace TRL documentation, and recent arXiv analyses[3][2][1][7].
- The advantage normalization and group-based comparison are what distinguish GRPO from PPO and DPO[6][3][2].
In Words
GRPO trains a policy by generating multiple responses for each prompt, scoring them, normalizing those scores within the group, and updating the model to favor responses that outperform their peers—while keeping the model close to a reference policy using a KL penalty and stabilizing updates with clipping[3][2][6][7].
References:
[1] YouTube: Group Relative Policy Optimization (GRPO) - Formula and Code
[2] HuggingFace TRL Documentation: GRPO Trainer
[3] arXiv: What is the Alignment Objective of GRPO?
[6] DiamantAI: Teaching Machines to Reason
[7] LinkedIn: Group Relative Policy Optimization (GRPO) in Reinforcement ...