[BugFix] Fix checkpointing of replay buffer prefetch state - #4089
Open
theap06 wants to merge 2 commits into
Open
[BugFix] Fix checkpointing of replay buffer prefetch state#4089theap06 wants to merge 2 commits into
theap06 wants to merge 2 commits into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/4089
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 8d23cd0 with merge base 7923c6d ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
Contributor
|
| Prefix | Label Applied | Example |
|---|---|---|
[Algorithm] |
new algo | [Algorithm] Add new RL objective |
[BE] |
BE | [BE] Improve error messages |
[Benchmark] or [Benchmarks] |
Benchmarks | [Benchmark] Add collector benchmark |
[BugFix] |
BugFix | [BugFix] Fix memory leak in collector |
[Example] or [Examples] |
Examples | [Example] Add training script |
[Feature] |
Feature | [Feature] Add new optimizer |
[Doc] or [Docs] |
Documentation | [Doc] Update installation guide |
[Refactor] |
Refactoring | [Refactor] Clean up module imports |
[CI] |
CI | [CI] Fix workflow permissions |
[Test] or [Tests] |
Tests | [Tests] Add unit tests for buffer |
[Trainer] or [Trainers] |
Trainers | [Trainer] Add trainer config |
[Environment] or [Environments] |
Environments | [Environments] Add Gymnasium support |
[Data] |
Data | [Data] Fix replay buffer sampling |
[LLM] |
llm/ | [LLM] Add reward model integration |
[Minor] |
small change | [Minor] Fix typo in error message |
[Performance] or [Perf] |
Performance | [Performance] Optimize tensor ops |
[BC-Breaking] |
bc breaking | [BC-Breaking] Remove deprecated API |
[Deprecation] |
Deprecation | [Deprecation] Mark old function |
[Algorithm] or [Algorithms] |
new algo | [Algorithm] Add new objective |
[Quality] |
Quality | [Quality] Fix typos and add codespell |
[Versioning] |
versioning | [Versioning] Bump release version |
[WIP] |
WIP | [WIP] Draft implementation |
Note: Common variations like singular/plural are supported (e.g., [Doc] or [Docs]).
vmoens
force-pushed
the
bugfix/prefetch-checkpoint-state
branch
from
August 13, 2026 09:07
96d5822 to
8d23cd0
Compare
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.
Summary
Persist the replay buffer prefetch queue across
state_dict/load_state_dict,dumps/loads, and Python pickle round-trips.A restored buffer now returns the batches that had already been prefetched at checkpoint time before producing new samples. This keeps sample order, sampler state, and RNG state aligned, including for
SamplerWithoutReplacement.Root cause
Prefetch workers advance the sampler and RNG when they produce a batch, before that batch is consumed by
ReplayBuffer.sample(). Previous checkpoints serialized the already-advanced sampler and RNG but discarded the completed or in-flight queue entries. Restoring such a checkpoint therefore skipped the prefetched batches and resumed from a later sampling state.Implementation
futures -> replaylock order and avoiding deadlock with workers sampling under the replay lock.Futureobjects so the existing sampling path can consume them without starting replacement worker pools or special-casing restored batches.Autograd and ownership
Queued samples can contain non-leaf tensors, either because list-backed samples are stacked or because a transform produced autograd-connected values.
copy.deepcopyrejects these tensors.The queue clone now walks the sample PyTree, detaches and clones tensor leaves, restores their
requires_gradproperty, and deep-copies non-tensor leaves. Checkpoints preserve sample values and tensor metadata without retaining a live autograd graph.Ownership is handled per checkpoint API:
state_dict()and__getstate__()clone queued results so returned state cannot alias the live queue.dumps()serializes the queue directly while checkpoint locks are held, avoiding a redundant full queue-sized pre-copy.loads()and__setstate__()transfer ownership of freshly deserialized queue entries into completed futures instead of cloning them again.load_state_dict()still clones queue entries because the caller retains ownership of the supplied state dictionary.The unavoidable checkpoint-size increase is bounded by the configured prefetch capacity: at most
prefetchcompleted, post-transform batches are stored. No additional executor is created for state-dict or disk restoration, and the restored queue remains bounded by that capacity.Validation
Added or extended coverage for:
Local results:
ufmt,flake8,pydocstyle,codespell, andgit diff --checkpassed for the changed files.Current status
Rebased onto
mainat7923c6da0. Conflict resolution preserves the current replay-buffer sample-unit and storage mutation-revision changes alongside prefetch checkpointing. The force-push to the contributor branch triggered fresh CI.