Skip to content

Preserve source shards when shuffling iterable datasets - #8670

Merged
lhoestq merged 1 commit into
huggingface:mainfrom
yuchenwang3:fix/shuffle-preserve-shards-8669
Oct 5, 2026
Merged

lhoestq merged 1 commit into
huggingface:mainfrom
yuchenwang3:fix/shuffle-preserve-shards-8669

Conversation

@yuchenwang3

Copy link
Copy Markdown
Contributor

IterableDataset.shuffle() currently interleaves the source shards before node and DataLoader worker sharding. With 19 source shards and the default settings, the resulting dataset reports one shard, leaving three of four DataLoader workers idle.

This adds a dedicated iterable that preserves the source shard count and interleaves only the shards assigned to each node and worker. The existing cycling implementation and its general interleave semantics are unchanged. The max_buffer_input_shards=1 path and skip/take ordering constraints are preserved.

Fixes #8669.

Validation

On macOS arm64, Python 3.12, PyTorch 2.14 (CPU):

  • tests/test_iterable_dataset.py -m "not integration": 528 passed, 27 skipped, 2 integration tests deselected.
  • tests/test_distributed.py -m "not integration": 14 passed, 4 integration tests deselected.
  • Added 23 regression cases covering Arrow and Python sources, default/explicit input shard limits, complete and unique row coverage, deterministic epochs, and checkpoint restoration with a one-example buffer. Real DataLoader tests cover four workers over 19 shards, more workers than source shards, and two node partitions each iterated with four workers.
  • On unmodified main, the focused source/node partition regression subset produces 8 failures and 2 passing controls.
  • An existing single-shard checkpoint generated on unmodified main resumes successfully after this change.
  • Repository-wide make quality and git diff --check pass.

The existing limitation that shuffle-buffer contents are not restored is unchanged. Worker assignments now follow the actual source shards; compatibility with old checkpoints created under the reduced worker assignment is not claimed. No GPU or multi-host training was run.

Implementation and tests generated with OpenAI Codex; the checks above were executed locally.

Assisted-by: OpenAI Codex
Signed-off-by: Yuchen Wang <[email protected]>
@yuchenwang3

Copy link
Copy Markdown
Contributor Author

@rwightman Could you take a look at this fix for #8669? It preserves the source shards through shuffle and interleaves only the shards assigned to each node and worker. The regression suite exercises the reported 19-shard case with four real DataLoader workers, including complete and unique row coverage. Full multi-host training has not been tested.

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

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.

@lhoestq lhoestq left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing ! lgtm !

@lhoestq
lhoestq merged commit 2989571 into huggingface:main Oct 5, 2026
11 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IterableDataset.shuffle() in 5.0 reports num_shards = min(files per source), leaving DataLoader workers and distributed nodes without data

3 participants