Skip to content

[Feature] Mixed-source replay with ReplayBufferEnsemble - #4773

Open
theap06 wants to merge 6 commits into
pytorch:mainfrom
theap06:pr/rb-ensemble-mixed-source
Open

theap06 wants to merge 6 commits into
pytorch:mainfrom
theap06:pr/rb-ensemble-mixed-source

Conversation

@theap06

@theap06 theap06 commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Makes ReplayBufferEnsemble work for batches that mix data sources (RLPD, offline-to-online, HIL-SERL), all opt-in with unchanged defaults: prioritized and uniform members together, priority updates routed to each member, exact per-member fractions with a flat batch, and routing_mode="mask" writes that store a record in several members and return every copy's address for update_if_present. Benchmark numbers against two hand-wired buffers doing the same work are in the benchmark commit message.

Motivation and Context

On main a prioritized member next to a uniform one crashes on sampling, a record can go to one member only, there is no update_tensordict_priority, and the p= split is random per batch.

Stack (merge in this order; #4771, #4772, #4773 and #4799 are independent of each other and each is based on main, #4774 needs all four):

  1. [BugFix] Run ModuleTransform on reset when it declares observations #4771 [BugFix] Run ModuleTransform on reset when it declares observations
  2. [Feature] InterventionModule for human or scripted takeovers #4772 [Feature] InterventionModule for human or scripted takeovers
  3. [Feature] Mixed-source replay with ReplayBufferEnsemble #4773 [Feature] Mixed-source replay with ReplayBufferEnsemble (this PR)
  4. [Feature] SACLoss backup_entropy option #4799 [Feature] SACLoss backup_entropy option
  5. [Algorithm] HIL-SERL in simulation #4774 [Algorithm] HIL-SERL in simulation
  • I have raised an issue to propose this change (required for new features and bug fixes)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds core functionality)
  • Documentation (update in the documentation)

Checklist

  • I have read the CONTRIBUTION guide (required)
  • My change requires a change to the documentation.
  • I have updated the tests accordingly (required for a bug fix or a new feature).
  • I have updated the documentation accordingly.

Makes ReplayBufferEnsemble usable for RLPD, offline-to-online and HIL-SERL
style training, where each batch mixes data from several sources. Every new
behavior is opt-in and the defaults are unchanged.

- Mixed member samplers: a prioritized member next to a uniform member no
  longer crashes when infos are stacked. Uniform rows get priority_weight 1.
  Any other info key missing from some members raises a KeyError naming the
  key, and members with different index layouts raise a clear ValueError.
- Routed priority updates: update_priority and update_tensordict_priority
  send each row to the member it came from, with member-local indices.
- Exact quotas: fractions=[...] gives every batch the same per-member counts
  (largest-remainder rounding) and a flat [batch_size] batch ordered by
  member. With slice samplers the counts are in slices. A member that cannot
  serve its share blocks sampling instead of passing it on. fractions is
  settable between batches, saved in state_dict, kept (renormalized) by
  rb[...] sub-ensembles, and exposed on SamplerEnsembleConfig.
- Multi-membership writes: routing_mode="mask" writes a record to every
  member marked in a boolean mask and returns [N, num_members] addresses
  with a present mask. update_if_present and update_priority skip absent
  copies, and a per-record patch reaches every copy. The mask is not stored.
- Performance: ReplayBuffer.sample counted items with torch.as_tensor on the
  ensemble's index tensordict, which walks it row by row (the change
  proposed in pytorch#4720). Flat sampling, mask writes and priority updates avoid
  per-row work. Adds test_mixed_source_replay_buffer, which compares the
  ensemble against equivalent hand-wired code.

CPU, batch 256, 1k-frame writes, median us (ensemble vs hand-wired):
sample 585 vs 517, extend 778 vs 620, priority update 197 vs 168.
…hares

Extends test_mixed_source_replay_buffer to every pair of uniform and
prioritized members and to 10% or 100% of rows routed to both members. The
hand-wired baseline now does the same work as the ensemble: writes return the
write generation of every copy, and sampled batches carry each row's member,
index and write generation, which routed write-back needs.

CPU (Mac, under load), ensemble time over hand-wired time:
sample 0.86 to 0.98, priority update 1.00 to 1.11, extend 1.09 to 1.17 with
10% shared rows and 0.85 to 0.87 with 100% shared rows.
@pytorch-bot

pytorch-bot Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 3 New Failures

As of commit 064ee84 with merge base df1db3d (image):

NEW FAILURES - The following jobs have failed:

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 Nov 23, 2026
@github-actions github-actions Bot added Feature New feature Documentation Improvements or additions to documentation Benchmarks rl/benchmark changes ReplayBuffers Trainers labels Nov 23, 2026
@theap06 theap06 mentioned this pull request Nov 23, 2026
4 of 6 tasks
update_priority with a plain tensor index goes to the ensemble's sampler as
it did before, instead of raising a TypeError. Class-level defaults for
routing_mode and the sampler's fractions let whole-object pickles saved
before this change load, sample and extend.
Split flat batches by the sampled member ids instead of recomputing the
quotas, so a fractions change during prefetch cannot mislabel rows, and
take the replay lock in the fractions setter. dumps and loads now keep
fractions, non-strict slice samplers are rejected with fractions,
samples_returned counts records for multi-dim members, and mask-mode
update_priority accepts one priority per record.
…e them unset too

Assigning fractions after construction skipped the check that they cannot be combined with p, sample_from_all or num_buffer_sampled, which were then silently ignored; the setter now raises like the constructor. Checkpoints only stored fractions when set, so loading one saved without them kept the fractions already on the buffer; the current value is now always saved, None included. The docstrings also say a tensor is accepted.
Priority updates now hold the ensemble's locks, so they cannot run while a
prefetched sample reads the same priority trees. Samples drawn with fractions
are pinned when pin_memory=True. Fractions are renormalized so large batches
always get counts that add up to the batch size. update_priority skips members
without a prioritized sampler, as documented.
@theap06
theap06 marked this pull request as ready for review November 29, 2026 09:13

This branch has not been deployed

No deployments
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. 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