LLM RL I: Language Models as Policies

Language-model RL inherits policy-gradient mathematics but changes the action space, trajectory boundary and systems state. This stage maps prompts, tokens, completions and turns into explicit RL objects and proves sequence log-probability, masking, version and sampling contracts before optimizing a reward.

Section 8 of 12 · 10 units · about 41 hours

0/10

By the end you can Given a language or tool task, define the state, action, trajectory and reward boundary, then make masked sequence log probabilities, generation-policy metadata and immutable policy-versioned rollouts pass hand-computed tests.

Units

Build it

Mini project · about 10 to 14 hours

Token-policy rollouts with versioned trajectory contracts

Extend policylab with an llm/ trajectory laboratory on a tiny CPU text policy: boundaries, masks, rewards, roles and immutable rollout records. Prove scoring against fixed oracles before any update, and account every token and verifier call. Move to a small decoder only if your GPU fits it.

  1. 1Fix the state, action, trajectory and reward boundary for one small task in llm/, check it against hand-computed trajectory probabilities, then write the bandit, token or environment formulation with its hidden-state list and the optimizer it permits.
  2. 2Implement prompt, completion, padding and EOS masks on the rollout records, run the RLHF Book masked-aggregation script on them, then place per-token rewards with the outcome at EOS, propagate returns, and log KL, reward and return traces with truncation handled apart.
  3. 3Freeze a base model, train a small SFT baseline and score both on a frozen evaluation set in llm/, assign every tensor to policy, reference, critic, reward or verifier, and record checkpoint identity with frozen-or-trainable status for every later stage.
  4. 4Store immutable versioned rollout records in llm/ with old log probabilities written once, all five outcome classes counted, and sampling, training and evaluation prompts separated with dedup, then prove a stored-versus-recomputed test and a fixed log-probability and mask oracle.
  5. 5Recompute the companion token KL hand calculation on the rollout traces, then record token entropy, sequence diversity and reference KL with the estimator and direction named on every number.
  6. 6Account raw tokens, verifier calls, latency and compute for each sampling run in ledger/, compare the SFT change against best-of-N and self-consistency at matched budgets, and preserve failed runs with explicit reasons.
What you hand in
  • Tiny text policy with boundary, formulation, masks and reward placement proved on fixed oracles.
  • Immutable versioned rollout records with prompt splits, outcome classes and KL, compute and latency traces.
  • Frozen base, SFT and evaluation baselines with checkpoint identity and matched-budget selection controls.
How to check it
How it's graded

Mask correctness

Pass: The aggregation script passes on rollout records, prompt positions carry zero weight, and EOS handling is decided and tested.

Great: A template-shift probe fails the oracle as designed, and per-sequence versus per-token weighting is defended.

Reward placement

Pass: Per-token rewards put KL at every position with the outcome at EOS only, returns propagate by hand, and truncation takes no outcome score.

Great: Recorded KL, reward and return traces reconcile position by position on a held-out rollout.

Version binding

Pass: Every record binds policy, tokenizer, template, reward, verifier and RNG versions, stored log probabilities survive updates, and no outcome class is dropped.

Great: Sampling, training and evaluation splits share no prompt id, and prefix dedup evidence is kept.

Compute honesty

Pass: The ledger counts tokens, verifier calls, latency and compute, learned change is compared with matched selection, and failures stay counted.

Great: A small-decoder run, only if your GPU fits it, is reported apart with its own budget table.

Resources

  • RLHF Book - Introduction, Training Overview and Instruction TuningNathan Lambert

    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

    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

    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

    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.

  • LLM Post-Training Laboratory - Baseline and State ContractRoadmap-Final

    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.

  • Berkeley CS 185/285 Deep Reinforcement Learning (Spring 2026) - LLM RLUC Berkeley

    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)

    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

    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

    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.

  • TRL GRPO Trainer - KL estimation and lossHugging Face

    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.