Skip to content

Commit 2c45eab

Browse files
pkooijPepijn
andauthored
Fix batch(by_column=...) crashing after shard/shuffle/split (#8259)
MappedExamplesIterable.shuffle_data_sources, shard_data_sources and reshard_data_sources did not forward is_batch_accumulate_arrow_table_function, so any re-created iterable (DataLoader workers, .shard(), .shuffle(), split_dataset_by_node) stopped injecting the tables_accumulator/length kwargs and the accumulate function crashed: TypeError: _batch_accumulate_arrow_table_by_columns() missing 2 required positional arguments: 'tables_accumulator' and 'length' Forward the flag in all three methods and add a regression test covering shard() and shuffle() on a by_column-batched stream. Co-authored-by: Pepijn <pepijn1999kooijmans@gmail.com>
1 parent 9afab5c commit 2c45eab

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

src/datasets/iterable_dataset.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,7 @@ def shuffle_data_sources(self, generator: np.random.Generator) -> "MappedExample
17231723
formatting=self.formatting,
17241724
features=self.features,
17251725
max_num_running_async_map_functions_in_parallel=self.max_num_running_async_map_functions_in_parallel,
1726+
is_batch_accumulate_arrow_table_function=self.is_batch_accumulate_arrow_table_function,
17261727
)
17271728

17281729
def shard_data_sources(self, num_shards: int, index: int, contiguous=True) -> "MappedExamplesIterable":
@@ -1740,6 +1741,7 @@ def shard_data_sources(self, num_shards: int, index: int, contiguous=True) -> "M
17401741
formatting=self.formatting,
17411742
features=self.features,
17421743
max_num_running_async_map_functions_in_parallel=self.max_num_running_async_map_functions_in_parallel,
1744+
is_batch_accumulate_arrow_table_function=self.is_batch_accumulate_arrow_table_function,
17431745
)
17441746

17451747
def reshard_data_sources(self) -> "MappedExamplesIterable":
@@ -1756,6 +1758,7 @@ def reshard_data_sources(self) -> "MappedExamplesIterable":
17561758
formatting=self.formatting,
17571759
features=self.features,
17581760
max_num_running_async_map_functions_in_parallel=self.max_num_running_async_map_functions_in_parallel,
1761+
is_batch_accumulate_arrow_table_function=self.is_batch_accumulate_arrow_table_function,
17591762
)
17601763

17611764
@property

tests/test_iterable_dataset.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2897,6 +2897,25 @@ def test_iterable_dataset_batch(num_shards: int):
28972897
assert batch["text"] == [f"Text {3 * i}", f"Text {3 * i + 1}", f"Text {3 * i + 2}"]
28982898

28992899

2900+
def test_iterable_dataset_batch_by_column_survives_resharding():
2901+
# Re-creating the iterable (shard / shuffle / split_by_node, e.g. inside torch DataLoader
2902+
# workers) must keep accumulating whole groups instead of crashing with a missing
2903+
# tables_accumulator argument (regression test).
2904+
data = {
2905+
"id": list(range(10)),
2906+
"category": ["A"] * 5 + ["B"] * 5,
2907+
}
2908+
ds = IterableDataset.from_dict(data, num_shards=2)
2909+
batched_ds = ds.batch(by_column="category")
2910+
2911+
sharded = [batch["category"][0] for i in range(2) for batch in batched_ds.shard(num_shards=2, index=i)]
2912+
assert sorted(sharded) == ["A", "B"]
2913+
2914+
shuffled = list(batched_ds.shuffle(seed=0, buffer_size=2))
2915+
assert sorted(batch["category"][0] for batch in shuffled) == ["A", "B"]
2916+
assert all(len(set(batch["category"])) == 1 for batch in shuffled)
2917+
2918+
29002919
@pytest.mark.parametrize("num_shards", [1, 2, 3, 7, 10])
29012920
def test_iterable_dataset_batch_by_column(num_shards: int):
29022921
# Create a Dataset with a column to group by

0 commit comments

Comments
 (0)