Repository navigation
Add IterableDataset.save_to_disk - #9145
Open
behroozazarkhalili wants to merge 2 commits into
Open
behroozazarkhalili wants to merge 2 commits into
behroozazarkhalili wants to merge 2 commits into
Conversation
An IterableDataset could not be saved to a dataset directory without first converting it with Dataset.from_generator, which writes it twice. save_to_disk now writes the examples to Arrow files while iterating, in the layout of Dataset.save_to_disk, so load_from_disk returns a Dataset. The number of examples is unknown in advance, so a new file is started when the current one reaches max_shard_size, and the files are renamed with the -SSSSS-of-NNNNN pattern at the end, as the builder does. With num_proc, each process saves a contiguous group of the dataset shards. IterableDatasetDict.save_to_disk writes one directory per split. Fixes huggingface#3753
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 #3753
As suggested in #3753, an
IterableDatasetcan now be saved to a dataset directory and reloaded withload_from_disk:Until now the way to do this was
Dataset.from_generator(ds.__iter__).save_to_disk(...), which writes the data twice (cache, then copy).How it works:
ArrowWriter(embed_local_files=True)likeDataset.save_to_disk, so the directory has the same layout (data-XXXXX-of-NNNNN.arrow,state.json,dataset_info.json).max_shard_size, and the files are renamed with the-SSSSS-of-NNNNNpattern at the end, like the builder does in_prepare_split.num_proc: each process saves a contiguous group of the dataset shards (spawn pool +iflatmap_unordered, likeIterableDataset.push_to_hub), so the order of the examples is kept.num_procis capped atdataset.num_shards.from_generatorwithoutfeatures) are resolved first with_resolve_features(), like inpush_to_hub.state.json.IterableDatasetDict.save_to_diskwritesdataset_dict.jsonand one directory per split, likeDatasetDict.save_to_disk.state.jsonanddataset_info.jsonis moved out ofDataset.save_to_diskinto a small function used by both.num_shardsisn't supported, since the number of examples is only known at the end;max_shard_sizecontrols the shards instead.Tests (new functions in
tests/test_iterable_dataset.py): reload gives the same examples and features (with and withoutnum_proc), several shards withmax_shard_sizeand their names, unknown features, split and format, local images embedded (the image file is deleted before reloading), empty dataset,IterableDatasetDict. Onmainthe 9 tests fail; with this change they pass.tests/test_iterable_dataset.py,test_dataset_dict.py,test_arrow_dataset.pyandtest_load.pyon Python 3.12: same 6 failures onmainand on this branch (5 need torch, which wasn't installed in that environment, andtest_load_dataset_builder_then_edit_then_load_againfails on our Lustre file system onmaintoo), and the 9 new tests pass. On Python 3.10,test_iterable_dataset_save_to_disk_embeds_local_imagesfails with thexPatherror ofmainthat #9119 fixes, like many other tests that read local files.