IterableDataset skip/take fails when iterating a nonzero epoch
Describe the bug
After IterableDataset.skip() or take(), iteration works at epoch 0 but raises DataSourcesShufflingDisallowed after set_epoch(1). This also affects a frozen subset with a shuffle buffer, so a multi-epoch data-loading loop stops when it starts its second epoch.
Reproduced with Datasets 5.0.1 and unmodified main at fa995bdaea171d50d086743364f23e9db6b557d8.
Steps to reproduce the bug
from datasets import Dataset
for method in ("skip", "take"):
ds = Dataset.from_dict({"id": list(range(16))}).to_iterable_dataset(num_shards=4)
ds = getattr(ds, method)(4)
print(method, "epoch 0:", [row["id"] for row in ds])
ds.set_epoch(1)
try:
print(method, "epoch 1:", [row["id"] for row in ds])
except Exception as error:
print(method, "epoch 1:", type(error).__name__)Output:
skip epoch 0: [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
skip epoch 1: DataSourcesShufflingDisallowed
take epoch 0: [0, 1, 2, 3]
take epoch 1: DataSourcesShufflingDisallowedExpected behavior
The subset should remain iterable in later epochs, respecting the shard order frozen by skip()/take(). With a shuffle buffer added after the subset operation, the selected samples should remain the same and shuffling should be reproducible for a given epoch.
_prepare_ex_iterable_for_iteration() calls shuffle_data_sources() for every nonzero epoch without handling the exception, whereas IterableDataset.shuffle() already handles it. A local fix preserves the frozen shard order and shifts RNGs on shallow copies of the iterable wrappers. Copying matters here: suppressing only the exception mutates the stored pipeline and changes the output on repeated iterations of the same epoch.
Environment info
- Datasets 5.0.1; also 5.0.2.dev0 at the commit above
- Python 3.12.3, macOS 26.6.2 arm64
- PyArrow 25.0.1, NumPy 2.5.3, Hugging Face Hub 1.33.0
- Synthetic data; no model downloads or GPU needed
AI assistance: Codex assisted with the investigation, patch, tests, and this report. The reproduction was run locally.
Source: huggingface/datasets