Skip to content

Refill the shuffle buffer when resuming an IterableDataset - #9169

Open
lhoestq wants to merge 1 commit into
huggingface:mainfrom
lhoestq:shuffle-buffer-warmup-on-resume
Open

lhoestq wants to merge 1 commit into
huggingface:mainfrom
lhoestq:shuffle-buffer-warmup-on-resume

Conversation

@lhoestq

@lhoestq lhoestq commented Nov 25, 2026

Copy link
Copy Markdown
Member

The content of the shuffle buffers was not saved in the state dict, so the examples that were in the buffers were lost when resuming from a checkpoint, and the buffers were simply refilled with the next examples. This makes the iteration lose up to buffer_size examples per checkpoint and take distance from a single uninterrupted iteration.

Instead, past states of the underlying iterable are saved in the state dict (a few ints, not examples), and the buffers are refilled when resuming by reading config.SHUFFLE_BUFFER_WARMUP_NUM_STD * buffer_size examples again and dropping the ones they yield. Since an example stays in the buffer for buffer_size examples on average, reading five of them is enough to get a buffer as well shuffled as it would have been without interrupting the iteration: only the examples that were in the buffer when the checkpoint was saved are lost, and they are replaced by examples that are read later on.

This enables almost perfect resuming, with <1% of loss or repeats of examples

The content of the shuffle buffers was not saved in the state dict, so the examples that were in the buffers were
lost when resuming from a checkpoint, and the buffers were simply refilled with the next examples. With frequent
checkpoints, this makes the iteration lose up to `buffer_size` examples per checkpoint and take distance from a
single uninterrupted iteration.

Instead, past states of the underlying iterable are saved in the state dict (a few ints, not examples), and the
buffers are refilled when resuming by reading `config.SHUFFLE_BUFFER_WARMUP_NUM_STD * buffer_size` examples again and
dropping the ones they yield. Since an example stays in the buffer for `buffer_size` examples on average, reading
five of them is enough to get a buffer as well shuffled as it would have been without interrupting the iteration:
only the examples that were in the buffer when the checkpoint was saved are lost, and they are replaced by examples
that are read later on.
@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.

Pages changed in this PR:

@JonnyFaeth

Copy link
Copy Markdown

Checked cb0d9d1 through the public Dataset -> IterableDataset.shuffle -> state_dict/load_state_dict path, including JSON serialization and restoration into a fresh dataset. For short streams entirely within the replay window, the resumed suffix matches uninterrupted iteration exactly, in default and Arrow formatting. Checkpoints include the last yielded row, so restoration must also remain exhausted there.

6 new cases and 26 existing buffer_shuffled tests pass. Forcing is_exact_replay=False makes the two 23-row cases fail on order (four smaller cases still pass), exercising the replay distinction through the public wrapper. No new bug found or general exact-resume claim for longer streams.

Suggested standalone tests/test_shuffle_public_resume.py:

import json

import pytest

from datasets import Dataset


@pytest.mark.parametrize("n", [1, 7, 23])
@pytest.mark.parametrize("format_type", [None, "arrow"])
def test_public_shuffle_short_stream_checkpoint_roundtrip(n, format_type):
    def make_dataset():
        ds = Dataset.from_dict({"id": list(range(n))}).to_iterable_dataset()
        return ds.shuffle(seed=42, buffer_size=10).with_format(format_type)

    def ids(rows):
        return [row["id"].to_pylist()[0] if format_type == "arrow" else row["id"] for row in rows]

    expected = ids(make_dataset())
    for checkpoint in sorted({1, max(1, n // 2), n}):
        dataset = make_dataset()
        iterator = iter(dataset)
        prefix = ids([next(iterator) for _ in range(checkpoint)])
        state = json.loads(json.dumps(dataset.state_dict()))
        restored = make_dataset()
        restored.load_state_dict(state)
        assert prefix + ids(restored) == expected

Windows/Python 3.12/PyArrow 25, CPU, in-memory data; Ruff check/format pass. No multi-worker/distributed training or full-suite claim. Mutation restored after testing.

AI disclosure: Codex wrote and ran this regression under @JonnyFaeth's standing authorization; Jonathan has not personally reviewed this run.

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.

3 participants