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

Fallback on dataset script if user wants to load default config #6498

Merged
merged 3 commits into from
Dec 15, 2023

Conversation

lhoestq
Copy link
Member

@lhoestq lhoestq commented Dec 14, 2023

Right now this code is failing on main:

load_dataset("openbookqa")

This is because it tries to load the dataset from the Parquet export but the dataset has multiple configurations and the Parquet export doesn't know which one is the default one.

I fixed this by simply falling back on using the dataset script (which tells the user to pass trust_remote_code=True):

load_dataset("openbookqa", trust_remote_code=True)

Note that if the user happened to specify a config name I don't fall back on the script since we can use the Parquet export in this case (no need to know which config is the default)

load_dataset("openbookqa", "main")

@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 the fix.

Some comments below.

@@ -1621,6 +1621,7 @@ def dataset_module_factory(
data_dir: Optional[str] = None,
data_files: Optional[Union[Dict, List, str, DataFilesDict]] = None,
trust_remote_code: Optional[bool] = None,
_ignore_default_config=False,
Copy link
Member

Choose a reason for hiding this comment

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

Why the leading underscore?

Copy link
Member

@albertvillanova albertvillanova Dec 15, 2023

Choose a reason for hiding this comment

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

I would suggest naming it differently, to make clear that config_name was passed or not: if config_name is passed, we don't even care whether we should ignore or not that DEFAULT_CONFIG_NAME is set.

Maybe no_config_name?

Copy link
Member Author

Choose a reason for hiding this comment

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

The underscore is to signify this is an internal parameter (not supposed to be used by users)

Copy link
Member Author

Choose a reason for hiding this comment

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

I think I like this name: if _require_default_config_name=True we require the builder class to have a default config name. If the user doesn't specify a config name we set it to True

Copy link
Member

Choose a reason for hiding this comment

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

The underscore is to signify this is an internal parameter (not supposed to be used by users)

I mean, it is the first time I see a "private" function parameter... Wondering if that makes sense...

Copy link
Member Author

@lhoestq lhoestq Dec 15, 2023

Choose a reason for hiding this comment

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

It's not a good practice :/ but it's simple and does the job

I hesitated to not have the feature to use the parquet export if the user does specify a config name explicitly just because I don't like doing "private" arguments

Copy link
Member

Choose a reason for hiding this comment

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

I see...

Comment on lines 1777 to 1780
with fs.open(f"datasets/{path}/{filename}", "r", revision=revision, encoding="utf-8") as f:
can_load_config_from_parquet_export = (
"DEFAULT_CONFIG_NAME" not in f.read() or _ignore_default_config
)
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be better to refactor this code and avoid the network call and file read if the variable "_ignore_default_config" is True.

Copy link
Member Author

Choose a reason for hiding this comment

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

done !

try:
return HubDatasetModuleFactoryWithParquetExport(
path, download_config=download_config, revision=dataset_info.sha
).get_module()
except _datasets_server.DatasetsServerError:
pass
# Otherwise we must use the dataset script if the user trusts it
trust_remote_code = resolve_trust_remote_code(trust_remote_code, path)
Copy link
Member

Choose a reason for hiding this comment

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

I guess this is no longer necessary?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's already called inside the HubDatasetModuleFactoryWithScript - no need to have it here

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.

I was just thinking: what if the user does not pass a config name and the dataset has only a config with a name different from "default"?

@lhoestq
Copy link
Member Author

lhoestq commented Dec 15, 2023

I was just thinking: what if the user does not pass a config name and the dataset has only a config with a name different from "default"?

You mean if there is a DEFAULT_CONFIG_NAME defined in the script but the dataset only has one configuration ? We can't easily get the number of configs without running the python code so I don't think we can support detect this case

@lhoestq
Copy link
Member Author

lhoestq commented Dec 15, 2023

Most datasets with a script don't define DEFAULT_CONFIG_NAME if there is only one configuration anyway.

So there is no issue e.g. for squad

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.

You mean if there is a DEFAULT_CONFIG_NAME defined in the script but the dataset only has one configuration ? We can't easily get the number of configs without running the python code so I don't think we can support detect this case

I was trying to mean the case where DEFAULT_CONFIG_NAME is None but there is only a single config in BUILDER_CONFIGS, with a name different from "default".

@lhoestq
Copy link
Member Author

lhoestq commented Dec 15, 2023

I was trying to mean the case where DEFAULT_CONFIG_NAME is None but there is only a single config in BUILDER_CONFIGS, with a name different from "default".

In this case we can detect if "DEFAULT_CONFIG_NAME" is not mentioned and use the Parquet export. If it is mentioned (and maybe it is set to None or to the single config) I consider that it may have multiple configs and fall back on using the script

@albertvillanova
Copy link
Member

... but the user does not pass the config name.

@lhoestq
Copy link
Member Author

lhoestq commented Dec 15, 2023

In this case we load the single configuration (this is how a DatasetBuilder works)

@lhoestq
Copy link
Member Author

lhoestq commented Dec 15, 2023

see

builder_config = self.BUILDER_CONFIGS[0]

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 the explanations!! 😅

@lhoestq lhoestq merged commit 30f6a2d into main Dec 15, 2023
12 checks passed
@lhoestq lhoestq deleted the default-config-fallback branch December 15, 2023 13:10
Copy link

Show benchmarks

PyArrow==8.0.0

Show updated benchmarks!

Benchmark: benchmark_array_xd.json

metric read_batch_formatted_as_numpy after write_array2d read_batch_formatted_as_numpy after write_flattened_sequence read_batch_formatted_as_numpy after write_nested_sequence read_batch_unformated after write_array2d read_batch_unformated after write_flattened_sequence read_batch_unformated after write_nested_sequence read_col_formatted_as_numpy after write_array2d read_col_formatted_as_numpy after write_flattened_sequence read_col_formatted_as_numpy after write_nested_sequence read_col_unformated after write_array2d read_col_unformated after write_flattened_sequence read_col_unformated after write_nested_sequence read_formatted_as_numpy after write_array2d read_formatted_as_numpy after write_flattened_sequence read_formatted_as_numpy after write_nested_sequence read_unformated after write_array2d read_unformated after write_flattened_sequence read_unformated after write_nested_sequence write_array2d write_flattened_sequence write_nested_sequence
new / old (diff) 0.005122 / 0.011353 (-0.006231) 0.003565 / 0.011008 (-0.007443) 0.062706 / 0.038508 (0.024198) 0.049314 / 0.023109 (0.026205) 0.247325 / 0.275898 (-0.028573) 0.269788 / 0.323480 (-0.053692) 0.003895 / 0.007986 (-0.004090) 0.002788 / 0.004328 (-0.001540) 0.048615 / 0.004250 (0.044365) 0.037591 / 0.037052 (0.000539) 0.253495 / 0.258489 (-0.004994) 0.281200 / 0.293841 (-0.012641) 0.027712 / 0.128546 (-0.100834) 0.010901 / 0.075646 (-0.064745) 0.205577 / 0.419271 (-0.213694) 0.035989 / 0.043533 (-0.007544) 0.252978 / 0.255139 (-0.002161) 0.268042 / 0.283200 (-0.015157) 0.017857 / 0.141683 (-0.123826) 1.096633 / 1.452155 (-0.355521) 1.147026 / 1.492716 (-0.345691)

Benchmark: benchmark_getitem_100B.json

metric get_batch_of_1024_random_rows get_batch_of_1024_rows get_first_row get_last_row
new / old (diff) 0.095609 / 0.018006 (0.077603) 0.311941 / 0.000490 (0.311451) 0.000211 / 0.000200 (0.000011) 0.000043 / 0.000054 (-0.000012)

Benchmark: benchmark_indices_mapping.json

metric select shard shuffle sort train_test_split
new / old (diff) 0.019042 / 0.037411 (-0.018369) 0.060549 / 0.014526 (0.046023) 0.074761 / 0.176557 (-0.101796) 0.121729 / 0.737135 (-0.615406) 0.075661 / 0.296338 (-0.220677)

Benchmark: benchmark_iterating.json

metric read 5000 read 50000 read_batch 50000 10 read_batch 50000 100 read_batch 50000 1000 read_formatted numpy 5000 read_formatted pandas 5000 read_formatted tensorflow 5000 read_formatted torch 5000 read_formatted_batch numpy 5000 10 read_formatted_batch numpy 5000 1000 shuffled read 5000 shuffled read 50000 shuffled read_batch 50000 10 shuffled read_batch 50000 100 shuffled read_batch 50000 1000 shuffled read_formatted numpy 5000 shuffled read_formatted_batch numpy 5000 10 shuffled read_formatted_batch numpy 5000 1000
new / old (diff) 0.284774 / 0.215209 (0.069565) 2.764576 / 2.077655 (0.686921) 1.489926 / 1.504120 (-0.014194) 1.387276 / 1.541195 (-0.153919) 1.400931 / 1.468490 (-0.067559) 0.555623 / 4.584777 (-4.029154) 2.409488 / 3.745712 (-1.336224) 2.781053 / 5.269862 (-2.488808) 1.750472 / 4.565676 (-2.815204) 0.062232 / 0.424275 (-0.362043) 0.004974 / 0.007607 (-0.002633) 0.336324 / 0.226044 (0.110280) 3.286619 / 2.268929 (1.017691) 1.825070 / 55.444624 (-53.619554) 1.537993 / 6.876477 (-5.338484) 1.586520 / 2.142072 (-0.555553) 0.640090 / 4.805227 (-4.165138) 0.117637 / 6.500664 (-6.383027) 0.042318 / 0.075469 (-0.033151)

Benchmark: benchmark_map_filter.json

metric filter map fast-tokenizer batched map identity map identity batched map no-op batched map no-op batched numpy map no-op batched pandas map no-op batched pytorch map no-op batched tensorflow
new / old (diff) 0.964051 / 1.841788 (-0.877736) 11.706259 / 8.074308 (3.631951) 10.752311 / 10.191392 (0.560919) 0.128117 / 0.680424 (-0.552307) 0.014001 / 0.534201 (-0.520200) 0.286255 / 0.579283 (-0.293028) 0.263810 / 0.434364 (-0.170554) 0.329347 / 0.540337 (-0.210991) 0.437349 / 1.386936 (-0.949587)
PyArrow==latest
Show updated benchmarks!

Benchmark: benchmark_array_xd.json

metric read_batch_formatted_as_numpy after write_array2d read_batch_formatted_as_numpy after write_flattened_sequence read_batch_formatted_as_numpy after write_nested_sequence read_batch_unformated after write_array2d read_batch_unformated after write_flattened_sequence read_batch_unformated after write_nested_sequence read_col_formatted_as_numpy after write_array2d read_col_formatted_as_numpy after write_flattened_sequence read_col_formatted_as_numpy after write_nested_sequence read_col_unformated after write_array2d read_col_unformated after write_flattened_sequence read_col_unformated after write_nested_sequence read_formatted_as_numpy after write_array2d read_formatted_as_numpy after write_flattened_sequence read_formatted_as_numpy after write_nested_sequence read_unformated after write_array2d read_unformated after write_flattened_sequence read_unformated after write_nested_sequence write_array2d write_flattened_sequence write_nested_sequence
new / old (diff) 0.005303 / 0.011353 (-0.006050) 0.003586 / 0.011008 (-0.007422) 0.049339 / 0.038508 (0.010831) 0.051287 / 0.023109 (0.028178) 0.274397 / 0.275898 (-0.001501) 0.292977 / 0.323480 (-0.030503) 0.004029 / 0.007986 (-0.003957) 0.002727 / 0.004328 (-0.001602) 0.048779 / 0.004250 (0.044528) 0.040075 / 0.037052 (0.003022) 0.277676 / 0.258489 (0.019187) 0.301963 / 0.293841 (0.008122) 0.029340 / 0.128546 (-0.099206) 0.010714 / 0.075646 (-0.064932) 0.057253 / 0.419271 (-0.362018) 0.033426 / 0.043533 (-0.010107) 0.276673 / 0.255139 (0.021534) 0.291053 / 0.283200 (0.007854) 0.017660 / 0.141683 (-0.124023) 1.122354 / 1.452155 (-0.329800) 1.180381 / 1.492716 (-0.312335)

Benchmark: benchmark_getitem_100B.json

metric get_batch_of_1024_random_rows get_batch_of_1024_rows get_first_row get_last_row
new / old (diff) 0.091903 / 0.018006 (0.073897) 0.300720 / 0.000490 (0.300231) 0.000288 / 0.000200 (0.000088) 0.000044 / 0.000054 (-0.000011)

Benchmark: benchmark_indices_mapping.json

metric select shard shuffle sort train_test_split
new / old (diff) 0.021521 / 0.037411 (-0.015890) 0.068233 / 0.014526 (0.053707) 0.081245 / 0.176557 (-0.095312) 0.119996 / 0.737135 (-0.617139) 0.082483 / 0.296338 (-0.213856)

Benchmark: benchmark_iterating.json

metric read 5000 read 50000 read_batch 50000 10 read_batch 50000 100 read_batch 50000 1000 read_formatted numpy 5000 read_formatted pandas 5000 read_formatted tensorflow 5000 read_formatted torch 5000 read_formatted_batch numpy 5000 10 read_formatted_batch numpy 5000 1000 shuffled read 5000 shuffled read 50000 shuffled read_batch 50000 10 shuffled read_batch 50000 100 shuffled read_batch 50000 1000 shuffled read_formatted numpy 5000 shuffled read_formatted_batch numpy 5000 10 shuffled read_formatted_batch numpy 5000 1000
new / old (diff) 0.302776 / 0.215209 (0.087567) 2.950776 / 2.077655 (0.873122) 1.631032 / 1.504120 (0.126912) 1.502021 / 1.541195 (-0.039174) 1.514213 / 1.468490 (0.045723) 0.578246 / 4.584777 (-4.006531) 2.443768 / 3.745712 (-1.301944) 2.827811 / 5.269862 (-2.442051) 1.771529 / 4.565676 (-2.794148) 0.064479 / 0.424275 (-0.359797) 0.005061 / 0.007607 (-0.002546) 0.350966 / 0.226044 (0.124922) 3.458616 / 2.268929 (1.189687) 1.967917 / 55.444624 (-53.476707) 1.704661 / 6.876477 (-5.171815) 1.698895 / 2.142072 (-0.443178) 0.663259 / 4.805227 (-4.141968) 0.122140 / 6.500664 (-6.378525) 0.041099 / 0.075469 (-0.034371)

Benchmark: benchmark_map_filter.json

metric filter map fast-tokenizer batched map identity map identity batched map no-op batched map no-op batched numpy map no-op batched pandas map no-op batched pytorch map no-op batched tensorflow
new / old (diff) 0.972080 / 1.841788 (-0.869708) 12.123286 / 8.074308 (4.048978) 10.819854 / 10.191392 (0.628462) 0.131486 / 0.680424 (-0.548938) 0.015785 / 0.534201 (-0.518416) 0.290048 / 0.579283 (-0.289235) 0.277822 / 0.434364 (-0.156542) 0.325949 / 0.540337 (-0.214388) 0.577681 / 1.386936 (-0.809255)

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.

3 participants