Skip to content

fix: honor NeMo Gym's mask_sample in the Gym rollout path - #3201

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

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

Conversation

@linj-glitch

Copy link
Copy Markdown
Contributor

What does this PR do ?

Honors NeMo Gym's mask_sample signal in the Gym rollout path so rollouts with unreliable rewards are excluded from the gradient instead of trained on (or crashing the step).

Gym agent servers (e.g. swe_agents, anyterminal_agent) set mask_sample: true on the verify response when a rollout's reward is unreliable — agent/eval timeout, OOM-killed container, failed token-trace capture — and document the contract as "downstream RL drops the gradient for this rollout." Nothing in NeMo RL consumed it, so today:

  • masked rollouts train anyway with garbage rewards contaminating GRPO group advantages — the worst sub-case being reward=1.0 accidental resolves (patch passed but the agent died on max-iteration/context-window) reinforced as positives;
  • a masked rollout carrying no token-annotated generations (e.g. a failed trace capture on the Gym↔Switchyard path) raises ValueError in _postprocess_nemo_gym_to_nemo_rl_result and fails the whole rollout batch.

Changes (both mirror existing conventions):

  • run_async_nemo_gym_rollout zeroes the sample's loss_multiplier when full_result["mask_sample"] is truthy — the same mechanism used for truncated samples (loss_multiplier[truncated] = 0).
  • The converter emits a prompt-only message log for masked rollouts with no generations, so tensorization proceeds and the zeroed loss multiplier excludes the sample; unmasked empty rollouts still raise the existing actionable ValueError.

Producer side: NeMo Gym promotes mask_sample to a top-level BaseVerifyResponse field (NVIDIA-NeMo/Gym#2026), so it also lands in the per-agent auto-aggregated metrics (rollouts.py scalar aggregation) — masked-sample rates become visible in training metrics for free.

Issues

None filed; found while validating the Gym↔Switchyard token-capture integration end-to-end (NVIDIA-NeMo/Gym#2026).

Usage

No API change. Any Gym rollout whose verify response carries mask_sample: true is now excluded from the gradient:

# Gym agent server (producer side)
return MyVerifyResponse(..., reward=reward, mask_sample=True)  # unreliable reward
# → NeMo RL zeroes that sample's loss_multiplier; no training signal from it.

Before your PR is "Ready for review"

Pre checks:

  • Make sure you read and followed Contributor guidelines
  • Did you write any new necessary tests? (test_nemo_gym_postprocess_masked_rollout_without_generations, test_nemo_gym_postprocess_unmasked_rollout_without_generations_raises)
  • Did you run the unit tests and functional tests locally? (converter paths + loss-multiplier zeroing exercised directly on CPU; [email protected] check/format clean)
  • Did you add or update any necessary documentation?

Additional Information

The end-to-end verification that surfaced this: Switchyard token capture → Gym rollout reconstruction → this converter → GRPO tensors, with the masked-sample and non-contiguous-token failure modes reproduced against real code.

🤖 Generated with Claude Code

Gym agent servers set mask_sample on the verify response when a
rollout's reward is unreliable (agent/eval timeout, OOM-killed
container, failed trace capture), documented as 'downstream RL drops
the gradient for this rollout' — but nothing consumed it, so masked
rollouts trained on garbage rewards (worst case: reward=1.0 accidental
resolves reinforced as positives).

- run_async_nemo_gym_rollout zeroes the sample's loss multiplier when
  full_result carries mask_sample, mirroring truncated-sample handling
- the converter emits a prompt-only message log for masked rollouts
  with no token-annotated generations (e.g. failed trace capture)
  instead of raising and failing the whole step; unmasked
  empty rollouts still raise

Co-Authored-By: Claude Fable 5 <[email protected]>
Signed-off-by: Lin Jia <[email protected]>
@linj-glitch
linj-glitch requested review from a team as code owners July 14, 2026 23:12
@copy-pr-bot

copy-pr-bot Bot commented Jul 14, 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.

@terrykong

Copy link
Copy Markdown
Collaborator

@ananthsub @bxyu-nvidia to review

@jepio

jepio commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

@linj-glitch Gym's mask_sample is not granular enough to always honor, for example if rollouts hit max iteration but solves the task, Gym sets "mask_sample=True" but that is a trainable sample and we want to train our workload on such samples.

I would suggest adding a config setting that let's users decide whether to honor mask_sample or not.

michal2409 added a commit to michal2409/RL that referenced this pull request Jul 29, 2026
Gym flags rollouts that hit max iterations even when they solve the
task, and those are samples worth training on (see NVIDIA-NeMo#3201
discussion).

Signed-off-by: Michal Futrega <[email protected]>
@linj-glitch
linj-glitch marked this pull request as draft October 5, 2026 06:10
@linj-glitch

Copy link
Copy Markdown
Contributor Author

Closing the loop on this one. The problem is real, but this implementation is obsolete: main has moved on, this branch conflicts with it, and both halves of the change now live in newer PRs on current main.

Closing this draft in favour of #4435 and #4494 (the ported converter tests are already on the #4494 branch). Thanks @jepio for the config-gate feedback; env.should_mask_flagged_samples is that setting.

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.

3 participants