Unit 9 of 10 · about 4 hours · implement

Fixed-sequence log-probability and mask oracles and prompt separation

Oracles catch mask and scoring bugs before they become training runs. Split discipline keeps evaluation honest.

You'll be able to

  • Implement a fixed-sequence oracle that checks gathered log probabilities and masks against hand values.
  • Separate sampling, training and evaluation prompts with dedup and split ownership.
  • Detect tokenizer or template mismatch between sampling and scoring from oracle failures.

Covers

Fixed-sequence log-probability and mask oraclesSampling, training and evaluation prompt separation
1

Read

In this order, only the parts listed

0 of 4 done

  1. Generate versus forward oracle patternRLHF Book - Introduction, Training Overview and Instruction Tuning

    Implementation Example, Steps 1 to 5: generate() sampling versus forward() scoring on the same tokens, with gather and sum as the oracle pattern

  2. Minimal generation function and model loadingBuild a Reasoning Model (From Scratch) - Chapters 2 to 5

    Chapter 2, Sections 2.4 to 2.7, pp. 25 to 45: input preparation, loading Qwen3-0.6B, the sequential generation process and the minimal generation function

  3. Prompt selection and reuse riskRLHF Book - Introduction, Training Overview and Instruction Tuning

    Training Process Step by Step, step 0 (prompt selection; reusing every SFT prompt risks overfitting) with Generating Completions (N completions per prompt)

  4. Prompt filtering and dedup at scaleTraining Language Models to Follow Instructions with Human Feedback

    Appendix C.4 of arXiv:2203.02155v1: 256k episodes over about 31k unique prompts after PII filtering and common-prefix dedup, batch size 512 with minibatch 64

2

Focus on

  • The oracle fixes token ids once, runs policy and reference forwards, and asserts gathered per-token log probabilities, masks and sums against hand values. Same ids in, exact numbers out.
  • Sampling, training and evaluation prompts are disjoint sets with recorded ownership. Prefix dedup matters because shared prefixes leak across the split even when full prompts differ.
  • Format evaluation prompts with the identical template and tokenizer as training. The Stanford project formats conversation context consistently for this reason; a mismatch voids the comparison.
  • This mirrors llm-foundations/tokenization-corpus-data/packing-and-loss-masks split discipline: split ownership is fixed before fitting or packing, and the same rule holds before sampling.
Common mistakes · 3
  • Evaluating on sampling prompts and reporting the score as generalization. Resampled familiar prompts measure memory plus selection, not new-task behavior.
  • Deduping on full strings only. InstructGPT dedups on common prefixes because near-duplicate prompts leak just as well.
  • Unrolling multi-turn data into overlapping windows that include future turns. Each training example must exclude turns after the supervised one.
3

Try it yourself

Closed-book, before checking anything

From a blank file, build a fixed oracle on 5 token ids with a 2-row logits fixture: assert the gathered log probabilities, the prompt-zeroed mask and the sequence sum, then flip one template token and show the oracle failing.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.What does the fixed-sequence oracle pin down that a loss-going-down check does not?Answer

    Exact per-token log probabilities, exact mask positions and the exact sum on fixed ids. A falling loss is consistent with scoring the wrong tokens.

  2. 2.Why is prefix dedup needed on top of exact-match dedup?Answer

    Prompts sharing a long prefix put nearly identical contexts in both splits. The model then completes a memorized continuation rather than solving the task.

  3. 3.Sampling used template version A and evaluation uses version B with one extra header token. What breaks?Answer

    Token positions shift by one, so masks, gathered log probabilities and any position-sensitive reward attach to the wrong tokens. The oracle fails until versions match.

You're done when

Your oracle passes on fixed ids, fails on a template change, and no prompt id appears in two splits.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 7 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.

  • 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.

  • 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.

  • 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_kl2 and approx_kl3 functions 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/chapterNN folders 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.