Skip to content

Fix: Dataloader restarting on second resume - #3908

Merged
tianyu-l merged 1 commit into
pytorch:mainfrom
francesco-bertolotti:dataset<4.8.0
Jul 13, 2026
Merged

tianyu-l merged 1 commit into
pytorch:mainfrom
francesco-bertolotti:dataset<4.8.0

Conversation

@francesco-bertolotti

Copy link
Copy Markdown
Collaborator

PR fixing dataloader restarting on second resume: #3907.

Fix

datasets < 4.8.0.

Note

It should be enough to do datasets < 5.0.0.
I have been a little more conservative to be consistent with

datasets >= 3.6.0, < 4.8.0

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 13, 2026
@tianyu-l tianyu-l linked an issue Jul 13, 2026 that may be closed by this pull request
@tianyu-l
tianyu-l merged commit 6ad8c3d into pytorch:main Jul 13, 2026
13 of 15 checks passed
jemitche1 pushed a commit to jemitche1/torchtitan that referenced this pull request Jul 15, 2026
PR fixing dataloader restarting on second resume:
pytorch#3907.

## Fix
datasets < 4.8.0.

## Note
It should be enough to do datasets < 5.0.0. 
I have been a little more conservative to be consistent with 

https://github.com/pytorch/torchtitan/blob/51c197c86d7c703da96f666d5a7dbd5432b4afbf/.ci/docker/requirements.txt#L2
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
tianyu-l pushed a commit that referenced this pull request Nov 17, 2026
…6095)

## Summary (human-written)
<!-- Write this yourself, not with an AI tool. 2-5 sentences: the
problem, why
     this approach, and what you checked yourself. -->

`datasets` caps `fsspec` in every release, so `datasets >= 3.6.0, <
4.8.0` in `.ci/docker/requirements.txt` holds `fsspec <= 2026.2.0`. Both
upstream bugs that motivated the pin (#2631 and #3907 / #3908) are fixed
in `datasets >= 5.1.0`, and `datasets 5.2.0` is the first release that
allows `fsspec 2026.9.0`.

## What changed
- [`.ci/docker/requirements.txt`](.ci/docker/requirements.txt): require
`datasets >= 5.2.0` (fixes #6094).
-
[`tests/unit_tests/cpu/components/data/test_grain_data.py`](tests/unit_tests/cpu/components/data/test_grain_data.py):
- `test_hugging_face_streaming_source_shards_and_restores`: assert exact
per-rank rows (`[[0, 2, 4, 6, 8], [1, 3, 5, 7, 9]]`), since the set
disjoint/union checks passed even when rank 1 received `[]` on `4.8.x`
(#2631).
- `test_hf_cursor_state_after_restore`: compare rows resumed from each
post-restore state against the uninterrupted original stream instead of
raw state bytes (`RebatchedArrowExamplesIterable` in `5.x` keeps a stale
child dict after `load_state_dict` while resuming from
`previous_state`).

## User-facing changes
| Before | After |
| --- | --- |
| `datasets >= 3.6.0, < 4.8.0` (`fsspec <= 2026.2.0`) | `datasets >=
5.2.0` (`fsspec <= 2026.9.0`) |
| `datasets` 4.x streaming dataloader state tree | `datasets` 5.2.0 adds
a `StepExamplesIterable` level to the streaming state when ranks split
by example (`num_shards` not divisible by the data-parallel world size).
Such `< 5.0` checkpoints fail to load with a `KeyError`; resume them
with `--checkpointer.exclude_from_loading dataloader` |

## Numerics
Bitwise-identical `loss` and `grad_norm` between `datasets==4.7.0`
(`fsspec==2026.2.0`) and `datasets==5.2.0` (`fsspec==2026.9.0`) on A100
(`seed=42`, `deterministic=True`, `scripts/loss_compare.py
--assert-equal`):

| Config | GPUs | Steps | Result |
| --- | --- | --- | --- |
| `llama3_debugmodel` (FSDP4, `c4_test`, random access) | 4 | 20 |
Bitwise identical |
| `llama3_debugmodel` with the dataset overridden to `DATASETS["c4"]`
(FSDP4, streaming `allenai/c4`, 1024 shards, split by shard) | 4 | 20 |
Bitwise identical |
| `llama3_debugmodel` with the dataset overridden to a streaming source
over `tests/assets/c4_test/data.json` (FSDP4, 1 shard, split by example)
| 4 | 20 | Bitwise identical |
| `llama3_debugmodel_sft` (FSDP2, SFT) | 2 | 10 | Bitwise identical |
| `flux_debugmodel_hsdp2x2_cp2_validation` (HSDP 2x2 + CP2, `cc12m-wds`)
| 8 | 20 | Bitwise identical |

## Testing
- **CPU & RL unit tests:** `pytest tests/unit_tests/cpu` (`2058
passed`), `pytest tests/rl/unit_tests/cpu` (`test_alphabet_sort.py`,
`test_dapo_math.py`, `test_geometry3k.py`, `test_search_r1.py`,
`test_model_defaults.py`).
- **GPU integration suites (`run_tests.py`):** `features` (`50/50`) and
`flux` (`hsdp+cp+validation+inference`).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dataloader is restarting on the second resume

2 participants