Skip to content

Language models, retrieval, prompting, and language adaptation · Instruction, preference, and parameter-efficient adaptation · LNG079 · Training method

Group Relative Policy Optimization (GRPO)

Optimizes language-model policies using relative rewards among groups of responses to the same prompt, avoiding a separately trained value model in the original formulation.

Explained here Component demo, not a trained model CodeSplash's own run

In CodeSplash

Documented in the Academy only. There's no Set up button.

Availability
Explained here
Code example
Component demo, not a trained model
Who ran it
CodeSplash's own run
CodeSplash's run
The reference's example ran in Python and R here, and they agree.
The reference's record
Python: The chapter's authors report running it; there is no record for this entry alone. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Optimizes language-model policies using relative rewards among groups of responses to the same prompt, avoiding a separately trained value model in the original formulation. It is an optimization method used in reasoning-focused training, not a standalone language model.

Question / objective

Can group-relative rewards train a language policy without a separate value network?

Architecture / computational structure

A policy samples response groups, rewards generate normalized advantages, and a clipped objective updates policy weights or adapters. The reference, if used, is frozen; group size, clipping, KL weight, and reward definitions are hyperparameters.

Assumptions

Equal-reward groups carry little relative signal, reward variance affects normalization, and imperfect verifiers can be exploited.

Formula / computational definition

Ai=ri−rˉsr+ϵ,L=1G∑imin⁡ ⁣(ρiAi,clip⁡(ρi,1−ε,1+ε)Ai)−βKL⁡.A_i=\frac{r_i-\bar r}{s_r+\epsilon},\qquad L=\frac1G\sum_i\min\!\left(\rho_iA_i,\operatorname{clip}(\rho_i,1-\varepsilon,1+\varepsilon)A_i\right)-\beta\operatorname{KL}.

For rewards r_1:G, form A_i=(r_i-mean(r))/(sd(r)+epsilon). A clipped policy-ratio surrogate uses these advantages, commonly with a reference-policy KL term; exact normalization and token/sequence aggregation must follow the chosen implementation.

Parameters and notation

Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Group size, sampling temperature, reward functions, clipping range, KL coefficient, epsilon, rollout length, and update schedule.

Fitting, fine-tuning, and adaptation

Train full policy weights or supported adapters with verifiable or carefully audited rewards. Retain the specified old/reference policies, normalize exactly as documented, and handle zero-variance groups. Validate independent accuracy, reward hacking, divergence, response length, compute, and broad-capability forgetting.

How to use and calibrate

Track independent task accuracy, reward saturation, group variance, KL, response length, compute, and general-capability retention. Use development data for settings and an independent test set for the final estimate. Report uncertainty across examples or seeds when the evaluation design supports it; model scores and self-assessments are not automatically calibrated probabilities of correctness.

Example application

Improve mathematical answer accuracy using executable verification and multiple sampled responses per prompt.

Interpretation / pitfalls

Watch out

Removing a critic does not remove rollout or reference-model cost. The code below isolates one stated component; successful execution does not validate model quality or reproduce the cited experiment.

Python and R code examples

Full implementation route: TRL documents GRPO training with specific dataset/reward interfaces. R bridges Python through reticulate. The native example computes group-relative advantages only; it deliberately does not imply a complete clipped-policy optimizer or reproduction of a reasoning model.

Example scope: GRPO group-relative advantages and a simplified scalar clipped ratio term; KL omitted.

Dependencies: Python 3 standard library; base R; shared setup above. No model downloads or external services.

Validation: Python component executed in the combined example script; R counterpart supplied but not runtime-tested here. All numbers are synthetic.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — GRPO group-relative advantages and a simplified scalar clipped ratio term; KL omitted.

Shared setup for this chapter
import math

def softmax(xs):
    m = max(xs)
    es = [math.exp(x-m) for x in xs]
    return [e/sum(es) for e in es]

def dot(a, b):
    return sum(x*y for x, y in zip(a, b))

def matvec(matrix, vector):
    return [dot(row, vector) for row in matrix]

def meanvec(rows):
    return [sum(row[j] for row in rows)/len(rows) for j in range(len(rows[0]))]

def sigmoid(x):
    return 1/(1+math.exp(-x))

def nll(probabilities):
    return -sum(math.log(p) for p in probabilities)
rewards = [0,1,2,1]; mu = sum(rewards)/4
sd = math.sqrt(sum((r-mu)**2 for r in rewards)/4)
advantages = [(r-mu)/(sd+1e-8) for r in rewards]
ratios = [1.1,0.9,1.3,0.8]; epsilon = 0.2
surrogate = sum(min(r*a,min(max(r,1-epsilon),1+epsilon)*a) for r,a in zip(ratios,advantages))/4
print(advantages,surrogate)
R

Component demo, not a trained model — GRPO group-relative advantages and a simplified scalar clipped ratio term; KL omitted.

Shared setup for this chapter
softmax <- function(x) {
  shifted <- exp(x-max(x))
  shifted/sum(shifted)
}
rewards <- c(0,1,2,1); mu <- mean(rewards)
sd_population <- sqrt(mean((rewards-mu)^2)); advantages <- (rewards-mu)/(sd_population+1e-8)
ratios <- c(1.1,0.9,1.3,0.8); epsilon <- 0.2
surrogate <- mean(pmin(ratios*advantages,pmin(pmax(ratios,1-epsilon),1+epsilon)*advantages))
print(list(advantages=advantages,surrogate=surrogate))

Sources

From the CodeSplash reference, entry LNG079.