AttributeError: module 'torch' has no attribute 'frombuffer' when finetuning multilang bert
Author: amine-boukriba
Pull request: no

### System Info

- `transformers` version: 4.25.1
- Platform: Windows-10-10.0.22621-SP0
- Python version: 3.9.13
- Huggingface_hub version: 0.15.1
- PyTorch version (GPU?): 1.7.1+cu110 (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?

_No response_

### Information

- [X] The official example scripts
- [ ] 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

I am using the fine-tuning script provided by the official hugging face website :
https://huggingface.co/docs/transformers/tasks/sequence_classification
```
from transformers import AutoModelForSequenceClassification, TrainingArguments, Trainer

model = AutoModelForSequenceClassification.from_pretrained(
    "bert-base-multilingual-cased", num_labels=18, id2label=id2label, label2id=label2id
)
```


### Expected behavior

In this part I got the following error :AttributeError: module 'torch' has no attribute 'frombuffer'
I have torch 1.7 and for some resons i cannot install torch 2.0 so I tried downgrading transformers to a version where torch 2.0 hasn't been made yet (march 2023) I used transformers 2.25.1 but nothing happaned

Comments:
I have the same problem, anyone know how to fix it?

@amine-boukriba @Vio1etovo So that we can best help you, could you share the full traceback of the error? 

Does the error occur when running: 
```
model = AutoModelForSequenceClassification.from_pretrained(
    "bert-base-multilingual-cased", num_labels=18, id2label=id2label, label2id=label2id
)
```
?

Note: on the most recent release of transformers, torch >= 1.9 is supported. Could you try installing a later version of torch and the most recent transformers version? 

> @amine-boukriba @Vio1etovo So that we can best help you, could you share the full traceback of the error?
> 
> Does the error occur when running:
> 
> ```
> model = AutoModelForSequenceClassification.from_pretrained(
>     "bert-base-multilingual-cased", num_labels=18, id2label=id2label, label2id=label2id
> )
> ```
> 
> ?
> 
> Note: on the most recent release of transformers, torch >= 1.9 is supported. Could you try installing a later version of torch and the most recent transformers version?

sorry，I did not encounter this problem in reproducing this job. Instead, using GroundingDINO on [Playground](https://github.com/open-mmlab/playground/blob/main/README.md#-mmdet-sam) causes this issue.

Although you mentioned that `torch>=1.9.0` is supported, I found in the official` torch 1.9.0` document that `torch.frombuffer` does not exist, and `torch 2.0.0` does.

My environment configuration is as follows:
```
ubuntu18.04.6-torch1.9-py3.8-cuda11.1-gpu
```

The first two commands ran normally, while the third (using GroundingDINO) ran incorrectly. The error is as follows:

```
root@vscode-1690977852929548290-8588864677-qfmcx:/work/home/sam/playground-main/playground-main/mmdet_sam# python detector_sam_demo.py ../images/cat_remote.jpg \
>     configs/GroundingDINO_SwinT_OGC.py \
>     ../models/groundingdino_swint_ogc.pth \
>     -t "cat . remote" \
>     --sam-device cpu
final text_encoder_type: bert-base-uncased
Traceback (most recent call last):
  File "detector_sam_demo.py", line 529, in <module>
    main()
  File "detector_sam_demo.py", line 450, in main
    det_model = build_detecter(args)
  File "detector_sam_demo.py", line 168, in build_detecter
    detecter = __build_grounding_dino_model(args)
  File "detector_sam_demo.py", line 124, in __build_grounding_dino_model
    model = build_model(gdino_args)
  File "/work/home/sam/playground-main/GroundingDINO/groundingdino/models/__init__.py", line 17, in build_model
    model = build_func(args)
  File "/work/home/sam/playground-main/GroundingDINO/groundingdino/models/GroundingDINO/groundingdino.py", line 388, in build_groundingdino
    model = GroundingDINO(
  File "/work/home/sam/playground-main/GroundingDINO/groundingdino/models/GroundingDINO/groundingdino.py", line 108, in __init__
    self.bert = get_tokenlizer.get_pretrained_language_model(text_encoder_type)
  File "/work/home/sam/playground-main/GroundingDINO/groundingdino/util/get_tokenlizer.py", line 25, in get_pretrained_language_model
    return BertModel.from_pretrained(text_encoder_type)
  File "/usr/local/lib/python3.8/dist-packages/transformers/modeling_utils.py", line 2805, in from_pretrained
    state_dict = load_state_dict(resolved_archive_file)
  File "/usr/local/lib/python3.8/dist-packages/transformers/modeling_utils.py", line 469, in load_state_dict
    return safe_load_file(checkpoint_file)
  File "/usr/local/lib/python3.8/dist-packages/safetensors/torch.py", line 310, in load_file
    result[k] = f.get_tensor(k)
AttributeError: module 'torch' has no attribute 'frombuffer'
```



i solved it only by upgrading torch to 2.0.1
```
pip install torch==2.0.1+cu117 torchvision==0.15.2+cu117 -f https://download.pytorch.org/whl/torch_stable.html
```

@Vio1etovo Thanks for providing the full traceback. It looks like the issue is coming from the safetensors library using more recent torch features. 

cc @Narsil @ydshieh `frombuffer` was [added in torch 1.10](https://github.com/pytorch/pytorch/commit/8854817f448fa23485b24303d4665b2db71feba5) 22 months ago. [Torch 1.9 was released](https://github.com/pytorch/pytorch/releases/tag/v1.9.0) > 2 years ago, which I believe is our support cut-off. How would you both suggest handling this? We could conditionally use safetensors if torch>=1.10 is available or deprecate and move to torch 1.10 (~2 months early) or something else?

From  
```bash
  File "/usr/local/lib/python3.8/dist-packages/safetensors/torch.py", line 310, in load_file
    result[k] = f.get_tensor(k)
```
(`safetensors`)
I will leave @Narsil to express. But in terms of maintenance, torch 1.9 is no longer supported by `transformers` and we should not to try to use it even in a conditional way.

I have the same issue and I can't change the version of torch (1.9.0). It is possible to have the code working by downgrade safetensors ? I tried 0.3.1 and 0.3.2 and still not work. There is another way ? Thanks for help

Ok it seems to work with `transformers==4.29.2` and `safetensors==0.3.0`

> i solved it only by upgrading torch to 2.0.1
> 
> ```
> pip install torch==2.0.1+cu117 torchvision==0.15.2+cu117 -f https://download.pytorch.org/whl/torch_stable.html
> ```

Upgrading to 1.10.0 also works

At the time of making safetensors a  core dependency @sgugger suggested that leaving 1.9 deprecated was OK.
It would be a quite sizeable chunk of work to support 1.9 (lots of code has changed around around storage which is currently what's being used).

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.

> Ok it seems to work with `transformers==4.29.2` and `safetensors==0.3.0`

For anyone facing the issue. This seems to work (without changing torch version)