Skip to content

Fix IterableDataset.add_column error for short columns - #9305

Open
mertkayacs wants to merge 2 commits into
huggingface:mainfrom
mertkayacs:fix/iterable-add-column-length
Open

mertkayacs wants to merge 2 commits into
huggingface:mainfrom
mertkayacs:fix/iterable-add-column-length

Conversation

@mertkayacs

Copy link
Copy Markdown

I found this while comparing IterableDataset.add_column with Dataset.add_column. The map-style API raises ValueError for a row-count mismatch. With an iterable dataset, I reproduced an IndexError during iteration when the new column was shorter than the dataset.

from datasets import IterableDataset

dataset = IterableDataset.from_dict({"a": [1, 2, 3]})
list(dataset.add_column("b", [10, 20]))

I fixed the short-column case by checking the index before reading the value and raising a ValueError with the column name and length. Detecting extra values in a long column requires a dataset-wide exhaustion signal because a worker can exhaust its own shard while other shards still have rows.

I verified the list and NumPy regression tests before and after the fix, ran the CPU unit tests in tests/test_iterable_dataset.py, and ran make quality.

This touches the same lines in add_column_fn as #8981. Whichever lands first, I will rebase the other side if needed.

@atishay-kasliwal atishay-kasliwal left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A short column now raises the new ValueError for from_dict and for a 3-shard from_generator dataset; master raised IndexError: list index out of range. An exact-length column gives the same rows as before across shards, and a longer one is still silently truncated, as the description says. Side note, same on master: add_column("b", [10, 20]).take(2) on a 3-row dataset also raises, so the third row is mapped before take stops.

@mertkayacs

Copy link
Copy Markdown
Author

Thanks for running these. The take(2) case fails the same way on master, so I kept it out of this PR and left it focused on replacing the IndexError.

@mertkayacs

Copy link
Copy Markdown
Author

Thanks for checking this. I changed TakeExamplesIterable.__iter__ so it stops without pulling one more row from upstream, which means add_column("b", [10, 20]).take(2) on a 3-row dataset now returns the first two rows. The regression test covers that case for both list and NumPy columns, and full iteration still raises once the third row is reached.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants