Unit 7 of 10 · about 4 hours · implement
Version binding, immutable rollout records and outcome classes
Stale or version-mismatched rollouts are the quietest failure in LLM RL. Records make freshness checkable instead of assumed.
You'll be able to
- Bind policy, tokenizer, template, reward, verifier and RNG versions to every rollout record.
- Implement immutable rollout storage where old log probabilities are written once at sampling time.
- Classify rollouts into valid, invalid, refused, timed-out and truncated outcomes without dropping any.
Covers
Read
In this order, only the parts listed
0 of 4 done
- Version binding and immutable rolloutsLLM Post-Training Laboratory - Baseline and State Contract
Architecture constraints (every record binds prompt, template, tokenizer, model, reference, reward, verifier and policy versions; rollouts immutable; old log probabilities never recomputed) with Correctness requirements
- Experience record fieldsKL Estimators - Schulman's Note and the RLHF Book Code
The Experience dataclass:
sequence_ids,attention_mask,action_mask, advantages,log_probs_oldfrom the rollout policy,log_probs_ref,values_old, and the rewards TensorDict (total, correctness, format, penalty, binary) - Behavior versus target log probabilitiesStanford CS224R 2026 LLM RL Default Project
Section 1.3.3, Importance Weighting (p. 8): vLLM sampling versus Hugging Face update paths can yield different token log probabilities, so the update treats sampling as a behavior policy with per-sample importance weights
Read as a design specification. The course action items are not your assignment.
- Truncation practiceRLHF Book - Introduction, Training Overview and Instruction Tuning
Implementation opening paragraphs: hard generation cap, reward-model scoring only on the
eos_token, and a penalty for generating too long
Focus on
- Write the sampler's log probabilities at sampling time and never recompute them from a newer policy. A trainer-stack recomputation at the same weights is a second, different number (numerics differ); keep both if the update uses both, as the CS224R spec does with its behavior-policy weights.
- Bind versions, not just names: policy checkpoint, tokenizer, chat template, reward or verifier build, and RNG seed. Any one of them can change the distribution.
- Every rollout lands in exactly one outcome class (valid, invalid, refused, timed-out, truncated) and stays counted. These five labels operationalize the lab rule that failed and filtered rollouts remain counted with explicit reasons. Dropping failures changes the estimand toward easy prompts.
- Separate the attention mask (valid tokens) from the action mask (generated tokens supervised by the loss), as the Experience record does.
Common mistakes · 3
- Refreshing stored log probabilities with the policy being updated, mid-epoch. The ratio becomes 1 everywhere and clipping never binds, hiding all off-policy drift.
- Deleting timed-out or refused rollouts. Timeouts correlate with hard prompts, so deletion biases returns upward; keep them with their outcome label.
- Mixing sampling stacks without recording which produced each log probability. vLLM and Transformers numerics differ, so the mismatch looks like policy drift.
Try it yourself
Closed-book, before checking anything
Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Why must old log probabilities be stored rather than recomputed, even with the same checkpoint?Answer
Recomputation may use a different code path, dtype or kernel with different numerics, and after any update the checkpoint itself differs. The stored values are the ones the sampler actually used.
2.A rollout timed out after 200 tool calls with no answer. Which class, and does it enter the gradient?Answer
Timed-out. It stays in the records with its penalty reward and enters accounting; whether it enters the gradient depends on the declared rule, but it must never vanish silently.
3.The tokenizer version changed between sampling and a later audit. Which records are still comparable?Answer
Only records sharing the full version binding. Token ids from different tokenizers are different objects, so log probabilities and masks do not transfer.
4.What distinguishes the attention mask from the action mask in the Experience record?Answer
The attention mask marks valid (non-padding) tokens for the forward pass. The action mask marks generated tokens that enter the loss; prompt tokens are valid but masked from supervision.
You're done when
Your records carry every version, survive a policy update unchanged, and account for all five outcome classes with none dropped.
Only when you can do this without notes.
Leads to
Notes
Resources · 4 for this unit, 6 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.
- 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. - 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.
- 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.
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.
- 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.
- 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.
- 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. - 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.