Fix Dataset.map(batched=True, drop_last_batch=True) returning unchanged input when dataset is smaller than batch_size - #8456
Open
mayuriphad wants to merge 1 commit into
Conversation
…ed input when dataset is smaller than batch_size When drop_last_batch=True and the dataset has fewer rows than batch_size, every batch is incomplete and dropped, so the mapped function is never invoked. update_data therefore stays False and _map_single fell through to returning the original, unprocessed shard unchanged instead of an empty dataset, disagreeing with IterableDataset.batch and Dataset.iter which both correctly yield zero rows in this situation. Fixes huggingface#8386
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #8386
Bug
When
drop_last_batch=Trueand the dataset has fewer rows thanbatch_size,Dataset.map(batched=True, ...)was returning the input dataset unchanged instead of an empty dataset. Every batch is incomplete in this situation, so the mapped function is never invoked andupdate_datastays falsy;_map_singlethen fell through toyield rank, True, shard, silently passing the original, unprocessed rows straight through.This disagreed with
IterableDataset.batch()andDataset.iter(), which both correctly yield zero rows when every batch is incomplete, and it also affectedDataset.batch()since it is implemented on top ofmap(batched=True).Fix
In
Dataset._map_single, whenbatched=True,drop_last_batch=True, and the shard has fewer rows thanbatch_size(so the function was never called andupdate_datais still falsy), return an empty dataset (shard.select([])) carrying the input schema instead of the untouchedshard. This matches option (1) discussed in the issue, and aligns the eagermap/batchbehavior with the streamingIterableDataset.batch/Dataset.iterbehavior.Test
Added
test_map_batched_drop_last_batch_smaller_than_batch_sizeintests/test_arrow_dataset.py, which fails onmain(AssertionError: 30 != 0) and passes with this fix.