Skip to content

Preserve the latest transitions when replay batches exceed capacity - #384

Merged
ViktorM merged 2 commits into
Denys88:masterfrom
Afloat16:rl-games-replay-oversized-batches
Oct 29, 2026
Merged

ViktorM merged 2 commits into
Denys88:masterfrom
Afloat16:rl-games-replay-oversized-batches

Conversation

@Afloat16

@Afloat16 Afloat16 commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

When a vectorized SAC rollout inserts more transitions than the replay capacity, the two-slice writer can retain an older prefix or raise a tensor-shape error. Discard the rows that cannot survive the ring and advance the write index over that discarded prefix before the existing writer runs. All transition fields remain aligned, and the resulting contents and next write index match sequential insertion.

The regression tests use an independent chronological queue across capacities, initial write positions and oversized batches. They also cover checkpoint restoration, sampled field alignment, omitted truncation flags, unchanged small-batch controls, and a real CPU SAC update with more environments than replay capacity.

Validation used the complete, unchanged original test scope on Windows with CPython 3.12.13, PyTorch 2.7.1 CPU and NumPy 1.26.4:

  • Before: 316 passed, 14 skipped, 10 failed. Nine failures are new regression cases.
  • After: 325 passed, 14 skipped, 1 failed. All 10 new regression cases pass.
  • The same existing PBT test fails in both runs because its expected checkpoint path is hard-coded in Unix format; the implementation uses os.path.abspath, which returns a Windows path. The original test was kept unchanged, so the local full suite is not entirely passing.
  • Whole Flake8 critical checks, the project's whole Flake8 style command with --exit-zero, and uv lock --check passed before and after. Linux and CUDA CI matrices were not run locally.

Discard the overwritten prefix and advance the circular write position before retaining the last capacity-sized suffix. Verify chronological equivalence, field alignment, checkpoint restoration and a genuine SAC optimizer update.
@ViktorM

ViktorM commented Oct 13, 2026

Copy link
Copy Markdown
Collaborator

Thanks @Afloat16, this is a clean, well-scoped fix. I checked it against an independent deque reference (capacities 1-8, every starting idx, mixed add sequences, checkpoint round-trips) and it matched in every case. On master the same inputs either crash with a shape error (when n > 2*capacity - idx) or leave the ring out of order: e.g. capacity=31, n=32 puts row 0 in the newest slot instead of row 31. The per-field encoding in the tests is a nice touch.

Before merge

  • tests/test_replay_oversized_batches.py:122 needs the package-qualified import (suggestion inline). Right now pytest tests/test_replay_oversized_batches.py fails with ModuleNotFoundError. It only passes in a full-suite run because test_sac_correctness.py puts tests/ on sys.path when another module imports it.

Optional

  • An add can only exceed capacity when replay_buffer_size < num_actors * num_agents, i.e. the replay can't hold even one env step. All rows of one add() come from the same step, so with this fix the lower-index envs never reach replay and nothing tells the user. That's not a regression (master drops envs too, just different ones, or crashes), but a one-time warning next to the UTD warning in SACAgent.__init__, right after self.replay_buffer_size = ..., would make it visible:
    rows_per_step = self.num_actors * self.num_agents
    if self.replay_buffer_size < rows_per_step:
        print(f"WARNING: replay_buffer_size={self.replay_buffer_size} < num_actors*num_agents={rows_per_step}; "
              f"replay holds less than one env step, so only the last {self.replay_buffer_size} env rows of each step are kept")

Out of curiosity, which config hit this?

@Afloat16

Copy link
Copy Markdown
Contributor Author

Changed the import to from tests.test_sac_correctness import make_fake_env_sac_agent.

I reproduced the isolation issue by running only tests/test_replay_oversized_batches.py: the unchanged head gives 9 passed, 1 failed, with ModuleNotFoundError: No module named 'test_sac_correctness' in the SAC case. With the one-line import change, the same isolated module gives 10 passed, including the real CPU SAC update.

This check used Linux/Python 3.12.14, PyTorch 2.7.1+cpu and NumPy 2.3.5. The other source/config files were checked against the original head's Git blob hashes. I have not rerun the full repository suite or CUDA tests for this import-only update.

The triggering case was a synthetic boundary test, using 11 environments and replay capacity 3, derived from the existing SAC fixture. I do not have a production training config to attribute it to. I kept the optional startup warning separate so this update remains focused on replay retention and making its regression run independently.

@codecov-commenter

Copy link
Copy Markdown

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered ☂️

@ViktorM
ViktorM merged commit 5f6d61e into Denys88:master Oct 29, 2026
6 checks passed
@Denys88 Denys88 mentioned this pull request Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants