diff --git a/src/datasets/arrow_dataset.py b/src/datasets/arrow_dataset.py index dcd559f292f..98ceec1a66a 100644 --- a/src/datasets/arrow_dataset.py +++ b/src/datasets/arrow_dataset.py @@ -4100,6 +4100,7 @@ def batch( drop_last_batch (`bool`, defaults to `False`): Whether to drop the last incomplete batch. + Batches made with `by_column` are always complete groups, so no batch is dropped in that case. num_proc (`int`, *optional*, defaults to `None`): Max number of processes when generating cache. Already cached shards are loaded sequentially. new_fingerprint (`str`, *optional*, defaults to `None`): @@ -4130,6 +4131,8 @@ def batch( _batch_accumulate_arrow_table_by_columns, columns=columns, tables_accumulator=[], length=len(self) ) with_indices = True + # groups are never incomplete, and dropping the last input batch would drop or truncate groups + drop_last_batch = False else: _batch_fn = _batch_arrow_table with_indices = False diff --git a/src/datasets/iterable_dataset.py b/src/datasets/iterable_dataset.py index 7f06d292b0c..de5b6ac5c30 100644 --- a/src/datasets/iterable_dataset.py +++ b/src/datasets/iterable_dataset.py @@ -4706,6 +4706,7 @@ def batch( drop_last_batch (`bool`, defaults to `False`): Whether to drop the last incomplete batch. + Batches made with `by_column` are always complete groups, so no batch is dropped in that case. Example: @@ -4738,7 +4739,6 @@ def batch( with_indices=True, batched=True, batch_size=batch_size or config.DEFAULT_MAX_BATCH_SIZE, - drop_last_batch=drop_last_batch, features=features, is_batch_accumulate_arrow_table_function=True, ) diff --git a/tests/test_arrow_dataset.py b/tests/test_arrow_dataset.py index e605d24f9b6..9a67fecf08a 100644 --- a/tests/test_arrow_dataset.py +++ b/tests/test_arrow_dataset.py @@ -5255,6 +5255,34 @@ def batch_ids(ds, by_column): assert batch_ids(ds, "category") == [[0, 1, 2]] +@pytest.mark.parametrize("streaming", [False, True]) +def test_dataset_batch_by_column_with_drop_last_batch(streaming): + def batch_ids(ds): + if streaming: + ds = ds.to_iterable_dataset() + return [batch["id"] for batch in ds.batch(by_column="category", batch_size=4, drop_last_batch=True)] + + # groups are always complete, so drop_last_batch must not drop or truncate any of them + ds = Dataset.from_dict({"id": list(range(10)), "category": [0, 0, 0, 1, 1, 2, 2, 2, 3, 3]}) + assert batch_ids(ds) == [[0, 1, 2], [3, 4], [5, 6, 7], [8, 9]] + + # a group straddling the last input batch + ds = Dataset.from_dict({"id": list(range(10)), "category": [0, 0, 0, 1, 1, 2, 2, 2, 2, 3]}) + assert batch_ids(ds) == [[0, 1, 2], [3, 4], [5, 6, 7, 8], [9]] + + # a single group larger than batch_size + ds = Dataset.from_dict({"id": list(range(6)), "category": [0] * 6}) + assert batch_ids(ds) == [[0, 1, 2, 3, 4, 5]] + + # a length that is an exact multiple of batch_size + ds = Dataset.from_dict({"id": list(range(8)), "category": [0, 0, 1, 1, 2, 2, 3, 3]}) + assert batch_ids(ds) == [[0, 1], [2, 3], [4, 5], [6, 7]] + + # a dataset shorter than batch_size, read through an indices mapping + ds = Dataset.from_dict({"id": list(range(6)), "category": [9, 0, 9, 0, 9, 1]}).select([5, 1, 3]) + assert batch_ids(ds) == [[5], [1, 3]] + + @pytest.mark.parametrize("format_type", ["pyarrow", "pandas"]) def test_dataset_batch_with_table_format(format_type): ds = Dataset.from_dict({"a": [1, 2, 3, 4]})