diff --git a/grain/_src/python/dataset/transformations/shuffle.py b/grain/_src/python/dataset/transformations/shuffle.py index ed7f041bc..8eec37907 100644 --- a/grain/_src/python/dataset/transformations/shuffle.py +++ b/grain/_src/python/dataset/transformations/shuffle.py @@ -141,15 +141,30 @@ def __str__(self) -> str: def _shuffled_index(self, index: int) -> int: index = int(index) - window_index, index_in_window = divmod(index, self._window_size) - seed = self._seed + window_index + parent_length = len(self._parent) + if parent_length == 0: + # Empty parent: let the parent raise the out-of-range error as before. + return index + # Windows are aligned within each epoch. When the parent length is not a + # multiple of `window_size`, the last window of each epoch is shorter. + # Permuting it with `max_index=self._window_size - 1` can map an index + # past the end of the epoch, which the parent then wraps into an earlier + # window, dropping and duplicating elements. Use the actual window length. + epoch, index_in_epoch = divmod(index, parent_length) + num_windows = -(-parent_length // self._window_size) + window_index, index_in_window = divmod(index_in_epoch, self._window_size) + window_start = window_index * self._window_size + window_length = min(self._window_size, parent_length - window_start) + # For epoch-aligned windows (and all windows when parent_length is a + # multiple of `window_size`) this seed is identical to the old one. + seed = self._seed + epoch * num_windows + window_index shuffled_index_in_window = index_shuffle.index_shuffle( index_in_window, - max_index=self._window_size - 1, + max_index=window_length - 1, seed=seed, rounds=4, ) - return shuffled_index_in_window + window_index * self._window_size + return shuffled_index_in_window + window_start + epoch * parent_length def __getitem__(self, index): if isinstance(index, slice): diff --git a/grain/_src/python/dataset/transformations/shuffle_test.py b/grain/_src/python/dataset/transformations/shuffle_test.py index 6ba1ef3a0..1f8158dcf 100644 --- a/grain/_src/python/dataset/transformations/shuffle_test.py +++ b/grain/_src/python/dataset/transformations/shuffle_test.py @@ -214,6 +214,51 @@ def test_iter(self): for i in range(0, 400, 10): self.assertBetween(elements[i], i, i + (window_size - 1)) + def test_partial_last_window(self): + # When the parent length is not a multiple of `window_size`, the final + # window of each epoch is shorter. Shuffling it with + # `max_index=window_size - 1` mapped indices past the end of the epoch, + # which wrapped into earlier windows and dropped/duplicated elements. + for parent_length, window_size, seed in ( + (12, 5, 42), + (13, 5, 42), + (7, 4, 7), + (20, 6, 1), + ): + with self.subTest( + parent_length=parent_length, window_size=window_size, seed=seed + ): + parent = list(range(parent_length)) + ds = shuffle.WindowShuffleMapDataset( + dataset.MapDataset.range(parent_length), + window_size=window_size, + seed=seed, + ) + shuffled = [ds[i] for i in range(parent_length)] + self.assertCountEqual(shuffled, parent) + for start in range(0, parent_length, window_size): + self.assertCountEqual( + shuffled[start : start + window_size], + parent[start : start + window_size], + ) + + def test_partial_last_window_matches_iter_dataset(self): + # With `reverse=False`, `WindowShuffleIterDataset` is documented to + # produce the same sequence as `WindowShuffleMapDataset`. + parent_length, window_size, seed = 12, 5, 42 + map_ds = shuffle.WindowShuffleMapDataset( + dataset.MapDataset.range(parent_length), + window_size=window_size, + seed=seed, + ) + iter_ds = shuffle.WindowShuffleIterDataset( + dataset.MapDataset.range(parent_length).to_iter_dataset(), + window_size=window_size, + seed=seed, + reverse=False, + ) + self.assertEqual(list(map_ds), list(iter_ds)) + def test_example_docstring(self): window_size = 3 parent_ds = dataset.MapDataset.range(12)