Before you start
Complete the response-masking and preference-optimization lessons. You should know log-softmax, token masks and the chosen-minus-rejected log-probability margin. Install the CPU course dependencies and edit projects/posttraining-audit/starter/model.py from the repository root.
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r projects/posttraining-audit/requirements.txt
python3 projects/posttraining-audit/tests/check.py --implementation starter --stage 1
python3 projects/posttraining-audit/tests/check.py --implementation starter --stage 2
python3 projects/posttraining-audit/tests/check.py --implementation starter --stage 3 --evidence-dir runs/posttraining-audit
python3 projects/posttraining-audit/tests/check.py --implementation starter --stage 4On Windows PowerShell, activate .venv\Scripts\Activate.ps1 and use python instead of python3.
Stage 1 masks out the first and third token. For the selected token, the target logit is and the other logit is , so the SFT loss is , about . Chosen and rejected policy log probabilities are and . With an equal-probability frozen reference, its log-probability difference is zero. Their margin is ; with the DPO loss is , about .
DPO uses the reference-corrected margin . Stage 2 also checks a nonuniform reference: reference log probabilities and turn the corrected margin into , even though the policy alone favors the chosen response.
Stage 2 swaps the two policy preference scores. The margin becomes and the DPO loss must rise. An empty response mask must raise a clear error. If the SFT loss changes when only masked prompt logits change, inspect the mask before reduction.
Stage 3 saves response-token count, SFT loss, margin and DPO loss to runs/posttraining-audit/posttraining-audit-evidence.json. Without --evidence-dir, the file is temporary. Stage 4 runs the full check. Passing output begins PASS posttraining-audit stage N.
These are objective mechanics on synthetic values. They do not demonstrate a trained language model, preference-data quality, retained behavior or user-facing response quality. Reference command: python3 projects/posttraining-audit/tests/check.py --implementation solution --stage 4.
Stage 5: sequence PPO on a fixed synthetic fixture
Stage 5 extends the token-level preference exercise into short autoregressive responses. It uses a tiny table policy conditioned on task, previous token and response position, plus a learned table critic. The reference policy is copied before updates. A linear reward model is fitted with pairwise softplus loss on the two fixed synthetic preference pairs, then frozen. This is a small mechanics fixture: its preference labels are not human data, and the policy is not a Transformer.
Optional appendix: trace sequence PPO with the saved 10,880-parameter decoder. Registered Stage 5 remains the table-policy exercise. The separate decoder mechanics and replay checks passed independent review, while its strict heldout reward quality floor failed; it does not establish language-model quality.
Optional preparation: the learned values and GAE lesson works through fitted values and a hand-calculated recursion. It is useful background, but it is outside this project’s required lesson route.
Use projects/posttraining-audit/starter/sequence_ppo.py for this stage. It includes the checked-in recipe, token/model helpers, frozen component checks, evaluation and serialization scaffold. Implement the six marked functions: fit_reward_model, rollout, generalized_advantage_estimate, ppo_policy_loss, clipped_value_loss, and train. The rollout record uses time-major [time, batch] arrays; target and input role masks are separate; the two loss functions return scalar means over valid response targets. The fixed recipe is loaded from reference/sequence_ppo/recipe.json. The project pins its CPU dependencies in projects/posttraining-audit/requirements.txt.
Its independent command is:
python3 projects/posttraining-audit/tests/check_sequence_ppo.py --implementation starter --stage 5The integrated reference command runs the preserved stages 1 through 4 and the sequence checks:
python3 projects/posttraining-audit/tests/check_sequence_ppo.py --implementation solution --stage allThe checker accepts an alternate learner module with --candidate /path/to/sequence_ppo.py. It grades that file through numeric and rollout protocol checks. Source-text mutation probes run only against the instructor reference module; they do not impose spelling requirements on a learner implementation.
To retain the complete machine-readable rollout trace from the reference implementation, add --evidence-dir runs/posttraining-audit-sequence-ppo. It writes sequence-ppo-evidence.json with per-token actions, masks, rewards, values, advantages and targets, frozen hashes, per-epoch losses and heldout metrics. The stage 5 starter command writes to the same location only if it completes, which the untouched starter does not.
Follow one generated response
The policy reads a task ID, the previous token and the response position. Its target-role mask names the generated actions that receive loss. The input-role mask answers a different question: was the previous context token a response token? At the first prediction, the context is BOS/prompt context, but the target is already a response token. Using the input mask for the loss would drop that first target. Follow an action at position into the next input at to check the shift.
Each response has at least two generated tokens. EOS can occur from the second action onward; all later positions are padding. A response that reaches the four-token collection limit is truncated. The frozen fitted reward score for the observed response goes on its final valid action. Each valid action also receives sampled KL shaping:
The sampled log-ratio can be negative for an individual action. It is a sample used for shaping, rather than an exact divergence. The evaluation below separately computes categorical KL from the full token distributions at each visited context.
Work through termination and timeout
Before looking at the figure, predict what happens to the last bootstrap when the response ends with EOS. Now ask the same question when collection stops at a time limit but the modeled process has a continuation.
Read the figure. Positions along the horizontal axis are generated response actions. Orange bars show observed reward, teal bars show the old value estimate, and purple bars show the advantage. All heights use reward units. An early action can have a positive advantage even when its immediate reward is zero, because the recursion carries later evidence backward.
These are two different illustrative responses, with no KL shaping. Their heights are not a controlled comparison of learning performance. Values printed on the bars are rounded. The fixed fixture and independent plotting script retain the full calculation; neither calls the PPO solution.
Use and . The temporal-difference residual and reverse recursion are:
Here at EOS and at a timeout; both stops set the trace continuation . An ordinary valid action has both indicators equal to one. Padding has zero residual, advantage and target and contributes no loss.
For the left response, the old values are , rewards are , and next values are . The EOS residual is , so the displayed next value is ignored. The full residual vector is , and the advantages are .
For the right response, the old values are , rewards are , and next values are . The timeout residual is . The residuals are , giving advantages . Try setting only the timeout bootstrap to zero: the advantages become . This is why the stop type matters.
The runnable fixture trains the post-timeout critic value against a declared one-token continuation: task 0 appends response A, task 1 appends response C, then ends. This scripted continuation makes the boundary value learnable and testable. It is an assumption of this small environment, not evidence about natural-language continuation.
Keep snapshots fixed while learning
Each outer update samples a fresh rollout. The behavior log probabilities and old values from that rollout stay fixed through every inner optimizer epoch. Compute the advantages and targets once, stop their gradients, and reuse them. The current policy and critic change; the reference policy and fitted reward model remain frozen for the whole run.
The policy uses the signed clipped PPO minimum and averages over valid response tokens. The critic clips changes around the old values and uses a valid-token mean, with a separate declared continuation-target loss. Record those units when comparing losses. The checker keeps actions, masks, rewards, old/reference probabilities, values, advantages, targets, per-epoch losses and component hashes. It checks independent scalar calculations, numerical gradients, changed conditions and fresh-process reload. Deliberately broken implementations must fail, including refreshed old probabilities, wrong stop handling and padding included in the loss.
The objective follows PPO, the recursion follows Generalized Advantage Estimation, and frozen targets use JAX stop_gradient.
Interpret the evaluation
Read every metric with its denominator. Reward is averaged per generated sequence. Sampled KL and exact categorical KL at sampled visited contexts are reported per valid response token. The per-sequence sum adds those visited-context conditional KL values. It is not the exact divergence between complete generated-sequence distributions.
The evaluation uses fixed held-out prompt instances, but both splits share the same two task features. The tiny policy sees task ID rather than prompt token content. Passing this check therefore tests the declared mechanics and repeated tasks; it does not measure unseen-task or prompt-semantic generalization.
Nonfinite or empty evaluations remain failures. The report also records failure when reward does not exceed the frozen reference or conditional KL exceeds its declared limit. Keep a failed result in the evidence instead of changing the recipe after seeing it. Reload the saved tiny state in a fresh Python process to reconstruct the same evaluation.
This stage covers synthetic sequence PPO mechanics, not model-quality qualification. The optional saved-decoder appendix records bounded mechanics and replay checks that passed, while its strict heldout reward-quality floor failed. Human-preference quality, retained behavior, human evaluation, C02 qualification, and full capstone evaluation still need separate evidence.
Review handoff: explain a changed condition
Improve a chosen-versus-rejected log-probability margin while decreasing the chosen probability. Construct concrete numbers.
Keep for review: Before/after chosen, rejected and fixed-reference values. For example, moving from to improves the raw margin from to , while the chosen probability falls from to . Independent quality evaluation is still needed.
Write your prediction before opening the reference implementation. Distinguish what the existing checker verifies from your proposed extension; record any extension command, actual output and limitation separately. A reviewer should be able to locate the first failed contract from your saved evidence.