[IterableDataset] Fix `drop_last_batch`in map after shuffling or sharding
Opened by lhoestq (closed)

It was not taken into account e.g. when passing to a DataLoader with num_workers>0

Fix https://github.com/huggingface/datasets/issues/6565

Comments:
The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/datasets/pr_6575). 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.005095 / 0.011353 (-0.006257) | 0.003531 / 0.011008 (-0.007478) | 0.063634 / 0.038508 (0.025126) | 0.031187 / 0.023109 (0.008078) | 0.246375 / 0.275898 (-0.029523) | 0.261204 / 0.323480 (-0.062276) | 0.002898 / 0.007986 (-0.005088) | 0.003280 / 0.004328 (-0.001049) | 0.050739 / 0.004250 (0.046488) | 0.042905 / 0.037052 (0.005852) | 0.244506 / 0.258489 (-0.013983) | 0.269403 / 0.293841 (-0.024438) | 0.027588 / 0.128546 (-0.100959) | 0.010860 / 0.075646 (-0.064787) | 0.208332 / 0.419271 (-0.210939) | 0.035762 / 0.043533 (-0.007771) | 0.244448 / 0.255139 (-0.010691) | 0.278464 / 0.283200 (-0.004735) | 0.019839 / 0.141683 (-0.121844) | 1.145340 / 1.452155 (-0.306815) | 1.173240 / 1.492716 (-0.319476) |

### 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.090472 / 0.018006 (0.072466) | 0.300883 / 0.000490 (0.300394) | 0.000202 / 0.000200 (0.000003) | 0.000049 / 0.000054 (-0.000006) |

### Benchmark: benchmark_indices_mapping.json

| metric | select | shard | shuffle | sort | train_test_split |
|--------|---|---|---|---|---|
| new / old (diff) | 0.017884 / 0.037411 (-0.019527) | 0.060629 / 0.014526 (0.046103) | 0.073157 / 0.176557 (-0.103400) | 0.120065 / 0.737135 (-0.617070) | 0.074519 / 0.296338 (-0.221820) |

### 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.289586 / 0.215209 (0.074377) | 2.821042 / 2.077655 (0.743387) | 1.515515 / 1.504120 (0.011395) | 1.390569 / 1.541195 (-0.150625) | 1.433238 / 1.468490 (-0.035252) | 0.567357 / 4.584777 (-4.017420) | 2.345483 / 3.745712 (-1.400229) | 2.803964 / 5.269862 (-2.465898) | 1.775343 / 4.565676 (-2.790334) | 0.063186 / 0.424275 (-0.361089) | 0.005013 / 0.007607 (-0.002594) | 0.335607 / 0.226044 (0.109562) | 3.307071 / 2.268929 (1.038143) | 1.875228 / 55.444624 (-53.569396) | 1.618286 / 6.876477 (-5.258191) | 1.615963 / 2.142072 (-0.526109) | 0.642633 / 4.805227 (-4.162594) | 0.117222 / 6.500664 (-6.383443) | 0.042590 / 0.075469 (-0.032879) |

### 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.960724 / 1.841788 (-0.881064) | 11.652978 / 8.074308 (3.578670) | 10.069318 / 10.191392 (-0.122074) | 0.128161 / 0.680424 (-0.552263) | 0.014095 / 0.534201 (-0.520106) | 0.288386 / 0.579283 (-0.290897) | 0.260373 / 0.434364 (-0.173991) | 0.327443 / 0.540337 (-0.212894) | 0.419020 / 1.386936 (-0.967916) |

</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.005018 / 0.011353 (-0.006335) | 0.003503 / 0.011008 (-0.007505) | 0.049718 / 0.038508 (0.011210) | 0.029311 / 0.023109 (0.006202) | 0.271097 / 0.275898 (-0.004801) | 0.297370 / 0.323480 (-0.026110) | 0.004230 / 0.007986 (-0.003755) | 0.002741 / 0.004328 (-0.001587) | 0.049686 / 0.004250 (0.045435) | 0.044171 / 0.037052 (0.007119) | 0.274851 / 0.258489 (0.016362) | 0.309554 / 0.293841 (0.015714) | 0.029488 / 0.128546 (-0.099058) | 0.010767 / 0.075646 (-0.064880) | 0.057739 / 0.419271 (-0.361532) | 0.053319 / 0.043533 (0.009786) | 0.277739 / 0.255139 (0.022600) | 0.291341 / 0.283200 (0.008142) | 0.019587 / 0.141683 (-0.122096) | 1.113823 / 1.452155 (-0.338332) | 1.169409 / 1.492716 (-0.323307) |

### 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.091889 / 0.018006 (0.073883) | 0.309162 / 0.000490 (0.308672) | 0.000222 / 0.000200 (0.000022) | 0.000055 / 0.000054 (0.000000) |

### Benchmark: benchmark_indices_mapping.json

| metric | select | shard | shuffle | sort | train_test_split |
|--------|---|---|---|---|---|
| new / old (diff) | 0.022202 / 0.037411 (-0.015209) | 0.076113 / 0.014526 (0.061587) | 0.088416 / 0.176557 (-0.088141) | 0.126822 / 0.737135 (-0.610314) | 0.089540 / 0.296338 (-0.206798) |

### 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.293697 / 0.215209 (0.078487) | 2.880680 / 2.077655 (0.803026) | 1.580122 / 1.504120 (0.076002) | 1.449492 / 1.541195 (-0.091703) | 1.478900 / 1.468490 (0.010410) | 0.563402 / 4.584777 (-4.021375) | 2.408692 / 3.745712 (-1.337020) | 2.794108 / 5.269862 (-2.475754) | 1.728549 / 4.565676 (-2.837128) | 0.063152 / 0.424275 (-0.361123) | 0.004985 / 0.007607 (-0.002622) | 0.343340 / 0.226044 (0.117295) | 3.426454 / 2.268929 (1.157525) | 1.932918 / 55.444624 (-53.511706) | 1.649533 / 6.876477 (-5.226944) | 1.673416 / 2.142072 (-0.468656) | 0.640000 / 4.805227 (-4.165227) | 0.115501 / 6.500664 (-6.385163) | 0.040756 / 0.075469 (-0.034713) |

### 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.992468 / 1.841788 (-0.849319) | 12.392072 / 8.074308 (4.317764) | 11.025362 / 10.191392 (0.833970) | 0.130788 / 0.680424 (-0.549635) | 0.015647 / 0.534201 (-0.518554) | 0.285914 / 0.579283 (-0.293369) | 0.277208 / 0.434364 (-0.157156) | 0.322917 / 0.540337 (-0.217420) | 0.427308 / 1.386936 (-0.959628) |

</details>
</details>

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