Overview

7 Reinforcement learning: From policy gradients to GRPO

This chapter shifts from building a strong base language model to improving its behavior after pretraining. It explains how reinforcement learning can turn text prediction into deliberate problem solving by rewarding complete responses instead of supervised next-token targets, using language generation as a sequential decision process with prompts as states, sampled tokens as actions, and final scores as rewards. The chapter’s core motivation is that reasoning tasks often have delayed, sparse feedback, so the model must learn which intermediate behaviors help it reach a correct final answer.

The chapter then develops the main policy-gradient ideas used to train language models from rewards. It shows why sampling is necessary for exploration, how raw rewards are noisy, and why advantages relative to a baseline are more stable than using rewards alone. PPO is presented as a practical solution that keeps policy updates near the data-generating policy with clipped ratios, while also relying on a value model for a learned baseline and a frozen reference model for KL control. In contrast, GRPO removes the value model and replaces it with group-relative normalization: multiple completions are sampled for the same prompt, scored, and compared against one another so that better completions get positive advantages and worse ones get negative ones.

The chapter closes by connecting these methods to the DeepSeek-R1 family and to a minimal PyTorch implementation. It explains how verifiable rewards from deterministic checkers can drive reasoning on math and code tasks, why cold-start supervised examples and later mixed-data fine-tuning are still needed for readability and general assistant behavior, and how rejection sampling turns strong RL outputs into supervised data for distillation. The code walkthrough makes the algorithm concrete with a verifier, group-relative advantage calculation, a clipped GRPO loss, and a one-step training skeleton, while also emphasizing real-world concerns such as masking, parsing, KL monitoring, reward hacking, and the distinction between algorithmic ideas and production-scale training systems.

Our four-stage journey to build the DeepSeek model. This chapter moves into post-training, where reinforcement learning, GRPO, verifiable rewards, and reference-policy control become central.
The agent-environment interface. The cleaning robot observes the room, chooses an action, receives a reward, and then repeats the cycle from the updated room state.
Mapping reinforcement learning concepts to LLM generation. The growing context is the state; each sampled next token is an action, and the final response receives a reward.
Sparse reward timing in LLM reinforcement learning. Tokens are generated one at a time, but the task reward is often assigned only after the full response is complete.
A probability distribution over the next token. During RL training, the model samples from this distribution rather than always selecting the highest-probability token.
PPO-style language-model training uses a trainable policy, a reward signal, a learned value baseline, and a frozen reference policy for KL control.
GRPO group sampling with reference control. Multiple completions are generated for the same prompt, scored, normalized into advantages, and updated under a KL constraint.
Group-relative advantage calculation. For binary rewards [1, 1, 0, 0], the group mean is 0.5, so correct completions receive positive advantages and incorrect completions receive negative advantages.
From PPO to GRPO to GRPO with verifiable rewards. GRPO removes the value model; RLVR replaces a learned reward model only for objectively checkable tasks.
RLVR can replace learned reward models for objectively verifiable tasks such as math and code; open-ended assistant behavior still requires preference signals.
The aha moment example from DeepSeek-R1 training. The <think> tag marks the model's reasoning region; the notable behavior is that the model notices a flawed path and changes strategy.
Accuracy and response length of DeepSeek-R1-Zero during RL training. Source: Guo et al., DeepSeek-R1, Figure 1.
The DeepSeek-R1 model lineup. R1-Zero demonstrates emergent reasoning; R1 improves readability and robustness; Distill transfers R1-generated reasoning data into smaller students.
DeepSeek-R1 training pipeline, adapted from Guo et al., DeepSeek-R1, Figure 2. The flow from R1-Zero into R1 represents filtered training data, not direct parameter transfer.
Implementation workflow for the chapter code. Prompts are sampled, completions are verified, rewards are normalized, and the policy is updated with a clipped GRPO objective plus KL control.

Summary

  • LLM reinforcement learning treats the growing context as the state and each sampled token as an action.
  • Policy-gradient methods update the probability of sampled actions in proportion to reward or advantage.
  • PPO stabilizes policy updates with clipping and commonly uses a learned value model as a baseline.
  • The value function Vϕ(s) is produced by a learned value model or value head trained from rollout returns.
  • GRPO removes the learned value model by normalizing rewards within groups of completions sampled for the same prompt.
  • GRPO is cheaper than PPO for the same total number of sampled completions because it eliminates the value-model path, not because it samples fewer responses.
  • RLVR replaces learned reward models only when a task has objectively verifiable answers, such as many math and code tasks.
  • DeepSeek-R1-Zero demonstrates emergent reasoning from GRPO plus verifiable rewards; DeepSeek-R1 adds staged SFT and RL to make the behavior more usable.
  • The flow from R1-Zero into R1 is a data flow: filtered outputs become training data, not direct parameter transfer.
  • The chapter code provides concrete hooks for verifier rewards, group-relative advantages, clipped GRPO loss, and a training-step skeleton.

FAQ

What is the main goal of reinforcement learning in this chapter’s LLM training setup?To push a pretrained language model beyond next-token prediction and toward useful reasoning behavior by letting it generate full responses, score them with rewards, and update the policy to make rewarded behavior more likely.
How is a language model mapped to the reinforcement learning framework?The prompt plus generated tokens form the state, the next sampled token is the action, appending that token creates the next state, and a reward is assigned to the completed response or trajectory.
Why does the chapter emphasize sampling instead of greedy decoding during RL training?Sampling is needed for exploration. If the model always chose the most likely token, it would be much harder to discover alternative response paths that might lead to better rewards.
What is sparse reward, and why does it create a credit-assignment problem?Sparse reward means the model gets feedback only after a full sequence of actions, such as at the final answer. This makes it hard to tell which individual tokens or intermediate steps were responsible for success or failure.
What is the basic policy-gradient idea used to update an LLM from reward?The update increases the probability of sampled tokens that led to high reward and decreases the probability of sampled tokens that led to low reward, using gradients of the log probability of the sampled actions.
Why are raw rewards considered too noisy, and what is an advantage?Raw rewards do not account for how hard a prompt was. An advantage compares a reward against a baseline, showing whether a response was better or worse than expected, which reduces variance and stabilizes training.
What components does PPO typically use in LLM post-training?PPO commonly uses a trainable policy model, a reward signal, a value model to estimate a baseline, and a frozen reference model to control drift with a KL penalty.
How does GRPO simplify PPO?GRPO removes the learned value model and replaces it with group-relative reward normalization. Instead of predicting a baseline with a critic, it samples multiple completions for the same prompt and compares their rewards within the group.
What is reinforcement learning with verifiable rewards (RLVR)?RLVR is reinforcement learning where the reward comes from a deterministic checker rather than a learned reward model. It is especially useful for tasks like math or code where correctness can be objectively verified.
What is the relationship between DeepSeek-R1-Zero, DeepSeek-R1, and DeepSeek-R1-Distill?R1-Zero is the pure RL experiment with verifiable rewards, R1 is the more polished staged pipeline that combines supervised data, RL, rejection sampling, and hybrid rewards, and R1-Distill transfers reasoning behavior into smaller models through supervised fine-tuning on teacher-generated data.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build a DeepSeek Model (From Scratch) ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build a DeepSeek Model (From Scratch) ebook for free