transformers icon indicating copy to clipboard operation
transformers copied to clipboard

Pegasus: OSError: Unable to load weights from pytorch checkpoint file.

Open yxyzzz opened this issue 6 years ago • 13 comments

Environment info

  • transformers version: 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. 

yxyzzz avatar Aug 20 '20 16:08 yxyzzz

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)

sshleifer avatar Aug 20 '20 17:08 sshleifer

I set force_download=True and it worked. Thanks!

yxyzzz avatar Aug 20 '20 19:08 yxyzzz

I set force_download=True and it worked. Thanks!

can you describe in detail how did you solved the problem

55Ankur55 avatar Oct 10 '20 15:10 55Ankur55

Just upgrading the PyTorch and TensorFlow version solved the problem for me.

svjan5 avatar Nov 04 '20 23:11 svjan5

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.

jawadSajid avatar Jan 04 '21 11:01 jawadSajid

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. 

cemanughian avatar Jan 04 '21 17:01 cemanughian

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. 

Vamsi995 avatar Jan 05 '21 19:01 Vamsi995

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?

CarolLi avatar Jan 29 '21 08:01 CarolLi

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')

velocityCavalry avatar May 07 '21 00:05 velocityCavalry

can anyone share how to solve this error to me? plz , i have met this problem too

mengxun1437 avatar May 13 '21 14:05 mengxun1437

Same issue here ..

UmutAlihan avatar Aug 27 '21 13:08 UmutAlihan

I set force_download=True and 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)

Sc-prasin avatar Sep 07 '21 09:09 Sc-prasin

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

leoschet avatar Feb 22 '23 22:02 leoschet