Keep batches_yielded monotonic across StatefulDataLoader resumes#58
Open
Keep batches_yielded monotonic across StatefulDataLoader resumes#58
Conversation
StatefulDataLoader._batches_yielded was reset to 0 on every __iter__ call and not restored from state_dict. Combined with the sampler's single-shot _skip (consumed inside iter() then zeroed), this made batches_yielded mean "batches since last iter()" rather than "total batches in this epoch." Save save resumes after the first one silently re-read only the middle-run delta, misaligning the data order on the second and later preemptions within the same epoch — the common case on busy clusters. Two fixes: - load_state_dict writes the saved value onto self._batches_yielded (previously read into a local and discarded). - __iter__ re-applies the sampler skip based on the current _batches_yielded and no longer zeros it. The field now advances monotonically within the epoch and resets only on StopIteration. Regression test covers three-way resume within one epoch; existing single-resume, skip-ahead, and epoch-boundary tests still pass.
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
StatefulDataLoader.load_state_dictnow writes toself._batches_yielded(previously read into a local and discarded).__iter__re-appliessampler.set_skip(self._batches_yielded * self.batch_size)on every call and no longer zeros_batches_yielded. The field advances monotonically within the epoch and resets only onStopIteration.Closes #57
Test plan
uv run pytest tests/integration/test_data_resumption.py -v: the new three-way resume test and all existing tests pass.