Repository navigation
fix(rollout): release finished rollout tasks during collection - #4644
Merged
Merged
Conversation
When no resident task bound is set, the rollout dispatcher created every rollout task up front in a list and handed that list to tqdm's as_completed. The list stayed alive in the generator frame for the whole run, and a finished asyncio task keeps its result. Every parsed /run response therefore stayed in memory until the last rollout finished, even with retain_results_in_memory=false. Pass a generator instead. asyncio.as_completed drains it in order before building its set, so tasks are still created in input order, and nothing in the dispatcher keeps a finished task once the consumer has taken its result. The bounded admission path already released finished tasks. This affects both gym eval run and run_examples, which NeMo RL calls. Signed-off-by: Ananth Subramaniam <[email protected]>
yuhezhang-ai
approved these changes
Oct 15, 2026
yuhezhang-ai
left a comment
Contributor
There was a problem hiding this comment.
LGTM. The generator removes unintended retention of consumed rollout results while preserving task start order. Verified all 461 rollout-collection tests pass, and the new release tests fail on the original implementation as expected.
Contributor
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/38791736498 |
khazic
pushed a commit
to khazic/Gym-lao
that referenced
this pull request
Oct 17, 2026
…ht (NVIDIA-NeMo#4029) `run_examples()` now exposes `aclose()` so direct callers and collection owners can cancel unfinished client work. `aclose_and_drain()` stops admission before suspending and preserves completed, unconsumed outcomes for deadline consumers such as [RL NVIDIA-NeMo#4482](NVIDIA-NeMo/RL#4482). Completed failures remain observable; consumed payloads are released; outcomes are delivered once. Concurrent eager consumers now use one queue notification per finished rollout. Drain wakes every parked consumer. A deterministic public-API regression at 1,024 individually arriving outcomes measures 1,024 consumer resumptions, versus 524,800 with the shared event. Tests also cover cancelled notified consumers, multiple parked drain consumers, completed backlogs, exception retrieval, and result lifetime. Both lifecycle methods wait for local client tasks. They do not acknowledge server/agent cleanup or guarantee sandbox release. Existing sequential use remains compatible; no configuration or benchmark-score behavior changes. Main `c2000f79f6bea21e9b21f7f021d57a06f6082974` is integrated additively. The remaining test import conflict is resolved by retaining `signal`, `sys`, and `time`; NVIDIA-NeMo#4646's asynchronous artifact reads are preserved. Collector cleanup retains main's timing tasks, SIGTERM restoration, and report, while closing both iterator modes. Its capture-write barrier and reward telemetry are preserved. The queue-based iterator also preserves NVIDIA-NeMo#4644's consumed-result release; all four new upstream weak-reference cases pass unchanged. Validation on head `f28029f5b359858b09596c45a476fc1dd5b52596`: ```bash python3 -m pytest -o addopts= -q \ tests/unit_tests/test_rollout_collection.py \ tests/unit_tests/test_server_utils.py \ tests/unit_tests/telemetry/test_observer.py \ tests/unit_tests/test_run_timing.py \ tests/unit_tests/test_base_responses_api_model.py \ tests/unit_tests/telemetry/test_rollout_reward.py \ tests/unit_tests/test_rollout_health.py \ tests/unit_tests/test_rollout_observability.py \ tests/unit_tests/test_rollout_correlation.py \ tests/unit_tests/test_token_id_capture.py \ tests/unit_tests/test_trajectory_builder.py # 1,244 passed (including all 498 rollout tests) pre-commit run --all-files # All applicable hooks passed; no files modified. The guardwords hook retains its catalog-availability guard. ``` RL NVIDIA-NeMo#4482 at `778a9d7ee3c8811174745137519748a6b9443271`: all 17 native real-Gym interoperability cases passed again against Gym head `f28029f5b359858b09596c45a476fc1dd5b52596` after the main integration. The original three performance regressions failed before the queue fix and passed afterward. Disabling drain shutdown also makes the new parked-consumer regression fail. Real HTTP client cancellation remains covered. Model rollout evidence: N/A for this shared-library lifecycle repair. No training A/B was run. A separate issue is unnecessary because this addresses the [P2 feedback on this PR](NVIDIA-NeMo#4029 (comment)). [NVIDIA-NeMo#3302](NVIDIA-NeMo#3302) should follow this change and retain both lifecycle methods. RL activation needs a subsequent Gym pin update. Ananth's concrete review requests are addressed. Full mirrored CI and the repository's required approval remain outstanding. --------- Signed-off-by: Lin Jia <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Rollout collection kept every finished
/runresult in the driver's memory until the last rollout of the run finished, even withretain_results_in_memory=false.The cause is in
_run_examples_with_metadatainnemo_gym/rollout_collection.py, the dispatcher shared bygym eval run(throughrun_from_config) and byrun_examples, which NeMo RL calls. Whenmax_resident_tasksis unset, its inner generator_start_in_input_ordercreated every rollout task up front in a list and passed that list totqdm.as_completed. The list stayed alive in the generator frame for the whole run, andtqdm.as_completedalso kept it as itsfsargument. A finished asyncio task holds its result, so the consumer dropping a result did not free it.The fix passes a generator of tasks instead of a list.
_start_in_input_orderwas added in #3822 becauseasyncio.as_completedbuilds its tasks from a set, which would start rollouts in an arbitrary order. That property still holds:asyncio.as_completediterates the generator in order (set(aws)) before building its set, soasyncio.ensure_futurestill creates the tasks in input order. After that, onlyasyncio.as_completed's own bookkeeping references a task, and it lets go of a task once it hands the result to the consumer. The progress bar keeps its explicittotal, so it is unchanged.The bounded admission path (
_BoundedCompletionIterator, used whenmax_resident_tasksis set) already released finished tasks: it pops each task from its ready list before awaiting it, so it needed no change.NeMo RL's
run_examplespath benefits too, since it goes through the same dispatcher.No issue was filed; the bug and the fix are both contained in this PR.
Validation
test_dispatch_releases_a_taken_result_while_rollouts_are_pending, runs bothrun_examplesand_run_examples_with_metadata, with and withoutmax_resident_tasks. The first rollout finishes while the others are held pending. The test takes and drops the first result, then asserts through a weak reference that the result was freed before the remaining rollouts finish. On the code before this change, the cases withoutmax_resident_tasksfail and the bounded cases pass.test_default_dispatch_starts_rollouts_in_input_orderstill passes, confirming that rollouts start in input order.PYTHONPATH=. .venv/bin/python -m pytest -n 16 -q tests/unit_tests --ignore=tests/unit_tests/test_exporters.pyin a venv built withuv sync --extra dev --extra telemetry. The only failures are environment-dependent ones intest_opensandbox_compose.py,test_reward_profile.py,test_dataset_orchestrator.py, andtest_sandbox_stop_retry.py, which fail identically onmainon the same machine.pre-commit run --all-files.Rollouts
N/A: the change only affects when the driver releases results it has already handed to the consumer. It does not change which rollouts run, in what order they start, or any agent, model, or verifier decision.
Memory evidence comes from a CPU capacity simulator with the production topology (simulated engine and tools, real Gym servers and driver). Each run collected 4,096 long-context rollouts (64 repeats, 80 history messages of 2,000 characters each) with 1,024 in flight,
++retain_results_in_memory=false, and rollout observability on. The driver'sprocess.memory.usagecame from console metric exports every 10 seconds. The table shows the highest reading taken while rollouts were still completing, with the number of completed rollouts at that sample.main(974d1a9)main(974d1a9)On
main, driver memory grew steadily with the number of completed rollouts. With this change it stayed between 0.5 and 0.8 GB for the whole collection. Both variants still jump to about 5.6 GB after the last rollout, because the no-retention mode reads the output file back into memory for aggregation. That is a separate issue and is not addressed here.Compatibility and benchmark impact
None. Results, their order of delivery, and the progress bar are unchanged.
Checklist
retain_results_in_memorydescription now holds.pre-commit run --all-files) (so CI lint/format checks pass).git commit -s) (so the DCO check passes).