Skip to content

fix(training): exclude prompt history from reward lengths and loss masks - #4487

Merged
terrykong merged 6 commits into
NVIDIA-NeMo:mainfrom
linj-glitch:linj/overlong-shaping-all-turns
Oct 18, 2026
Merged

terrykong merged 6 commits into
NVIDIA-NeMo:mainfrom
linj-glitch:linj/overlong-shaping-all-turns

Conversation

@linj-glitch

@linj-glitch linj-glitch commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Overlong reward shaping now sums tokens across all generated assistant turns while excluding assistant turns in the original prompt history. The legacy message-log and data-plane paths use the same generation-logprob marker, and the sync rollout actor measures lengths before flattening fills missing logprobs. Sync PPO, async PPO, and distillation also use that marker for their loss masks. Regression tests cover 100 history tokens followed by 15 generated tokens, multi-turn totals, empty placeholders, actor ordering, and training-loop masks; DAPO documentation and fixtures describe the same behavior. The affected native unit files pass 185 tests, the final commit passes 20 focused cases, and all applicable pre-commit hooks pass; full training and CI remain unrun.

@copy-pr-bot

copy-pr-bot Bot commented Oct 5, 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.

@linj-glitch
linj-glitch marked this pull request as draft October 5, 2026 06:10
@linj-glitch
linj-glitch force-pushed the linj/overlong-shaping-all-turns branch from 54f9339 to af66055 Compare October 8, 2026 00:15
@linj-glitch
linj-glitch marked this pull request as ready for review October 13, 2026 05:02
linj-glitch and others added 3 commits October 13, 2026 02:58
DAPO overlong shaping measured only the first assistant turn, so multi-turn
rollouts were never penalized for their total generation. Sum all assistant
turns in apply_reward_shaping and in decompose_message_log's
response_token_lengths. Single-turn behavior is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Lin Jia <[email protected]>
Document the multi-turn semantics introduced by the previous commit: in
docs/guides/dapo.md, state that the response length measured by overlong
reward shaping is the total over all assistant turns in a sample, and
that user, environment, and tool turns are excluded. Update the
tests/unit/data_plane/README.md entry for the renamed
test_decompose_message_log_sums_assistant_turns, which previously
described the old first-turn-only behaviour.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: Lin Jia <[email protected]>
…compose

The test inventory header for tests/unit/data_plane/test_message_log_decompose.py
still said 11 tests and omitted the two realistic multi-turn tests that were
added upstream (test_decompose_realistic_multi_turn_jagged_count and
test_decompose_reconstruct_roundtrip_realistic_multi_turn). The file now
defines 13 tests; update the count and add one-line entries for both.

Pre-existing drift, adjacent to the entry corrected in the previous commit.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: Lin Jia <[email protected]>
@linj-glitch
linj-glitch force-pushed the linj/overlong-shaping-all-turns branch from af66055 to c169600 Compare October 13, 2026 16:37
@github-actions github-actions Bot added the Documentation Improvements or additions to documentation label Oct 13, 2026
@linj-glitch

Copy link
Copy Markdown
Contributor Author

/ok to test c169600

@terrykong
terrykong self-requested a review October 15, 2026 05:33
@terrykong terrykong added the CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) label Oct 15, 2026

@terrykong terrykong left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for a clean, well-scoped fix and a thorough PR body.

  • The new test runs both input paths and fails if either fix is reverted. We checked this by reverting each one.
  • We re-checked the recipe audit on our own and it holds: no shipped config uses both reward shaping and multi-turn rollouts.
  • Two side benefits: both input paths now give the same result for an empty first assistant turn (before: 1.0 vs 0.5), and HelpSteer3's empty assistant turn in the prompt is no longer measured in place of the generated response.

One ask, in the inline comments: count only assistant turns that have generation_logprobs, the rule the GRPO loss mask already uses. Apply it to both length paths in this PR, and to the PPO and distillation loss masks.

CI: no unit tests have run on this head. The only label is Documentation, so CI quality check (No tests run: Label CI:L*) fails. This PR changes rewards, so per the CI label table please add CI:L1, then re-run /ok to test <new head SHA>.

Visual explainer: https://terrykong.github.io/gh-pages-poc/terryk/pr-4487-overlong-all-turns.html

Generated by Claude Code

Comment thread nemo_rl/algorithms/reward_functions.py Resolved
Comment thread nemo_rl/data/llm_message_utils.py Resolved
@linj-glitch linj-glitch added the CI:L1 Run doctests, unit tests, and functional tests label Oct 15, 2026
Count generated assistant turns before logprob backfilling and align sync PPO, async PPO, and distillation masks with the existing GRPO marker. Exercise both reward paths, TQ actor ordering, and real training-loop masks with prompt history and generated turns.

Signed-off-by: Lin Jia <[email protected]>
@linj-glitch linj-glitch removed the CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) label Oct 15, 2026
@linj-glitch linj-glitch changed the title fix(reward-shaping): measure overlong length over all assistant turns fix(training): exclude prompt history from reward lengths and loss masks Oct 15, 2026
@terrykong terrykong added CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) and removed CI:L1 Run doctests, unit tests, and functional tests labels Oct 17, 2026
@terrykong
terrykong enabled auto-merge (squash) October 17, 2026 07:50
terrykong
terrykong previously approved these changes Oct 17, 2026
@terrykong

Copy link
Copy Markdown
Collaborator

/ok to test 5373980

@linj-glitch

Copy link
Copy Markdown
Contributor Author

/ok to test 1259510

@terrykong
terrykong merged commit e12d18b into NVIDIA-NeMo:main Oct 18, 2026
94 checks passed
nicolo-domyn added a commit to martin-cimmino/RL that referenced this pull request Nov 3, 2026
…e first

apply_reward_shaping's DAPO overlong-length branch broke on the first
role=="assistant" message in message_log and used only its length -- correct
for upstream's assumed single-turn layout, silently wrong for our multi-turn
swe_agents/NeMo-Gym rollouts (it measured turn 1 only, ignoring every later
turn, including the one that actually runs long).

Hand-ported (not cherry-picked) from upstream e12d18b "fix(training):
exclude prompt history from reward lengths and loss masks" (NVIDIA-NeMo/RL
PR NVIDIA-NeMo#4487). A straight cherry-pick produced spurious modify/delete conflicts
on ppo.py/sync_rollout_actor.py/decompose_message_log, none of which exist on
this branch -- only the reward_functions.py fix applies to us. See the
[CUSTOM-PORTED] comment in reward_functions.py for the full porting note and
drop-this-patch-when condition.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) Documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants