Lazy sorting added for replay buffer - #523
Merged
Merged
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #523 +/- ##
==========================================
- Coverage 73.25% 73.14% -0.12%
==========================================
Files 59 59
Lines 10082 10115 +33
Branches 1402 1408 +6
==========================================
+ Hits 7386 7399 +13
- Misses 2237 2255 +18
- Partials 459 461 +2
🚀 New features to boost your workflow:
|
josephdviviano
approved these changes
Apr 28, 2026
josephdviviano
left a comment
Collaborator
There was a problem hiding this comment.
lgtm, great work, thank you
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Lazy sorting for replay buffer
Summary
Adds an opt-in
lazy_sortmode toReplayBufferthat defers concatenation and sorting until the buffer reaches 2x capacity, significantly reducing the cost of_local_addon buffer manager ranks.Problem
On buffer manager ranks, every incoming batch triggered an
extend()(which copies the entire buffer viatorch.cat) followed by anargsort+ truncation. With 32 training agents each submitting ~100 batches, this resulted in ~3,200 extend-sort-truncate cycles — dominated by the quadratic copy cost of repeatedly concatenating onto a growing tensor.Changes
replay_buffer.py
lazy_sortparameter — newboolflag onReplayBuffer.__init__(defaultFalse, backward compatible). When enabled, incoming batches are accumulated in_pending_batcheslist instead of being immediately concatenated._flush_pending()— merges all pending batches into a single combined batch first, then extendstraining_containeronce — avoids the quadratic cost of extending one-by-one._sort_and_truncate()— extracted from_local_addinto a helper; argsorts bylog_rewards(if prioritized) and truncates to capacity. Both paths (lazy and eager) share this method._local_add()— branches onself.lazy_sort: lazy path accumulates and flushes at 2x capacity; eager path preserves the original extend-sort-truncate-every-call behavior.__len__— includes_pending_lenso length queries reflect uncommitted items.sample()— samples from committed container only (does not flush pending). Guard updated to handle emptytraining_container.save()— calls_flush_pending()before serialization.NormBasedDiversePrioritizedReplayBuffer—lazy_sortparameter threaded through tosuper().__init__._local_addflushes pending batches when the buffer is full (diversity filtering requires a consistent view), but delegates to the base lazy path while filling up.replay_buffer_manager.py
lazy_sort=Trueandtiming=self.timingto bothReplayBufferandNormBasedDiversePrioritizedReplayBufferconstructors.Performance
Benchmarked on 32 training agents, 1 buffer manager (Intel EMR (64 cores) x 2, single node), 100 iterations:
Backward Compatibility
lazy_sortdefaults toFalse— existing code is unaffected. The eager path is unchanged aside from being wrapped in timers and delegating to the shared_sort_and_truncatehelper.