Skip to content

feat(nemo-gym): optionally mask unusable rollouts instead of failing the batch - #4494

Open
linj-glitch wants to merge 7 commits into
NVIDIA-NeMo:mainfrom
linj-glitch:linj/nemo-gym-mask-unusable-rollouts
Open

linj-glitch wants to merge 7 commits into
NVIDIA-NeMo:mainfrom
linj-glitch:linj/nemo-gym-mask-unusable-rollouts

Conversation

@linj-glitch

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

Copy link
Copy Markdown
Contributor

What does this PR do ?

Adds opt-in env.nemo_gym.mask_unusable_rollouts (default false) so one rollout with no trainable output or a non-contiguous token chain does not fail the whole rollout batch.

With the option enabled, a no-generation result becomes a text-only placeholder (128 prompt tokens plus EOS); a broken chain retains its contiguous prefix. Both receive an unusable-rollout reason and zero loss weight regardless of env.should_mask_flagged_samples. Synthetic placeholders never reacquire the original prompt's media. A retained real prefix keeps its media, and a mixed group takes its prompt from the first real sibling.

The integrated main includes #4160: these rows acquire zero dataset weight before trainer filtering, so their rewards are excluded from group statistics under either grpo.masked_reward_policy, on legacy, TransferQueue and SingleController paths. The default strict behavior remains unchanged.

Issues

None.

Usage

env:
  nemo_gym:
    mask_unusable_rollouts: true

This option supports the token-echo path. Combining it with the top-level token_capture.enabled receipt mode is rejected during setup.

Before your PR is "Ready for review"

Pre checks:

  • Read and followed the Contributor guidelines.
  • Added regression tests.
  • Ran local unit tests (scope below; no functional training run repeated).
  • Updated the integration design doc and example configuration.

Additional Information

Merged main d48b2e0f3d7fc4891b418ae603c8e8fa71482b9c additively at 1fef5689f2ed69b05973a467a45b98305aab7b17. The only source conflict contained independent additions: main's Gym log-directory helper and this PR's unusable-rollout helpers; both are retained. Documentation now reflects #4160's baseline handling. An independent audit verified preservation of both parent behaviors.

Validation on the integrated tree, 2026-10-28 (native pytest with repository conftest and a local CPU Ray cluster):

unit/environments/test_nemo_gym.py
unit/environments/test_nemo_gym_utils.py
unit/experience/test_payload.py
unit/experience/test_reward_penalties.py
unit/experience/test_rollout_manager.py
-m 'not vllm and not nemo_gym'
323 passed, 9 deselected

Coverage includes opt-in/default-strict conversion, broken-chain cleanup, input truncation/fallback, unconditional zero loss, receipt-mode rejection, and media-free placeholders beside real multimodal siblings. The media tests check packed model-input alignment; a full Nemotron Omni forward or distributed training run was not repeated.

Full-repository pre-commit was run. Pyrefly reports 44 existing diagnostics, identical by file/message to the independently measured main baseline (0e36f2e6a), with no new diagnostics. All remaining hooks pass; the final full-file hook rerun skips only that separately measured Pyrefly check. GitHub's current required checks and review gates remain authoritative.

@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

Copy link
Copy Markdown
Contributor Author

Dependency: this PR is stacked on #4435 (first-class mask_sample reader). The branch carries #4435's two commits rebased onto main (109a4c127, f81a2246f = e488c1b38 on fe1d97ce2) below its own two commits, so the diff shown here includes them until #4435 merges; after that this branch rebases to its own two commits. Please review #4435 first. It also supersedes the closed #3201 and #4489 (their converter and reader tests are ported here).

@linj-glitch

Copy link
Copy Markdown
Contributor Author

/ok to test a453f6b

@linj-glitch linj-glitch added the CI:L2 Run doctests, unit tests, functional tests, and convergence tests label Oct 18, 2026
@linj-glitch
linj-glitch force-pushed the linj/nemo-gym-mask-unusable-rollouts branch from a453f6b to 792c9b2 Compare October 19, 2026 04:36
@linj-glitch

Copy link
Copy Markdown
Contributor Author

/ok to test 792c9b2

linj-glitch and others added 6 commits October 23, 2026 23:53
…the batch

_postprocess_nemo_gym_to_nemo_rl_result raises ValueError when a Gym
result carries no generation data (the first prompt already exceeds
max_model_len, or the agent produced no assistant turn), and an
AssertionError when the token chain is not contiguous across turns (agent
harnesses that rewrite or compact their history). Either exception ends
the whole run_rollouts stream, so one bad rollout fails its batch; the
async collector then re-runs the entire batch while the abandoned
rollouts keep running in Gym.

Add env.nemo_gym.mask_unusable_rollouts (default false, which keeps the
current fail-loudly behavior). When enabled, such a rollout becomes a
placeholder sample instead:
- no generation data: a fixed 128-token prompt (start of the input text,
  padded with EOS) plus one EOS "generation" token with logprob 0;
- non-contiguous chain: the contiguous prefix is kept.
The result gets top-level mask_sample=True and an _ng_unusable_rollout
marker. Rollout batching zeroes the loss weight of marked samples
(loss_multiplier on the NeMo-Gym batch path, sample_mask on the
RolloutManager payload path) regardless of
env.should_mask_flagged_samples, so the placeholder never trains.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Lin Jia <[email protected]>
…ompt media

With env.nemo_gym.mask_unusable_rollouts on, a result with no generation
data becomes a text/EOS placeholder. The static-media restoration then
copied the prompt's original image tensors onto it, in both rollout
paths: _reattach_static_multimodal_payloads_to_result on the batched
path and attach_static_multimodal_payload in the RolloutManager. The
placeholder holds no media placeholder tokens, and the native Nemotron
Omni forward (Megatron-Bridge ec835530e,
modeling_nemotron_omni.py::NemotronOmniModel._merge_projected_media)
requires one text placeholder per projected media feature, so a legacy
Gym Nemotron Omni VLM run failed on "found 0 valid placeholders for 1
projected features" before the row's zero loss weight could exclude it.

Identify the synthetic placeholder explicitly (reason constants and
is_synthetic_unusable_rollout in experience/interfaces.py) and skip the
restoration only for it. A non-contiguous rollout reduced to its
contiguous prefix keeps real Gym tokens and still gets its media back.
In the RolloutManager, take the group prompt from the first real sibling
so the synthetic prompt never stands in for the group's prompt length,
prompt ids or media while a real sibling exists.

Reject mask_unusable_rollouts together with token_capture.enabled at
setup: receipt rollouts bypass the converter that emits the placeholder,
so the knob would be silently ignored.

Tests: conversion -> static-media restoration -> flattened model inputs
with a real PackedTensor, checked against the pinned Nemotron Omni
alignment invariant (input invariant only, not a model forward), for the
placeholder, the retained contiguous prefix, both restoration entry
points, the RolloutManager path (gate on and off, mixed and all-synthetic
groups) and record_to_train_batch. Also pin that a Gym-flagged
no-generation result converts only under the knob and still raises by
default, that an empty input yields the fixed-length placeholder prompt,
the receipt-mode rejection, and top-level mask_sample rows beside an
empty or absent instance_config.

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

With mask_unusable_rollouts on, the non-contiguous branch broke out of the
converter loop before the per-item pops, so every trainable item from the
first non-contiguous one onward kept prompt_token_ids, generation_token_ids,
generation_log_probs and the base64 routed_experts envelope in full_result,
while every other row was popped and decoded. That inconsistent shape flowed
into Completion.env_extras, the per-agent metrics and the opt-in W&B
full_result table. Pop the payloads from the dropped tail before breaking so
a masked result has the same shape as any other row, and assert it in the
non-contiguous test.

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

The setup-time ValueError and the design doc told the operator that
mask_unusable_rollouts is rejected together with
`env.nemo_gym.token_capture.enabled`, but token capture is not an
env.nemo_gym key: it is the top-level single-controller `token_capture:`
block (MasterConfig.token_capture), which setup passes into
build_nemo_gym_config. Name the real key so the operator can find which of
the two settings to flip.

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

The _mark_unusable_rollout docstring promised that the placeholder is masked
from loss and baseline. Zero loss holds on every path, but only the
single-controller advantage stage passes sample_mask into the baseline's
valid mask; the batched GRPO and grpo_sync paths call
calculate_baseline_and_std_per_prompt with an all-ones valid mask, so the
placeholder's reward still enters its siblings' leave-one-out baseline
(tracked in NVIDIA-NeMo#4160 / NVIDIA-NeMo#4490). Say exactly that in the docstring and the design
doc, and note in _run_rollouts why a mixed group's placeholder may carry a
prompt_ids_for_adv row cut at the real prompt's length (the SC baseline keys
on GROUP_ID_TAG, so prompt ids are not a grouping key).

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

Three branches of the no-generation placeholder had no test: the slice to
_UNUSABLE_ROLLOUT_PROMPT_TOKENS (which is what keeps the placeholder under
any sequence limit; every existing test fed at most three tokens), _input_text
over the Responses input shapes (plain string, list-of-parts content with an
interleaved image part, system+user, non-dict and non-string items, dict,
None) and the tokenizer.encode failure fallback to a filler-only prompt.

Also pin the placeholder's input_message_log by length and identity instead
of `== [user]`: that comparison passed only through CPython's identity
shortcut, and a converter that copied the prompt turn would have turned it
into "Boolean value of Tensor with more than one value is ambiguous" rather
than a clean assertion failure.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: Lin Jia <[email protected]>
@linj-glitch

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (792c9b2 → 6 commits on 6705e9d). #4435 has merged, so the branch now carries only this PR's own commits; the diff is this PR alone.

Two conflicts resolved against main's multimodal-reattachment change (processed_target_ids / tokenizer threading in rollout_manager.py and rollouts.py): the synthetic no-generation placeholder is still skipped for media reattachment, and real completions go through main's shared-identity-set path.

Local run: tests/unit/environments/test_nemo_gym.py tests/unit/experience/test_rollouts.py tests/unit/experience/test_rollout_manager.py tests/unit/experience/test_rollout_generation_failures.py → 277 passed. Pre-commit (ruff, ruff-format) clean on the touched files.

@linj-glitch
linj-glitch force-pushed the linj/nemo-gym-mask-unusable-rollouts branch from 792c9b2 to aeafeb9 Compare October 24, 2026 07:27
@linj-glitch

Copy link
Copy Markdown
Contributor Author

/ok to test 1fef568

@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:L2 Run doctests, unit tests, functional tests, and convergence tests labels Oct 29, 2026
@terrykong terrykong removed the Documentation Improvements or additions to documentation label Nov 28, 2026

This branch has not been deployed

No deployments
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)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants