Issue: Fix webdataset pickling
State: closed
Author: lhoestq

...by making tracked iterables picklable.

This is important to make streaming datasets compatible with multiprocessing e.g. for parallel data loading

Comments:
The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/datasets/pr_6972). 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.005195 / 0.011353 (-0.006157) | 0.003734 / 0.011008 (-0.007275) | 0.063087 / 0.038508 (0.024579) | 0.031467 / 0.023109 (0.008358) | 0.245183 / 0.275898 (-0.030715) | 0.280071 / 0.323480 (-0.043409) | 0.003205 / 0.007986 (-0.004780) | 0.003311 / 0.004328 (-0.001018) | 0.049967 / 0.004250 (0.045717) | 0.044927 / 0.037052 (0.007875) | 0.262244 / 0.258489 (0.003755) | 0.284549 / 0.293841 (-0.009292) | 0.027595 / 0.128546 (-0.100952) | 0.010521 / 0.075646 (-0.065126) | 0.206928 / 0.419271 (-0.212343) | 0.036179 / 0.043533 (-0.007354) | 0.254256 / 0.255139 (-0.000883) | 0.272733 / 0.283200 (-0.010467) | 0.020456 / 0.141683 (-0.121226) | 1.118527 / 1.452155 (-0.333628) | 1.152741 / 1.492716 (-0.339975) |

### 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.096642 / 0.018006 (0.078636) | 0.306981 / 0.000490 (0.306491) | 0.000220 / 0.000200 (0.000020) | 0.000042 / 0.000054 (-0.000012) |

### Benchmark: benchmark_indices_mapping.json

| metric | select | shard | shuffle | sort | train_test_split |
|--------|---|---|---|---|---|
| new / old (diff) | 0.019031 / 0.037411 (-0.018380) | 0.063960 / 0.014526 (0.049435) | 0.074428 / 0.176557 (-0.102129) | 0.121226 / 0.737135 (-0.615909) | 0.077111 / 0.296338 (-0.219228) |

### 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.279830 / 0.215209 (0.064621) | 2.748243 / 2.077655 (0.670588) | 1.481554 / 1.504120 (-0.022566) | 1.355015 / 1.541195 (-0.186180) | 1.379655 / 1.468490 (-0.088835) | 0.560378 / 4.584777 (-4.024399) | 2.407241 / 3.745712 (-1.338471) | 2.837090 / 5.269862 (-2.432771) | 1.767084 / 4.565676 (-2.798593) | 0.063517 / 0.424275 (-0.360758) | 0.005024 / 0.007607 (-0.002584) | 0.334845 / 0.226044 (0.108800) | 3.290712 / 2.268929 (1.021783) | 1.836923 / 55.444624 (-53.607702) | 1.543671 / 6.876477 (-5.332806) | 1.582319 / 2.142072 (-0.559754) | 0.637689 / 4.805227 (-4.167538) | 0.119515 / 6.500664 (-6.381149) | 0.042191 / 0.075469 (-0.033278) |

### 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.980018 / 1.841788 (-0.861770) | 11.620211 / 8.074308 (3.545903) | 9.697799 / 10.191392 (-0.493593) | 0.131733 / 0.680424 (-0.548691) | 0.014007 / 0.534201 (-0.520193) | 0.286046 / 0.579283 (-0.293237) | 0.264776 / 0.434364 (-0.169588) | 0.325041 / 0.540337 (-0.215296) | 0.452740 / 1.386936 (-0.934196) |

</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.005603 / 0.011353 (-0.005750) | 0.003810 / 0.011008 (-0.007199) | 0.050773 / 0.038508 (0.012265) | 0.032601 / 0.023109 (0.009492) | 0.268035 / 0.275898 (-0.007863) | 0.292614 / 0.323480 (-0.030866) | 0.005076 / 0.007986 (-0.002910) | 0.004487 / 0.004328 (0.000159) | 0.049988 / 0.004250 (0.045737) | 0.040258 / 0.037052 (0.003205) | 0.284145 / 0.258489 (0.025656) | 0.318291 / 0.293841 (0.024450) | 0.029672 / 0.128546 (-0.098875) | 0.010534 / 0.075646 (-0.065113) | 0.059020 / 0.419271 (-0.360252) | 0.033451 / 0.043533 (-0.010082) | 0.270220 / 0.255139 (0.015081) | 0.290500 / 0.283200 (0.007300) | 0.017123 / 0.141683 (-0.124560) | 1.130870 / 1.452155 (-0.321285) | 1.160038 / 1.492716 (-0.332678) |

### 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.097045 / 0.018006 (0.079039) | 0.314573 / 0.000490 (0.314083) | 0.000203 / 0.000200 (0.000003) | 0.000044 / 0.000054 (-0.000011) |

### Benchmark: benchmark_indices_mapping.json

| metric | select | shard | shuffle | sort | train_test_split |
|--------|---|---|---|---|---|
| new / old (diff) | 0.022396 / 0.037411 (-0.015015) | 0.079393 / 0.014526 (0.064867) | 0.088460 / 0.176557 (-0.088097) | 0.128050 / 0.737135 (-0.609085) | 0.093070 / 0.296338 (-0.203268) |

### 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.293858 / 0.215209 (0.078649) | 2.819956 / 2.077655 (0.742301) | 1.540181 / 1.504120 (0.036061) | 1.419671 / 1.541195 (-0.121524) | 1.441594 / 1.468490 (-0.026897) | 0.565200 / 4.584777 (-4.019577) | 0.963967 / 3.745712 (-2.781745) | 2.752137 / 5.269862 (-2.517725) | 1.779239 / 4.565676 (-2.786438) | 0.063787 / 0.424275 (-0.360488) | 0.005344 / 0.007607 (-0.002263) | 0.344283 / 0.226044 (0.118239) | 3.353263 / 2.268929 (1.084334) | 1.898678 / 55.444624 (-53.545947) | 1.607868 / 6.876477 (-5.268609) | 1.781938 / 2.142072 (-0.360134) | 0.652119 / 4.805227 (-4.153108) | 0.117883 / 6.500664 (-6.382781) | 0.048811 / 0.075469 (-0.026658) |

### 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) | 1.013154 / 1.841788 (-0.828634) | 12.421963 / 8.074308 (4.347655) | 10.352056 / 10.191392 (0.160664) | 0.143784 / 0.680424 (-0.536640) | 0.016370 / 0.534201 (-0.517831) | 0.283668 / 0.579283 (-0.295615) | 0.127070 / 0.434364 (-0.307294) | 0.326199 / 0.540337 (-0.214138) | 0.432776 / 1.386936 (-0.954160) |

</details>
</details>

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