Skip to content

fix(gym): hold the tokenizer in NemoGym instead of passing it per rollout - #3668

Merged
terrykong merged 1 commit into
NVIDIA-NeMo:mainfrom
saumishr:fix/gym-hold-tokenizer
Aug 19, 2026
Merged

terrykong merged 1 commit into
NVIDIA-NeMo:mainfrom
saumishr:fix/gym-hold-tokenizer

Conversation

@saumishr

@saumishr saumishr commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

Overview

run_rollouts took the tokenizer as an argument and the rollout pump calls it once per prompt, so Ray deserialized a 7.45 MB tokenizer on the actor's task-execution thread for every prompt. That thread holds the GIL, so it blocked the actor's event loop and no rollout could issue its first HTTP request until its own copy finished loading.

Measured: 286 ms to serialize, 1052 ms to deserialize. A SingleController recipe admitting ~1000 prompts per step spends ~18 minutes deserializing one admission burst.

Reproduced on one CPU node with 1024 concurrent calls against one actor: 153 of 1024 prompts finished in 300 s passing the tokenizer per call, versus all 1024 in 16 s installing it once. Passing an ObjectRef instead does not help; Ray caches the object buffer and not the deserialized value, so the actor still pays per task.

Install it through the spinup paths so no call site can forget: the shared spinup_nemo_gym_actor helper takes it as a keyword argument, and distillation, which builds its actor inline, sets it alongside its own _spinup call. On the SingleController path it threads through _spinup_gym, since the kwargs that used to sit at the partial now live inside that function.

run_rollouts fails loudly rather than silently if it is somehow reached without one -- postprocessing with no tokenizer is worse than a named error, and the attribute is initialized in init so the check reads as state rather than as an absent attribute.

Test fakes that stood in for the actor method lose the argument with it, and the SingleController setup test now asserts the tokenizer reaches spinup, which is the property that keeps it off the per-call path.

What does this PR do ?

Add a one line overview of what this PR aims to accomplish.

Issues

List issues that this PR closes (syntax):

Usage

  • You can potentially add a usage example below
# Add a code snippet demonstrating how to use this

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

  • ...

@saumishr
saumishr requested review from a team as code owners August 16, 2026 19:33
@copy-pr-bot

copy-pr-bot Bot commented Aug 16, 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.

@cursor

cursor Bot commented Aug 16, 2026

Copy link
Copy Markdown

Bugbot is not enabled for your account, so this pull request was not reviewed.

Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs.

@saumishr
saumishr requested review from mehraakash and yuki-97 August 16, 2026 19:33
@saumishr
saumishr force-pushed the fix/gym-hold-tokenizer branch from 76ee498 to e8499e9 Compare August 18, 2026 05:55

@saumishr saumishr left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two findings, both low severity: the new _tokenizer attribute is declared in _spinup() rather than in __init__ (a latent order dependency, and not what the PR description says the code does), and the new distillation assertion does not check which object gets installed.

Comment thread nemo_rl/environments/nemo_gym.py Resolved
@saumishr saumishr added the CI:L1 Run doctests, unit tests, and functional tests label Aug 18, 2026
@saumishr
saumishr requested a review from terrykong August 18, 2026 06:36
@NVIDIA-NeMo NVIDIA-NeMo deleted a comment from copy-pr-bot Bot Aug 18, 2026
@saumishr

Copy link
Copy Markdown
Contributor Author

/ok to test 4b8b485

@saumishr
saumishr force-pushed the fix/gym-hold-tokenizer branch from b1cd26c to 1c6f073 Compare August 18, 2026 18:16
@saumishr

Copy link
Copy Markdown
Contributor Author

Force-pushed 1c6f073 — two changes, plus a history rewrite to keep internal identifiers out of the record.

Scrubbed the internal references. The set_tokenizer docstring cited a Slurm job id and an internal recipe label to establish the cost; the PR description carried the recipe label too. Both now describe the shape generically ("a SingleController recipe admitting ~1000 prompts per step", "runs on that shape stalled without completing a rollout"), and the measurements that actually carry the argument are unchanged. I folded that edit into the original commit and reworded its message rather than stacking a fix on top, so no commit in the branch — message or tree — contains the job id.

Fixed the L0_Unit_Tests_Nemo_Gym failure, which was real and not flake: test_async_nemo_gym_rollout_manager died on NemoGym.set_tokenizer must be called before run_rollouts. The shared nemo_gym fixture yields an actor after _spinup only, so once run_rollouts stopped taking a tokenizer per call, every test reaching the actor through RolloutManager had no way to install one — test_nemo_gym_sanity was patched inline and was the only one that could pass. The fixture now installs it the way spinup_nemo_gym_actor does, and the inline call is gone. That closes the audit: all three actor construction sites (spinup_nemo_gym_actor, distillation's inline build, and the fixture) install a tokenizer, and there are no others.

The earlier CI quality check failure was just the missing CI label, which is on the PR now.

@saumishr

Copy link
Copy Markdown
Contributor Author

/ok to test 1c6f073

@saumishr
saumishr force-pushed the fix/gym-hold-tokenizer branch from 1c6f073 to e483970 Compare August 18, 2026 18:40
@saumishr

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (f9a2b30c0) and force-pushed e483970; conflicts are resolved.

One conflict, in _spinup_gym's build_tasks entry in nemo_rl/algorithms/single_controller_utils/setup.py. #3590 changed base_urls to prefer the generation router's single URL when the router is live, and this PR adds tokenizer=tokenizer to the same call — both sides kept, since they are independent arguments.

The patch is otherwise unchanged: same 11 files and same +106/-23 as before the rebase, with only hunk offsets moving. I re-ran the audit that the last round's CI failure came from, against the new base: #3590 adds no new NemoGym construction or run_rollouts call site, so the three construction sites (spinup_nemo_gym_actor, distillation's inline build, the nemo_gym fixture) still all install a tokenizer, both spinup_nemo_gym_actor callers still pass one, and both remote run_rollouts calls still omit it. ruff format, ruff check and py_compile are clean on all 11 files.

test_single_controller_setup.py's base_urls assertion still matches, because the router is disabled in that test's config and the resolved expression falls through to dp_openai_server_base_urls.

@saumishr

Copy link
Copy Markdown
Contributor Author

/ok to test e483970

…lout

run_rollouts took the tokenizer as an argument and the rollout pump calls it
once per prompt, so Ray deserialized a 7.45 MB tokenizer on the actor's
task-execution thread for every prompt. That thread holds the GIL, so it
blocked the actor's event loop and no rollout could issue its first HTTP
request until its own copy finished loading.

Measured: 286 ms to serialize, 1052 ms to deserialize. A SingleController
recipe admitting ~1000 prompts per step spends ~18 minutes deserializing one
admission burst. Runs on that shape completed zero rollouts in 30 minutes, with
no connection error of any kind in a Gym build that logs them.

Reproduced on one CPU node with 1024 concurrent calls against one actor: 153 of
1024 prompts finished in 300 s passing the tokenizer per call, versus all 1024
in 16 s installing it once. Passing an ObjectRef instead does not help; Ray
caches the object buffer and not the deserialized value, so the actor still
pays per task.

Install it through the spinup paths so no call site can forget: the shared
spinup_nemo_gym_actor helper takes it as a keyword argument, and distillation,
which builds its actor inline, sets it alongside its own _spinup call. On the
SingleController path it threads through _spinup_gym, since the kwargs that
used to sit at the partial now live inside that function. The nemo_gym test
fixture is the third such site and installs it the same way, so every path that
hands out an actor hands out one that can actually run rollouts.

The attribute is declared in __init__, not at the tail of _spinup where the
value is first needed. __init__ already declares every other _spinup-populated
field -- rh, rch, head_server_config, node_ip, head_server_port -- because Ray
recreates a restarted actor through __init__ only and this actor runs with
max_restarts=-1. Declaring it in _spinup would also let a re-run of _spinup wipe
an installed tokenizer and then report "set_tokenizer must be called before
run_rollouts", an error naming a call that had in fact been made.

run_rollouts fails loudly rather than silently if it is somehow reached without
one; postprocessing with no tokenizer is worse than a named error.

Test fakes that stood in for the actor method lose the argument with it. The
SingleController setup test asserts the tokenizer reaches spinup, which is the
property that keeps it off the per-call path. The guard test constructs through
__init__ rather than object.__new__, so the None it checks comes from the
declaration itself rather than from an assignment by hand. The distillation test
asserts that the spinup wait still comes first and that the install follows it
with the right tokenizer: that call site is the only guard on its path, and
checking the call count alone would pass just as well if the two swapped places
or if a processor were forwarded instead.

Signed-off-by: Saurabh Mishra <[email protected]>
@saumishr
saumishr force-pushed the fix/gym-hold-tokenizer branch from e483970 to aca1415 Compare August 18, 2026 18:43
@saumishr

Copy link
Copy Markdown
Contributor Author

/ok to test aca1415

@terrykong
terrykong enabled auto-merge (squash) August 18, 2026 21:57
@terrykong
terrykong merged commit 5197ed7 into NVIDIA-NeMo:main Aug 19, 2026
255 of 259 checks passed
rrs45 added a commit that referenced this pull request Oct 29, 2026
NemoGym.__init__ is also constructed without the nemo_gym extra -- main's
test_run_rollouts_requires_an_installed_tokenizer (added in #3668) does exactly
that -- so the unconditional 'import nemo_gym.telemetry' added for early
span-group registration raised ModuleNotFoundError in the no-extra unit shard.

Guard it: the early import only avoids a cosmetic lens warning (_spinup
re-imports via nemo_gym.cli and lens re-resolves on registration), while a
genuine missing dependency inside nemo_gym.telemetry still propagates.

Signed-off-by: Raj Singh <[email protected]>
rrs45 added a commit that referenced this pull request Oct 30, 2026
NemoGym.__init__ is also constructed without the nemo_gym extra -- main's
test_run_rollouts_requires_an_installed_tokenizer (added in #3668) does exactly
that -- so the unconditional 'import nemo_gym.telemetry' added for early
span-group registration raised ModuleNotFoundError in the no-extra unit shard.

Guard it: the early import only avoids a cosmetic lens warning (_spinup
re-imports via nemo_gym.cli and lens re-resolves on registration), while a
genuine missing dependency inside nemo_gym.telemetry still propagates.

Signed-off-by: Raj Singh <[email protected]>
rrs45 added a commit that referenced this pull request Nov 3, 2026
NemoGym.__init__ is also constructed without the nemo_gym extra -- main's
test_run_rollouts_requires_an_installed_tokenizer (added in #3668) does exactly
that -- so the unconditional 'import nemo_gym.telemetry' added for early
span-group registration raised ModuleNotFoundError in the no-extra unit shard.

Guard it: the early import only avoids a cosmetic lens warning (_spinup
re-imports via nemo_gym.cli and lens re-resolves on registration), while a
genuine missing dependency inside nemo_gym.telemetry still propagates.

Signed-off-by: Raj Singh <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:L1 Run doctests, unit tests, and functional tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants