Skip to content

[Feature] Add flat replay writes to collectors - #4020

Open
vmoens wants to merge 3 commits into
mainfrom
codex/ray-flatten-replay-data
Open

[Feature] Add flat replay writes to collectors#4020
vmoens wants to merge 3 commits into
mainfrom
codex/ray-flatten-replay-data

Conversation

@vmoens

@vmoens vmoens commented Jul 16, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • add an opt-in flatten_data collector argument that writes batched rollouts to 1-D replay storage with one reshape and one extend
  • support local, asynchronous, multi-process, and Ray collector paths, including Hydra configs and lazy shared-storage initialization
  • document flat 1-D replay buffers as TorchRL's preferred layout and add regression and benchmark coverage

Motivation

Batched environments produce [N, T] rollouts. Direct replay writes currently preserve that batch shape, so 1-D storage records N items with length-T payloads instead of N * T transitions. This makes transition sampling and frame accounting awkward, particularly for Ray-backed collectors.

With flatten_data=True, collectors reshape the rollout once immediately before replay insertion, preserving one replay-buffer extend call while storing individual transitions.

Compatibility

The option defaults to False, so existing multidimensional replay-buffer layouts are unchanged. The deprecated extend_buffer=False path is rejected with flattening, and padded split_trajs=True output remains separate from flat replay writes.

Testing

  • pre-commit run --files <changed files>
  • python -m pytest test/test_collectors.py::TestCollectorRB -q (19 passed, 25 skipped)
  • python -m pytest test/test_collectors.py::TestTrajsPerBatchReplayBuffer -q (28 passed)

Ray and config regression tests were added but not executed locally because their optional dependencies were unavailable.

@pytorch-bot

pytorch-bot Bot commented Jul 16, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/4020

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (1 Unrelated Failure)

As of commit 3794707 with merge base f2a2337 (image):

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 16, 2026
@github-actions github-actions Bot added Feature New feature Documentation Improvements or additions to documentation Benchmarks rl/benchmark changes Collectors Trainers Integrations/torch_geometric Integrations and removed Feature New feature labels Jul 16, 2026
@vmoens
vmoens marked this pull request as ready for review July 16, 2026 12:26
@github-actions github-actions Bot added the Feature New feature label Jul 16, 2026
vmoens and others added 2 commits July 16, 2026 14:43
- Raise TypeError when flatten_data=True is passed without a replay
  buffer (Collector, multi-process collectors, RayCollector) instead of
  silently ignoring the flag.
- Use setdefault when forwarding flatten_data to Ray collector_kwargs,
  matching the trajs_per_batch convention.
- Comment the conditional flatten kwargs in _main_async_collector
  (backward compatibility with custom collector classes) and the
  LLMCollector flatten_data attribute coupling in _postproc.
- Document that trajs_per_batch supersedes flatten_data and that
  fake_tensordict() returns flat data when flattening is active.
- Add tests: missing-buffer TypeError for all collector entry points,
  postproc runs on the batched rollout before flattening, and config
  forwarding via the raised error.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@vmoens
vmoens force-pushed the codex/ray-flatten-replay-data branch from df559de to 2c7c3c5 Compare July 16, 2026 13:44
# Conflicts:
#	docs/source/reference/collectors_replay.rst
#	torchrl/collectors/distributed/ray.py
#	torchrl/trainers/algorithms/configs/collectors.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Benchmarks rl/benchmark changes CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Collectors Documentation Improvements or additions to documentation Feature New feature Integrations/torch_geometric Integrations Trainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant