Skip to content

[RLlib] IMPALA/APPO: Bound the learner thread's results queue - #67299

Merged
pseudo-rnd-thoughts merged 2 commits into
ray-project:masterfrom
ArturNiederfahrenhorst:rllib-impala-bounded-results-queue
Oct 26, 2026
Merged

pseudo-rnd-thoughts merged 2 commits into
ray-project:masterfrom
ArturNiederfahrenhorst:rllib-impala-bounded-results-queue

Conversation

@ArturNiederfahrenhorst

@ArturNiederfahrenhorst ArturNiederfahrenhorst commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Before this PR: With APPO's circular buffer (circular_buffer_iterations_per_batch K > 1, default 2), the _LearnerThread runs up to K updates per IMPALALearner.update() call. It put one reduced result on an unbounded queue per update, while update() collected one per call, so K-1 results per cycle were never collected. Whenever the Learner is faster than sampling (e.g. a GPU Learner):

  • the Learner's memory grows without bound (+540 MiB/h on an L4 Learner);
  • the Algorithm's NUM_*_TRAINED_LIFETIME counters advance at 1/K, which also slows APPO's target-network updates by K;
  • the learner metrics the Algorithm reports fall further and further behind.

Introduced by #59544 (Ray 2.54).

After this PR: The results queue holds at most one result, and the learner thread only reduces once update() collected the previous one. Until then, metrics keep accumulating in the Learner's MetricsLogger, so the next result covers all updates and nothing is lost.

Tests

  • New test_learner_thread_results_with_circular_buffer (APPO, K=2, stubbed gradient update): fails on master (4 results queued after 8 updates), passes with this PR.
  • python -m pytest python/ray/rllib/algorithms/appo/tests/test_appo_learner.py: 3 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, 20 to 90 minute runs on Anyscale): the queue stays at 1 instead of passing 10,000 after 20 minutes, and the trained-steps counters and target-network updates match ground truth.

No existing issue or PR covers this; I searched open issues and PRs for the learner results queue and the circular buffer. 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

With APPO's circular buffer (K = circular_buffer_iterations_per_batch > 1), the
_LearnerThread runs up to K updates per IMPALALearner.update() call. It put one
reduced result on an unbounded queue per aggregation, while update() collected one
per aggregation, so K-1 results per cycle were never collected. The queue grew
without bound, the Algorithm's NUM_*_TRAINED_LIFETIME counters advanced at 1/K
(slowing APPO's target-network updates by K), and the learner metrics reported to
the Algorithm fell further and further behind.

The queue now holds at most one result: the learner thread only reduces once
update() collected the previous result. Until then, metrics keep accumulating in
the Learner's MetricsLogger and the next reduce covers them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Artur Niederfahrenhorst <[email protected]>

@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 prevents learner results from piling up in the output queue when a batch is trained multiple times, such as with APPO's circular buffer. It limits the _learner_thread_out_queue size to 1 in impala_learner.py and ensures that the learner thread only pushes reduced metrics to the queue if it is currently empty. Additionally, a new unit test test_learner_thread_results_with_circular_buffer has been added to verify this behavior. There are no review comments to address, and no further feedback is provided.

…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 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@pseudo-rnd-thoughts
pseudo-rnd-thoughts merged commit 2810581 into ray-project:master Oct 26, 2026
7 checks passed
pseudo-rnd-thoughts pushed a commit that referenced this pull request Oct 26, 2026
…n the learner thread (#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
#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 #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, #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]>
justinyeh1995 pushed a commit to justinyeh1995/ray that referenced this pull request Oct 28, 2026
…oject#67299)

Before this PR: With APPO's circular buffer
(`circular_buffer_iterations_per_batch` K > 1, default 2), the
`_LearnerThread` runs up to K updates per `IMPALALearner.update()` call.
It put one reduced result on an unbounded queue per update, while
`update()` collected one per call, so K-1 results per cycle were never
collected. Whenever the Learner is faster than sampling (e.g. a GPU
Learner):
- the Learner's memory grows without bound (+540 MiB/h on an L4
Learner);
- the Algorithm's `NUM_*_TRAINED_LIFETIME` counters advance at 1/K,
which also slows APPO's target-network updates by K;
- the learner metrics the Algorithm reports fall further and further
behind.

Introduced by ray-project#59544 (Ray 2.54).

After this PR: The results queue holds at most one result, and the
learner thread only reduces once `update()` collected the previous one.
Until then, metrics keep accumulating in the Learner's `MetricsLogger`,
so the next result covers all updates and nothing is lost.

**Tests**
- New `test_learner_thread_results_with_circular_buffer` (APPO, K=2,
stubbed gradient update): fails on master (4 results queued after 8
updates), passes with this PR.
- `python -m pytest
python/ray/rllib/algorithms/appo/tests/test_appo_learner.py`: 3 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, 20 to 90 minute runs on
Anyscale): the queue stays at 1 instead of passing 10,000 after 20
minutes, and the trained-steps counters and target-network updates match
ground truth.

No existing issue or PR covers this; I searched open issues and PRs for
the learner results queue and the circular buffer. 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]>
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