liveBook · Manning

Chapter 6: Training reasoning models with reinforcement learning

What you will learn in chapter 6 of Build a Reasoning Model (From Scratch)

The introduction places reinforcement learning among the ways to improve reasoning models, contrasting training methods with increased inference compute.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction places reinforcement learning among the ways to improve reasoning models, contrasting training methods with increased inference compute.

Key terms: inference compute budget, model-training methods, reinforcement learning (RL)

6.1 Introduction to RL for LLMs

This section introduces reinforcement learning as a way to improve LLM reasoning during training, contrasts it with inference-time scaling and pretraining, and places it within common LLM training pipelines.

Key terms: inference-time scaling, training-time scaling, post-training, pretrained base model, instruction fine-tuning, reasoning training, preference tuning, pretraining, next-token prediction, whole outputs

6.1.1 The original RL pipeline with human feedback (RLHF)

This section explains how RLHF uses human rankings to train a reward model, then uses its scores to fine-tune an LLM’s responses.

Key terms: RLHF, preference-tuning, next-token prediction, whole outputs, human preference, human annotators, pairwise preferences, reward model, scalar reward score, target LLM

6.1.2 From human feedback to verifiable rewards

This section explains how reinforcement learning with verifiable rewards (RLVR) replaces a learned reward model with automatic checks, and where that simplification helps or limits training.

Key terms: RLVR, verifiable rewards, math verifier, ground truth, RLVR training loop, verifier, rewards, reliable verification signals, deterministic, reproducible

6.2 RLVR using GRPO

The section distinguishes RLVR’s verifiable-reward setup from GRPO’s role in updating the LLM and explains why GRPO is a resource-friendly alternative to PPO.

Key terms: RLVR, automatically verifiable rewards, policy, Group Relative Policy Optimization (GRPO), Proximal Policy Optimization (PPO), GRPO, value model, relative comparisons, sampled responses

6.2.1 High-level GRPO intuition via a chef analogy

A chef analogy introduces how GRPO generates and compares several responses, then updates the model while limiting changes from its original behavior.

Key terms: prompt, rollouts, completions, reward, relative feedback, logprobs, policy gradient loss, original cookbook, style-preservation penalty, overall loss

6.2.2 The high-level GRPO procedure

This section introduces the GRPO overview and explains why the book begins with a version that omits the KL loss term.

Key terms: GRPO, KL loss term

6.3 Loading a pretrained model

This section loads a pretrained base model for GRPO rollouts and checks that text generation runs correctly before training.

Key terms: pretrained model, tokenizer, base model, rollouts, GRPO, temperature, top-p sampler, math prompt, CPU, GPU

6.4 Loading a MATH training subset

This section loads a MATH training subset that excludes evaluation problems and explains which fields are used to train and verify model responses.

Key terms: MATH training set, MATH-500, data leakage, problem, answer, math verifier, solution, intermediate reasoning steps, overfitting

6.5 Sampling rollouts

This section defines rollouts for GRPO and adapts the existing text-generation code to sample responses that can be used in training.

Key terms: rollout, prompt, GRPO, @inference_mode, @torch.no_grad, backward pass, gradient tracking, sample_response, token IDs, answer text

6.6 Calculating rewards

This section explains how GRPO assigns rewards to generated rollouts using final-answer correctness and a required answer format, then proposes a partial-credit variation.

Key terms: GRPO, rollout, math verifier, reward_rlvr, fallback=None, \boxed{}, format-aware reward shaping, partial credit, process reward models, intermediate solution steps

6.7 Preparing learning signals from rollouts via advantages

This section turns rollout rewards into group-relative advantages and explains how those advantages guide policy updates.

Key terms: advantage, reward, rollout, mean reward, standard deviation, group relative, gradient, policy update, GRPO loss, zero-advantage cases

6.8 Scoring rollouts with sequence log probabilities

This section computes a differentiable log probability for each complete rollout and explains why GRPO uses that score rather than an average per-token score.

Key terms: token-level logprobs, sequence-level logprobs, rollout, advantage, torch.sum, @torch.inference_mode(), computation graph, torch.gather, SumBackward0, sequence length

6.9 From advantages to policy updates via the GRPO loss

This section combines rollout advantages and sequence-level log probabilities into the GRPO policy gradient loss and explains why the loss is negated and the advantages are detached.

Key terms: policy gradient loss, advantage, sequence-level logprob, rollout, maximization, minimization, .detach(), advantages, gradients, logprobs

6.10 Putting everything together in a single GRPO function

This section combines the GRPO stages in one loss function, explains two implementation choices, and interprets a test result with zero loss.

Key terms: compute_grpo_loss, GRPO stages, pg_loss, policy gradient loss, KL loss term, MATH training dataset, rewards, advantages, loss, \boxed{6}

6.11 Implementing the GRPO training loop

This section puts GRPO into a PyTorch training loop, explains practical settings for running it, and shows how to interpret its reward and loss output.

Key terms: RLVR, GRPO loss, training loop, checkpoints, cpu, cuda, mps, num_rollouts, max_new_tokens, checkpoint_every

6.12 Loading and evaluating saved model checkpoints

Shows how to load and evaluate saved GRPO checkpoints on MATH-500, then interprets their accuracy, response lengths, and training limitations.

Key terms: checkpoint, PyTorch state dict, .pth, MATH-500 verification pipeline, max tokens, rollout, final boxed answer, average tokens, MATH-500, GRPO

6.13 Summary

The section summarizes how RLHF and RLVR train LLMs, how GRPO turns verified answers into a training loss, and the practical cost and potential benefit of that training.

Where this is used later

Concepts explained in this chapter

← Chapter 5: Inference-time scaling via self-refinement Chapter 7: Improving GRPO for reinforcement learning →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.