Skip to content

Save the shuffle buffer content in the state dict so resuming is exact - #8408

Open
thejamesgore wants to merge 1 commit into
huggingface:mainfrom
thejamesgore:fix/shuffle-buffer-checkpoint-resume
Open

thejamesgore wants to merge 1 commit into
huggingface:mainfrom
thejamesgore:fix/shuffle-buffer-checkpoint-resume

Conversation

@thejamesgore

Copy link
Copy Markdown

Overview

There are two problems when resuming a shuffled iterable dataset. The known one from the warning and the discussion in #7901 is that the shuffle buffer content isn't saved in the state dict so you lose roughly buffer_size examples every resume.

The second is you can get an example twice. The RebatchedArrowExamplesIterable that wraps the pipeline resumes by rewinding to a previous_state snapshot and skipping chunks it already delviered which assumes the inner stream is deterministic from its state. The shuffle buffer isn't as it restarts empty with a fresh rng so the rewound rows get reshuffled differently and an already yielded row can come out again. I instrumented it at the issue's k=20: the resume re-reads rows 58 and 158. 158 was still in the buffer but 58 had already been yielded so that's the {58: 2} in the issue. Silent duplicates in a training run that resumes often is worse than the documented loss.

## Fix

Save the buffer content, the rng pick count and the end-of-data shuffle flag in the state dict. Resume restores the buffer and fast-forwards the rng, so it carries on exactly where it left off and both problems go away.

Two costs dealt with: state gets captured once per yielded example, so state_dict() keeps the buffer as a tuple instead of deep-copying it. And the buffered arrow tables are usually zero-copy slices of a whole source table, a buffer_size=1000 checkpoint over 50k rows came out at 1.6 GiB. Slices get rewritten as stand-alone tables before entering the buffer, which brings it down to ~400 KiB.

  • The repro from ShuffledDataSourcesArrowExamplesIterable cannot properly resume from checkpoint #7901: no duplicates, nothing missing. Sweeping the checkpoint over k=1..100 for buffer sizes 4/8/16/32: zero duplicates (was 22/9/3/0).
  • Resumed iteration is identical to an uninterrupted run across shard counts, buffer sizes and checkpoint positions, including mid-drain and resume/checkpoint/resume chains.
  • Removed the BufferShuffledExamplesIterable exemption in test_no_iter_arrow so the standard resume assertion covers it, plus new resume tests. All of them fail without the fix.
  • 501 passed in test_iterable_dataset.py, ruff clean, iteration order unchanged from main for a fixed seed.
  • 50k examples with buffer_size=1000: ~2% overhead with state tracking, checkpoint ~400 KiB.

Resolves #7901

@MohammadSadeghSalehi

Copy link
Copy Markdown

I reported the duplicate this fixes over in #7901, so I checked this branch against the full sweep I had rather than just the one case.

Twenty-seven configurations, varying seed, checkpoint offset, buffer size and shard count, on pr8408 at bfcf1eac5:

 seed    k  buf shards |  dupes  missing  exact
   17    5   16      4 |      0        0   True
   17   20   16      4 |      0        0   True
   17   20    8      4 |      0        0   True
   17   20   16      2 |      0        0   True
   ...
27 configurations: 0 with duplicates, 0 with missing rows,
0 not exactly equal to the uninterrupted stream

The third column is the strict one: the resumed stream is compared element by element against the uninterrupted stream, so this is exact resume rather than merely the absence of a repeat. On 5.0.1 the same sweep gives duplicates in 3 of 27 and loses buffer_size rows in all 27.

The backward-compatible path also behaves. Stripping the new keys out of a checkpoint, which is what a state dict written by an older version looks like, reproduces the old behaviour precisely rather than failing in some new way:

Loading a state dict of a shuffle buffer of a dataset without the buffer content....
loaded a pre-fix state dict without raising: 164 rows after resume
duplicates: 1   missing: 17

That is the same 1 duplicate and 17 missing rows as the original report, so old checkpoints stay loadable and stay exactly as lossy as they were, which seems like the right call.

One note for anyone reproducing this: the new keys are nested under
examples_iterable.examples_iterable.* and examples_iterable.previous_state.*, not at the top level of the state dict. My first attempt at the compatibility check stripped only top-level keys, which silently tested nothing and looked like a pass.

Your account of the mechanism also matches what I saw and is more precise than mine. I had stopped at BufferShuffledExamplesIterable holding no state of its own and reporting the source position; the part I missed is that the rewind-and-skip in the rebatching layer is only correct while the inner stream replays deterministically, which the buffer breaks by coming back empty with a fresh rng. That explains why it reproduces at some checkpoint offsets and not others, which I could not account for.

@aaronanish-t

Copy link
Copy Markdown

hey @thejamesgore, ran into the duplicate on resume bug while testing shuffle checkpoints and found this pr, so wanted to share where it's at now

it still happens on the latest release and on main. 40 row dataset, shuffle(seed=1, buffer_size=8), checkpoint after every possible k:

version shards cut points that re-yield an already seen example
5.1.0 1 20 / 39
5.1.0 4 20 / 39
main (ef61228) 1 20 / 39
main (ef61228) 4 20 / 39

eg with 1 shard, resuming at k=7 gives back [10, 11] again even though both already came out before the checkpoint. with buffer_size=1 or a buffer bigger than the dataset resume is exact, so it's the buffer like u said

the thing is this branch doesn't merge anymore. #8407 (merged sep 7) changed BufferShuffledExamplesIterable to the "fill, shuffle, yield half, keep half" approach, and merging this into main conflicts in src/datasets/iterable_dataset.py and tests/test_iterable_dataset.py. i think the saved state (buffer + rng position) needs to be updated for the new algorithm too, not just rebased, since it uses one permutation per refill now instead of one pick per example

if u don't have time to pick it back up i'm happy to help with the rebase or the state part, just didn't want to open a competing pr without asking. can also share the script i used for the table if it helps

disclosure: used claude code to help with the investigation

@thejamesgore

thejamesgore commented Oct 23, 2026 via email

Copy link
Copy Markdown
Author

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.

ShuffledDataSourcesArrowExamplesIterable cannot properly resume from checkpoint

3 participants