Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add split argument to Generator #7015

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

piercus
Copy link

@piercus piercus commented Jul 1, 2024

Actual

When creating a multi-split dataset using generators like

datasets.DatasetDict({
  "val": datasets.Dataset.from_generator(
      generator=generator_val,
      features=features
  ),
  "test": datasets.Dataset.from_generator(
      generator=generator_test,
      features=features,
  )
})

It displays (for both test and val)

Generating train split

Expected

I would like to be able to improve this behavior by doing

datasets.DatasetDict({
  "val": datasets.Dataset.from_generator(
      generator=generator_val,
      features=features,
      split="val"
  ),
  "test": datasets.Dataset.from_generator(
      generator=generator_test,
      features=features,
      split="test"
  )
})

It would display

Generating val split

and

Generating test split

Proposal

Current PR is adding an explicit split argument and replace the implicit "train" split in the following classes/function :

  • Generator
  • from_generator
  • AbstractDatasetInputStream
  • GeneratorDatasetInputStream

Please share your feedbacks

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Member

@albertvillanova albertvillanova left a comment

Choose a reason for hiding this comment

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

Thanks for your proposed contribution @piercus.

This is a nice one!

Some comments below. Basically, I would propose to define the split parameter just as an attribute of GeneratorConfig instead of Generator, GeneratorDatasetInputStream, AbstractDatasetInputStream and SqlDatasetReader.

src/datasets/arrow_dataset.py Outdated Show resolved Hide resolved
@@ -1088,6 +1089,8 @@ def from_generator(
Number of processes when downloading and generating the dataset locally.
This is helpful if the dataset is made of multiple files. Multiprocessing is disabled by default.
If `num_proc` is greater than one, then all list values in `gen_kwargs` must be the same length. These values will be split between calls to the generator. The number of shards will be the minimum of the shortest list in `gen_kwargs` and `num_proc`.
split (`str`, defaults to `"train"`):
Split name to be assigned to the dataset.
Copy link
Member

Choose a reason for hiding this comment

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

This docstring should go below <Added version="2.7.0"/>, because the version added tag corresponds to the num_proc parameter above split.

I would suggest to align its type with the rest of the code as: ([`NamedSplit`], defaults to `Split.TRAIN`).

I would also add a specific version added tag for the split parameter: . We may eventually change this depending on the next release.

Copy link
Author

Choose a reason for hiding this comment

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

just used <Added version="2.21.0"/>, please cross-check

src/datasets/io/abc.py Outdated Show resolved Hide resolved
src/datasets/io/abc.py Outdated Show resolved Hide resolved
src/datasets/io/generator.py Outdated Show resolved Hide resolved
src/datasets/iterable_dataset.py Outdated Show resolved Hide resolved
@@ -2074,7 +2075,8 @@ def from_generator(
Keyword arguments to be passed to the `generator` callable.
You can define a sharded iterable dataset by passing the list of shards in `gen_kwargs`.
This can be used to improve shuffling and when iterating over the dataset with multiple workers.

split(`str`, default="train"):
Split name to be assigned to the dataset.
Copy link
Member

Choose a reason for hiding this comment

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

Same comments as before.

Copy link
Author

Choose a reason for hiding this comment

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

I also added <Added version="2.21.0"/> please cross-check

src/datasets/packaged_modules/generator/generator.py Outdated Show resolved Hide resolved
src/datasets/packaged_modules/generator/generator.py Outdated Show resolved Hide resolved
dataset = Dataset.from_generator(data_generator, features=features, cache_dir=cache_dir)
_check_generator_dataset(dataset, expected_features)
dataset = Dataset.from_generator(data_generator, features=features, cache_dir=cache_dir, split=split)
_check_generator_dataset(dataset, expected_features, split)

Copy link
Member

Choose a reason for hiding this comment

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

I would add a specific test_dataset_from_generator_split with a parametrized split values, such as not passing any value, passing NamedSplit("train"), passing literal "train", passing other NamedSplit, etc.

Copy link
Author

Choose a reason for hiding this comment

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

test_dataset_from_generator_split added, still i have impacted _check_generator_dataset to share the same generic check everywhere

@piercus
Copy link
Author

piercus commented Jul 10, 2024

@albertvillanova thanks for the review, please take a look

Copy link
Member

@albertvillanova albertvillanova left a comment

Choose a reason for hiding this comment

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

Thanks! Good work.

Just a fix of the non-passing test and a nit.

src/datasets/iterable_dataset.py Outdated Show resolved Hide resolved
tests/test_arrow_dataset.py Outdated Show resolved Hide resolved
Co-authored-by: Albert Villanova del Moral <[email protected]>
@piercus
Copy link
Author

piercus commented Jul 11, 2024

@albertvillanova please take a look

Copy link
Member

@albertvillanova albertvillanova left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution!

Note the CI action to generate the docs is failing due to an unrelated issue: https://github.com/huggingface/datasets/actions/runs/9887484572/job/27309892176?pr=7015

Therefore, if we do not want to break the generation of docs, this other PR should be merged before yours:

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.

from_generator does not allow to specify the split name
3 participants