Title: Fix parallel downloads for datasets without scripts
Author: lhoestq (MEMBER)
State: closed
Created: 2024-01-02T18:06:18Z

Enable parallel downloads using multiprocessing when `num_proc` is passed to `load_dataset`.

It was enabled for datasets with scripts already (if they passed lists to `dl_manager.download`) but not for no-script datasets (we pass dicts {split: [list of files]} to `dl_manager.download` for those ones).

I fixed this by parallelising on the lists contained in the data files dicts when possible.

I also added a context manager `stack_multiprocessing_download_progress_bars` in `DownloadManager` to stack the progress bard of the downloads (from `cached_path(...)` calls). Otherwise the progress bars overlap each other with an annoying flickering effect.

Comments:
The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/datasets/pr_6551). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

<details>
<summary>Show benchmarks</summary>

PyArrow==8.0.0

<details>
<summary>Show updated benchmarks!</summary>

### 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.005002 / 0.011353 (-0.006350) | 0.003300 / 0.011008 (-0.007708) | 0.062509 / 0.038508 (0.024001) | 0.029807 / 0.023109 (0.006698) | 0.249935 / 0.275898 (-0.025963) | 0.264320 / 0.323480 (-0.059160) | 0.003790 / 0.007986 (-0.004195) | 0.002554 / 0.004328 (-0.001774) | 0.048207 / 0.004250 (0.043956) | 0.042033 / 0.037052 (0.004981) | 0.245725 / 0.258489 (-0.012764) | 0.276695 / 0.293841 (-0.017146) | 0.026502 / 0.128546 (-0.102044) | 0.010379 / 0.075646 (-0.065268) | 0.207002 / 0.419271 (-0.212269) | 0.034648 / 0.043533 (-0.008885) | 0.247957 / 0.255139 (-0.007182) | 0.263921 / 0.283200 (-0.019278) | 0.017710 / 0.141683 (-0.123973) | 1.105851 / 1.452155 (-0.346304) | 1.163315 / 1.492716 (-0.329401) |

### 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.089842 / 0.018006 (0.071836) | 0.352499 / 0.000490 (0.352009) | 0.000201 / 0.000200 (0.000001) | 0.000054 / 0.000054 (-0.000000) |

### Benchmark: benchmark_indices_mapping.json

| metric | select | shard | shuffle | sort | train_test_split |
|--------|---|---|---|---|---|
| new / old (diff) | 0.018094 / 0.037411 (-0.019317) | 0.060463 / 0.014526 (0.045937) | 0.073257 / 0.176557 (-0.103300) | 0.119771 / 0.737135 (-0.617364) | 0.075210 / 0.296338 (-0.221128) |

### 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.288365 / 0.215209 (0.073156) | 2.825377 / 2.077655 (0.747722) | 1.532436 / 1.504120 (0.028316) | 1.393475 / 1.541195 (-0.147719) | 1.381859 / 1.468490 (-0.086632) | 0.564155 / 4.584777 (-4.020622) | 2.398177 / 3.745712 (-1.347535) | 2.730271 / 5.269862 (-2.539590) | 1.713779 / 4.565676 (-2.851898) | 0.062789 / 0.424275 (-0.361486) | 0.004991 / 0.007607 (-0.002616) | 0.340789 / 0.226044 (0.114744) | 3.323543 / 2.268929 (1.054615) | 1.861925 / 55.444624 (-53.582700) | 1.555181 / 6.876477 (-5.321296) | 1.559512 / 2.142072 (-0.582560) | 0.634565 / 4.805227 (-4.170663) | 0.116529 / 6.500664 (-6.384135) | 0.041312 / 0.075469 (-0.034157) |

### 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.945739 / 1.841788 (-0.896049) | 11.376130 / 8.074308 (3.301822) | 10.007752 / 10.191392 (-0.183640) | 0.126815 / 0.680424 (-0.553609) | 0.013898 / 0.534201 (-0.520303) | 0.287438 / 0.579283 (-0.291845) | 0.261532 / 0.434364 (-0.172832) | 0.320197 / 0.540337 (-0.220140) | 0.414444 / 1.386936 (-0.972492) |

</details>
PyArrow==latest

<details>
<summary>Show updated benchmarks!</summary>

### 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.004994 / 0.011353 (-0.006359) | 0.003407 / 0.011008 (-0.007601) | 0.049281 / 0.038508 (0.010773) | 0.042815 / 0.023109 (0.019706) | 0.268291 / 0.275898 (-0.007607) | 0.285877 / 0.323480 (-0.037603) | 0.004006 / 0.007986 (-0.003980) | 0.002607 / 0.004328 (-0.001721) | 0.047682 / 0.004250 (0.043431) | 0.044281 / 0.037052 (0.007228) | 0.268287 / 0.258489 (0.009798) | 0.298649 / 0.293841 (0.004808) | 0.028607 / 0.128546 (-0.099939) | 0.010367 / 0.075646 (-0.065279) | 0.057114 / 0.419271 (-0.362158) | 0.053753 / 0.043533 (0.010220) | 0.269010 / 0.255139 (0.013871) | 0.285057 / 0.283200 (0.001858) | 0.017693 / 0.141683 (-0.123990) | 1.134718 / 1.452155 (-0.317436) | 1.186609 / 1.492716 (-0.306107) |

### 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.091109 / 0.018006 (0.073103) | 0.298603 / 0.000490 (0.298113) | 0.000216 / 0.000200 (0.000016) | 0.000050 / 0.000054 (-0.000004) |

### Benchmark: benchmark_indices_mapping.json

| metric | select | shard | shuffle | sort | train_test_split |
|--------|---|---|---|---|---|
| new / old (diff) | 0.022125 / 0.037411 (-0.015286) | 0.076570 / 0.014526 (0.062044) | 0.088903 / 0.176557 (-0.087654) | 0.126427 / 0.737135 (-0.610708) | 0.091001 / 0.296338 (-0.205338) |

### 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.300332 / 0.215209 (0.085123) | 2.971106 / 2.077655 (0.893452) | 1.617886 / 1.504120 (0.113766) | 1.476679 / 1.541195 (-0.064516) | 1.483750 / 1.468490 (0.015260) | 0.582569 / 4.584777 (-4.002208) | 2.441804 / 3.745712 (-1.303908) | 2.753927 / 5.269862 (-2.515935) | 1.733546 / 4.565676 (-2.832130) | 0.062653 / 0.424275 (-0.361622) | 0.005019 / 0.007607 (-0.002588) | 0.355556 / 0.226044 (0.129512) | 3.497431 / 2.268929 (1.228503) | 1.951711 / 55.444624 (-53.492913) | 1.663874 / 6.876477 (-5.212602) | 1.657363 / 2.142072 (-0.484709) | 0.653488 / 4.805227 (-4.151739) | 0.117055 / 6.500664 (-6.383609) | 0.040687 / 0.075469 (-0.034782) |

### 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.969485 / 1.841788 (-0.872303) | 12.064793 / 8.074308 (3.990485) | 10.851531 / 10.191392 (0.660139) | 0.129060 / 0.680424 (-0.551364) | 0.015339 / 0.534201 (-0.518862) | 0.287215 / 0.579283 (-0.292069) | 0.276545 / 0.434364 (-0.157819) | 0.322748 / 0.540337 (-0.217589) | 0.421363 / 1.386936 (-0.965573) |

</details>
</details>

![](https://cml.dev/watermark.png#d26abadce0b884db32382b92422d8a6aa997d40a "CML watermark")


@lhoestq 
<img width="1015" alt="image" src="https://github.com/huggingface/datasets/assets/17604849/b19b9d92-c6f7-4e3a-8c9d-1178e56c67ea">
it's still not fixed =(

@lhoestq i was thinking uninstalling `datasets` and then `pip install git+https://github.com/huggingface/datasets.git` has to fix it. Buuuuut. I'm not sure what's going on actually...

Now instead of showing progress bars one after another it seems to be downloading the dataset way way way faster (like 4 mins instead of 58, thank you very much) but does not show any progress bars related to downloading at all.

<img width="1170" alt="image" src="https://github.com/huggingface/datasets/assets/17604849/21a84908-c44d-41b4-bb0d-8061cab3bc64">

<img width="1159" alt="image" src="https://github.com/huggingface/datasets/assets/17604849/26684a8a-c10a-4fa2-bd84-cab4f938ffcc">