Skip to content

feat(capture): support message-level advantage penalties with token capture - #5022

Draft
yfw wants to merge 6 commits into
mainfrom
yifuw/capture-message-level-advantage-penalties
Draft

yfw wants to merge 6 commits into
mainfrom
yifuw/capture-message-level-advantage-penalties

Conversation

@yfw

@yfw yfw commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do ?

This PR lets grpo.malformed_thinking_advantage and grpo.invalid_tool_call_advantage run with token capture. Today setup rejects them, because the capture finalizer writes no malformed_thinking_mask or invalid_tool_call_mask.

How it works:

  • Setup asks Gym to record each call's last served output item in the capture ledger (token_id_capture.record_last_output_item, added in feat(capture): opt-in last output item on ordinary capture rows Gym#5447). Setup asks only when one of the two penalties is set, so other runs send Gym the same configuration as before.
  • The finalizer (RolloutReassembler) runs the echo path's detector, _detect_invalid_tool_call_and_malformed_thinking, on that item for each call of the terminal chain. It uses the same env.nemo_gym.invalid_tool_call_patterns and env.nemo_gym.thinking_tags as the echo path. Forest already uses this detector on last_output_item.
  • The finalizer writes both masks over each call's generated tokens, with the same code that Forest uses. The advantage stage then applies the penalties as it does on the echo path.
  • The finalizer logs finalize/invalid_tool_call_rate and finalize/malformed_thinking_rate, the capture versions of the echo path's invalid_tool_call_rate and malformed_thinking_rate.
  • If a chain call has no recorded item, the finalizer rejects the rollout as missing_last_output_item, so the penalty never turns off without a sign.
  • The finalizer computes the flags from the receipt, which recovery already saves. This PR does not change the recovery schema.
  • Forest keeps its own guard (validate_forest_objective).

This PR pins the Gym submodule to the Gym PR's head, which is the current pin 5911d628c plus that one commit. After the Gym PR merges, the pin must move to a Gym main commit.

Overlap with #4776: #4776 adds a thinking_tags argument to RolloutReassembler for the malformed think-tag reward penalty. Both come from env.nemo_gym.thinking_tags, so the second PR to merge can use one argument for both. I tested the two PRs together (see Testing).

Issues

Removes the guard and its TODO(token-capture) in single_controller_utils/config.py.

Usage

token_capture:
  enabled: true
grpo:
  malformed_thinking_advantage: -5.0   # and/or invalid_tool_call_advantage

Testing

  • New unit tests:
    • test_finalize_group_masks_flagged_calls_from_last_output_items: one call leaks a thinking tag and one writes a tool call as text. Each mask covers exactly that call's generated tokens, and a placeholder row has no flags.
    • test_finalize_rollout_rejects_a_chain_call_without_last_output_item.
    • test_spinup_asks_gym_for_last_output_items_only_when_requested: validates the generated Gym configuration with Gym's TokenIdCaptureConfig.
    • test_token_capture_allows_message_level_advantage_penalties replaces the test of the removed guard.
  • Default lane: tests/unit/single_controller, tests/unit/experience, tests/unit/data_plane, tests/unit/algorithms/test_grpo.py: 3,959 passed, 24 skipped.
  • Gym lane, with this Gym commit: tests/unit/experience, tests/unit/data_plane, tests/unit/single_controller, tests/unit/environments/test_nemo_gym_utils.py: 705 passed, 4 skipped. Three tests error in the setup of nemo_gym_vllm_generation, which starts vLLM and Gym servers. That setup times out after 300 s on my workstation: test_async_nemo_gym_rollout_manager, test_async_nemo_gym_rollout_manager_matches_original and test_run_async_nemo_gym_rollout.
  • Pre-commit passes except pyrefly, which needs the submodules that my local worktree does not have.
  • End to end: Super 3.5 video teacher recipe with token capture and grpo.malformed_thinking_advantage: -5.0, on 32 GB200 nodes. The code was main 6e68b179 + fix(sc): SWE PPO fixes for token capture and Megatron critic memory #4776 + this PR + the one-line routed-expert fix from fix(vllm): router replay for MoE models with >255 experts #4543, with Gym 5911d628c + feat(capture): opt-in last output item on ordinary capture rows Gym#5447.
    • The run did 12 training steps in its 2-hour limit, with no errors. Steps 2-12 averaged 416 s.
    • After 6 steps, all 16,384 ledger rows had last_output_item.
    • finalize/malformed_thinking_rate was 0.001, 0.0005 and 0.0005 at steps 3, 8 and 10, and 0 at the other steps. finalize/invalid_tool_call_rate was 0. finalize/invalid_row_rate was 0 at every step, so no rollout was rejected as missing_last_output_item.
    • Reward (0.49-0.57), token_mult_prob_error (1.013-1.015) and gen_kl_error (0.0012-0.0015) matched the same recipe without capture.
    • This run set the Gym policy model server to 8 uvicorn workers (num_workers: 8). An earlier run with the default 1 worker lost 24% and 39% of its rows in steps 1 and 2 to manifest request timeouts.

Before your PR is "Ready for review"

Pre checks:

  • Make sure you read and followed Contributor guidelines
  • Did you write any new necessary tests?
  • Did you run the unit tests and functional tests locally? Visit our Testing Guide for how to run tests
  • Did you add or update any necessary documentation? Visit our Document Development Guide for how to write, build and test the docs.

Additional Information

Draft until the Gym PR merges.

🤖 Generated with Claude Code

yfw and others added 2 commits November 2, 2026 03:05
…apture

grpo.invalid_tool_call_advantage and grpo.malformed_thinking_advantage were rejected with token capture because the finalizer emitted no invalid_tool_call_mask / malformed_thinking_mask. Setup now asks Gym to record each call's last served output item in the capture ledger (token_id_capture.record_last_output_item), and the finalizer runs the echo path's detector, with env.nemo_gym's patterns and tags, on each call of the terminal chain. It writes both masks over that call's generated tokens and logs finalize/invalid_tool_call_rate and finalize/malformed_thinking_rate. A chain call without a recorded item rejects the rollout. Forest keeps its own guard.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Signed-off-by: Yi-Fu Wu <[email protected]>
Gym 5911d628c plus the commit that adds token_id_capture.record_last_output_item. Move the pin to a Gym main commit after that change merges.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Signed-off-by: Yi-Fu Wu <[email protected]>
@copy-pr-bot

copy-pr-bot Bot commented Nov 2, 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 2, 2026
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

✅ Submodule Fast-Forward Check Results

Check based on commit: 476c7eb (PR #5022 from yifuw/capture-message-level-advantage-penalties)

✅ Submodules that are properly updated:

Gym: ✅ PR branch is ahead of main branch (fast-forward)

All submodule changes look good! ✨

@terrykong terrykong added the CI:L1 Run doctests, unit tests, and functional tests label Nov 5, 2026
Main gained #4776, which adds a thinking_tags argument to
RolloutReassembler and RolloutReassemblerActorConfig for the malformed
think-tag reward penalty. This branch added an argument with the same name
for the message-level detector, and the auto-merge kept both. Both resolve
to env.nemo_gym.thinking_tags or the default, so one argument now serves
both checks: setup sets it when the think-tag penalty or a message-level
penalty is on.

Other conflicts: _spinup_gym keeps main's rollout-recovery arguments, and
the single-controller guide keeps main's reflowed text and drops its
"remain unsupported with capture" sentence for the new paragraph.

Also:
- Pin Gym to c3e5931262, NVIDIA-NeMo/Gym#5447 merged with Gym main. It
  descends from main's pin 9e2e5d0ad0, and Gym's dependencies and version
  did not change, so uv.lock stays valid.
- Every check reads the active algorithm block, so PPO gets the same
  support. Add a PPO validation test, and name both blocks in the guide.
- Shorten three in-function comments to two lines.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Yi-Fu Wu <[email protected]>
@github-actions

Copy link
Copy Markdown

✅ Submodule Fast-Forward Check Results

Check based on commit: dde42cc (PR #5022 from yifuw/capture-message-level-advantage-penalties)

✅ Submodules that are properly updated:

Gym: ✅ PR branch is ahead of main branch (fast-forward)

All submodule changes look good! ✨

…pin Gym

Review fixes for message-level advantage penalties under token capture.

- Gym pin: e70909a30e, Gym #5447 with the Chat think-block fix. Calls
  through /v1/chat/completions no longer get false malformed_thinking flags.
- Metrics: ordinary capture rows publish num_assistant_messages,
  num_invalid_tool_calls and num_malformed_thinking in the row tags, as the
  echo path does. The controller then pools invalid_tool_call_rate and
  malformed_thinking_rate per step. The per-group finalize/ rates are gone.
- message_level_advantage_penalties_enabled moves to config.py as the one
  check that setup, the validator and the advantage stage use.
- Tests: setup passes the penalties to the partitions, Gym and the
  finalizers; the actor forwards the detector settings; the masks test uses
  non-default tags and patterns.
- Docs: the rates are pooled per step, and turning on an override when
  resuming from a checkpoint saved without it is not supported.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Yi-Fu Wu <[email protected]>
@github-actions

Copy link
Copy Markdown

✅ Submodule Fast-Forward Check Results

Check based on commit: f805618 (PR #5022 from yifuw/capture-message-level-advantage-penalties)

✅ Submodules that are properly updated:

Gym: ✅ PR branch is ahead of main branch (fast-forward)

All submodule changes look good! ✨

yfw and others added 2 commits November 18, 2026 13:55
Brings in #5290 (test-only, no overlap with this PR).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Yi-Fu Wu <[email protected]>
Gym 4a47e2086 is e70909a30e merged with Gym main a57f9cd437, which now has
#5771 (allow_system_suffix, Messages harnesses under source-history
capture). It descends from RL main's Gym pin 9e2e5d0ad0.

#5771 closed the Messages gap, so a Messages request is no longer refused
with 422. Drop that case from test_existing_framework_capture_protocol_gap.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Yi-Fu Wu <[email protected]>
@github-actions

Copy link
Copy Markdown

✅ Submodule Fast-Forward Check Results

Check based on commit: aa91574 (PR #5022 from yifuw/capture-message-level-advantage-penalties)

✅ Submodules that are properly updated:

Gym: ✅ PR branch is ahead of main branch (fast-forward)

All submodule changes look good! ✨

@terrykong terrykong removed the Documentation Improvements or additions to documentation label Nov 28, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:L1 Run doctests, unit tests, and functional tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants