6 Post-training and Alignment
  1. In this chapter
  2. SFT Data
  3. LoRA Fine-Tuning in Practice
  4. Reward Models
  5. Reinforcement Learning Basics
  6. Preference Optimization
  7. Reinforcement Learning for Reasoning
  8. Reasoning RL systems
All chapters →

Part 2 · Train LLMs Chapter 6

Post-training and Alignment

SFT, LoRA, reward models, reinforcement learning basics (policy gradients, advantages, PPO), preference optimization (RLHF and DPO) and reinforcement learning for reasoning: how a base model is shaped into something useful and controllable that learns to solve problems deliberately.

7 pages about 5 h Chapter labs on GitHub ↗ Intermediate → Advanced Updated

After this chapter you will be able to

  • Build and clean an SFT dataset, and say how data size, label noise and the loss mask affect the result.
  • Fine-tune with LoRA under limited memory, and explain the trade-offs of rank, learning rate and target modules.
  • Explain how a reward model learns a score from pairwise comparisons, what ceiling label noise puts on its accuracy, and what happens when it is optimized too hard.
  • Derive the policy gradient, say why a baseline does not change its expectation, and tell what PPO's clipping and the KL penalty each constrain.
  • Write the three-step RLHF pipeline and the DPO objective, and say which part of the pipeline DPO removes.
  • Explain how verifiable rewards (RLVR) and GRPO teach a model long chains of reasoning, and read its advantage estimate.

A base model can only “continue text”. Making it understand instructions, match human preferences, and reason step by step on math and code is the job of post-training. This chapter follows the order of the pipeline and covers six things: what data teaches it to follow instructions (SFT), how to do that with very little memory (LoRA), how to turn human preferences into a score that can be optimized (reward models), the reinforcement learning basics (policy gradients, baselines and advantages, PPO and KL), how to make its answers fit human preferences (RLHF and DPO), and how verifiable rewards teach it to reason (RLVR and GRPO).

Every page uses small experiments to measure how much each factor matters, instead of only stating conclusions: how much data size, label noise, rank and learning rate each change, how true quality first rises and then falls when a reward model is optimized too hard, and how much noise and drift baselines and clipping each remove. A model that comes out of post-training goes next into Inference and Serving, and must be checked in Evaluation to see whether it actually got better.

Matching paper explainers: InstructGPT, Learning to Summarize from Human Feedback, PPO, LoRA, DPO, DeepSeekMath (where GRPO comes from), DAPO, GSPO and DeepSeek-R1.

Rollout and learner scheduling follows reasoning RL through generation, grading, batching, and weight publication.

Chapter roadmap

  1. 6.1

    SFT Data: Construction and Quality Control

    How to weigh the quantity and quality of SFT data and check it: measured effects of data size, label noise, the loss mask and mixed-in general data (Qwen2.5-0.5B on banking77), plus rule filters, near-duplicate removal and contamination checks on Alpaca, Alpaca-cleaned and Dolly.

    Intermediate 66 min read

    Prerequisites5.1The LLM Training Pipeline

  2. 6.2

    LoRA Fine-Tuning in Practice

    Over fifty training runs of Qwen2.5-0.5B on banking77: the formula, the parameter count and a from-scratch implementation, then learning rate, rank, target modules, epochs, full fine-tuning and QLoRA compared, and the pitfalls of merging and deployment, with code and numbers behind every claim.

    Intermediate 72 min read

    Prerequisites3.4Transformer Block Explained5.1The LLM Training Pipeline

  3. 6.3

    Reward Models: Turning Human Preferences into a Score to Optimize

    A reward model scores a prompt-and-response pair with one number, trained on pairwise comparisons; RLHF, rejection sampling and best-of-n all use it. This page derives the Bradley–Terry loss, measures the accuracy ceiling set by label noise, and reproduces overoptimization in simulation.

    Intermediate 46 min read

    Prerequisites6.1SFT Data2.4Information Theory Refresher

  4. 6.4

    Reinforcement Learning Basics: Policy Gradients, Advantages and PPO

    Policy gradients, baselines, GAE, PPO clipping and the KL penalty are the toolkit behind RLHF and reasoning RL. This page measures each on a toy task small enough to enumerate exactly: how baselines cut variance, why clipping is not a trust region, and where the KL belongs.

    Intermediate 58 min read

    Prerequisites2.2From Language Modeling to Next-Token Prediction2.4Information Theory Refresher6.3Reward Models

  5. 6.5

    Preference Optimization: From RLHF to DPO

    After SFT a model follows instructions, but its answers are not always the ones people prefer. Preference optimization aligns it using comparisons: RLHF trains a reward model and optimizes with PPO, while DPO reduces the pipeline to a classification-style loss on preference pairs.

    Advanced 17 min read

    Prerequisites6.1SFT Data6.3Reward Models2.4Information Theory Refresher6.4Reinforcement Learning Basics

  6. 6.6

    Reinforcement Learning for Reasoning: RLVR and GRPO

    Since o1 and DeepSeek-R1, making a model think longer before answering has become the main line of post-training. Why verifiable rewards (RLVR) can replace a reward model, how GRPO drops the value model, and the recipe and pitfalls of R1-Zero and R1.

    Advanced 21 min read

    Prerequisites6.5Preference Optimization6.4Reinforcement Learning Basics

  7. 6.7

    Reasoning RL systems: asynchronous rollouts and policy lag

    Reasoning RL joins generation, grading, learning, and weight publication. Asynchronous execution overlaps work and introduces policy lag. A runnable scheduling simulation separates elapsed time, accepted batches, and discarded trajectories.

    Intermediate 9 min read

    Prerequisites6.6Reinforcement Learning for Reasoning4.5Collective Communication