datasets icon indicating copy to clipboard operation
datasets copied to clipboard

Batched map / Dataset.batch with num_proc>1 splits batches at shard boundaries and drop_last_batch loses rows

Open udsy19 opened this issue 1 month ago • 3 comments

Describe the bug

With num_proc > 1, batched Dataset.map (and Dataset.batch, which goes through it) splits the dataset into num_proc equal row chunks via self.shard(num_shards, index=rank, contiguous=True), ignoring batch_size. Each chunk ends with its own short batch, so the function sees wrongly-sized batches in the middle of the dataset, and with drop_last_batch=True every chunk drops its tail, silently losing rows that a single process keeps.

Steps to reproduce the bug

from datasets import Dataset

ds = Dataset.from_dict({"x": list(range(10))})
[b["x"] for b in ds.batch(3)]                                  # [[0,1,2],[3,4,5],[6,7,8],[9]]
[b["x"] for b in ds.batch(3, num_proc=2)]                      # [[0,1,2],[3,4],[5,6,7],[8,9]]
[b["x"] for b in ds.batch(3, num_proc=2, drop_last_batch=True)]  # [[0,1,2],[5,6,7]]  rows 3,4,8,9 lost
len(ds.map(lambda b: b, batched=True, batch_size=3, drop_last_batch=True, num_proc=2))  # 6, single-process gives 9

Expected behavior

The same batches as with num_proc=None: num_proc should only parallelize, not change which rows form a batch or which are dropped.

Proposed fix

When batched and there are at least as many batches as processes, shard on batch boundaries (select(range(start, end)) with bounds at multiples of batch_size, distributing whole batches across processes) instead of equal row chunks; with drop_last_batch, cap num_proc at the number of full batches and only drop the dataset's final incomplete batch (the progress-bar total currently mirrors the per-shard drops and would be simplified accordingly). Dataset.batch caps num_proc at the number of batches.

Trade-offs worth your call:

  • When a plain batched map has fewer batches than processes (e.g. filter's internal batch_size=1000 on a small dataset), I kept the current row split rather than capping num_proc, so small datasets still get parallelism; that case keeps per-shard partial batches.
  • Shard sizes can now differ by up to one batch (e.g. 2000/500 instead of 1250/1250 rows for 2500 rows, batch_size=1000, 2 procs).

I have this implemented with tests (ds.batch output equal to single-process for num_proc 2/3/8 with and without drop_last_batch; every batch full except the last in batched map), passing tests/test_arrow_dataset.py. Happy to open the PR if this approach looks right: https://github.com/huggingface/datasets/compare/main...udsy19:fix/batched-map-num-proc-shard-boundaries

Environment info

  • datasets main (62e0b421)
  • Python 3.13, macOS
  • pyarrow 26.0.0

udsy19 avatar Oct 31 '26 20:10 udsy19

#self-assign

IlyaKhalafi avatar Nov 01 '26 20:11 IlyaKhalafi

@IlyaKhalafi I already have this implemented with tests (https://github.com/huggingface/datasets/compare/main...udsy19:fix/batched-map-num-proc-shard-boundaries), as mentioned in the issue. Happy to open it as a PR so we don't duplicate work, unless you've already started on a different approach. Let me know which you'd prefer.

udsy19 avatar Nov 03 '26 19:11 udsy19

Please go ahead with your PR. I haven't started a patch. Could a maintainer unassign me?

IlyaKhalafi avatar Nov 03 '26 19:11 IlyaKhalafi