StatefulDistributedSampler loses loaded progress when re-saved before iteration
🐛 Describe the bug
With StatefulDistributedSampler, loading a StatefulDataLoader checkpoint and saving it again before consuming another batch loses the loaded sampler position. Loading that second checkpoint repeats batches that were already consumed. Loading the original checkpoint directly resumes correctly.
This affects workflows that load and re-save a training checkpoint before iteration resumes. I reproduced it on unmodified main at ee79a399d3a53676544e0d3f0e826ffa58d1fec9 and on torchdata 0.11.0. This is a minimal reproduction, rather than a report from an observed production training run.
from copy import deepcopy
from torchdata.stateful_dataloader import StatefulDataLoader
from torchdata.stateful_dataloader.sampler import StatefulDistributedSampler
dataset = list(range(20))
def make_loader():
sampler = StatefulDistributedSampler(
dataset, num_replicas=2, rank=0, shuffle=False
)
return StatefulDataLoader(
dataset, batch_size=2, sampler=sampler, num_workers=0
)
loader = make_loader()
iterator = iter(loader)
print("consumed:", [next(iterator).tolist() for _ in range(2)])
state = deepcopy(loader.state_dict())
expected = [batch.tolist() for batch in iterator]
restored = make_loader()
restored.load_state_dict(state)
state_saved_again = deepcopy(restored.state_dict())
print("direct restore:", [batch.tolist() for batch in restored])
restored_again = make_loader()
restored_again.load_state_dict(state_saved_again)
actual = [batch.tolist() for batch in restored_again]
print("expected:", expected)
print("after re-saving:", actual)
Output on unmodified main and 0.11.0:
consumed: [[0, 2], [4, 6]]
direct restore: [[8, 10], [12, 14], [16, 18]]
expected: [[8, 10], [12, 14], [16, 18]]
after re-saving: [[0, 2], [4, 6], [8, 10], [12, 14], [16, 18]]
StatefulDistributedSampler.load_state_dict() places the loaded position in next_yielded, to be applied when its next iterator starts. Before that happens, state_dict() serializes the instance's old yielded value. Although it also includes NEXT_YIELDED, load_state_dict() reads yielded, so the second restore uses the wrong position.
A minimal fix is to serialize next_yielded as yielded when it is not None, preserving the existing state keys and iteration behavior. I have prepared that fix with sampler and public DataLoader regressions and can submit a PR. The sampler test module passes with the fix; a separate CPU probe also covers two spawn workers with and without persistent workers. Full-suite and GPU validation have not been run.
I checked the related epoch work in #1515 and the torchdata.nodes.Loader work in #1548. They address different state paths. #1479 touches the same sampler files but changes constructor options rather than this serialization behavior.
Versions
- OS: macOS 26.6.2, arm64
- Python: 3.12.3
- torch: 2.8.0, CPU execution; CUDA unavailable
- torchdata: unmodified main at
ee79a399d3a53676544e0d3f0e826ffa58d1fec9; separately reproduced with PyPI 0.11.0 - NumPy: 2.3.5
Investigation and patch preparation were assisted by Codex. The results above come from local runs; they are not upstream CI results.