Skip to content

fix(nemo-gym): keep token capture ledgers on node-local disk - #5249

Merged
terrykong merged 8 commits into
mainfrom
ananthsub/node-local-token-capture
Dec 4, 2026
Merged

terrykong merged 8 commits into
mainfrom
ananthsub/node-local-token-capture

Conversation

@ananthsub

@ananthsub ananthsub commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor
  • I, the PR author, have personally reviewed every line of this PR.

What and why

Gym's capture ledgers turn every model call into small-file metadata operations, and with token_capture.capture_dir unset, setup put them under <logger.log_dir>/gym_token_capture. Recipes worked around this one by one by setting capture_dir to a node-local path.

Moves the default token capture directory from the run's log dir, which is usually on a shared filesystem, to a fresh per-run directory on each Gym node's local disk, and removes that directory when the Gym actor shuts down.

Why a node-local directory is safe

Setup resolves token_capture.capture_dir once, and build_nemo_gym_actors gives each Gym actor its own subdirectory, <capture_dir>/gym-instances/<shard>/replica-<n>. The actor hands that subdirectory to Gym as its token store and lineage store. Three facts make a node-local directory safe:

  • Only one node reads each subdirectory. Gym's RunHelper starts every server of an actor as a local subprocess, so the processes that open the directory, the policy model server and its workers and the actor's own rollout collection, all run on the actor's node. Each Gym actor is its own Gym deployment, and the actor is never restarted elsewhere (max_restarts=0). The default STRICT_SPREAD placement puts shards on distinct nodes, and actors that do share a node still have separate subdirectories.
  • NeMo RL never opens ledger files. It fetches manifests over Gym's control routes from the actor that ran the rollout, and staged token rows travel through TransferQueue.
  • Resume does not need the live ledgers. A Gym checkpoint commit calls export_model_records, which copies the ledger rows of each continued episode into <checkpoint_root>/gym-instances/<instance_id> on durable storage. A restore imports those rows into the new actor's live directory and retries every unsealed attempt under a fresh rollout ID. token_capture.capture_dir was already excluded from the bootstrap fingerprint, so a restart may use a different directory.

#4247 reads ledgers directly from the NemoGym actor and requires the actor and the policy model server to share the directory on one node, which a node-local default provides.

What changed

  • Default directory. With capture_dir unset, setup uses /tmp/nemo_rl-<user>/token_capture/<SLURM_JOB_ID or "local">-<8 hex chars>. Gym creates it on each node that hosts a Gym actor. The parent directory is per user because it outlives the run: a shared parent created by one user would refuse another user's run directory. Without a user name, for example in a container running as an arbitrary uid, the parent uses the uid instead. The random suffix keeps two runs on one node apart, even within one Slurm job, because Gym rollout IDs are deterministic and two runs sharing a directory could mix ledgers. The path does not depend on TMPDIR.
  • Cleanup on shutdown. A new user setting, token_capture.capture_dir_cleanup (default true), controls it. NemoGym.shutdown removes the actor's subdirectory after its servers stop and telemetry is flushed, and the last actor on a node also removes the empty layers above it up to capture_dir. Without this, each node's disk would keep every run's ledgers. This applies whether setup or the user chose capture_dir; set capture_dir_cleanup: false to keep the ledgers. A second shutdown, or one before Gym created the directory, does nothing.
  • Fingerprint. token_capture.capture_dir_cleanup joins token_capture.capture_dir in the bootstrap fingerprint exclusions, so a restart that changes the directory or the cleanup setting still restores.

Compatibility

Runs that set token_capture.capture_dir, such as recipes that use /tmp/nemo_rl/token_capture/${SLURM_JOB_ID}, keep their directory, but each Gym actor now deletes its own ledgers there on a clean shutdown. Runs that relied on the default no longer find ledgers under <log_dir>/gym_token_capture, and the default directory is gone after a clean shutdown. To keep ledgers for debugging, set token_capture.capture_dir_cleanup: false. Where /tmp is a RAM-backed filesystem, ledgers use memory, and until #4798 retires ledgers during the run, that use grows with the run.

Not covered here

  • Actors that die without shutting down. An actor killed before NemoGym.shutdown runs leaves its subdirectory on that node.
  • Shutdown that runs out of time. At the end of a run each Gym actor gets DEFAULT_ENV_SHUTDOWN_TIMEOUT_SECONDS to stop its servers, flush telemetry and remove its directory before ray.kill. If that runs out, the directory is left in place or partly removed. Ledgers are not retired during a run until feat(nemo-gym): retire capture ledgers NeMo RL is done with #4798, so the removal time grows with the run, and a long run can exceed this budget. The removal runs after the telemetry flush, so running out of time costs only the removal.
  • Interaction with feat(nemo-gym): retire capture ledgers NeMo RL is done with #4798. That PR's functional check reads the lineage directory after the run under <log_dir>/gym_token_capture. Whichever PR lands second needs to point that check at an explicitly configured capture_dir and set capture_dir_cleanup: false, or the ledgers are gone before the check reads them.

Issues

No tracking issue.

Convergence / perf / functionality proof

This PR changes where capture ledgers are written and when they are deleted. It does not change training math, so there are no convergence or perf runs; the proof is functional.

  • New tests check that setup derives a fresh node-local directory per run under Slurm and outside it, that a configured capture_dir is kept, that build_nemo_gym_actors passes the run's root to the actor, that the default directory is under a per-user parent, or a per-uid one when there is no user name, that shutdown removes one actor's ledgers while a sibling actor's stay and removes the run's directory after the last actor, that a second shutdown reports no failure, that shutdown leaves the directory alone when capture_dir_cleanup is false, and that capture_dir_cleanup does not change the bootstrap fingerprint. Each of these fails on the previous code.
  • Ran tests/unit/environments/test_nemo_gym_shutdown.py, test_nemo_gym_utils.py (with and without --nemo-gym-only), test_nemo_gym.py, test_nemo_gym_token_capture.py, test_gym_checkpoint_adapter.py, test_gym_environment_checkpoint.py, test_nemo_gym_shards.py, tests/unit/single_controller/test_setup.py and test_rollout_checkpoint.py. One video-defaults test in test_nemo_gym.py fails locally on main too, because of a Hugging Face cache permission error on the workstation.
  • pre-commit on the changed files, and pyrefly check with the project interpreter.
  • Not yet run: a real job with token capture on and capture_dir unset, checking that the run's directory is gone from the Gym node after a clean exit.
  • After the switch to capture_dir_cleanup and the merge of main: the same unit test files, plus the L0_Unit_Tests_Environments and L0_Unit_Tests_Vllm_Nemo_Gym shards, pass on a GPU node. The L0_Unit_Tests_Nemo_Gym shard was run against a test image built before main's latest Gym bump; 7 tests that depend on the newer Gym failed there and the rest passed, so CI is the check for that shard.

Needs upstream changes

None

PR commands and CI
  • CI label, exactly one:
    • CI:Lfast: the default for most PRs. Runs a fast test subset.
    • CI:L1: dependency or container changes (pyproject.toml, uv.lock, Dockerfile) or broad core changes. Builds a new image and runs unit and functional tests.
    • CI:docs: docs-only changes. Runs doc tests only.
  • /ok to test <full-commit-sha>: start CI for that commit. Post again after each push.
  • /nightly-ci test-case=<test>: run NVIDIA's nightly CI for the PR head; eligible teams only.
  • /review: automated review comments. Add mode=strict for a stricter review, reset=true for a fresh full review.
  • /resolve-conflict: Autobot merges main into this PR and resolves the conflicts. PRs from forks must allow edits by maintainers.
  • Locally: /review-pr-team <pr-number> for a team review before you ask for human review.
  • Contributor guidelines

With token capture enabled and no capture_dir set, setup put every Gym
ledger under <logger.log_dir>/gym_token_capture. The log dir is usually on
a shared filesystem, so each model call became small-file metadata traffic
on it.

Setup now derives a fresh directory under /tmp/nemo_rl/token_capture,
named after the Slurm job ID (or "local") plus a random suffix. Each Gym
shard runs all of its servers on one node, nothing in NeMo RL reads ledger
files directly, and a checkpoint commit copies the ledger rows it needs
into the checkpoint directory, so no reader needs the ledgers from another
node or after the run.

Because a node-local directory would otherwise keep every run's ledgers,
each Gym actor removes its subdirectory on shutdown when setup chose the
directory, and the last actor on a node removes the run's directory. A
configured capture_dir is never removed. capture_dir_derived joins
capture_dir in the bootstrap fingerprint exclusions.

Signed-off-by: Ananth Subramaniam <[email protected]>
@copy-pr-bot

copy-pr-bot Bot commented Nov 16, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

The shared /tmp/nemo_rl/token_capture parent outlives the run, so a
second user on the same node could not create a run directory in it.
The derived default is now /tmp/nemo_rl-<user>/token_capture/....

Shutdown skips cleanup when the actor's capture directory does not
exist, so a second shutdown, or one before Gym created the directory,
no longer reports a removal failure.

Comments now say that each Gym actor, not each shard, runs its servers
on one node, and name the case the getattr guard in shutdown covers.

Signed-off-by: Ananth Subramaniam <[email protected]>
getpass.getuser() raises OSError in a container running as a uid with no
user name, which would fail setup. The capture root then uses the uid.

Signed-off-by: Ananth Subramaniam <[email protected]>
Resolve conflicts with the Dynamo capture backend (#4979): keep
capture_dir_derived next to the three-way generation_backend literal, and
keep the dynamo_enabled backend derivation in setup.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Signed-off-by: Pranav Thombre <[email protected]>
@copy-pr-bot

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

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

Copy link
Copy Markdown
Contributor

/ok to test 5b6f29b

@pthombre pthombre added CI:L1 Run doctests, unit tests, and functional tests and removed CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) labels Nov 20, 2026
@pthombre
pthombre marked this pull request as ready for review November 20, 2026 01:59
… set, flush telemetry before removing ledgers

Setup now assigns token_capture.capture_dir_derived on every path, so a
value carried in by a reused config dump cannot mark a user-configured
capture_dir for removal on shutdown.

NemoGym.shutdown removes the derived capture directory after the
telemetry flush instead of before it. Removing a long run's ledgers can
outlast the shutdown timeout, and the actor is then killed; with this
order that costs only the removal, not the flush.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Signed-off-by: Pranav Thombre <[email protected]>
@pthombre

Copy link
Copy Markdown
Contributor

/ok to test 89a0f59

@terrykong terrykong left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

One design ask:

  1. (C1) Replace the hidden capture_dir_derived flag with a user setting, capture_dir_cleanup: bool = True.

Moving the default capture directory off the shared log dir and onto each Gym node's local disk is a good change, and the safety argument in the description matches the code:

  • Only the Gym actor's own servers open the ledger files, and Gym starts them as local subprocesses on that actor's node (server_supervisor.py).
  • Resume reads only the rows a checkpoint copied (export_model_records), never the live folder.
  • Shutdown deletes only the actor's own subfolder and any empty parents up to the run's folder, and leaves a folder the user set alone (_remove_derived_capture_dir).
  • The new tests guard each change: removing the cleanup call, skipping the parent cleanup, not resetting capture_dir_derived, not passing capture_root, or dropping the resume-check exclusion each makes a test fail.

Thanks for the clear write-up of what is and isn't covered.

Visual explainer: https://terrykong.github.io/gh-pages-poc/terryk/pr-5249-capture-dir-cleanup.html

nemo-rl-review: kind=expert harness=claude-code/2.1.332 model=claude-opus-5-5 effort=medium review-pr-team=v3 reviewed=89a0f59

…up setting

Review feedback on #5249: capture_dir_derived only recorded who picked
the capture directory, so a user who set capture_dir lost cleanup and
nobody could turn cleanup off.

token_capture.capture_dir_cleanup (default true) is a user setting. Each
Gym actor removes its own ledger directory on shutdown, then the empty
layers above it up to capture_dir, whether setup or the user picked the
path. Set it to false to keep the ledgers.

Setup no longer resets a flag on every run, and the actor always gets
the run's root.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Signed-off-by: Pranav Thombre <[email protected]>
@pthombre

pthombre commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

/ok to test 77c613f2c687100ba7e06b0bc32b597e422d8832

@copy-pr-bot

copy-pr-bot Bot commented Dec 4, 2026

Copy link
Copy Markdown

/ok to test 77c613f2c687100ba7e06b0bc32b597e422d8832

@pthombre, there was an error processing your request: E2

See the following link for more information: https://docs.gha-runners.nvidia.com/cpr/e/2/

@github-actions github-actions Bot added CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) and removed CI:L1 Run doctests, unit tests, and functional tests labels Dec 4, 2026
@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

CI label: CI:Lfast, set automatically (default for code and main CI changes).

  • Removed CI:L1 added by pthombre: only repo maintainers can change the CI label or skip CI.
    If this label is wrong, ask a maintainer to set the right one.

@pthombre

pthombre commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

/ok to test 77c613f

@svcnvidia-nemo-ci svcnvidia-nemo-ci added complexity:medium Moderate change. Set by Autobot PR classify (skills/pr-classify) convergence-risk:low Unlikely to change training results. Set by Autobot PR classify (skills/pr-classify) proof:functionality Needs proof that it works. Set by Autobot PR classify (skills/pr-classify) proof:perf Needs performance proof. Set by Autobot PR classify (skills/pr-classify) labels Dec 4, 2026

@terrykong terrykong left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

C2 · 1 action item. TL;DR — the code for C1 landed, but the PR description still says a configured capture_dir is never removed. That is now wrong: a run that sets capture_dir loses its ledgers at shutdown unless it sets capture_dir_cleanup: false.

This is a docs fix, not a code bug.

Fixed in 77c613f: capture_dir_derived is gone and capture_dir_cleanup: bool = True is the user setting (config.py L743-L747); shutdown reads it (nemo_gym.py L1038); every actor gets the run's root (nemo_gym.py L3271-L3273); the fingerprint exclusion is renamed (manifest.py L81); and a test covers capture_dir_cleanup: false (test_nemo_gym_shutdown.py L429).

Still open: five spots in the PR description still describe the old flag:

  • What changed → Cleanup on shutdown explains capture_dir_derived and says "A capture_dir the user configured is never removed."
  • What changed → Fingerprint names capture_dir_derived.
  • Compatibility says runs that set capture_dir "keep their directory and their files", and "To keep ledgers for debugging, set capture_dir explicitly."
  • Not covered here → #4798 says to point that check at an explicitly configured capture_dir. That check now also needs capture_dir_cleanup: false, or the ledgers are gone before it reads them.
  • Validation lists tests for the old flag ("not marked derived", "passes the run's root to the actor only for a derived directory", "capture_dir_derived does not change the bootstrap fingerprint").

AI-1

Rewrite those five spots for capture_dir_cleanup. For Compatibility, for example: "Runs that set token_capture.capture_dir keep their directory, but each Gym actor now deletes its own ledgers there on a clean shutdown. To keep ledgers for debugging, set token_capture.capture_dir_cleanup: false." Not a suggestion block: this is the PR description, not code.

After merging main, NemoGym reads token_capture["forest"]["enabled"]
when it is built, so the capture dicts in test_nemo_gym_shutdown.py
raised KeyError: 'forest'.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Signed-off-by: Pranav Thombre <[email protected]>
@pthombre

pthombre commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

/ok to test fc821d2

@terrykong
terrykong enabled auto-merge (squash) December 4, 2026 08:21
@terrykong
terrykong merged commit d6c70ef into main Dec 4, 2026
166 of 177 checks passed
@terrykong
terrykong deleted the ananthsub/node-local-token-capture branch December 4, 2026 09:28
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) complexity:medium Moderate change. Set by Autobot PR classify (skills/pr-classify) convergence-risk:low Unlikely to change training results. Set by Autobot PR classify (skills/pr-classify) proof:functionality Needs proof that it works. Set by Autobot PR classify (skills/pr-classify) proof:perf Needs performance proof. Set by Autobot PR classify (skills/pr-classify)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants