Bug Fix: Resuming Twice Resets the Dataloader#8295
Open
francesco-bertolotti wants to merge 2 commits into
Open
Bug Fix: Resuming Twice Resets the Dataloader#8295francesco-bertolotti wants to merge 2 commits into
francesco-bertolotti wants to merge 2 commits into
Conversation
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
For a streaming
IterableDataset, callingload_state_dict()and then continuing to iterate leavesstate_dict()at the initial position (shard_idx=0,shard_example_idx=0). Reading resumes from the correct place, but the the state never advances again.The practical consequence shows up on the second consecutive resume of a training run:
In our case, a torchtitan resumed-twice run over FineWeb produced a different training loss compared to the resume-once run.
Root cause
The resume call sites in
IterableDataset(_iter_pytorchfornum_workers>=1and_prepare_ex_iterable_for_iterationfornum_workers=0) buildself._state_dictso thatself._state_dict["examples_iterable"]references the example-iterable's own state dict, and then load the resume state into that example-iterable._BaseExamplesIterable.load_state_dictcallsself._init_state_dict(), which rebindsself._state_dictto a brand-new object. After the load,dataset._state_dict["examples_iterable"]still points at the stale zero-initialized state dict. During iteration the example-iterable mutates its state dict, whiledataset.state_dict()keeps deep-copying the stale one, so the reported position is frozen at the zero.On a fresh run the
load_state_dictbranch is skipped, the reference stays linked, and tracking works, which is why the bug only surfaces after a resume.The fix
Re-point
self._state_dict["examples_iterable"]at the live example-iterable state immediately after the resume load, at bothIterableDatasetresume sites:Reproduction
Tests
tests/test_iterable_dataset.py::test_resume_dataloader_twice, a test that takes a checkpoint, resumes from it, takes a second checkpoint from the resumed loader, and asserts the second checkpoint continues iteration from the right place instead of restarting. It fails onmainand passes with this fix.