Unit 6 of 10 · about 4 hours · explain, implement
On-policy, off-policy and replayed language data, and sampling and inference policies
Sampling settings are part of the policy. Unrecorded temperature or an unnamed rerank step invalidates every ratio and KL downstream.
You'll be able to
- Classify a language rollout as on-policy, off-policy or replayed, and state which estimators each class permits.
- Implement temperature scaling and top-p filtering on a logits vector and predict their effect on diversity.
- Distinguish greedy, sampling, best-of-N, rejection and search as inference policies, and separate all of them from a learned policy change.
Covers
Read
In this order, only the parts listed
0 of 3 done
- Temperature, top-p and self-consistencyBuild a Reasoning Model (From Scratch) - Chapters 2 to 5
Chapter 4, Sections 4.4 to 4.6, pp. 103 to 135: temperature rescaling with Listing 4.8
generate_text_temp_stream_cache, the top-p filter with the 10-token toy in Listing 4.9, and self-consistency - Rejection sampling and best-of-NRLHF Book - Introduction, Training Overview and Instruction Tuning
Training Process Step by Step (temperature, top-p, completions per prompt, then SFT on selected completions) and Related: Best-of-N Sampling
- On-policy vocabularyRLHF Book - Introduction, Training Overview and Instruction Tuning
Reinforcement Learning, On-policy: the strict sense (data from exactly the current agent) and the preference-tuning sense (generations from that edition of the model)
Focus on
- Temperature divides logits before softmax; top-p keeps the smallest set whose cumulative probability reaches . Both change the distribution, so both belong in the generation-policy record.
- Greedy is temperature 0 (argmax). Sampling draws once per prompt. Best-of-N draws and keeps the top scorer. Rejection fine-tunes on filtered draws. Search explores multiple paths with a selection rule. Only the fine-tuning and RL variants change weights.
- On-policy needs the exact current parameters and sampler. Anything else (older checkpoint, different temperature, another model, replayed text) is off-policy and needs ratios or must stay out of the gradient.
- This extends llm-foundations/causal-training-generation/decoding-policies: the decoding rules are the same, but here each setting defines the behavior policy for an estimator.
Common mistakes · 3
- Logging the model version but not the sampler. A temperature or top-p change without new stored log probabilities turns on-policy data off-policy silently.
- Reporting best-of-N accuracy as policy quality. Selection over N draws inflates scores without changing any weight; the baseline to beat is matched sampling budget, not greedy decoding.
- Reusing old generations as if they were fresh. Replayed text is off-policy for the current parameters even when nothing else changed.
Try it yourself
Closed-book, before checking anything
[-0.7, -3.0, 0.1, -1.2, 2.0, -1.0, -0.5, -2.0, 0.3, 1.5] at temperature 1.0 with top-p 0.9, derive the kept set {4, 9, 8, 2, 6} from cumulative probabilities [0.4538, 0.7290, 0.8119, 0.8798, 0.9170], and name the greedy token (index 4).Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Why does raising temperature increase diversity but never change the argmax?Answer
Temperature above 1 flattens the softmax, so lower-ranked tokens get sampled more often. Dividing every logit by the same positive temperature keeps their order, so the argmax never moves.
2.Self-consistency with 3 samples at temperature and top-p 0.9 beats greedy on MATH-500. Is that a learned improvement?Answer
No. It is selection (majority vote over 3 draws) with more inference compute. A learned claim needs a weight update and a matched-compute baseline.
3.Data was sampled with top-p 0.9 but the stored log probabilities were recomputed with full softmax. What breaks?Answer
The stored probabilities describe a different distribution from the sampler, so ratios and KL terms are wrong. Store the log probabilities from the sampling pass itself.
4.When is data from last week's checkpoint on-policy?Answer
Never for the current parameters under the strict sense. Under the loose preference-tuning sense it counts as that edition's on-policy data, but it is off-policy for any gradient on today's policy.
Practice
- Build a Reasoning Model, Exercise 4.2: temperature and top-p on MATH-500
Exercise 4.2 at the end of Chapter 4: extend
evaluate_math500_streamwith temperature and top-p at 0.9 and compare against the base runRun on CPU with Qwen3-0.6B per the book hardware note. Record the sampler settings beside the accuracy.
- Build a Reasoning Model, Exercise 4.3: self-consistency on MATH-500
Exercise 4.3 at the end of Chapter 4: self-consistency with sample size 3 at temperature and top-p 0.9
Report accuracy per draw versus majority-vote accuracy to separate selection from learning.
You're done when
You can implement temperature plus top-p from a blank file, predict the kept set on the toy logits, and classify any rollout batch by policy freshness.
Only when you can do this without notes.
Leads to
Notes
Resources · 2 for this unit, 8 more in the section
- RLHF Book - Introduction, Training Overview and Instruction TuningNathan Lambert · primary
Study Chapter 1 (introduction), Chapter 3 (training overview: problem formulation and canonical recipes), Chapter 4 (instruction tuning: chat templates and implementation details) and Appendix A (definitions) at https://rlhfbook.com/c/. Map every RLHF component into the track trajectory schema, then implement assistant-only sequence log probabilities and completion masks against hand examples.
- Build a Reasoning Model (From Scratch) - Chapters 2 to 5Sebastian Raschka (Manning) · support
Use Chapter 2 (loading Qwen3 0.6B and a minimal generation function), Chapter 4 (temperature, top-p and self-consistency) and Chapter 5 (token probabilities to log probabilities and sequence scores) as a code companion for log-probability, mask and inference-policy oracles. Chapters 2 to 5 run on a CPU.
Also in this section
- RLHF Book Course - Lectures 0 and 1Nathan Lambert · required
Watch Lecture 0 (the ML foundations of post-training, including KL) and Lecture 1 (overview of Chapters 1 to 3), with slides under https://rlhfbook.com/teach/course/. Produce a component and state-ownership diagram tied to executable tests, not a second set of general notes.
- Training Language Models to Follow Instructions with Human FeedbackOpenAI · required
Study the demonstration, comparison, reward-model and PPO pipeline plus evaluation design. Reconstruct the data and policy dependencies and state which results cannot be inferred from the paper at small scale.
- KL Estimators - Schulman's Note and the RLHF Book CodeJohn Schulman; Nathan Lambert · required
Read John Schulman's note "Approximating KL Divergence" (on joschu.net, linked from the code) for the three Monte Carlo estimators , , and their bias and variance, then the
approx_kl1,approx_kl2andapprox_kl3functions in this file at commit 1042143. Compute all three on sampled tokens from two small policies whose exact KL you know, and say which one each library you use reports. - Berkeley CS 185/285 Deep Reinforcement Learning (Spring 2026) - LLM RLUC Berkeley · support
Slides are public at https://rail.eecs.berkeley.edu/deeprlcourse/static/slides/lec-N.pdf; current videos are not, and the public Fall 2023 playlist numbers lectures differently. Study Lecture 14 (LLM RL) for the language-model-as-policy formulation and how it maps onto the policy-gradient lectures.
- Stanford CS224R 2026 LLM RL Default ProjectStanford University · support
Read Sections 1.1 through 1.4 for the Countdown task, distinct SFT, preference and verifier datasets, objective boundaries and evaluation. Use it as a design specification and write independent code; do not submit, copy or publish Stanford course solutions.
- Hands-on Modern RL - Chapter 13 RLHF PipelineWalking Labs · support
Use the base-model-to-assistant page and the PPO-RLHF loop page for the pipeline and the token-level ratio, clip and KL order of operations. Its KL sketch is the simple log-ratio difference; audit masks and log probabilities independently and defer scale engineering to RL-12. Pin commit 9231bab of walkinglabs/hands-on-modern-rl; its
code/chapterNNfolders do not follow the site chapter numbers. - LLM Post-Training Laboratory - Baseline and State ContractRoadmap-Final · required
Build or reuse the laboratory frozen base, SFT and evaluation baseline plus immutable tokenizer, template, model, policy and rollout manifests. Complete fixed log-probability, mask and state-identity tests only in this stage.
- TRL GRPO Trainer - KL estimation and lossHugging Face · reference
Read Estimating the KL divergence for the k3 formula with the reference-over-policy ratio, and the default KL-coefficient note. Use it as the correct-ratio reference beside the RLHF Book code estimators.