Title: Base parquet batch_size on parquet row group size
State: closed

This allows to stream datasets like [Major-TOM/Core-S2L2A](https://huggingface.co/datasets/Major-TOM/Core-S2L2A) which have row groups with few rows (one row is ~10MB). Previously the cold start would take a lot of time and OOM because it would download many row groups before yielding the first example.

I tried on OpenOrca and imagenet-hard and it does't affect overall throughput.

Even if the overall throughput doesn't change for datasets like imagenet-hard with big rows, note that it does create shorter and more frequent pauses to download the next row group. Though I find it fine because previously the pauses were less frequent but very long (downloading multiple row groups at a time)

Comments:
The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/datasets/pr_6701). 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.005490 / 0.011353 (-0.005863) | 0.003709 / 0.011008 (-0.007299) | 0.064192 / 0.038508 (0.025684) | 0.029581 / 0.023109 (0.006472) | 0.251086 / 0.275898 (-0.024812) | 0.267306 / 0.323480 (-0.056174) | 0.003074 / 0.007986 (-0.004912) | 0.003340 / 0.004328 (-0.000988) | 0.048820 / 0.004250 (0.044569) | 0.045370 / 0.037052 (0.008318) | 0.260384 / 0.258489 (0.001895) | 0.284558 / 0.293841 (-0.009283) | 0.027732 / 0.128546 (-0.100814) | 0.010661 / 0.075646 (-0.064986) | 0.213403 / 0.419271 (-0.205868) | 0.036283 / 0.043533 (-0.007250) | 0.250107 / 0.255139 (-0.005032) | 0.265220 / 0.283200 (-0.017980) | 0.021021 / 0.141683 (-0.120661) | 1.112058 / 1.452155 (-0.340096) | 1.169039 / 1.492716 (-0.323678) |

### 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.095008 / 0.018006 (0.077002) | 0.303509 / 0.000490 (0.303019) | 0.000233 / 0.000200 (0.000033) | 0.000052 / 0.000054 (-0.000002) |

### Benchmark: benchmark_indices_mapping.json

| metric | select | shard | shuffle | sort | train_test_split |
|--------|---|---|---|---|---|
| new / old (diff) | 0.018224 / 0.037411 (-0.019187) | 0.061366 / 0.014526 (0.046841) | 0.073584 / 0.176557 (-0.102972) | 0.119869 / 0.737135 (-0.617266) | 0.074228 / 0.296338 (-0.222111) |

### 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.288147 / 0.215209 (0.072938) | 2.824419 / 2.077655 (0.746764) | 1.478530 / 1.504120 (-0.025590) | 1.350127 / 1.541195 (-0.191067) | 1.349622 / 1.468490 (-0.118868) | 0.568058 / 4.584777 (-4.016719) | 2.377494 / 3.745712 (-1.368218) | 2.720767 / 5.269862 (-2.549094) | 1.710763 / 4.565676 (-2.854914) | 0.061498 / 0.424275 (-0.362778) | 0.004893 / 0.007607 (-0.002715) | 0.335633 / 0.226044 (0.109588) | 3.380646 / 2.268929 (1.111717) | 1.802436 / 55.444624 (-53.642188) | 1.562737 / 6.876477 (-5.313739) | 1.566267 / 2.142072 (-0.575806) | 0.629058 / 4.805227 (-4.176169) | 0.116307 / 6.500664 (-6.384357) | 0.042174 / 0.075469 (-0.033295) |

### 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.950945 / 1.841788 (-0.890842) | 11.279009 / 8.074308 (3.204701) | 9.433251 / 10.191392 (-0.758141) | 0.138964 / 0.680424 (-0.541460) | 0.014155 / 0.534201 (-0.520046) | 0.284065 / 0.579283 (-0.295218) | 0.263301 / 0.434364 (-0.171063) | 0.331932 / 0.540337 (-0.208406) | 0.441656 / 1.386936 (-0.945280) |

</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.005132 / 0.011353 (-0.006221) | 0.003484 / 0.011008 (-0.007524) | 0.049040 / 0.038508 (0.010532) | 0.030254 / 0.023109 (0.007145) | 0.277141 / 0.275898 (0.001243) | 0.295242 / 0.323480 (-0.028238) | 0.004295 / 0.007986 (-0.003690) | 0.002632 / 0.004328 (-0.001696) | 0.048540 / 0.004250 (0.044290) | 0.044787 / 0.037052 (0.007734) | 0.287736 / 0.258489 (0.029247) | 0.313146 / 0.293841 (0.019305) | 0.029340 / 0.128546 (-0.099206) | 0.010204 / 0.075646 (-0.065442) | 0.059058 / 0.419271 (-0.360214) | 0.051033 / 0.043533 (0.007500) | 0.274086 / 0.255139 (0.018947) | 0.293048 / 0.283200 (0.009848) | 0.019573 / 0.141683 (-0.122110) | 1.174032 / 1.452155 (-0.278123) | 1.227107 / 1.492716 (-0.265609) |

### 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.094896 / 0.018006 (0.076890) | 0.303519 / 0.000490 (0.303029) | 0.000223 / 0.000200 (0.000023) | 0.000049 / 0.000054 (-0.000005) |

### Benchmark: benchmark_indices_mapping.json

| metric | select | shard | shuffle | sort | train_test_split |
|--------|---|---|---|---|---|
| new / old (diff) | 0.021495 / 0.037411 (-0.015917) | 0.074234 / 0.014526 (0.059708) | 0.086212 / 0.176557 (-0.090345) | 0.125052 / 0.737135 (-0.612084) | 0.087464 / 0.296338 (-0.208874) |

### 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.297098 / 0.215209 (0.081889) | 2.970944 / 2.077655 (0.893289) | 1.650101 / 1.504120 (0.145981) | 1.532694 / 1.541195 (-0.008501) | 1.513652 / 1.468490 (0.045162) | 0.559614 / 4.584777 (-4.025163) | 2.404848 / 3.745712 (-1.340865) | 2.627851 / 5.269862 (-2.642011) | 1.707550 / 4.565676 (-2.858127) | 0.061821 / 0.424275 (-0.362454) | 0.005012 / 0.007607 (-0.002595) | 0.342462 / 0.226044 (0.116417) | 3.401703 / 2.268929 (1.132774) | 1.991632 / 55.444624 (-53.452993) | 1.737706 / 6.876477 (-5.138771) | 1.837457 / 2.142072 (-0.304616) | 0.638845 / 4.805227 (-4.166383) | 0.114773 / 6.500664 (-6.385891) | 0.040175 / 0.075469 (-0.035294) |

### 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.038286 / 1.841788 (-0.803501) | 11.885757 / 8.074308 (3.811448) | 10.061530 / 10.191392 (-0.129862) | 0.140824 / 0.680424 (-0.539600) | 0.015080 / 0.534201 (-0.519121) | 0.287992 / 0.579283 (-0.291291) | 0.273498 / 0.434364 (-0.160866) | 0.326478 / 0.540337 (-0.213860) | 0.426900 / 1.386936 (-0.960036) |

</details>
</details>

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