`UserWarning: TypedStorage is deprecated.` on loading `pytorch_model.bin` files from disk.
Author: tomaarsen
Pull request: no

### System Info

- `transformers` version: 4.37.1
- Platform: Windows-10-10.0.22631-SP0
- Python version: 3.9.17
- Huggingface_hub version: 0.20.2
- Safetensors version: 0.4.2
- Accelerate version: 0.21.0
- Accelerate config:    not found
- PyTorch version (GPU?): 2.1.2+cu121 (True)
- Tensorflow version (GPU?): not installed (NA)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using GPU in script?: Yes
- Using distributed or parallel set-up in script?: No

### Who can help?

@amyeroberts 

### Information

- [ ] The official example scripts
- [X] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [X] My own task or dataset (give details below)

### Reproduction


```python
from transformers import AutoModel

model = AutoModel.from_pretrained("bert-base-uncased", use_safetensors=False)
```

This resulted in:
```
C:\Users\tom\.conda\envs\transformers\lib\site-packages\torch\_utils.py:831: UserWarning: TypedStorage is deprecated. It will be removed in the future and UntypedStorage will be the only storage class. This should only matter to you if you are using storages directly.  To access UntypedStorage directly, use tensor.untyped_storage() instead of tensor.storage()
  return self.fget.__get__(instance, owner)()
```
See also https://github.com/UKPLab/sentence-transformers/issues/2450

Notably, I do not get this warning at transformers v4.36.2.

### Expected behavior

I don't expect any warnings from loading a model with `pytorch_model.bin`.

- Tom Aarsen

Comments:
Hi @tomaarsen, thanks for raising this! 

Indeed, this shouldn't be happening. Looking into it

Looking into it, this a consequence of https://github.com/huggingface/transformers/pull/27282 - when `weights_only=True`, then [_legacy_load is used by torch to load the state dict](https://github.com/pytorch/pytorch/blob/a72190fd51f19cbfb5c09ae3088729f94aef7141/torch/serialization.py#L1036). The error is thrown if the `storage_dtype` is type e.g. `torch.FloatStorage`.

cc'ing in @Narsil who knows more about the tensors and how they're stored. In particular, in the warning message we have: 

```
/data/ml/lib/python3.10/site-packages/torch/_utils.py:831: UserWarning: TypedStorage is deprecated. It will be removed in the future and UntypedStorage will be the only storage class. This should only matter to you if you are using storages directly.  To access UntypedStorage directly, use tensor.untyped_storage() instead of tensor.storage()
```

Will this cause issue for use? I'm unsure what is meant by "using storages directly". 

If it's not relevant for us (e.g. if we do not "use storages directly", then we could adopt this to avoid the warning: https://github.com/pytorch/pytorch/blob/a72190fd51f19cbfb5c09ae3088729f94aef7141/torch/storage.py#L475C9-L475C36


No it's just using a legacy load for this particular file.
Newer files can load perfectly with weights_only=True: https://github.com/pytorch/pytorch/blob/a72190fd51f19cbfb5c09ae3088729f94aef7141/torch/serialization.py#L1016

This should be fixed upstream, as I doubt it's our calls that trigger the warning.

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.