Skip to content

feat(forest): apply reward penalties to multi-row rollouts - #5068

Merged
yuki-97 merged 45 commits into
mainfrom
biguo/forest-ppo-penalty
Dec 2, 2026
Merged

yuki-97 merged 45 commits into
mainfrom
biguo/forest-ppo-penalty

Conversation

@bg51717

@bg51717 bg51717 commented Nov 4, 2026 •

Copy link
Copy Markdown
Member
  • I, the PR author, have personally reviewed every line of this PR.

What and why

Forest (multi-row) training rejected reward_penalties at startup, so runs whose rollouts span several rows could not penalise empty final answers or unwanted tokens. This PR applies the penalties to Forest rollouts: each logical rollout is penalised once, from its scored call, and token_capture.forest.penalty_scope: all_segments also runs the token checks on every other segment. Defaults of existing runs are unchanged.

The behaviour comes from Jiaqi Zeng's SWE PPO branch (fab3ba82d); penalty_scope: all_segments matches its recipe.

  • Text checks (empty final answer, duplicated reasoning) read the scored response once per rollout, before the reassembler drops the response text.
  • Token checks (unwanted tokens, malformed think tags) read the scored segment up to the scored call by default; forest.penalty_scope: all_segments also checks every other segment in full, and a hit in any of them zeroes the owner's reward.
  • Resume. penalty_scope is saved with the other capture reward settings and, like them, compared only while it applies: with an unwanted-token or malformed-think-tag penalty on.
  • Forest config block. token_capture.forest_training and token_capture.selection_policy move into a token_capture.forest block with an enabled/disabled pair (forest.enabled, forest.selection_policy, forest.penalty_scope); the old keys fail at startup with their new names.
  • Forest nightly (ppo-qwen3-0.6b-1n8g-megatron-gym-single-controller-forest) turns on the empty-final-answer and unwanted-token penalties with penalty_scope: all_segments, and checks that both rates are logged.
reward_penalties:
  penalize_empty_final_answer: true
  penalize_unwanted_tokens: true
token_capture:
  forest:
    enabled: true
    penalty_scope: all_segments  # optional, default scored_segment

Issues

Follow-up: #5472 (whether generations after the scored call are token-checked).

Convergence / perf / functionality proof

Penalties stay off by default, so existing runs train as before. With them on:

  • Forest PPO nightly with the penalties on, on GB200 as 2 nodes x 4 GPUs with the 1n8g layout: both runs (2 steps, then resume to 4) pass their metric checks. empty_final_answer_rate is 0.125-0.25 per step and unwanted_token_rate is 0; policy and critic GPUs peak at 34 GB.
  • Unit tests on GB200: default lane 3670 passed, NeMo-Gym lane 919 passed, Megatron lane 89 passed; test_aviary_actual_run_modes (with tenacity) 4 passed, including the transition_snapshots cases.

Needs upstream changes

None

bg51717 and others added 6 commits November 3, 2026 07:13
Forest training was GRPO-only. A PPO run now runs GAE on every physical
row with that row's own critic values; the logical owner still decides
validity, so a filtered segment masks all of its owner's rows.

- ppo.adv_estimator.normalize_over: kept_rows (default) or all_rows,
  which also whitens over rows masked from the loss. Execution padding
  never counts.
- The critic takes every Forest row as one optimizer step.
- Forest row padding covers the critic's DP and microbatch sizes too.
- The complete-group check now runs on the PPO path as well.
- Forest requires ppo.value_training_mode=whole_batch: its row padding
  covers the whole step, while split mode shards each chunk on its own.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
…tics

Execution padding and failed owners' borrowed inputs run through the
model, so with expert-bias updates enabled their tokens count toward the
router's expert loads. Forest keeps counting them by default, as
ordinary training does.

token_capture.exclude_padding_from_router_stats opts into masking them.
The finalizer publishes router_excluded_rows for Forest rows; the policy
and a PPO critic then fetch it, and the Megatron microbatch builder
gives those rows no router-visible tokens in the existing padding mask,
packed or not. The critic also masks sequence-packing padding.

Both workers check the mask only with the option on, and raise an error
naming it when the model cannot honour it, such as an expert-bias MoE
with HybridBlock chunkwise context parallelism.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
Forest pads every published group to the policy and critic row multiple.
With one rollout per group, most rows a step trains on are then padding:
a DP of 4 turns ~154 real rows into ~512.

token_capture.forest_padding_scope: step pads each training batch once
instead. Pad rows copy real rows under new sample IDs with no loss,
borrowed-input tags, and router exclusion flags;
token_capture.forest_pad_source picks row0 or cyclic copies.
ppo.adv_estimator.whiten_execution_padding lets those copies join
all_rows whitening. Defaults are unchanged.

A rollout snapshot taken during a step accepts that step's padding copies
(<sample_id>_padN) of replayed rows, and restore deletes them before
checking the replay inventory. Full trainer checkpoints run after the step
clears its padding and stay strict.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
SingleController PPO rejects a rollout-failure drop budget because the
critic expected a full step. A Forest critic trains on the step's actual
rows, so a short step fits and Forest PPO now accepts the budget.

Streaming PPO selects chunks in multiples of whole prompt groups so the
rows split evenly across the policy and critic data-parallel ranks.
Forest groups vary in row count and execution padding already aligns each
chunk to both models, so that alignment only held back ready groups,
rejected some num_prompts_per_step values, and made the sampler raise once
a dropped rollout shrank the step. Forest streaming PPO now selects any
group count, as streaming GRPO does. With forest_padding_scope=step, each
chunk is padded.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
Unit tests, as PPO counterparts of the Forest GRPO tests:
- Full-batch PPO trains every segment row per epoch and the critic takes
  the step's rows (gbs = rows); streaming PPO steps the policy once and
  trains the critic on all chunks after the refit.
- Invalid logical batches and all-invalid owners never train either model;
  a partial step trains the critic on the surviving rows.
- Whole-owner overlong filtering; the GAE advantage stage is never sharded.
- Forest PPO startup rejections, policy/critic padding validation order,
  checkpoint restore with a critic, the critic row multiple pinned in
  data-plane checkpoints, and value-loss equivalence across packing bins
  and segments.

Functional tests, both in L1_Functional_Tests_SingleController_3:
- ppo_async_gym_single_controller_context_compaction.sh: PPO (GAE,
  Megatron critic) on the context-compaction Forest rows, two steps with
  checkpoints, then a resume to step 4. Its config,
  examples/nemo_gym/ppo_qwen3_0_6b_single_controller.yaml, composes the PPO
  SingleController exemplar with the Gym GRPO config.
- grpo_async_gym_single_controller_context_compaction_resume.sh: the same
  checkpoint/resume for Forest GRPO.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
Forest training rejected reward penalties. They now run on Forest
owners: rollout-scoped text checks and token checks run once on the
scored segment, and the owner's rows share the resulting reward.

token_capture.forest_penalty_scope chooses which segments the token
checks cover: scored_segment (default) or all_segments, where a hit in
any segment zeroes the owner's reward. Penalty counts ride the scored
row so the finalizer metrics count each owner once.

Forest results keep scalar fields only, so the duplicated-reasoning and
empty-final-answer checks run on the agent's scored response when Gym
post-processes the receipt, and are passed to compute_reward_checks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
@bg51717 bg51717 added the CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) label Nov 4, 2026
@copy-pr-bot

copy-pr-bot Bot commented Nov 4, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions github-actions Bot added the Documentation Improvements or additions to documentation label Nov 4, 2026
bg51717 and others added 7 commits November 4, 2026 17:11
whiten_execution_padding only changes whitening under Forest step padding,
normalize_over=all_rows only with normalize_advantages=true, and the legacy
PPO entrypoint reads neither. Reject each of these at startup.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
…on note

PPO advantages are per physical row, each row a GAE episode, and Forest PPO
needs whole_batch critic training. Restore the truncation and
unsupported-features paragraph, which also applies to PPO overlong filtering.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
…ler exemplars

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
…sets

The critic's method also sets its router padding-mask flag, so call it
configure_forest_router_padding; setup calls the policy's check and the
critic's setup by their own names.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
…loss

With every microbatch size 1 the run never padded. A critic microbatch of 2
and step-scope padding pad steps with an odd row count, in the checkpointed
run and the resumed one. The critic-loss check gets an upper bound.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
Full-batch and streaming PPO: the padding copies reach the policy and the
critic, the critic gbs counts them, and the step deletes them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
@terrykong terrykong added CI:L1 Run doctests, unit tests, and functional tests CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) and removed CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) CI:L1 Run doctests, unit tests, and functional tests labels Nov 5, 2026
bg51717 and others added 7 commits November 7, 2026 22:32
Main moved rollout checkpointing into nemo_rl/algorithms/rollout_checkpointing
(#5011). Step padding handling of the replay inventory check now lives in
rollout_checkpointing.inventory.validate_replay_inventory, called from the
restorer ("clear") and the snapshotter ("allow"). Forest's estimator check
keeps main's new seq_logprob_error_in_loss rejection.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
The stage zeroed advantages outside the mask by multiplying with it, so a
dataset loss weight of 0.5 scaled the stored advantage again on top of GAE
and the loss. Zero the tokens outside the mask without rescaling the rest.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
The inventory exception for forest_padding_scope=step also applied to full
trainer checkpoints, so an extra _pad row passed the save check and restore
deleted it. A full checkpoint never owns a streamed step's rows, so gate the
exception on a rollout snapshot on save and from_rollout_snapshot on restore.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
raw_reward reads neither normalize_over nor whiten_execution_padding, so
setting them promised a whitening change that never happened. Fail at
config validation instead.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
The PPO exemplar and three guide summaries said every logical rollout keeps
one advantage, which only holds for GRPO.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
…uo/forest-ppo-penalty

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: bg51717 <[email protected]>
The Forest block readback only exercised pydantic's union choice, and both
scope values ran the same startup check. The reward-penalty docs now say
which spans Forest's token checks read.

Signed-off-by: bg51717 <[email protected]>
The bootstrap fingerprint now reads token_capture.forest, which every real
config has. Adds a check that the scope changes the fingerprint only while a
token penalty runs.

Signed-off-by: bg51717 <[email protected]>
@bg51717

bg51717 commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

/ok to test 8e1f832

@yuki-97 yuki-97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fixes: the forest block, the nightly with the penalties on and the shared snapshot helper all landed as asked, and the resume check for the penalty scope is a good addition.

Scope: fix commits a7fffd8 through 8e1f832, checked against the open threads from the previous review, plus a scan of the changed files for new issues.

Checked:

  • token_capture.forest: the two classes have the requested shape, every reader of a sub-key checks enabled first, and the three keys that shipped on main are rejected with their new names.
  • compute_reward_checks takes text_checks as a required argument in its one production caller and in every test.
  • The penalty-scope resume check (a33fff0, c1311e3): a changed scope is rejected only while a token penalty runs; non-Forest runs and checkpoints written before this PR still resume.
  • The Forest nightly turns on both penalties with all_segments, and its result is in the description.

Remaining comments are on the all_segments wording: the config comment and two guides still say every segment is checked in full, while the scored segment is cut at the scored call (the code question is in #5472).

nit: two doc sentences about the moved keys, and two error messages with the old key name.

nemo-rl-review: kind=expert harness=claude-code/2.1.333 model=claude-fable-5-2 effort=medium review-pr-team=v3 reviewed=8e1f832

Comment thread docs/guides/context-compaction.md Resolved
Comment thread docs/guides/context-compaction.md Resolved
bg51717 added a commit that referenced this pull request Dec 1, 2026
…en-history

Moves the option to token_capture.forest.invalidate_rewritten_history, as
#5068 moved the other Forest keys. The vLLM worker now marks the call on its
per-request capture state and fails its capture at the terminal write; the
non-vLLM rejection moves to validate_single_controller_config.

Signed-off-by: bg51717 <[email protected]>
… the code does

The scored segment is token-checked up to the scored call under both penalty
scopes; all_segments also checks every other segment in full. Name the new
key for each moved Forest key, call all_captured a selection_policy value,
and use token_capture.forest.enabled in the two replay-buffer errors about
the current run.

Signed-off-by: bg51717 <[email protected]>
@bg51717

bg51717 commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

/ok to test f0ffbf6

…e check

main's privileged-value validation still read the old
token_capture.forest_training key, which no longer exists.

Signed-off-by: bg51717 <[email protected]>
@bg51717

bg51717 commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

/ok to test ab01bfc

yuki-97
yuki-97 previously approved these changes Dec 2, 2026

@yuki-97 yuki-97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the wording fixes, and for catching the old key in the privileged-value check after the merge.

Scope: fix commits f0ffbf6 through ab01bfc, checked against the open threads from the previous review, plus a scan of the changed files for new issues.

Checked:

  • Penalty scope wording — the config comment, both guides and the description match the code: the scored segment is checked up to the scored call, and all_segments also checks every other segment in full.
  • Moved Forest keys — the guide names the new key for each old one, as the config check does.
  • Merge of main — no code reads the old Forest keys, and the only conflict keeps both sides.

The comments below do not block merging; they can be fixed in a follow-up PR.

Remaining comments:

  • A sentence in the PPO guide, from the merge, that still names the old key.

nit

  • A test pattern that also matches a later error.

nemo-rl-review: kind=expert harness=claude-code/2.1.334 model=claude-fable-5-2 effort=medium review-pr-team=v3 reviewed=ab01bfc

@yuki-97
yuki-97 enabled auto-merge (squash) December 2, 2026 07:19
Signed-off-by: bg51717 <[email protected]>

# Conflicts:
#	nemo_rl/algorithms/rollout_checkpointing/restore.py
#	nemo_rl/algorithms/rollout_checkpointing/snapshots.py
#	nemo_rl/algorithms/single_controller_utils/setup.py
#	tests/unit/single_controller/test_checkpoint_borrow_restore.py
#	tests/unit/single_controller/test_checkpointing.py
#	tests/unit/single_controller/test_setup.py
@bg51717

bg51717 commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

/ok to test d0c2fc9

The forest_utils move removed the replay-buffer import cycle, so the
deferred import and the TYPE_CHECKING guard are no longer needed. Drop the
two module docstrings to match the sibling packages.

Signed-off-by: bg51717 <[email protected]>
… test

The PPO guide still named token_capture.forest_training, and the test's
match also hit the token_capture.enabled check.

Signed-off-by: bg51717 <[email protected]>
@bg51717

bg51717 commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

/ok to test dde8340

Signed-off-by: Yuki Huang <[email protected]>

# Conflicts:
#	docs/guides/grpo.md
@yuki-97 yuki-97 added CI:docs Run doctest and removed CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) labels Dec 2, 2026

@yuki-97 yuki-97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI:Lfast passed at dde8340 and only docs changes after that, change to CI:docs for merge.

@yuki-97

yuki-97 commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

/ok to test f149674

@yuki-97
yuki-97 enabled auto-merge (squash) December 2, 2026 15:26
@yuki-97
yuki-97 merged commit 198affd into main Dec 2, 2026
74 of 77 checks passed
@yuki-97
yuki-97 deleted the biguo/forest-ppo-penalty branch December 2, 2026 16:09
shikicloud added a commit to shikicloud/RL that referenced this pull request Dec 3, 2026
…mulator code

- validate_simulator_config reads token_capture.forest.enabled (renamed on
  main by NVIDIA-NeMo#5068) and names it in the message.
- NoOpTrainer no longer takes or holds policy_config; nothing reads it.
- Drop the unreachable uses_no_op_trainer early return in
  validate_simulator_config.
- Build the on_policy_distillation test config with keyword arguments.

Signed-off-by: Shiki Wu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:docs Run doctest complexity:high Large or cross-cutting change. Set by Autobot PR classify (skills/pr-classify) convergence-risk:high Likely to change training results. Set by Autobot PR classify (skills/pr-classify) proof:convergence Needs convergence proof (W&B runs). Set by Autobot PR classify (skills/pr-classify)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants