Repository navigation
[RLlib] IMPALA/APPO: Drop the per-update torch.distributed barriers in the learner thread - #67300
Merged
pseudo-rnd-thoughts merged 2 commits intoOct 26, 2026
Conversation
…n the learner thread The _LearnerThread called torch.distributed.barrier() before and after every aggregation, i.e. after every learner update with the default broadcast_interval=1. The state fetch only reads the local Learner's parameters, and DDP's gradient all-reduce already keeps the Learners in lockstep, so the barriers only added synchronization. With GPU Learners each barrier is an NCCL all-reduce that leaks ~160 bytes of host memory (NCCL 2.27.5, torch 2.10), and Ray Train initializes a process group even for num_learners=1, so the Learner's memory grew by ~0.35 KB per update. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Artur Niederfahrenhorst <[email protected]>
Contributor
There was a problem hiding this comment.
Code Review
This pull request removes unnecessary torch.distributed.barrier() calls from the IMPALA learner thread to prevent host memory leaks caused by NCCL all-reduce operations during synchronization, and adds a unit test to verify this behavior. The review feedback suggests explicitly importing torch.distributed to avoid potential attribute errors during testing, and using self.addCleanup to guarantee that the learner group is properly shut down even if test assertions fail.
…ails Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Artur Niederfahrenhorst <[email protected]>
pseudo-rnd-thoughts
approved these changes
Oct 26, 2026
justinyeh1995
pushed a commit
to justinyeh1995/ray
that referenced
this pull request
Oct 28, 2026
…n the learner thread (ray-project#67300) Before this PR: `_LearnerThread.step()` called `torch.distributed.barrier()` before and after every aggregation, i.e. after every learner update with the default `broadcast_interval=1`. Ray Train initializes a process group even for `num_learners=1`, so with a GPU Learner each barrier is an NCCL all-reduce, and each one leaks ~160 bytes of host memory (NCCL 2.27.5, torch 2.10). The Learner's memory grew by ~0.35 KB per update, ~20 MiB/h at 17 updates/s. Introduced by ray-project#59544 (Ray 2.54). After this PR: The learner thread no longer calls barriers. Fetching the module state only reads the local Learner's parameters, and DDP's gradient all-reduce already keeps the Learners in lockstep. **Tests** - New `test_learner_thread_does_not_call_barrier_per_update` (IMPALA, stubbed gradient update and `torch.distributed`): fails on master (barrier called 10 times for 5 updates), passes with this PR. - `python -m pytest python/ray/rllib/algorithms/impala/tests/test_impala.py`: 4 passed. Run against the Ray 2.58.0 wheel with this PR's `impala_learner.py`, which matches master's apart from docstrings. - Multi-agent APPO on 1x L4 (30 EnvRunners, 90 minute runs on Anyscale, all with the results-queue fix from ray-project#67299): Learner memory grew 592 to 626 bytes per update in 7 runs with the barriers, and 236 to 289 bytes per update in 4 runs without. memray attributes the difference to `ncclAllReduce` called from `barrier()`. The remaining growth is a Ray Core bug, ray-project#67297. - 2-Learner APPO (gloo, local): the Learners stay in lockstep without the barriers. No existing issue or PR covers this; I searched open issues and PRs for the learner thread's barriers. Found while investigating a user report of APPO Learner memory growth. AI assistance (Claude Code) was used for the investigation, the change and the tests. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: Artur Niederfahrenhorst <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
elliot-barn
pushed a commit
that referenced
this pull request
Nov 17, 2026
Before this PR: - No release test checks RLlib's process memory over a training run. Leaks in the Learner, AggregatorActors or EnvRunners only showed up in long customer jobs (e.g. the per-update leaks fixed in #67299 and #67300). - No RLlib release test runs Learners on separate nodes. After this PR: - New weekly release test `rllib_footsies_appo_memory_multi_node`: APPO on Footsies in self-play, so there is no reward target and the module set never changes. It runs on a 4-node cluster: a CPU head, 2 single-GPU nodes with one Learner and its 2 AggregatorActors each, and a CPU node, for 24 EnvRunners in total. - Every iteration, a probe actor on each node reads the USS of every Learner, AggregatorActor and EnvRunner process. After a 3-minute warmup, the test fits each process's USS over the remaining 7 minutes and fails if any slope exceeds its category's limit (Learners 50, AggregatorActors 100, EnvRunners 50 MiB/h). The max and median slope per category, and the sample and train throughput, are reported as perf metrics. - The Footsies env takes its type aliases from `ray.rllib.utils.typing` instead of pettingzoo, so the test runs on the bare ray-torch cu133 image. Calibration: 3 runs of the script on the L4 equivalent of the new compute config (2× g6.4xlarge + 2× m5.4xlarge), 43 readings over a 7-minute window each. Slopes per process, in MiB/h: | | Learners (6) | AggregatorActors (12) | EnvRunners (72) | |---|---|---|---| | range | 1.6 to 4.0 | 1.1 to 26.0 (all others ≤1.5) | −7.9 to 13.8 | | limit | 50 | 100 | 50 | Gate check (CPU, 8 EnvRunners): a control run passes (EnvRunners ≤19 MiB/h). The same run with each EnvRunner keeping 512 KiB per `sample()` call fails, flagging all 8 EnvRunners at +410 to +417 MiB/h. That's the injected rate. Note: until #67594 lands, about 1 in 300 game servers never gets ready at startup, and the launcher waits for it forever. In this test that shows up as a timeout; that PR bounds the wait and relaunches the server. Not a duplicate: no open PR adds an RLlib memory or multi-node release test. Testing: ran the script on Anyscale (nightly image) like the release harness does (`TEST_OUTPUT_JSON` set, all 4 nodes up first): the 3 calibration runs above, plus 2 runs that showed Tune needs shared storage on multi-node clusters (now `/mnt/cluster_storage`), plus the CPU gate check. The release-test entry validates against `release/ray_release/schema.json`, and `pre-commit run --files` passes. AI assistance (Claude Code) was used for the investigation, the experiments and the change. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: Artur Niederfahrenhorst <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
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.
Before this PR:
_LearnerThread.step()calledtorch.distributed.barrier()before and after every aggregation, i.e. after every learner update with the defaultbroadcast_interval=1. Ray Train initializes a process group even fornum_learners=1, so with a GPU Learner each barrier is an NCCL all-reduce, and each one leaks ~160 bytes of host memory (NCCL 2.27.5, torch 2.10). The Learner's memory grew by ~0.35 KB per update, ~20 MiB/h at 17 updates/s. Introduced by #59544 (Ray 2.54).After this PR: The learner thread no longer calls barriers. Fetching the module state only reads the local Learner's parameters, and DDP's gradient all-reduce already keeps the Learners in lockstep.
Tests
test_learner_thread_does_not_call_barrier_per_update(IMPALA, stubbed gradient update andtorch.distributed): fails on master (barrier called 10 times for 5 updates), passes with this PR.python -m pytest python/ray/rllib/algorithms/impala/tests/test_impala.py: 4 passed. Run against the Ray 2.58.0 wheel with this PR'simpala_learner.py, which matches master's apart from docstrings.ncclAllReducecalled frombarrier(). The remaining growth is a Ray Core bug, [Core] Owner leaks a memory-store entry when a borrower drops the last reference #67297.No existing issue or PR covers this; I searched open issues and PRs for the learner thread's barriers. Found while investigating a user report of APPO Learner memory growth. AI assistance (Claude Code) was used for the investigation, the change and the tests.
🤖 Generated with Claude Code