Skip to content

Add IterableDataset.save_to_disk - #9145

Open
behroozazarkhalili wants to merge 2 commits into
huggingface:mainfrom
behroozazarkhalili:feat/3753-iterable-save-to-disk
Open

behroozazarkhalili wants to merge 2 commits into
huggingface:mainfrom
behroozazarkhalili:feat/3753-iterable-save-to-disk

Conversation

@behroozazarkhalili

@behroozazarkhalili behroozazarkhalili commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3753

As suggested in #3753, an IterableDataset can now be saved to a dataset directory and reloaded with load_from_disk:

ds = load_dataset("...", split="train", streaming=True)
ds = ds.filter(...).map(...)
ds.save_to_disk("path/to/dir")
ds = load_from_disk("path/to/dir")  # a Dataset

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:

  • The examples are written to Arrow files while iterating, with ArrowWriter(embed_local_files=True) like Dataset.save_to_disk, so the directory has the same layout (data-XXXXX-of-NNNNN.arrow, state.json, dataset_info.json).
  • The number of examples isn't known 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, like the builder does in _prepare_split.
  • num_proc: each process saves a contiguous group of the dataset shards (spawn pool + iflatmap_unordered, like IterableDataset.push_to_hub), so the order of the examples is kept. num_proc is capped at dataset.num_shards.
  • Unknown features (e.g. from_generator without features) are resolved first with _resolve_features(), like in push_to_hub.
  • The split and the format type are saved in state.json.
  • IterableDatasetDict.save_to_disk writes dataset_dict.json and one directory per split, like DatasetDict.save_to_disk.
  • The code that writes state.json and dataset_info.json is moved out of Dataset.save_to_disk into a small function used by both.

num_shards isn't supported, since the number of examples is only known at the end; max_shard_size controls the shards instead.

Tests (new functions in tests/test_iterable_dataset.py): reload gives the same examples and features (with and without num_proc), several shards with max_shard_size and their names, unknown features, split and format, local images embedded (the image file is deleted before reloading), empty dataset, IterableDatasetDict. On main the 9 tests fail; with this change they pass. tests/test_iterable_dataset.py, test_dataset_dict.py, test_arrow_dataset.py and test_load.py on Python 3.12: same 6 failures on main and on this branch (5 need torch, which wasn't installed in that environment, and test_load_dataset_builder_then_edit_then_load_again fails on our Lustre file system on main too), and the 9 new tests pass. On Python 3.10, test_iterable_dataset_save_to_disk_embeds_local_images fails with the xPath error of main that #9119 fixes, like many other tests that read local files.

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
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.

Expanding streaming capabilities

1 participant