Skip to content

fix(nemo-gym): honor top-level mask_sample on rollout results - #4489

Closed
linj-glitch wants to merge 1 commit into
NVIDIA-NeMo:mainfrom
linj-glitch:linj/nemo-gym-top-level-mask-sample
Closed

linj-glitch wants to merge 1 commit into
NVIDIA-NeMo:mainfrom
linj-glitch:linj/nemo-gym-top-level-mask-sample

Conversation

@linj-glitch

Copy link
Copy Markdown
Contributor

What does this PR do ?

Masks a NeMo-Gym rollout when the result carries mask_sample at the top level. This is where NeMo-Gym puts it. Before this change, NeMo-RL only read instance_config["mask_sample"].

Issues

None.

Usage

No new configuration. env.should_mask_flagged_samples still gates the flag, and its default is unchanged.

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 (unit tests only, CPU; see below)
  • Did you add or update any necessary documentation? Visit our Document Development Guide for how to write, build and test the docs. (N/A: no user-facing change)

Additional Information

Root cause. NeMo-Gym's BaseVerifyResponse defines mask_sample as a top-level field of the /run result. A resources server sets it when the environment or its infrastructure failed and the reward is not a valid measurement: a lost session, an unavailable judge, an OOM-killed container. Gym's token-id capture also sets mask_sample at the top level when a captured chain is broken, ambiguous, or incomplete. NeMo-RL looked only inside instance_config, which is the convention some agent servers use.

How it manifests. Rollouts that Gym flags as invalid are trained as if they were valid. Their reward, often a synthetic 0, gets a real advantage and enters the loss. Nothing is logged; the flag is silently ignored.

Fix. A single helper, _env_mask_sample_flag, reads both locations. It is used in three places:

  • _mask_sample_flags, which covers the batched NeMo-Gym rollout path (sync and async GRPO) and the RolloutManager payload path.
  • The RolloutManager token-capture finalization path (_record_streamed_completion). This path read only instance_config on its own.
  • The env.should_mask_flagged_samples: false gate in AsyncNemoGymRolloutImpl._results_to_completions, which now drops the flag from both locations through _drop_env_mask_sample_flag. Without this, a top-level flag would survive the gate and still mask the sample.

Tests.

  • tests/unit/experience/test_reward_penalties.py::TestMaskSampleFlags::test_reads_top_level_mask_sample
  • tests/unit/experience/test_rollout_manager.py::test_result_to_completion_top_level_mask_flag_follows_gate (token and receipt results, gate on and off)
  • tests/unit/experience/test_rollout_manager.py::TestGenerateForFinalizationFlow::test_request_carries_top_level_env_mask_flags

Commands run (CPU only):

python -m pytest tests/unit/experience/test_reward_penalties.py tests/unit/experience/test_rollout_manager.py  -> 114 passed
python -m pytest tests/unit/experience/test_payload.py                                                    -> 10 passed
ruff check / ruff check --select I / ruff format --check (v0.9.9) on touched files                       -> clean

🤖 Generated with Claude Code

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

NeMo-Gym's BaseVerifyResponse carries mask_sample as a top-level field of
the /run result, and token-id capture also sets it there when a captured
chain is unusable. NeMo-RL only read instance_config["mask_sample"], so
samples the environment asked to mask were trained as valid.

Read both locations through one helper (_env_mask_sample_flag) used by the
batched rollout path, the RolloutManager token-capture finalization path,
and drop both locations when env.should_mask_flagged_samples is false.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Lin Jia <[email protected]>
@linj-glitch
linj-glitch force-pushed the linj/nemo-gym-top-level-mask-sample branch from 3cc8704 to 8d8c778 Compare October 8, 2026 00:15
@linj-glitch

Copy link
Copy Markdown
Contributor Author

Superseded by #4435. Alexander's PR reads the same two flag locations (first-class BaseVerifyResponse.mask_sample OR instance_config.mask_sample) in _mask_sample_flags and in the rollout manager's sibling-seal read, behind the same env.should_mask_flagged_samples gate. I compared the two readers on 13 inputs (each form alone, both, a false top-level beside a true nested one, omitted, None, non-bool values, non-dict instance_config): identical results in every case. I also ran this PR's three tests against #4435's code on current main (one fixture kwarg renamed): all pass, alongside #4435's own 120.

#4435 also does two things this PR does not. When the gate is off it drops the flag from a copy, so the Gym result the caller holds is not mutated, and the per-agent metrics still report mask_sample/mean (this PR popped the keys in place, which also removed them from the metrics; the legacy batched path never lost them, so #4435 makes the two paths consistent). #4435 is Ready for review and merges cleanly with main; this branch conflicts in tests/unit/experience/test_rollout_manager.py.

What moves where:

Closing this PR in favour of #4435.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant