GRPO, or Group Relative Policy Optimization, is a reinforcement learning algorithm primarily used for training Large Language Models (LLMs), especially for complex reasoning tasks like math and coding[1]. It's known for being more memory-efficient than algorithms like Proximal Policy Optimization (PPO) because it doesn't require a separate value function network.
Instead of estimating future rewards with a value function, GRPO works by[1][2]:
- Generating a group of responses: For a given prompt or query (
q), the current policy (π_θ_old) generates multiple (G or N) responses (o)[3].
- Scoring responses: Each response (
o_i) is evaluated using a reward function (r(o_i) or R(a_i)), which measures its quality (e.g., correctness, formatting).
- Calculating Group Statistics: The mean (
μ) and standard deviation (σ) of the rewards for the entire group of responses are calculated.
- Calculating Advantage: The "advantage" (
A(o_i) or A(a_i)) for each response is computed by normalizing its reward relative to the group's mean and standard deviation[4]. This indicates how much better or worse a specific response is compared to the average response in the group[5][6].
- Updating the Policy: The model's policy (
π_θ) is updated to increase the probability of generating responses with higher advantages and decrease the probability of those with lower advantages[6]. This is typically done using a clipped objective function similar to PPO, often combined with a KL divergence penalty to prevent the policy from changing too drastically from a reference policy (π_ref or the previous policy π_θ_old).
Core Formulas:
-
Advantage Calculation:
The advantage for an output o (or action a_i) within a group generated for a query q is often calculated as:
A(o) = (r(o) - μ) / σ
Where:
r(o) is the reward for the specific output o.
μ is the mean reward of all outputs generated for query q under the old policy (E_[o ~ π_θ_old(.|q)] [r(o)]).
σ is the standard deviation of the rewards of all outputs generated for query q under the old policy (sqrt(Var_[o ~ π_θ_old(.|q)] [r(o)])).
-
GRPO Objective Function:
The overall objective that GRPO aims to maximize is often formulated similar to PPO's clipped objective, incorporating the group-relative advantage. A common form is:
maximize_θ E_q E_[o ~ π_θ_old(.|q)] [ f( π_θ(o|q) / π_θ_old(o|q), A(o) ) ] - β * KL(π_θ || π_ref)
Where:
θ represents the parameters of the policy network being optimized[7].
π_θ(o|q) is the probability of output o given query q under the current policy.
π_θ_old(o|q) is the probability under the policy used to generate the data (the "old" policy).
A(o) is the calculated advantage[5][8][9].
f(x, y) is the clipped surrogate objective function, often defined as:
f(x, y) = min( x * y, clip(x, 1-ε, 1+ε) * y )
This clipping limits how much the policy ratio (x) can change the objective, promoting stability.
β is a coefficient weighting the KL divergence penalty.
KL(π_θ || π_ref) is the Kullback-Leibler divergence between the current policy and a reference policy (often the initial pre-trained model or the previous iteration's policy), acting as a regularization term to prevent large policy shifts.
In essence, GRPO refines the LLM by comparing multiple generated answers for the same prompt, identifying which ones are relatively better based on a reward signal, and nudging the model to produce more answers like the better ones, all without needing a separate network to predict future rewards[1].