Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/datasets/arrow_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4100,6 +4100,7 @@ def batch(
<Added version="4.9.0"/>
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`):
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/datasets/iterable_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4706,6 +4706,7 @@ def batch(
<Added version="4.9.0"/>
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:

Expand Down Expand Up @@ -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,
)
Expand Down
28 changes: 28 additions & 0 deletions tests/test_arrow_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]})
Expand Down