Skip to content

[RLlib] IMPALA/APPO: Drop the per-update torch.distributed barriers in the learner thread - #67300

Merged
pseudo-rnd-thoughts merged 2 commits into
ray-project:masterfrom
ArturNiederfahrenhorst:rllib-impala-learner-thread-no-barriers
Oct 26, 2026
Merged

pseudo-rnd-thoughts merged 2 commits into
ray-project:masterfrom
ArturNiederfahrenhorst:rllib-impala-learner-thread-no-barriers

Conversation

@ArturNiederfahrenhorst

Copy link
Copy Markdown
Contributor

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 #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 [RLlib] IMPALA/APPO: Bound the learner thread's results queue #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, [Core] Owner leaks a memory-store entry when a borrower drops the last reference #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

…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]>
@ArturNiederfahrenhorst
ArturNiederfahrenhorst requested a review from a team as a code owner October 22, 2026 20:08

@gemini-code-assist gemini-code-assist Bot 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.

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]>
@ArturNiederfahrenhorst ArturNiederfahrenhorst added the go add ONLY when ready to merge, run all tests label Oct 22, 2026
@ray-gardener ray-gardener Bot added the rllib RLlib related issues label Oct 23, 2026
@pseudo-rnd-thoughts
pseudo-rnd-thoughts merged commit 3be0eff into ray-project:master Oct 26, 2026
7 checks passed
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go add ONLY when ready to merge, run all tests rllib RLlib related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants