Skip to content

feat(nemo-gym): retire capture ledgers NeMo RL is done with - #4798

Open
ananthsub wants to merge 4 commits into
NVIDIA-NeMo:mainfrom
ananthsub:ananthsub/retire-capture-ledgers
Open

ananthsub wants to merge 4 commits into
NVIDIA-NeMo:mainfrom
ananthsub:ananthsub/retire-capture-ledgers

Conversation

@ananthsub

@ananthsub ananthsub commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do ?

Retires each token-capture ledger once NeMo RL has finished with its rollout, so the policy model server's capture directory no longer grows by one ledger file per rollout attempt for the whole run.

Issues

No tracking issue. This is the NeMo RL side of NVIDIA-NeMo/Gym#3938 and NVIDIA-NeMo/Gym#3939, which add the retire and delete operations that NeMo RL now calls.

Why the ledgers need retiring

With token_capture.enabled=true, Gym's policy model server records a capture ledger for every rollout attempt under token_capture.capture_dir, which defaults to <logger.log_dir>/gym_token_capture on shared storage. NeMo RL reads a ledger once, when it fetches the rollout's manifest in NemoGym._postprocess_receipt_mode, and never reads it again: the receipt carries the manifest from then on, and a restore retries every unsealed attempt under a fresh rollout ID. Gym cannot tell when that moment has passed, so before this change nothing removed the ledger and the directory kept one file per attempt for the whole run.

Retiring a rollout deletes its ledger and leaves an empty fence file in its place. If a model call for the rollout finishes later, for example from an abandoned agent that is still running, Gym discards its row instead of starting a new ledger.

What changed

  • Gym version. No submodule bump is needed: the Gym commit already on main includes feat(token-capture): retire and delete external-staging capture ledgers Gym#3938 and feat(token-capture): retire and delete complete-record captures Gym#3939.
  • When ledgers are retired. AsyncNemoGymRolloutImpl._run_rollouts queues every row of a prompt group for retirement once its row re-dispatch loop is over, whether the dispatch succeeded, failed, timed out, or was cancelled. Until then a row can be re-sent under the same rollout ID, for example when receiving its result fails with a retryable Ray error, and retiring that ID earlier would make Gym discard the re-sent row's capture. Afterwards nothing reads those manifests again: a returned row's receipt carries its manifest, and a retry of the group runs under fresh rollout IDs. The retire goes to the actor that ran the rows, whose model server holds their ledgers, as one Ray call per prompt group.
  • Failure handling. The call is fire-and-forget and catches RayError, so it never delays a cancellation or replaces the exception that ended the dispatch. Rows without a _ng_rollout_id (token capture off) are never sent.
  • Batching. NemoGym.retire_rollout_ledgers only adds IDs to a per-actor queue. A background task sends the queue to POST /training-token-capture/control/rollouts/retire once 256 rollouts are queued or after one second, whichever comes first, with up to Gym's MAX_LEDGER_BATCH IDs per request. Requests go over Gym's reserved control connection pool, so cleanup does not compete with model calls for connections.
  • Retries. A rollout leaves the queue only after Gym confirms its batch. Timeouts, transport errors, HTTP 409 (Gym refuses removal while a checkpoint is being taken), and 5xx responses keep the batch queued, and the task retries with exponential backoff up to 30 seconds. HTTP 400, 401, and 422 cannot succeed on retry, so the batch is logged and dropped. Retire is idempotent, so resending a batch whose response was lost is safe.
  • Shutdown. NemoGym.shutdown logs how many queued ledgers were never retired and cancels the flush task.
  • Functional check. tests/functional/grpo_async_gym_single_controller.sh with ++token_capture.enabled=true now fails if the run leaves no retired fences, or leaves at least as many live ledgers as retired ones. Only rollouts in flight at shutdown should keep a live ledger.

Not covered here

  • Attempts abandoned by a restart. RolloutRecoveryLedger.prepare_for_restart abandons interrupted attempts from the previous launch. Their ledgers are still on disk when the capture directory is shared, and this change does not retire them.
  • Deleting fences. Each retired rollout leaves one empty fence file. feat(token-capture): retire and delete external-staging capture ledgers Gym#3938 describes why there is no safe point to delete fences during a run yet, so this change never calls delete.
  • Node-local capture directories. Finished and abandoned rollouts are retired on the actor that ran them, which stays correct if capture_dir moves to node-local disk. Moving it is a separate change.
  • Lock files. Gym still leaves an empty <rollout_id>.tokens.lock file per rollout in the lineage directory, which delete cannot safely remove. fix(token-capture): use striped lock files that are never deleted Gym#3937 replaces them with a fixed set of shared lock files.
  • Interaction with feat(token-capture): read Gym manifests directly from local ledgers #4247. That PR adds a local_file manifest transport. Whichever PR lands second needs to make sure the retire still happens on that path.

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? Unit tests and grpo_async_gym_single_controller.sh ++token_capture.enabled=true pass in nvcr.io/nvidian/nemo-rl:nightly. After that run, the lineage directory holds retired fences for every finished rollout and live ledgers only for the one prompt group still in flight at shutdown.
  • Did you add or update any necessary documentation?

@copy-pr-bot

copy-pr-bot Bot commented Oct 19, 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.

@ananthsub
ananthsub force-pushed the ananthsub/retire-capture-ledgers branch from 2a5b1cd to fe34649 Compare October 19, 2026 23:22
@copy-pr-bot

copy-pr-bot Bot commented Oct 19, 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.

@ananthsub ananthsub added the CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) label Oct 19, 2026
@ananthsub
ananthsub force-pushed the ananthsub/retire-capture-ledgers branch from fe34649 to 2bf9f2b Compare October 19, 2026 23:43

@macandro96 macandro96 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for this. Retiring each ledger right after the manifest read, and leaving abandoned rows alone while row re-dispatch can still recover them, fits how the recovery ledger mints fresh rollout IDs on retry. The claims in the description about who reads a ledger and when all hold on main, and the Gym-side contract (route, auth, status classes, fence filenames) matches the pinned Gym.

Two inline items: a shutdown() regression that turns four existing default-lane unit tests red, and a missing test for the cancellation path the except BaseException handler exists for.

Generated by Claude Code

Comment thread nemo_rl/environments/nemo_gym.py Resolved
macandro96
macandro96 previously approved these changes Oct 20, 2026
@ananthsub
ananthsub force-pushed the ananthsub/retire-capture-ledgers branch from a3f3ee0 to 1b10ba4 Compare October 20, 2026 23:27
@ananthsub
ananthsub marked this pull request as ready for review October 20, 2026 23:35
@copy-pr-bot

copy-pr-bot Bot commented Oct 20, 2026

Copy link
Copy Markdown

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

Contributors can view more details about this message here.

@ananthsub

Copy link
Copy Markdown
Contributor Author

/ok to test 1b10ba4

@svcnvidia-nemo-ci svcnvidia-nemo-ci left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The batching and terminal-dispatch cleanup are well separated, but one receipt-delivery ordering issue needs correction.

The earlier shutdown and cancellation items are addressed in the current source: the queue fields are initialized before spinup, and the cancellation regression checks that cancellation propagates and only the unreturned row is retired.

Inspected all six changed files and traced the local receipt, retry, recovery, and shutdown paths. Tests and linters were not executed.

Blocked inspection: the remaining upstream-verification task cannot be completed in this invocation. Both mounted Gym submodule directories are empty; direct reads also confirmed that the endpoint and client implementation files are absent. With read-only tools and no network access, I cannot independently verify Gym's retire endpoint or reserved control-pool implementation. This task remains pending rather than being marked complete.

Note: formal approval is not available for this request, so this review is posted as a comment only.

Review generated by codex. This was a full review. Request a full review with /review reset=true.

Comment thread nemo_rl/environments/nemo_gym.py Resolved
@ananthsub

Copy link
Copy Markdown
Contributor Author

/ok to test 30cb571

@ananthsub
ananthsub requested a review from macandro96 October 21, 2026 13:20
@macandro96
macandro96 requested a review from terrykong October 22, 2026 20:00
With token capture on, the policy model server writes one capture ledger
per rollout attempt and never removes it, because Gym cannot tell when
NeMo RL has finished with a rollout. A long run therefore fills the
capture directory with one ledger per attempt.

Each NemoGym actor now queues a rollout for retirement once its
manifest has been read, and AsyncNemoGymRolloutImpl queues the rows of
a failed, timed-out, or cancelled dispatch that never returned a result,
on the actor that ran them. The actor sends the queue to Gym's retire
route in batches over the reserved control connection pool. A rollout
leaves the queue only once Gym confirms it; transport errors, timeouts,
409, and 5xx responses are retried with exponential backoff.

Signed-off-by: Ananth Subramaniam <[email protected]>
The logger nests each run under an exp_NNN directory inside
logger.log_dir, so the ledger check now searches LOG_DIR recursively
for the gym_token_capture lineage files.

Signed-off-by: Ananth Subramaniam <[email protected]>
shutdown() reads the retire queue, but _spinup created it, so shutting
down an actor that never spun up raised AttributeError and masked the
error that stopped spinup. The queue is now declared in __init__ next
to the other defaults an unspun actor needs.

Also cover a cancelled dispatch: a staleness abort cancels it with
CancelledError, which is not an Exception, and its unreturned rows must
still be retired.

Signed-off-by: Ananth Subramaniam <[email protected]>
The actor queued a rollout for retirement as soon as it read the
manifest, before the controller had received the result. If receiving
that result failed with a retryable error, the row was re-dispatched
under the same rollout ID, and once the retire flushed Gym discarded
the new run's capture, so the row came back masked instead of
recovered.

AsyncNemoGymRolloutImpl._run_rollouts now retires every row of the
prompt group once, after its row re-dispatch loop is over, whether the
dispatch succeeded, failed, or was cancelled. From then on nothing
reads those manifests: a returned row's receipt carries its manifest,
and a retry of the group runs under fresh rollout IDs. This is one
Ray call per prompt group instead of one per row.

Signed-off-by: Ananth Subramaniam <[email protected]>
@ananthsub
ananthsub force-pushed the ananthsub/retire-capture-ledgers branch from 30cb571 to acbf7ef Compare October 26, 2026 23:44
@ananthsub

Copy link
Copy Markdown
Contributor Author

/ok to test acbf7ef

@terrykong terrykong added CI:L1 Run doctests, unit tests, and functional tests CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) and removed CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) CI:L1 Run doctests, unit tests, and functional tests labels Nov 5, 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.

4 participants