Skip to content

feat(single-controller): support reward_scaling on the SC path - #4878

Merged
yuki-97 merged 8 commits into
mainfrom
ruit/reward_scaling_sc
Nov 10, 2026
Merged

yuki-97 merged 8 commits into
mainfrom
ruit/reward_scaling_sc

Conversation

@RayenTian

@RayenTian RayenTian commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do ?

Applies reward_scaling on the SingleController path, which until now rejected it at setup.

The knob has always existed on the v1 path (grpo.scale_rewards). SC listed it
in _validate_algo_settings' unsupported set and raised, so any recipe
carrying it — every dapo-* recipe does — could not run on SC at all.

Where it is applied, and why there

scale_rewards is reused rather than reimplemented, and is called where v1
calls it: in the advantage stage, ahead of every other reader of rewards —
the baseline, the logged train/reward, and masked_correct_pct. That
ordering is what makes the two paths produce the same numbers.

The map is per-row, so a shard produces the same result as the whole batch and
the stage stays shardable. The DataPlane column keeps the environment's raw
reward; only the stage's view of it is scaled, so a replayed or re-read row is
never scaled twice.

One new setup check

An enabled block with source_min >= source_max is now rejected at setup. The
remap divides by that interval, so an empty or inverted one surfaces as inf/nan
or sign-flipped advantages a whole rollout later, with nothing naming the
config key that caused it.

reward_shaping and use_dynamic_sampling are still rejected — they are
tracked separately.

Issues

Closes the reward_scaling half of the SC feature gap tracked in the
PrimeRL-vs-NeMo-RL comparison (row 13: "V2 only, already supported in V1").

Usage

grpo:
  reward_scaling:
    enabled: true
    source_min: 0.0      # clamp the env reward to this interval ...
    source_max: 1.0
    target_min: -1.0     # ... then map it linearly onto this one
    target_max: 1.0
uv run examples/run_grpo_single_controller.py --config <your-sc.yaml>

[0,1] -> [-1,1] is what every dapo-* recipe in
examples/configs/recipes/llm sets.

Testing

Unit

TestRewardScaling in tests/unit/single_controller/test_advantage_stage.py
and TestRewardScalingValidation in test_setup.py.

One thing worth flagging for review: a naive test here passes whether or not
the scaling ran.
The fixture's rewards are 0/1 and GRPO normalizes by the
group std, so (r - mean) / std is invariant under any positive affine remap —
a test written on target_min=-1 would prove nothing. The tests therefore use
a source interval that clamps, which is the only non-affine part of the map
and so the only observable one.

Three-arm 100-step run on 4x4 GB200 (nanov3-30BA3B, DAPOMath17K)

image

Forest Test

image

PPO

image

Because of that same invariance, the on-cluster check is a parity test: the
pass condition is a non-difference in the learning dynamics, with a difference
only in the reported reward.

metric SC off SC on v1 on
train/reward 0.62984 0.24594 0.25375
train/loss 0.03337 0.03223 0.03262
train/grad_norm 0.12676 0.12419 0.12742
train/advantages/max 1.46335 1.53861 1.48546
train/advantages/min -2.22438 -2.13307 -2.35993

Converting the scaled arms back with acc = (reward + 1) / 2:

SC off = 0.6298   SC on = 0.6230   v1 on = 0.6269
SC on - SC off = -0.0069, against a theoretical SE of 0.0060  ->  1.14 sigma

6400 rollouts per arm. The difference sits inside the sampling noise, and its
sign flipped as the sample grew (-0.021 at 6 steps, +0.009 at 20, -0.002 at 57,
-0.007 at 100) rather than settling on a non-zero value. The v1 arm landing
between the two SC arms is the cross-path check: this is not just SC agreeing
with itself.

min(train/reward) on the scaled arms is -0.5625, which [0,1] -> [0,1]
cannot produce — direct evidence the scale reached the logged reward.

A sharper signal showed up partway through: at 57 steps both SC arms reported
advantages/{max,min} of ±2.47487, agreeing to six significant figures. That
value is the advantage of the single correct completion in an 8-generation
group, (1 - 1/8) / 0.35355; under the scaled reward the same group gives
(1 - (-0.75)) / 0.70711, the identical number. The residual is ~1e-6, the
size of the epsilon in advantage_estimator.py. That is the affine
invariance showing up directly in the metrics.

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

  • Conflicts with the reward_shaping PR. Both touch the unsupported list
    in config.py and the same block in advantage_stage.py. Whichever merges
    second must take the union of the two deletions from the list, and must
    keep the order scale first, then shape — the overlong penalty's magnitude
    is only meaningful against the scaled reward's range, which is how v1 orders
    them (grpo.py).
  • test_single_controller_actor.py builds the PPO advantage-stage stub with
    PPOConfig.model_construct() instead of a SimpleNamespace listing three
    fields. Reading a fourth field off that namespace raised AttributeError;
    model_construct fills every default and still skips validation, so adding a
    field to the block will not break it again.

@copy-pr-bot

copy-pr-bot Bot commented Oct 23, 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 Oct 23, 2026
@RayenTian RayenTian added the CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) label Oct 23, 2026
@RayenTian

Copy link
Copy Markdown
Contributor Author

/ok to test 483b6f9

@RayenTian
RayenTian force-pushed the ruit/reward_scaling_sc branch from 483b6f9 to 317c9d5 Compare October 26, 2026 08:25
@RayenTian

Copy link
Copy Markdown
Contributor Author

/ok to test 709b8de

@RayenTian
RayenTian marked this pull request as ready for review October 26, 2026 11:04

@svcnvidia-nemo-ci svcnvidia-nemo-ci 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.

Reviewed all eight changed files across three independent topics: runtime reward handling, setup/configuration contracts, and tests/fixtures. The shared scaling helper is applied before advantage and reward-metric consumers, while DataPlane rewards remain unchanged. The clamping tests make omitted scaling observable, and the PPO fixture change retains its GAE writeback assertions.

One correctness issue and two low-severity guideline suggestions remain:

  1. (C1) Enabled scaling accepts non-finite bounds that can produce NaN rewards.
  2. (C2) New test helpers and setup tests lack required type annotations.
  3. (C3) Two added in-function comment blocks exceed the pinned review guidance.

The supplied training comparisons provide quantitative parity evidence. This review was static: tests, linters, and training runs were not executed.

Note: formal approval is not available for this request, so this review is posted as a comment only.

Review generated by codex. This was a full review. Request a full review with /review reset=true.

@RayenTian

Copy link
Copy Markdown
Contributor Author

/ok to test ff51bdb

@RayenTian
RayenTian force-pushed the ruit/reward_scaling_sc branch from ff51bdb to 482754f Compare November 2, 2026 02:39
@RayenTian

Copy link
Copy Markdown
Contributor Author

/ok to test 482754f

@mehraakash mehraakash 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.

Please rebase first. docs/guides/single-controller.md conflicts with main (the same feature-gap paragraphs were edited there since this branch was cut). The seven code and test files merge cleanly.

Verdict: nothing blocks merge. The change does what it says: the scale is applied once, right after the stage reads the rewards and before the baseline, the logged reward and the training dump see them, mirroring v1's order in grpo.py and ppo.py. The DataPlane column stays raw. Five agents found no PR-introduced bug. The 24 new tests and the whole advantage-stage file pass on CPU; the parity run is the right shape of evidence, since a non-clamping remap cannot move std-normalised GRPO advantages and the logged-reward minimum of −0.5625 shows the scale reached the metric.

Design calls that look right and should stay: the inline call with a one-key wrapper (a shared v1/SC transform helper would be over-abstraction; the reward_shaping PR can slot in after truncated is read at L397-399 and keep scale-then-shape), removing the entry from the unsupported list rather than adding a second list, and PPOConfig.model_construct() for the PPO stub.

Inline: one low-severity test ask (PPO/GAE path), one optional design suggestion, one doc clause. One more nit for the rebase, no thread: main now flags in-function comment blocks of three or more lines (skills/contributing/references/comments-docstrings-and-docs.md); the two new blocks at advantage_stage.py:385-387 and test_single_controller_actor.py:4208-4210 are exactly three lines.

Generated by Claude Code

Comment thread docs/guides/single-controller.md Resolved
@RayenTian
RayenTian force-pushed the ruit/reward_scaling_sc branch from 482754f to ab0ae95 Compare November 6, 2026 10:32
@RayenTian

Copy link
Copy Markdown
Contributor Author

/ok to test ab0ae95

RayenTian and others added 7 commits November 7, 2026 00:25
The SingleController path rejected reward_scaling at setup rather than
silently skipping it. Apply it instead, in the advantage stage.

scale_rewards is reused from the v1 path rather than reimplemented, and
is called where v1 calls it: ahead of the baseline, the logged reward,
and masked_correct_pct. The map is per-row, so a shard produces the same
numbers as the whole batch and the stage stays shardable. The DataPlane
column keeps the env's raw reward; only the stage's view is scaled.

Setup now rejects an enabled block whose source_min >= source_max. The
remap divides by that interval, so an empty or inverted one surfaces as
inf/nan or sign-flipped advantages a whole rollout later, naming nothing.

reward_shaping and use_dynamic_sampling are still rejected.

The fixture's rewards are 0/1 and GRPO normalizes by the group std, so an
affine remap leaves the advantages untouched; the tests use a clamping
source interval, which is the observable part of the map.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Signed-off-by: ruit <[email protected]>
…_setup

They went into test_ppo_setup.py because that is where the rejection
test they replaced lived. But the class is TestPPOValidation in a file
documented as "the PPO half of the setup path", and these cover a GRPO
config -- the file already had that mismatch and this was making it
worse.

test_setup.py is the right home: it already exercises
validate_single_controller_config, and TestOPDFullValidation next door
is the same kind of test, rejecting at startup rather than once the
cluster is up.

test_ppo_setup.py keeps only the one change it owes this branch:
reward_scaling leaves the unsupported-knob parametrization, since setup
no longer raises NotImplementedError for it.

Two changes on the way over, so this is not a pure move:
  - dropped the grpo/ppo parametrization. The check reads the
    reward_scaling block off whichever algorithm block is present, so
    the surrounding one cannot affect it and GRPO alone covers it.
  - renamed the accepting test after what it covers, the [0, 1] ->
    [-1, 1] interval every dapo-* recipe sets.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Signed-off-by: ruit <[email protected]>
L0_Unit_Tests_Other caught what the local runs could not:

    advantage_stage.py:355, in run
        rewards = scale_rewards(
            BatchedDataDict({"total_reward": rewards}), cfg.algo.reward_scaling
    AttributeError: 'types.SimpleNamespace' object has no attribute
                    'reward_scaling'

test_advantage_stage_writes_gae_returns_alongside_advantages built the PPO
algo block as a SimpleNamespace listing only the three fields it cared about,
so reading a fourth off it raised. The stage is right to read the field
unconditionally -- PPOConfig and GRPOConfig both declare it, and a getattr
fallback there would hide a genuinely missing config.

Use PPOConfig.model_construct() instead. It fills every default, including
default_factory ones, and still skips validation, so the next field added to
the block does not break this test again. Its defaults are already the three
the test wanted: adv_estimator.name "gae", overlong_filtering False,
seq_logprob_error_threshold None. The file builds its other PPO stub the same
way at line 2914.

The GRPOConfig.model_construct stubs were never affected, for the same reason
the fix works.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Signed-off-by: ruit <[email protected]>
Run the sharding test with the recipe interval so the logged reward moves
and a shard-dependent scale bug is caught, fix the "scaling block"
docstring, and update the SC PPO exemplar yaml comment that still said SC
implements neither reward knob.

Co-Authored-By: Claude Code <[email protected]>
Signed-off-by: ruit <[email protected]>
An infinite bound ordered fine against the existing source-interval check
and then made the remap divide inf by inf; a nan bound compared False
against every comparison and slipped through the same way. Both reached
training and turned every finite reward into inf/nan, a whole rollout
later and naming no config key -- scale_rewards(0.5) returns nan under
source_min=-inf, and inf under target_max=inf.

Check math.isfinite on all four bounds ahead of the interval check, and
name the offending ones in the error. Target bounds are still not
required to increase: a constant [1, 1] or reversed [1, -1] target is a
legal remap. Both checks now share one `enabled` guard.

Cover the four bounds against nan and both infinities, the two finite
target intervals, a disabled block with an infinite bound, and the PPO
block through algo_config. Also annotate the five signatures the review
flagged and shorten the two over-long in-function comments.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Signed-off-by: ruit <[email protected]>
@RayenTian
RayenTian force-pushed the ruit/reward_scaling_sc branch from ab0ae95 to af8f145 Compare November 7, 2026 08:25
@RayenTian

Copy link
Copy Markdown
Contributor Author

/ok to test e8c94a7

@RayenTian
RayenTian requested a review from mehraakash November 9, 2026 01:59

@mehraakash mehraakash 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.

Approving. All three of my threads are fixed in 3375579 and verified at e8c94a7: the PPO test fails when scale_rewards is stubbed to the identity, the shared RewardScalingConfig validator rejects inverted and non-finite bounds for both v1 and SC entry points, and the docs match the code. CI is green on this head.

One note for @aroshanghias-nvd: this PR removes grpo.reward_scaling.enabled from the validate_forest_objective unsupported table (#4878 (comment)), so reward scaling is now allowed under token_capture.forest_training. The scale runs on the physical rows before the logical-owner baseline, with a regression test in test_logical_advantage.py (test_forest_reward_scaling_before_owner_baseline) and Forest GPU runs off/on linked in that thread. Flagging in case you want to look at it, since the Forest objective rules are yours.

Generated by Claude Code

@RayenTian
RayenTian requested a review from yuki-97 November 10, 2026 03:34
@yuki-97
yuki-97 merged commit 49bdb22 into main Nov 10, 2026
105 checks passed
@yuki-97
yuki-97 deleted the ruit/reward_scaling_sc branch November 10, 2026 06:57
@aroshanghias-nvd

aroshanghias-nvd commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for flagging this, @mehraakash. I checked the forest path: valid segments of a rollout receive the same finalized reward, so scaling each row before computing the logical-owner baseline is consistent with our current design. The multi-segment regression coverage looks good.

I also noticed that scaling can hide a mismatch from the existing segment-reward consistency check if both values get clamped. This is a defensive-check gap; I haven’t found a normal path that produces mismatched segment rewards.

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.

5 participants