Skip to content

[Feature] Add SampleUnit composition point for replay sampling - #4045

Open
theap06 wants to merge 1 commit into
pytorch:mainfrom
theap06:feature/rb-sample-unit-interface
Open

[Feature] Add SampleUnit composition point for replay sampling#4045
theap06 wants to merge 1 commit into
pytorch:mainfrom
theap06:feature/rb-sample-unit-interface

Conversation

@theap06

@theap06 theap06 commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Description

PR 1 of the split proposed in #4039: the interface layer of the sampler decomposition. Replay sampling combines two orthogonal decisions — which anchors the sampler's distribution selects, and what each anchor expands into. This PR adds the composition point for the second decision without changing any behavior.

  • SampleUnit abstract base: expand(index, info, storage) runs inside the buffer's sampling critical section, after the anchor sampler and before the storage read or any index bookkeeping, and returns the expanded index plus (possibly augmented) info. Units own the alignment of per-anchor info entries they change; metadata they add flows into sample(return_info=True) and becomes keys of TensorDict samples through the existing info-copy path.
  • Transition: the identity unit and implicit default. sample_unit=None and sample_unit=Transition() are behaviorally identical — pinned by tests under seeded generators, so this PR is a pure no-op for existing users.
  • sample_unit= keyword on ReplayBuffer, threaded through PrioritizedReplayBuffer and TensorDictPrioritizedReplayBuffer; Hydra config companions gain the matching field.

This settles the RFC's first open question (units are buffer-owned, matching the illustrative API in the issue) and gives the follow-ups from the #4039 split — the Sequence unit with boundary policies, burn-in/bootstrap, priority semantics — a stable target to build against.

Testing

TestSampleUnit in test/rb/test_rb_core.py: seeded-generator equivalence of default vs Transition() (data, info keys, indices), no metadata leakage from the identity unit, a toy expanding unit proving the composition end-to-end (expanded batch, info["index"] reflects expansion, metadata surfaces as TensorDict sample keys), constructor type validation, and the prioritized-buffer path including update_tensordict_priority after sampling through a unit. Full test/rb/ suite passes (4189 tests).

Part of #4039.

Introduces the interface layer of the sampler-decomposition RFC:
sampling combines an anchor distribution (the sampler) with a range
expansion (the sample unit), and this PR adds the composition point
without changing any behavior.

A SampleUnit receives the anchor index, the sampler info dict and the
storage inside the buffer's sampling critical section, after the
anchor sampler ran and before the storage read or index bookkeeping,
and returns the expanded index plus (possibly augmented) info. Unit
metadata added to info flows into sample(return_info=True) and becomes
keys of TensorDict samples through the existing info-copy path.
Transition is the identity unit and the implicit default:
sample_unit=None and sample_unit=Transition() are behaviorally
identical, which the tests pin under seeded generators.

The sample_unit keyword is exposed on ReplayBuffer and threaded
through the prioritized variants; Hydra config companions gain the
matching field. This settles the RFC's first open question (units are
buffer-owned) and gives the Sequence unit, boundary policies and
priority-semantics follow-ups a stable target.

Part of pytorch#4039.
@pytorch-bot

pytorch-bot Bot commented Jul 24, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

⚠️ 15 Awaiting Approval

As of commit 6795078 with merge base ae421b9 (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

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 24, 2026
@github-actions github-actions Bot added Documentation Improvements or additions to documentation ReplayBuffers Trainers Feature New feature labels Jul 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Documentation Improvements or additions to documentation Feature New feature ReplayBuffers Trainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant