Automatic Conversion for uint16/uint32 to Compatible PyTorch Dtypes
State: closed

This PR addresses an issue encountered when utilizing uint16 or uint32 datatypes with datasets, followed by attempting to convert these datasets into PyTorch-compatible formats. Currently, doing so results in a TypeError due to incompatible datatype conversion, as illustrated by the following example:
```python
from datasets import Dataset, Sequence, Value, Features

def gen():
    for i in range(100):
        yield {'seq': list(range(i, i + 20))}

ds = Dataset.from_generator(gen, features=Features({'seq': Sequence(feature=Value(dtype='uint16'), length=-1)}))

ds.set_format('torch')

print(ds[0])
```
This code snippet triggers the following error due to the inability to convert numpy.uint16 arrays to a PyTorch-supported format:
```
TypeError: can't convert np.ndarray of type numpy.uint16. The only supported types are: float64, float32, float16, complex64, complex128, int64, int32, int16, int8, uint8, and bool.
```

This PR introduces an automatic mechanism to convert np.uint16 and np.uint32 datatypes to np.int64 for seamless compatibility with PyTorch formats, simplifying workflows and improving developer experience by eliminating the need for manual conversion handling.

The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/datasets/pr_6660). 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.004995 / 0.011353 (-0.006357) | 0.003230 / 0.011008 (-0.007779) | 0.062836 / 0.038508 (0.024328) | 0.026684 / 0.023109 (0.003575) | 0.249286 / 0.275898 (-0.026612) | 0.272936 / 0.323480 (-0.050544) | 0.003952 / 0.007986 (-0.004033) | 0.002708 / 0.004328 (-0.001620) | 0.055346 / 0.004250 (0.051095) | 0.040023 / 0.037052 (0.002971) | 0.263350 / 0.258489 (0.004860) | 0.294727 / 0.293841 (0.000886) | 0.027280 / 0.128546 (-0.101266) | 0.010273 / 0.075646 (-0.065373) | 0.206035 / 0.419271 (-0.213236) | 0.035715 / 0.043533 (-0.007818) | 0.255474 / 0.255139 (0.000335) | 0.273960 / 0.283200 (-0.009240) | 0.018563 / 0.141683 (-0.123120) | 1.140013 / 1.452155 (-0.312142) | 1.188655 / 1.492716 (-0.304062) |

### 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.091895 / 0.018006 (0.073888) | 0.284621 / 0.000490 (0.284131) | 0.000213 / 0.000200 (0.000013) | 0.000043 / 0.000054 (-0.000011) |

### Benchmark: benchmark_indices_mapping.json

| metric | select | shard | shuffle | sort | train_test_split |
|--------|---|---|---|---|---|
| new / old (diff) | 0.018610 / 0.037411 (-0.018801) | 0.061554 / 0.014526 (0.047028) | 0.072454 / 0.176557 (-0.104103) | 0.120283 / 0.737135 (-0.616853) | 0.073744 / 0.296338 (-0.222595) |

### 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.288850 / 0.215209 (0.073641) | 2.836761 / 2.077655 (0.759107) | 1.533407 / 1.504120 (0.029287) | 1.409394 / 1.541195 (-0.131801) | 1.421667 / 1.468490 (-0.046823) | 0.566183 / 4.584777 (-4.018594) | 2.390670 / 3.745712 (-1.355043) | 2.732031 / 5.269862 (-2.537831) | 1.730886 / 4.565676 (-2.834791) | 0.064280 / 0.424275 (-0.359995) | 0.004959 / 0.007607 (-0.002648) | 0.342664 / 0.226044 (0.116619) | 3.398969 / 2.268929 (1.130040) | 1.887354 / 55.444624 (-53.557270) | 1.572955 / 6.876477 (-5.303522) | 1.596179 / 2.142072 (-0.545894) | 0.645844 / 4.805227 (-4.159383) | 0.118050 / 6.500664 (-6.382614) | 0.042158 / 0.075469 (-0.033311) |

### 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.959170 / 1.841788 (-0.882617) | 11.276491 / 8.074308 (3.202183) | 9.471198 / 10.191392 (-0.720194) | 0.128346 / 0.680424 (-0.552078) | 0.013851 / 0.534201 (-0.520350) | 0.286125 / 0.579283 (-0.293158) | 0.266915 / 0.434364 (-0.167449) | 0.332811 / 0.540337 (-0.207526) | 0.444780 / 1.386936 (-0.942156) |

</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.005665 / 0.011353 (-0.005687) | 0.003267 / 0.011008 (-0.007741) | 0.050238 / 0.038508 (0.011730) | 0.032882 / 0.023109 (0.009773) | 0.269320 / 0.275898 (-0.006578) | 0.293140 / 0.323480 (-0.030340) | 0.004127 / 0.007986 (-0.003858) | 0.002728 / 0.004328 (-0.001601) | 0.049360 / 0.004250 (0.045109) | 0.043764 / 0.037052 (0.006712) | 0.291211 / 0.258489 (0.032722) | 0.319745 / 0.293841 (0.025904) | 0.029371 / 0.128546 (-0.099175) | 0.010212 / 0.075646 (-0.065434) | 0.059064 / 0.419271 (-0.360207) | 0.051148 / 0.043533 (0.007615) | 0.276698 / 0.255139 (0.021559) | 0.292329 / 0.283200 (0.009129) | 0.018349 / 0.141683 (-0.123334) | 1.150816 / 1.452155 (-0.301338) | 1.184292 / 1.492716 (-0.308425) |

### 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.091646 / 0.018006 (0.073640) | 0.301737 / 0.000490 (0.301247) | 0.000214 / 0.000200 (0.000014) | 0.000051 / 0.000054 (-0.000004) |

### Benchmark: benchmark_indices_mapping.json

| metric | select | shard | shuffle | sort | train_test_split |
|--------|---|---|---|---|---|
| new / old (diff) | 0.021529 / 0.037411 (-0.015883) | 0.075596 / 0.014526 (0.061070) | 0.087912 / 0.176557 (-0.088645) | 0.125240 / 0.737135 (-0.611895) | 0.088035 / 0.296338 (-0.208303) |

### 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.305097 / 0.215209 (0.089888) | 2.979612 / 2.077655 (0.901957) | 1.647009 / 1.504120 (0.142889) | 1.520251 / 1.541195 (-0.020944) | 1.513361 / 1.468490 (0.044870) | 0.571733 / 4.584777 (-4.013044) | 2.415587 / 3.745712 (-1.330125) | 2.615983 / 5.269862 (-2.653879) | 1.732637 / 4.565676 (-2.833039) | 0.062830 / 0.424275 (-0.361445) | 0.004972 / 0.007607 (-0.002635) | 0.348559 / 0.226044 (0.122515) | 3.450567 / 2.268929 (1.181639) | 1.970743 / 55.444624 (-53.473882) | 1.702232 / 6.876477 (-5.174245) | 1.799592 / 2.142072 (-0.342480) | 0.649477 / 4.805227 (-4.155751) | 0.115940 / 6.500664 (-6.384724) | 0.040364 / 0.075469 (-0.035105) |

### 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.000014 / 1.841788 (-0.841773) | 11.937886 / 8.074308 (3.863578) | 10.169478 / 10.191392 (-0.021914) | 0.153359 / 0.680424 (-0.527064) | 0.015205 / 0.534201 (-0.518996) | 0.287812 / 0.579283 (-0.291471) | 0.278688 / 0.434364 (-0.155676) | 0.322831 / 0.540337 (-0.217507) | 0.425631 / 1.386936 (-0.961305) |

</details>
</details>

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