Add JAX-formatting documentation
Opened by alvarobartt (closed)

## What's in this PR?

As a follow-up of #5522, I've created this entry in the documentation to explain how to use `.with_format("jax")` and why is it useful.

@lhoestq Feel free to drop any feedback and/or suggestion, as probably more useful features can be included there!

Comments:
_The documentation is not available anymore as the PR was closed or merged._

> Awesome thank you !
> 
> Could you also explain how to use certain types like ClassLabel, Image or Audio with jax ? You can get a lot of inspiration from the "Other feature types" section in the [PyTorch page](https://huggingface.co/docs/datasets/use_with_pytorch)
> 
> I also think it's be nice if this page had the same structure as the pytorch or tf ones, with sections named
> 
>     * Dataset format
> 
>     * N-dimensional arrays
> 
>     * Other feature types
> 
>     * Data loading

Sure @lhoestq I'll do that later this afternoon whenever I'm done working! Thanks for the feedback as always 🤗

Also, @lhoestq do you want me to elaborate more on the `## Data loading` section on how to use `datasets` to train a JAX model offering alternatives e.g. `Flax`, or do I keep it pure JAX? Thanks!

If you have a good example with `flax` it can also be helpful for users

For now, I think that probably it's not worth adding a `Flax` example, as train loops need to be done manually as in pure JAX, so probably the JAX example is enough. Anyway, let me know if you see something missing/incomplete/misleading/etc. and I'll update that ASAP 👍🏻 

P.S. I see that the `benchmark` action is being triggered on every PR, is it worth it? e.g. now I'm just editing the docs, so does it make any sense to trigger still the whole CI pipeline (including `benchmark`)? Just asking because in this PR for example it could be skipped.

> P.S. I see that the benchmark action is being triggered on every PR, is it worth it? e.g. now I'm just editing the docs, so does it make any sense to trigger still the whole CI pipeline (including benchmark)? Just asking because in this PR for example it could be skipped.

We could restrict it to PRs modifying files in src/ indeed ^^'

> LGTM :)

Cool thanks! My bad I didn't update those code blocks 🙃 Thanks for doing so before merge!

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

PyArrow==6.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.009336 / 0.011353 (-0.002017) | 0.005037 / 0.011008 (-0.005971) | 0.102168 / 0.038508 (0.063659) | 0.035351 / 0.023109 (0.012242) | 0.299616 / 0.275898 (0.023718) | 0.333269 / 0.323480 (0.009789) | 0.008215 / 0.007986 (0.000229) | 0.005047 / 0.004328 (0.000718) | 0.074257 / 0.004250 (0.070007) | 0.045080 / 0.037052 (0.008028) | 0.300657 / 0.258489 (0.042168) | 0.357569 / 0.293841 (0.063728) | 0.038614 / 0.128546 (-0.089932) | 0.011995 / 0.075646 (-0.063651) | 0.369141 / 0.419271 (-0.050130) | 0.047603 / 0.043533 (0.004070) | 0.297694 / 0.255139 (0.042555) | 0.315380 / 0.283200 (0.032180) | 0.105009 / 0.141683 (-0.036674) | 1.421077 / 1.452155 (-0.031078) | 1.550024 / 1.492716 (0.057308) |

### 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.239026 / 0.018006 (0.221020) | 0.550010 / 0.000490 (0.549520) | 0.003294 / 0.000200 (0.003094) | 0.000093 / 0.000054 (0.000038) |

### Benchmark: benchmark_indices_mapping.json

| metric | select | shard | shuffle | sort | train_test_split |
|--------|---|---|---|---|---|
| new / old (diff) | 0.027180 / 0.037411 (-0.010231) | 0.107942 / 0.014526 (0.093416) | 0.121092 / 0.176557 (-0.055464) | 0.161028 / 0.737135 (-0.576108) | 0.124615 / 0.296338 (-0.171723) |

### 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.399492 / 0.215209 (0.184283) | 3.984685 / 2.077655 (1.907030) | 1.794784 / 1.504120 (0.290664) | 1.604849 / 1.541195 (0.063654) | 1.682994 / 1.468490 (0.214504) | 0.691197 / 4.584777 (-3.893580) | 3.741816 / 3.745712 (-0.003897) | 2.092151 / 5.269862 (-3.177711) | 1.319106 / 4.565676 (-3.246570) | 0.083875 / 0.424275 (-0.340400) | 0.012473 / 0.007607 (0.004866) | 0.514057 / 0.226044 (0.288012) | 5.110217 / 2.268929 (2.841288) | 2.259105 / 55.444624 (-53.185519) | 1.914021 / 6.876477 (-4.962455) | 1.958371 / 2.142072 (-0.183701) | 0.819800 / 4.805227 (-3.985428) | 0.161153 / 6.500664 (-6.339511) | 0.061967 / 0.075469 (-0.013502) |

### 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.198553 / 1.841788 (-0.643234) | 14.793201 / 8.074308 (6.718893) | 14.646807 / 10.191392 (4.455415) | 0.152805 / 0.680424 (-0.527619) | 0.029206 / 0.534201 (-0.504995) | 0.440875 / 0.579283 (-0.138408) | 0.434925 / 0.434364 (0.000561) | 0.533495 / 0.540337 (-0.006842) | 0.624479 / 1.386936 (-0.762457) |

</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.007346 / 0.011353 (-0.004007) | 0.005422 / 0.011008 (-0.005586) | 0.073930 / 0.038508 (0.035422) | 0.032978 / 0.023109 (0.009869) | 0.335182 / 0.275898 (0.059284) | 0.371916 / 0.323480 (0.048436) | 0.005851 / 0.007986 (-0.002135) | 0.005582 / 0.004328 (0.001254) | 0.073090 / 0.004250 (0.068839) | 0.048395 / 0.037052 (0.011342) | 0.353921 / 0.258489 (0.095432) | 0.380678 / 0.293841 (0.086837) | 0.036628 / 0.128546 (-0.091919) | 0.012392 / 0.075646 (-0.063254) | 0.086265 / 0.419271 (-0.333006) | 0.049262 / 0.043533 (0.005729) | 0.334790 / 0.255139 (0.079651) | 0.355278 / 0.283200 (0.072078) | 0.102714 / 0.141683 (-0.038969) | 1.536366 / 1.452155 (0.084211) | 1.565984 / 1.492716 (0.073268) |

### 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.216050 / 0.018006 (0.198043) | 0.554972 / 0.000490 (0.554482) | 0.002432 / 0.000200 (0.002232) | 0.000110 / 0.000054 (0.000055) |

### Benchmark: benchmark_indices_mapping.json

| metric | select | shard | shuffle | sort | train_test_split |
|--------|---|---|---|---|---|
| new / old (diff) | 0.028602 / 0.037411 (-0.008809) | 0.123681 / 0.014526 (0.109155) | 0.136763 / 0.176557 (-0.039793) | 0.170083 / 0.737135 (-0.567052) | 0.138771 / 0.296338 (-0.157567) |

### 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.420036 / 0.215209 (0.204827) | 4.188734 / 2.077655 (2.111079) | 2.014758 / 1.504120 (0.510638) | 1.818423 / 1.541195 (0.277228) | 1.940790 / 1.468490 (0.472300) | 0.691420 / 4.584777 (-3.893357) | 3.782996 / 3.745712 (0.037284) | 2.131278 / 5.269862 (-3.138583) | 1.363043 / 4.565676 (-3.202633) | 0.087182 / 0.424275 (-0.337093) | 0.012448 / 0.007607 (0.004841) | 0.519296 / 0.226044 (0.293252) | 5.220397 / 2.268929 (2.951469) | 2.474243 / 55.444624 (-52.970381) | 2.139726 / 6.876477 (-4.736751) | 2.200700 / 2.142072 (0.058627) | 0.841171 / 4.805227 (-3.964056) | 0.169234 / 6.500664 (-6.331430) | 0.063879 / 0.075469 (-0.011590) |

### 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.260262 / 1.841788 (-0.581526) | 14.853209 / 8.074308 (6.778901) | 13.944085 / 10.191392 (3.752693) | 0.192014 / 0.680424 (-0.488410) | 0.017811 / 0.534201 (-0.516390) | 0.427166 / 0.579283 (-0.152117) | 0.438263 / 0.434364 (0.003899) | 0.538815 / 0.540337 (-0.001523) | 0.641398 / 1.386936 (-0.745538) |

</details>
</details>

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