Pegasus: OSError: Unable to load weights from pytorch checkpoint file.
Environment info
transformersversion: 3.0.2- Platform: macOS-10.14.6-x86_64-i386-64bit
- Python version: 3.8.5
- PyTorch version (GPU?): 1.6.0 (False)
- Tensorflow version (GPU?): 2.2.0 (False)
- Using GPU in script?: No
- Using distributed or parallel set-up in script?: No
Who can help
@sshleifer
Information
Model I am using (Bert, XLNet ...): google/pegasus-cnn_dailymail
The problem arises when using:
import torch
from transformers import PegasusForConditionalGeneration, PegasusTokenizer
torch_device = 'cuda' if torch.cuda.is_available() else 'cpu'
model_name = 'google/pegasus-cnn_dailymail'
tokenizer = PegasusTokenizer.from_pretrained(model_name)
model = PegasusForConditionalGeneration.from_pretrained(model_name).to(torch_device)
Traceback:
RuntimeError Traceback (most recent call last)
~/projects/transformers/src/transformers/modeling_utils.py in from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)
854 try:
--> 855 state_dict = torch.load(resolved_archive_file, map_location="cpu")
856 except Exception:
~/anaconda3/envs/abstractive_summarizer/lib/python3.8/site-packages/torch/serialization.py in load(f, map_location, pickle_module, **pickle_load_args)
584 return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
--> 585 return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
586
~/anaconda3/envs/abstractive_summarizer/lib/python3.8/site-packages/torch/serialization.py in _legacy_load(f, map_location, pickle_module, **pickle_load_args)
771 assert key in deserialized_objects
--> 772 deserialized_objects[key]._set_from_file(f, offset, f_should_read_directly)
773 if offset is not None:
RuntimeError: unexpected EOF, expected 10498989 more bytes. The file might be corrupted.
During handling of the above exception, another exception occurred:
OSError Traceback (most recent call last)
<ipython-input-1-1ae6eb884edd> in <module>
7 model_name = 'google/pegasus-cnn_dailymail'
8 tokenizer = PegasusTokenizer.from_pretrained(model_name)
----> 9 model = PegasusForConditionalGeneration.from_pretrained(model_name).to(torch_device)
~/projects/transformers/src/transformers/modeling_utils.py in from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)
855 state_dict = torch.load(resolved_archive_file, map_location="cpu")
856 except Exception:
--> 857 raise OSError(
858 "Unable to load weights from pytorch checkpoint file. "
859 "If you tried to load a PyTorch model from a TF 2.0 checkpoint, please set from_tf=True. "
OSError: Unable to load weights from pytorch checkpoint file. If you tried to load a PyTorch model from a TF 2.0 checkpoint, please set from_tf=True.
works for me in in torch 1.5.1. and torch 1.6. Maybe this is a one off s3 failure? Can anybody else replicate?
from transformers import PegasusForConditionalGeneration
model = PegasusForConditionalGeneration.from_pretrained(model_name)
I set force_download=True and it worked. Thanks!
I set
force_download=Trueand it worked. Thanks!
can you describe in detail how did you solved the problem
Just upgrading the PyTorch and TensorFlow version solved the problem for me.
torch==1.6.0 tensorflow==2.3.1 transformers==3.5.1
And I'm trying to load my model trained on gpt2-small named train-on-test1. But I get the an OSERROR:
Unable to load weights from pytorch checkpoint file for '{pretrained_model_name_or_path}' OSError: Unable to load weights
from pytorch checkpoint file for '/mounted/models/train-on-test1/' at '/mounted/models/train-on-test1/pytorch_model.bin' If you
tried to load a PyTorch model from a TF 2.0 checkpoint, please set from_tf=True.
I'm getting the same using torch == 1.7.0 and transformers == 4.1.1 and a xlnet localy downloaded model :
from transformers import XLNetForSequenceClassification
model = XLNetForSequenceClassification.from_pretrained('/../../models/xlnet/', num_labels = 3)
OSError: Unable to load weights from pytorch checkpoint file. If you tried to load a PyTorch model from a TF 2.0 checkpoint, please set from_tf=True.
I keep getting the same error too. I have pretrained a distilbertmodel named amazon-distilbert. When I am trying to load it using from pretrained it is throwing the same error.
from transformers import DistilBertTokenizerFast, DistilBertForSequenceClassification
tokenizer = DistilBertTokenizerFast.from_pretrained("distilbert-base-cased");
model = DistilBertForSequenceClassification.from_pretrained("../models/amazon-distilbert")
And the error
f"Unable to load weights from pytorch checkpoint file for '{pretrained_model_name_or_path}' "
OSError: Unable to load weights from pytorch checkpoint file for '/GitHub/TextSentimentAnalysis/models/amazon-distilbert' at '//GitHub/TextSentimentAnalysis/models/amazon-distilbert/pytorch_model.bin'If you tried to load a PyTorch model from a TF 2.0 checkpoint, please set from_tf=True.
same error with torch 1.5.0, TensorFlow 2.4.1, and transformers 4.2.2. Does anyone know how to solve such a problem?
Same error with torch 1.8.1, transformers 4.5.1 when trying to call
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
AutoModelForSeq2SeqLM.from_pretrained('Helsinki-NLP/opus-mt-ja-en').save_pretrained('pretrained_models/opus-mt-ja-en')
can anyone share how to solve this error to me? plz , i have met this problem too
Same issue here ..
I set
force_download=Trueand it worked. Thanks!can you describe in detail how did you solved the problem
In the from_pretrained function, set one of the parameters as force_download=True Eg. - model = LayoutLMForTokenClassification.from_pretrained("microsoft/layoutlm-base-uncased", num_labels=num_labels, force_download=True)
In my case, I was running on a cpu only compute, and this issue got solved when installing a cpu version of PyTorch. For example: http://download.pytorch.org/whl/cpu/torch-1.13.0%2Bcpu-cp39-cp39-linux_x86_64.whl