Skip to content

Add num_proc to IterableDataset.map - #9142

Open
behroozazarkhalili wants to merge 1 commit into
huggingface:mainfrom
behroozazarkhalili:feat/2642-iterable-map-num-proc
Open

behroozazarkhalili wants to merge 1 commit into
huggingface:mainfrom
behroozazarkhalili:feat/2642-iterable-map-num-proc

Conversation

@behroozazarkhalili

Copy link
Copy Markdown
Contributor

Fixes #2642

IterableDataset.map now takes num_proc, as discussed in #2642 and #7470:

ds = load_dataset("...", split="train", streaming=True)
ds = ds.map(tokenize, batched=True, num_proc=4)

The examples are still read in the main process. A pool of num_proc processes runs the function, and the outputs are yielded in the order of the examples, so the result is the same as without num_proc. It also works for datasets with a single shard, unlike a DataLoader with num_workers.

How it works, in MappedExamplesIterable._iter:

  • The async path already ran up to N function calls at the same time and yielded the outputs in order, with checkpoints for state_dict. That loop is now shared by the async path and the new pool path, so resuming from a state_dict works the same way for both.
  • The pool uses the spawn context, like IterableDataset.push_to_hub(num_proc=...), since the main process may run threads to stream the data. The function and fn_kwargs are sent once per process (pool initializer).
  • Up to num_proc * config.MAX_NUM_RUNNING_MAP_FUNCTIONS_PER_PROCESS (2) inputs are sent in advance.
  • The pool is terminated when the iteration ends, raises, or is closed early (e.g. break).
  • If a process dies, map raises a RuntimeError instead of waiting forever, like iflatmap_unordered.
  • A function that returns its input example with remove_columns gives the same output as without num_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=0 means no multiprocessing and negative values raise, like Dataset.map. IterableDatasetDict.map passes num_proc through.

Timing on a CPU-bound batched function (16,000 examples, batch_size=50, on a machine limited to 6 CPUs): 8.9 s without num_proc, 5.8 s with num_proc=2, 3.3 s with num_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):

  • same examples as without num_proc: function returning its input with remove_columns, batched lambda with indices, input_columns (including []), fn_kwargs
  • resume from state_dict several times, batched and not batched
  • the processes are stopped when the iterator is closed early
  • errors of the function are raised; a dying process raises RuntimeError
  • invalid uses: negative num_proc, async function, arrow format; num_proc=0

On main these 11 tests fail; with this change they pass. tests/test_iterable_dataset.py, test_dataset_dict.py and test_distributed.py on Python 3.12: 750 passed; the only 4 failures are test_interleave_dataset_with_sharding, which needs torch (not installed in that environment) and fails the same way on main. (On Python 3.10, 58 tests of these files fail on main too, with the xPath error that #9119 fixes.)

Also checked by hand, with the same output as without num_proc: inside a torch DataLoader with num_workers=2 (fork and spawn workers), with the "numpy" and "torch" formats, and with a decoded Image column.

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

Support multi-worker with streaming dataset (IterableDataset).

1 participant