Fix `datasets.load_from_disk`, `DatasetDict.load_from_disk` and `Dataset.load_from_disk`
Opened by alvarobartt (closed)

## What's in this PR?

After playing around a little bit with 🤗`datasets` in Google Cloud Storage (GCS), I found out some things that should be fixed IMO in the code:

* `datasets.load_from_disk` is not checking whether `state.json` is there too when trying to load a `Dataset`, just `dataset_info.json` is checked
* `DatasetDict.load_from_disk` is not checking whether `state.json` is there too when redirecting the user to load it as `datasets.load_from_disk`, just `dataset_info.json` is checked, which is misleading, as it won't be loadable that way either
* `Dataset.load_from_disk` is missing the `extract_path_from_uri` call before checking in the `fs` whether `dataset_info.json` and `dataset_dict.json` exist, which when using `gcsfs` leads to 400 error code (not blocking) due to `gcsfs.retry.HttpError: Invalid bucket name: 'gs:', 400`
* And, finally, the exception messages are a little bit misleading / incomplete IMO so I've tried to include all the relevant information in the messages to avoid issues when interpreting the exceptions

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

Hmm, should this also be updated in `Dataset.load_from_disk` and `DatasetDict.load_from_disk`? https://github.com/huggingface/datasets/pull/5466 As there the paths are joined using `Path(..., ...)` and it won't work on Windows OS according to that PR, right?

Hi, @lhoestq could you review this PR? Thank you in advance and sorry for the ping 🤗 

Besides that, I was also thinking of adding a `skip_validation` boolean arg in both `Dataset.load_from_disk` and `DatasetDict.load_from_disk` to avoid duplicating those calls too when those functions are called from `datasets.load_from_disk`.

So that `skip_validation` is set to `False` by default, but passed as `True` if called from `datasets.load_from_disk`, and that just affects the file checking part of the code on both functions, do you agree @lhoestq?

I think we should always verify

> I think we should always verify

But with the current way we're also verifying twice right? First on `datasets.load_from_disk` then on `Dataset.load_from_disk`, right?

Maybe a warning before calling either `Dataset.load_from_disk` or `DatasetDict.load_from_disk` is enough?

e.g. **"Consider using `Dataset.load_from_disk` instead to avoid `fsspec` from verifying the presence of `dataset_info.json` and `state.json` in the remote filesystem twice."** to be showed just when `fs` is remote.

I don't think it's worth adding a new argument just for that. Usually we keep the set of arguments to the strict minimum

> I don't think it's worth adding a new argument just for that. Usually we keep the set of arguments to the strict minimum

What about the warning?

Anyway, if you don't think that's worth it feel free to merge 👍🏻 

> What about the warning?

We may show warnings for suggestions, but only if the user does a very unoptimized thing. Here we're not at that level ^^'

Thanks for the explanation and feedback @lhoestq 🤗 

> Thank you :) Added my last suggestions:

Thanks for the feedback, I agree with everything besides one nit! 👍🏻 

<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.011556 / 0.011353 (0.000203) | 0.006213 / 0.011008 (-0.004796) | 0.132390 / 0.038508 (0.093882) | 0.034609 / 0.023109 (0.011500) | 0.361156 / 0.275898 (0.085258) | 0.402524 / 0.323480 (0.079044) | 0.009138 / 0.007986 (0.001152) | 0.005728 / 0.004328 (0.001399) | 0.115406 / 0.004250 (0.111156) | 0.041440 / 0.037052 (0.004388) | 0.370232 / 0.258489 (0.111742) | 0.409944 / 0.293841 (0.116103) | 0.053803 / 0.128546 (-0.074744) | 0.022029 / 0.075646 (-0.053617) | 0.400325 / 0.419271 (-0.018946) | 0.055324 / 0.043533 (0.011791) | 0.368699 / 0.255139 (0.113560) | 0.391836 / 0.283200 (0.108636) | 0.099356 / 0.141683 (-0.042327) | 1.687881 / 1.452155 (0.235726) | 1.752202 / 1.492716 (0.259485) |

### 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.012992 / 0.018006 (-0.005014) | 0.518756 / 0.000490 (0.518267) | 0.004702 / 0.000200 (0.004502) | 0.000105 / 0.000054 (0.000050) |

### Benchmark: benchmark_indices_mapping.json

| metric | select | shard | shuffle | sort | train_test_split |
|--------|---|---|---|---|---|
| new / old (diff) | 0.028371 / 0.037411 (-0.009041) | 0.127058 / 0.014526 (0.112532) | 0.136908 / 0.176557 (-0.039649) | 0.210168 / 0.737135 (-0.526968) | 0.139600 / 0.296338 (-0.156738) |

### 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.570901 / 0.215209 (0.355692) | 5.967213 / 2.077655 (3.889558) | 2.286745 / 1.504120 (0.782626) | 1.950682 / 1.541195 (0.409487) | 2.062536 / 1.468490 (0.594046) | 1.255671 / 4.584777 (-3.329106) | 5.454951 / 3.745712 (1.709238) | 3.076429 / 5.269862 (-2.193433) | 2.082871 / 4.565676 (-2.482806) | 0.150069 / 0.424275 (-0.274206) | 0.014864 / 0.007607 (0.007257) | 0.774672 / 0.226044 (0.548627) | 7.873992 / 2.268929 (5.605064) | 3.196165 / 55.444624 (-52.248459) | 2.366854 / 6.876477 (-4.509623) | 2.407381 / 2.142072 (0.265309) | 1.419130 / 4.805227 (-3.386097) | 0.249210 / 6.500664 (-6.251454) | 0.088648 / 0.075469 (0.013179) |

### 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.528368 / 1.841788 (-0.313420) | 17.554000 / 8.074308 (9.479692) | 20.773300 / 10.191392 (10.581908) | 0.216903 / 0.680424 (-0.463521) | 0.046544 / 0.534201 (-0.487657) | 0.538238 / 0.579283 (-0.041045) | 0.673926 / 0.434364 (0.239562) | 0.656108 / 0.540337 (0.115770) | 0.774026 / 1.386936 (-0.612910) |

</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.010177 / 0.011353 (-0.001176) | 0.006334 / 0.011008 (-0.004675) | 0.100097 / 0.038508 (0.061589) | 0.039996 / 0.023109 (0.016887) | 0.420225 / 0.275898 (0.144327) | 0.437694 / 0.323480 (0.114214) | 0.007987 / 0.007986 (0.000002) | 0.005782 / 0.004328 (0.001454) | 0.106421 / 0.004250 (0.102171) | 0.046993 / 0.037052 (0.009941) | 0.397304 / 0.258489 (0.138815) | 0.441780 / 0.293841 (0.147939) | 0.064594 / 0.128546 (-0.063952) | 0.020823 / 0.075646 (-0.054823) | 0.108854 / 0.419271 (-0.310417) | 0.076457 / 0.043533 (0.032924) | 0.401712 / 0.255139 (0.146573) | 0.459292 / 0.283200 (0.176093) | 0.125044 / 0.141683 (-0.016639) | 1.765531 / 1.452155 (0.313377) | 1.845429 / 1.492716 (0.352713) |

### 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.225549 / 0.018006 (0.207543) | 0.524402 / 0.000490 (0.523913) | 0.006994 / 0.000200 (0.006794) | 0.000120 / 0.000054 (0.000065) |

### Benchmark: benchmark_indices_mapping.json

| metric | select | shard | shuffle | sort | train_test_split |
|--------|---|---|---|---|---|
| new / old (diff) | 0.033787 / 0.037411 (-0.003624) | 0.144895 / 0.014526 (0.130369) | 0.147185 / 0.176557 (-0.029371) | 0.228227 / 0.737135 (-0.508908) | 0.164967 / 0.296338 (-0.131371) |

### 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.628242 / 0.215209 (0.413033) | 6.348176 / 2.077655 (4.270522) | 2.615832 / 1.504120 (1.111712) | 2.217481 / 1.541195 (0.676286) | 2.287058 / 1.468490 (0.818568) | 1.322854 / 4.584777 (-3.261923) | 5.547831 / 3.745712 (1.802119) | 3.199467 / 5.269862 (-2.070395) | 2.135297 / 4.565676 (-2.430380) | 0.165134 / 0.424275 (-0.259141) | 0.014753 / 0.007607 (0.007146) | 0.778579 / 0.226044 (0.552535) | 7.982329 / 2.268929 (5.713401) | 3.331712 / 55.444624 (-52.112913) | 2.642606 / 6.876477 (-4.233871) | 2.699362 / 2.142072 (0.557290) | 1.572268 / 4.805227 (-3.232959) | 0.273348 / 6.500664 (-6.227316) | 0.082975 / 0.075469 (0.007506) |

### 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.730421 / 1.841788 (-0.111367) | 18.154495 / 8.074308 (10.080187) | 20.969885 / 10.191392 (10.778493) | 0.233652 / 0.680424 (-0.446772) | 0.026609 / 0.534201 (-0.507592) | 0.546874 / 0.579283 (-0.032410) | 0.602891 / 0.434364 (0.168527) | 0.641073 / 0.540337 (0.100736) | 0.772138 / 1.386936 (-0.614798) |

</details>
</details>

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