Repository navigation
Reset random sampler counters when restoring checkpoints into a used iterator - #1566
Open
liangqianxing wants to merge 1 commit into
Open
liangqianxing wants to merge 1 commit into
liangqianxing wants to merge 1 commit into
Conversation
When `_StatefulRandomSamplerIterator.load_state_dict` replays a checkpoint onto an already-used iterator, `self.yielded` and `self.perm_index` retained their previous values before replaying `self.next_yielded` steps. A partially consumed iterator returned the wrong sequence, and an exhausted iterator raised `StopIteration` during replay. Reset `self.yielded` and `self.perm_index` to `0` before regenerating `self.perm` and advancing through `self.next_yielded`, and add a regression test covering both replacement modes, partially consumed and exhausted targets, and repeated restores. Co-Authored-By: Claude Code <noreply@anthropic.com>
Author
|
Hi @psaikaushik @divyanshk — following up on #1562, this PR fixes a race in |
This branch has not been deployed
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
Follow-up from the discussion on #1562.
When
_StatefulRandomSamplerIterator.load_state_dictreplays a checkpoint onto an iterator that has already yielded samples,self.yieldedandself.perm_indexretained their previous values before replayingself.next_yieldedsteps:StopIterationduring replay.Changes
torchdata/stateful_dataloader/sampler.py: Resetself.yielded = 0andself.perm_index = 0before rebuildingself.permand replayingself.next_yieldedsteps in_StatefulRandomSamplerIterator.load_state_dict.test/stateful_dataloader/test_sampler.py: Addtest_random_sampler_checkpoint_restores_used_iteratorcovering both replacement modes (replacement=Falseandreplacement=True), partially consumed (5) and exhausted (70) target iterators, and restoring the same snapshot twice.Validation
replacement×consumed) before the two resets and passes afterward.test/stateful_dataloader/test_sampler.pysuite and applicable pre-commit hooks pass.🤖 Generated with Claude Code