Unit 4 of 10 · about 4 hours · derive

Sequence, token and step rewards, reward placement and return propagation

Credit assignment is where language RL differs most from SFT. Placement decides which tokens the gradient reinforces.

You'll be able to

  • Place a terminal outcome reward, per-step KL penalties and optional step rewards onto token positions.
  • Derive per-token returns from that placement with by hand.
  • Distinguish EOS-terminated from truncated completions in reward assignment.

Covers

Sequence, terminal, token and step-level rewardsReward placement and return propagation
1

Read

In this order, only the parts listed

0 of 4 done

  1. Response-level rewards and no discountingRLHF Book - Introduction, Training Overview and Instruction Tuning

    Manipulating the Standard RL Setup, point 3 (response-level attribution over token sequences) with the rewritten objective Equation 10 using

  2. Per-token rewards with the outcome at EOSRLHF Book - Introduction, Training Overview and Instruction Tuning

    The PPO critic header comment: rewards as (B, L) post-KL per-token tensors where the EOS row includes the outcome, with done_mask and often 1.0 for LM RLHF

  3. Token reward with the reward-model score at the endHands-on Modern RL - Chapter 13 RLHF Pipeline

    The token-reward equation (per-step KL at every t, reward-model score only at t = T) with the Every preceding token versus Final EOS token box (page read 2026-10-14)

  4. Sparse and delayed rewardsRLHF Book Course - Lectures 0 and 1

    Slide 28 of 31: one scalar at the end of the response (sparse, delayed, no token-level target) as the motivation for policy gradients

2

Focus on

  • Standard placement: every token gets , and only the EOS position additionally gets the outcome score. The critic then spreads the total across tokens.
  • With , the return at token is the plain sum of later per-token rewards. Any discounts the terminal outcome and changes the optimum, not just the variance.
  • Truncated completions never emitted EOS, so their outcome score is missing. Score only EOS-terminated generations with the reward model and penalize overlong ones instead.
Common mistakes · 3
  • Discounting a terminal reward with out of habit. Earlier tokens then see a smaller return than later ones for no horizon reason.
  • Feeding truncated generations to the reward model. The model scores an unfinished answer out of distribution; cap length and penalize truncation instead.
  • Reading the per-step KL as environment reward. It depends on the policy that sampled the batch, so it is computed from the rollout-time log probabilities for each new batch and never carried over to data from another policy version.
3

Try it yourself

Closed-book, before checking anything

Closed-book, for a 4-token completion with per-step KL [0.1, 0.2, 0.1, 0.4], , terminal score 1.3 and , derive per-token rewards [-0.005, -0.01, -0.005, 1.28] and returns [1.26, 1.265, 1.275, 1.28].
4

Quiz yourself

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

0 got · 0 missed

  1. 1.In Reproduce, why is the first return 1.26 and not 1.3?Answer

    It sums all four per-token rewards: the terminal 1.3 minus the KL at EOS (0.02) minus the three earlier KL penalties (0.005, 0.01, 0.005). With , every later penalty reaches the first token.

  2. 2.The policy hits the 4-token cap without emitting EOS, and the reward model would score the text 1.3. Where does the 1.3 go?Answer

    Nowhere. A capped generation has no EOS outcome, so under the placement the book describes it gets no reward-model score. It keeps its per-step KL terms and takes the too-long penalty.

  3. 3.What breaks if the EOS position gets the outcome score but the EOS token is excluded from the completion mask?Answer

    The outcome still enters returns through the critic, but the stop token itself gets no gradient. The policy never learns when to stop, and lengths drift.

You're done when

You can propagate a terminal score plus per-step KL into per-token returns by hand and defend the truncation rule.

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.

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

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

Also in this section

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

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

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

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