Repository navigation
Bug Fix: Resuming Twice Resets the Dataloader - #8295
Merged
Merged
Conversation
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
Elsword016
pushed a commit
to Elsword016/datasets
that referenced
this pull request
Aug 6, 2026
* re-sync state_dict after resume * num workers test
zhixiangli
added a commit
to zhixiangli/torchtitan
that referenced
this pull request
Nov 13, 2026
datasets caps fsspec in every release, and the `< 4.8.0` pin held fsspec at <= 2026.2.0. datasets 5.2.0 is the first release that allows fsspec 2026.9.0. The pin covered two upstream bugs, both fixed in datasets >= 5.1.0: - pytorch#2631: 4.8.x split_dataset_by_node gave nonzero ranks no samples when num_shards < world_size. - pytorch#3907 / pytorch#3908: 5.0.0 restarted the data stream on the second resume (huggingface/datasets#8308, fixed by huggingface/datasets#8295). test_hf_cursor_state_after_restore compared pickled Hugging Face state bytes after a restore. In datasets 5.x, load_state_dict re-initializes the child iterable's state, so RebatchedArrowExamplesIterable keeps a stale nested examples_iterable entry; resume reads previous_state instead, so the rows are unaffected. The test now checks that a state saved after a restore resumes to the same rows as the original's, which fails on datasets 5.0.0 (pytorch#3907) and passes on 4.7.0 and 5.1.0. Streaming-source dataloader state saved with datasets < 5 does not load with 5.x (the state tree gained a RebatchedArrowExamplesIterable level); it fails with a KeyError. Resume such runs with checkpointer.exclude_from_loading=["dataloader"] or finish them on the old version.
zhixiangli
added a commit
to zhixiangli/torchtitan
that referenced
this pull request
Nov 13, 2026
datasets caps fsspec in every release, and the `< 4.8.0` pin held fsspec at <= 2026.2.0. datasets 5.2.0 is the first release that allows fsspec 2026.9.0. The pin covered two upstream bugs, both fixed in datasets >= 5.1.0: - pytorch#2631: 4.8.x split_dataset_by_node gave nonzero ranks no samples when num_shards < world_size. - pytorch#3907 / pytorch#3908: 5.0.0 restarted the data stream on the second resume (huggingface/datasets#8308, fixed by huggingface/datasets#8295). test_hf_cursor_state_after_restore compared pickled Hugging Face state bytes after a restore. In datasets 5.x, load_state_dict re-initializes the child iterable's state, so RebatchedArrowExamplesIterable keeps a stale nested examples_iterable entry; resume reads previous_state instead, so the rows are unaffected. The test now checks that a state saved after a restore resumes to the same rows as the original's, which fails on datasets 5.0.0 (pytorch#3907) and passes on 4.7.0, 5.1.0, and 5.2.0. Streaming-source dataloader state saved with datasets < 5 does not load with 5.x (the state tree gained a RebatchedArrowExamplesIterable level); it fails with a KeyError. Resume such runs with checkpointer.exclude_from_loading=["dataloader"] or finish them on the old version.
zhixiangli
added a commit
to zhixiangli/torchtitan
that referenced
this pull request
Nov 13, 2026
datasets caps fsspec in every release, and the `< 4.8.0` pin held fsspec at <= 2026.2.0. datasets 5.2.0 is the first release that allows fsspec 2026.9.0. The pin covered two upstream bugs, both fixed in datasets >= 5.1.0: - pytorch#2631: 4.8.x split_dataset_by_node gave nonzero ranks no samples when num_shards < world_size. - pytorch#3907 / pytorch#3908: 5.0.0 restarted the data stream on the second resume (huggingface/datasets#8308, fixed by huggingface/datasets#8295). Update test_grain_data.py: - test_hugging_face_streaming_source_shards_and_restores: assert the exact per-rank rows (`[[0, 2, 4, 6, 8], [1, 3, 5, 7, 9]]`), since the set disjoint/union assertions passed even when rank 1 received `[]` on 4.8.x. - test_hf_cursor_state_after_restore: in datasets 5.x, load_state_dict re-initializes the child iterable's state, so RebatchedArrowExamplesIterable keeps a stale nested examples_iterable entry while resume reads previous_state. Compare the rows resumed from each post-restore state against the uninterrupted original stream across the epoch boundary (fails on 5.0.0, passes on 4.7.0, 5.1.0, and 5.2.0). Streaming-source dataloader state saved with datasets < 5 does not load with 5.x (the state tree gained a RebatchedArrowExamplesIterable level); it fails with a KeyError. Resume such runs with checkpointer.exclude_from_loading=["dataloader"] or finish them on the old version. Fixes pytorch#6094
zhixiangli
added a commit
to zhixiangli/torchtitan
that referenced
this pull request
Nov 17, 2026
datasets caps fsspec in every release, and the `< 4.8.0` pin held fsspec at <= 2026.2.0. datasets 5.2.0 is the first release that allows fsspec 2026.9.0. The pin covered two upstream bugs, both fixed in datasets >= 5.1.0: - pytorch#2631: 4.8.x split_dataset_by_node gave nonzero ranks no samples when num_shards < world_size. - pytorch#3907 / pytorch#3908: 5.0.0 restarted the data stream on the second resume (huggingface/datasets#8308, fixed by huggingface/datasets#8295). Update test_grain_data.py: - test_hugging_face_streaming_source_shards_and_restores: assert the exact per-rank rows (`[[0, 2, 4, 6, 8], [1, 3, 5, 7, 9]]`), since the set disjoint/union assertions passed even when rank 1 received `[]` on 4.8.x. - test_hf_cursor_state_after_restore: in datasets 5.x, load_state_dict re-initializes the child iterable's state, so RebatchedArrowExamplesIterable keeps a stale nested examples_iterable entry while resume reads previous_state. Compare the rows resumed from each post-restore state against the uninterrupted original stream across the epoch boundary (fails on 5.0.0, passes on 4.7.0, 5.1.0, and 5.2.0). Streaming-source dataloader state saved with datasets < 5 does not load with 5.x (the state tree gained a RebatchedArrowExamplesIterable level); it fails with a KeyError. Resume such runs with checkpointer.exclude_from_loading=["dataloader"] or finish them on the old version. Fixes pytorch#6094
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.