Skip to content

feat(grpo): per-environment GRPO settings (grpo.agent_overrides) - #5321

Merged
yfw merged 2 commits into
NVIDIA-NeMo:mainfrom
trintamaki:grpo-agent-overrides
Nov 21, 2026
Merged

yfw merged 2 commits into
NVIDIA-NeMo:mainfrom
trintamaki:grpo-agent-overrides

Conversation

@trintamaki

@trintamaki trintamaki commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

What

grpo.agent_overrides sets GRPO options per environment in multi-environment NeMo-Gym runs on the SingleController path with token capture. Each entry is keyed by the rows' attribution key, the same key as grpo.length_penalty.agent_overrides: the Gym agent name for flat rows, the task_id.taskset for materialized task rows. Set fields replace the run-wide value for that agent's rows; unset fields inherit it.

grpo:
  overlong_filtering: false                 # run-wide
  adv_estimator: {normalize_rewards: true}  # run-wide
  agent_overrides:
    rubric_judge_agent:
      overlong_filtering: true      # this agent's truncated rows leave the loss
      normalize_rewards: false      # no division by the group's reward std
Field Replaces Effect on the agent's rows
overlong_filtering grpo.overlong_filtering Truncated rows leave the loss (true) or train at their reward (false)
normalize_rewards grpo.adv_estimator.normalize_rewards Group advantages are divided by the group's reward std, or not

With agent_overrides unset nothing changes: rows are not tagged, and the mask helper and the estimator get the same arguments as before.

Why

Multi-environment runs mix pass/fail environments with judge-graded ones. A rubric-graded reward (for example the share of criteria passed) often differs little between the answers of one group. Dividing those advantages by the group's std turns small judge disagreements (0.80 vs 0.85 of criteria) into full-size advantages, which suits binary rewards but not graded ones. Cut-off handling has the same issue: some environments want truncated rows penalised, others masked. Both settings are run-wide today.

How

  • Rollout finalizer (rollout_reassembler.py): when overrides are configured, each published row's tag gets AGENT_NAME_TAG (data_plane/schema.py), by rollout slot. Rows without a reward context, such as Forest execution padding, take their group's agent.
  • Advantage stage (advantage_stage.py): agent_override_rows turns the tags into per-row switches.
    • _apply_async_sample_masks accepts a per-row overlong_filtering tensor.
    • GRPOAdvantageEstimator.compute_advantage accepts an optional per-row normalize_rows tensor.
    • Both the ordinary and the Forest owner paths use them.
  • Metrics: num_masked_seqs_by_overlong_filtering/<agent> for each configured agent, and num_rows_without_agent_tag (rows that fell back to the run-wide values).
  • Validation:
    • at least one key, every override sets something, and normalize_rewards overrides need the grpo estimator;
    • an overlong_filtering override must equal the run-wide value under dynamic sampling with masked_reward_policy: exclude, because dynamic sampling votes with the run-wide flag;
    • SingleController rejects it without token_capture.enabled (only the token-capture finalizer tags rows);
    • the legacy setup() path raises before the Logger and checkpoint manager start.
  • Docs: "Per-Environment Settings" in docs/guides/grpo.md, plus one line each in single-controller.md and nemo-gym-integration.md.

Tests

CPU run in the rl-nt4 container, this branch against main 492e1b7:

Suite main This PR
tests/unit/algorithms/test_grpo_agent_overrides.py (new) n/a 20 passed
tests/unit/test_config_v2.py 14 passed 14 passed
tests/unit/single_controller/test_advantage_stage.py 58 passed 62 passed
tests/unit/single_controller/test_setup.py 338 passed 341 passed
tests/unit/single_controller/test_logical_advantage.py 123 passed 124 passed
tests/unit/experience/test_rollout_reassembler_actor.py 44 passed 46 passed
test_advantage_estimator.py, test_advantage_validity.py, test_grpo_group_identity.py 66 passed 66 passed
tests/unit/algorithms/test_grpo.py -k "mask or overlong or config" 296 passed 296 passed
tests/unit/algorithms/test_grpo.py -k "agent_overrides or router_replay_before_side_effects" 1 passed 2 passed
tests/unit/data_plane/test_rollout_reassembler.py --nemo-gym-only 126 passed, 2 failed 128 passed, same 2 failed

The two finalizer failures (test_finalize_group_publishes_minf_routes_staged_through_the_stager, test_megatron_capture_two_turn_media_finalizes_to_the_engine_pixels) fail the same way on main in this environment.

The stage tests check, in both directions (run-wide off/on, override flipping both fields), that each key's rows come out exactly as a run with that key's settings applied run-wide, on the ordinary and the Forest owner paths, and that untagged rows keep the run-wide settings. ruff check and ruff format (0.9.9) are clean.

Not yet exercised in a full training run.

Checklist

  • Unit tests added
  • Documentation

🤖 Generated with Claude Code

@copy-pr-bot

copy-pr-bot Bot commented Nov 19, 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.

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

Thank you for this PR. It adds grpo.agent_overrides, which sets overlong_filtering and normalize_rewards per environment on SingleController. The per-row normalize_rows input on the estimator is a clean place for this. With agent_overrides unset, the code passes exactly the same arguments as before.

The key is the row's attribution key, the same key as grpo.length_penalty.agent_overrides. That works for per-environment settings, also when one harness agent serves many environments, because taskset rows key by taskset. C6 only asks to say this in the text and the docs.

Findings, most severe first:

  1. (C1) CI blocker: the reference grpo_math_1B.yaml is missing the new default.
  2. (C2) Without token capture, SingleController ignores the overrides with no error. The legacy raise also runs after Logger(...) (which opens the W&B run when W&B logging is on), CheckpointManager and the dataloaders.
  3. (C3) Dynamic sampling under masked_reward_policy: exclude keeps or drops groups by the run-wide overlong_filtering.
  4. (C4) Test gaps in the advantage stage and the Forest owner path.
  5. (C5) Wiring tests for stamp_agent_names, a shared tag constant, and a wrong code comment.
  6. (C6) Key wording and user docs.

After the fixes, please do one short SingleController run with token capture and two environments, one of them overridden. Please use a run without Forest (Forest does not log these metrics) and share that num_rows_without_agent_tag is 0, and that with an overlong_filtering: true override on an environment whose rows get truncated, num_masked_seqs_by_overlong_filtering/<key> is above 0. The unit tests cannot cover the real Ray actor hand-off. Convergence curves are not needed, because the feature is opt-in and the unset path does not change.

Local CPU results: the PR's new tests pass, and test_config_v2_same_as_v1 fails as C1 describes. The two test_rollout_reassembler.py failures that the PR body lists also fail on main here, because the venv has no megatron-core. pre-commit (ruff, ruff-format) passes on the 9 files. CI did not run on this head yet.

Generated by Claude Code

Comment thread nemo_rl/algorithms/grpo.py Resolved
Comment thread nemo_rl/algorithms/grpo.py Resolved
Comment thread nemo_rl/algorithms/grpo.py Resolved
Comment thread nemo_rl/algorithms/grpo.py Resolved
@github-actions github-actions Bot added the Documentation Improvements or additions to documentation label Nov 20, 2026
@trintamaki

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review. All six are addressed in c0b77a6:

  • C1: agent_overrides: null is added after overlong_filtering in both examples/configs/grpo_math_1B.yaml and the reference copy. test_config_v2.py passes (14).
  • C2:
    • _validate_algo_settings rejects grpo.agent_overrides without token_capture.enabled, with a test in test_setup.py.
    • The legacy setup() raise now sits right after the on_policy_distillation.full raise, with test_setup_rejects_agent_overrides_before_side_effects.
  • C3: your validator guard is in. An overlong_filtering override that differs from the run-wide value is rejected under dynamic sampling with exclude, with tests for both directions and for the allowed cases (same value, or include).
  • C4: both stage tests and the Forest owner test are in.
  • C5: both wiring tests are in. AGENT_NAME_TAG moved to data_plane/schema.py and is used by the finalizer and the stage. The getattr is gone.
  • C6: the docstring, field comment and errors now say "attribution key". There is a "Per-Environment Settings" section in docs/guides/grpo.md, and one line each in single-controller.md and nemo-gym-integration.md.

The CPU suites are in the updated PR body. The only failures are the same two reassembler failures that main also has.

I'll follow up with the SingleController smoke run: token capture on, no Forest, two environments with one overridden, reporting num_rows_without_agent_tag and num_masked_seqs_by_overlong_filtering/<key>.

@trintamaki

Copy link
Copy Markdown
Contributor Author

Smoke run done: SingleController with token capture, no Forest, two NeMo-Gym environments (flat rows), one of them overridden.

Setup (1 node, 4 GPUs: 2 vLLM + 2 Megatron, Qwen3-0.6B, 6 steps × 4 prompts × 4 generations):

grpo:
  overlong_filtering: false                 # run-wide
  adv_estimator: {normalize_rewards: true}  # run-wide
  agent_overrides:
    mcqa_simple_agent: {overlong_filtering: true, normalize_rewards: false}
token_capture: {enabled: true}
env.nemo_gym.config_paths: [vllm_model_for_training, mcqa, string_match]
policy.max_total_sequence_length: 512       # small context so long answers truncate

The data is the two example sets interleaved (mcqa_simple_agent, string_match_simple_agent), so every step has two groups of each.

Result (every step, from the step metrics):

Step num_rows_without_agent_tag num_masked_seqs_by_overlong_filtering/mcqa_simple_agent run-wide num_masked_seqs_by_overlong_filtering mcqa_simple_agent/truncation_rate string_match_simple_agent/truncation_rate
1 0 8 8 1.0 0.5
2 0 8 8 1.0 0.25
3 0 8 8 1.0 0.5
4 0 8 8 1.0 0.0
5 0 8 8 1.0 0.5
6 0 8 8 1.0 0.0
  • Every row reached the advantage stage with its key, through the real Ray hand-off: setup → finalizer actors → row tags → advantage stage.
  • The overridden environment's truncated rows left the loss: all 8 mcqa rows per step were cut off, and all 8 were masked.
  • The other environment kept the run-wide overlong_filtering: false. Its truncated rows (up to 4 per step) stayed in the loss, so the run-wide masked count equals the mcqa count.

One caveat on the base. The cluster I ran on only has the internal post-training container (vLLM 0.29), and main's vLLM patches target vLLM 0.33. So this run used this PR's two commits cherry-picked onto that container's matching source commit, not main itself. The diff is the same feature code. The only conflicts were nearby lines that main added since then: length_penalty wiring, the shard-invariant estimator set, and the finalizer setup refactor.

trintamaki and others added 2 commits November 20, 2026 14:34
Multi-environment runs mix pass/fail environments with judge-graded ones,
which need different settings. grpo.agent_overrides maps a NeMo-Gym agent
name to settings that replace the run-wide value for that agent's rows;
unset fields inherit the run:

  grpo:
    agent_overrides:
      rubric_judge_agent:
        overlong_filtering: true    # truncated rows leave the loss
        normalize_rewards: false    # no division by the group reward std

- The rollout finalizer stamps each published row's tag with its rollout's
  agent (agent_name) when overrides are configured.
- The SingleController advantage stage turns the tags into per-row switches:
  _apply_async_sample_masks accepts a per-row overlong_filtering tensor and
  GRPOAdvantageEstimator.compute_advantage a per-row normalize_rows tensor.
- Metrics: num_masked_seqs_by_overlong_filtering/<agent> per configured agent
  and num_rows_without_agent_tag (rows that fell back to run-wide values).
- Validation: at least one agent, every override sets something,
  normalize_rewards overrides need the grpo estimator; the legacy setup()
  raises (SingleController path only).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Tuomas Rintamaki <[email protected]>
- Add the agent_overrides default to grpo_math_1B.yaml (exemplar + reference).
- Reject agent_overrides on SingleController without token capture, and move
  the legacy setup() raise before the Logger and checkpoint manager.
- Reject an overlong_filtering override that differs from the run-wide value
  under dynamic sampling with masked_reward_policy 'exclude'.
- Move AGENT_NAME_TAG to data_plane/schema.py; drop a stale getattr in setup.
- Say 'attribution key' (agent name for flat rows, task_id.taskset for task
  rows) in docstrings, errors and the new docs section.
- Tests: opposite run-wide settings, per-agent counts, Forest owners, finalizer
  wiring (setup and actor), the legacy raise, the two new guards.

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

yfw commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

/ok to test 33f611a

@yfw
yfw enabled auto-merge (squash) November 20, 2026 23:38
@yfw
yfw merged commit 5e27117 into NVIDIA-NeMo:main Nov 21, 2026
94 checks passed
yfw added a commit that referenced this pull request Nov 21, 2026
…kind-advantages

Brings in 69 commits, including per-environment GRPO settings
(grpo.agent_overrides, #5321) and the advantage statistics fix across
filtering and batch splitting (#5216). Two conflict blocks, both resolved by
keeping both sides:

- grpo.py: DEFAULT_POLICY_FAILURE_KINDS next to main's AgentOverrideConfig.
- test_rollout_reassembler.py: this PR's infra-failure weight test next to
  main's agent-name stamping tests.

One more change keeps this PR's test working: the sync-trainer case of
test_trainers_infra_masked_reward_never_moves_valid_siblings sets
data_plane.backend, which grpo_train_sync now reads (#5151 made the same
change to main's tests).

exclude_infra needs no change for agent_overrides: its voters do not depend
on overlong_filtering, so main's dynamic-sampling check stays limited to
masked_reward_policy "exclude". Main's ledger schema is still 8, so
failure_kind stays in schema 9.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Yi-Fu Wu <[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