Repository navigation
Add num_proc to IterableDataset.map - #9142
Open
behroozazarkhalili wants to merge 1 commit into
Open
behroozazarkhalili wants to merge 1 commit into
behroozazarkhalili wants to merge 1 commit into
Conversation
The map function of an IterableDataset could only run in the main process (or in DataLoader workers, which need several shards). With num_proc, the examples are still read in the main process, a spawn pool runs the function, and the outputs are yielded in the order of the examples. The async path and the new pool path share the same ordered window, so state_dict checkpoints work the same way for both. The pool is stopped when the iteration ends, fails or is closed early, and a dead subprocess raises instead of hanging. Fixes huggingface#2642
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 #2642
IterableDataset.mapnow takesnum_proc, as discussed in #2642 and #7470:The examples are still read in the main process. A pool of
num_procprocesses runs the function, and the outputs are yielded in the order of the examples, so the result is the same as withoutnum_proc. It also works for datasets with a single shard, unlike a DataLoader withnum_workers.How it works, in
MappedExamplesIterable._iter:state_dict. That loop is now shared by the async path and the new pool path, so resuming from astate_dictworks the same way for both.IterableDataset.push_to_hub(num_proc=...), since the main process may run threads to stream the data. The function andfn_kwargsare sent once per process (pool initializer).num_proc * config.MAX_NUM_RUNNING_MAP_FUNCTIONS_PER_PROCESS(2) inputs are sent in advance.break).RuntimeErrorinstead of waiting forever, likeiflatmap_unordered.remove_columnsgives the same output as withoutnum_proc(the identity check happens in the subprocess).The processes use the spawn context, so in a script the iteration must run under
if __name__ == "__main__":(documented in the docstring).Not supported, with a clear error: async functions (map already runs them in parallel) and datasets formatted as tables ("arrow", "pandas", "polars").
num_proc=0means no multiprocessing and negative values raise, likeDataset.map.IterableDatasetDict.mappassesnum_procthrough.Timing on a CPU-bound batched function (16,000 examples,
batch_size=50, on a machine limited to 6 CPUs): 8.9 s withoutnum_proc, 5.8 s withnum_proc=2, 3.3 s withnum_proc=4. Starting the pool takes about 1 s each time the dataset is iterated, so it helps when the function is slow.Tests (new functions in
tests/test_iterable_dataset.py):num_proc: function returning its input withremove_columns, batched lambda with indices,input_columns(including[]),fn_kwargsstate_dictseveral times, batched and not batchedRuntimeErrornum_proc, async function, arrow format;num_proc=0On
mainthese 11 tests fail; with this change they pass.tests/test_iterable_dataset.py,test_dataset_dict.pyandtest_distributed.pyon Python 3.12: 750 passed; the only 4 failures aretest_interleave_dataset_with_sharding, which needs torch (not installed in that environment) and fails the same way onmain. (On Python 3.10, 58 tests of these files fail onmaintoo, with thexPatherror that #9119 fixes.)Also checked by hand, with the same output as without
num_proc: inside a torchDataLoaderwithnum_workers=2(fork and spawn workers), with the "numpy" and "torch" formats, and with a decodedImagecolumn.