Skip to content

Fix WindowShuffleMapDataset dropping and duplicating elements on partial final windows - #1495

Open
feiiiiii5 wants to merge 1 commit into
google:mainfrom
feiiiiii5:fix/1129-grain-r1
Open

feiiiiii5 wants to merge 1 commit into
google:mainfrom
feiiiiii5:fix/1129-grain-r1

Conversation

@feiiiiii5

Copy link
Copy Markdown

Problem

grain.experimental.WindowShuffleMapDataset silently corrupts data when len(parent) is not a multiple of window_size: the short final window of each epoch is permuted over a full-size window, so an index can land past the epoch end and wrap into an earlier window. Elements are lost and duplicated.

ds = grain.experimental.WindowShuffleMapDataset(
    grain.MapDataset.range(12), window_size=5, seed=42)
list(ds)  # [4,1,2,3,0,7,5,8,6,9,2,11] — 10 missing, 2 duplicated

The sibling WindowShuffleIterDataset permutes with the actual window length and is unaffected, so the documented Map/Iter equivalence for reverse=False was also violated.

Root cause

_shuffled_index() used global window boundaries and max_index=self._window_size - 1 even for a shorter final window.

Fix

Align windows within each epoch, clamp the permutation domain to the actual window length, and offset the result by the epoch. The seed is unchanged for all epoch-aligned windows, so behavior is bit-identical when len(parent) is divisible by window_size.

Verification

  • New regression tests fail on the base source with 5 assertion failures (dropped/duplicated elements, Map vs Iter mismatch) and pass with the fix.
  • shuffle_test.py: 49/49 pass. slice_repeat_shuffle_test.py: 9/9 pass.
  • Confirmed multi-epoch multiset/window invariants, _getitems consistency, negative and NumPy indices.
  • py_compile and whitespace checks clean.

Not verified: Bazel/CI, other platforms, and free-threaded Python. Pre-existing negative-index equivalence behavior was not changed.

AI-assisted development. Human review requested before merge.

This branch has not been deployed

No deployments
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.

1 participant