flair icon indicating copy to clipboard operation
flair copied to clipboard

[Bug]: RuntimeError: Error(s) in loading state_dict for XLMRobertaModel: Unexpected key(s) in state_dict: "embeddings.position_ids".

Open brestok-1 opened this issue 10 months ago • 2 comments

Describe the bug

I'm trying to use this model. I even have an example from the site that doesn't work. Unknown error that I do not know how to solve. Please help or fix it

To Reproduce

from flair.data import Sentence
from flair.models import SequenceTagger

# load tagger
tagger = SequenceTagger.load("flair/ner-dutch-large")

# make example sentence
sentence = Sentence("George Washington ging naar Washington")

# predict NER tags
tagger.predict(sentence)

# print sentence
print(sentence)

# print predicted NER spans
print('The following NER tags are found:')
# iterate over entities and print
for entity in sentence.get_spans('ner'):
    print(entity)

Expected behavior

Span [1,2]: "George Washington" [− Labels: PER (1.0)] Span [5]: "Washington" [− Labels: LOC (1.0)]

Logs and Stack traces

Traceback (most recent call last):
  File "/home/maksim/Development/pythonprojects/gpt_training.py", line 5, in <module>
    tagger = SequenceTagger.load("flair/ner-dutch-large")
  File "/home/maksim/.local/lib/python3.10/site-packages/flair/models/sequence_tagger_model.py", line 1035, in load
    return cast("SequenceTagger", super().load(model_path=model_path))
  File "/home/maksim/.local/lib/python3.10/site-packages/flair/nn/model.py", line 559, in load
    return cast("Classifier", super().load(model_path=model_path))
  File "/home/maksim/.local/lib/python3.10/site-packages/flair/nn/model.py", line 191, in load
    state = load_torch_state(model_file)
  File "/home/maksim/.local/lib/python3.10/site-packages/flair/file_utils.py", line 359, in load_torch_state
    return torch.load(f, map_location="cpu")
  File "/home/maksim/.local/lib/python3.10/site-packages/torch/serialization.py", line 1014, in load
    return _load(opened_zipfile,
  File "/home/maksim/.local/lib/python3.10/site-packages/torch/serialization.py", line 1422, in _load
    result = unpickler.load()
  File "/home/maksim/.local/lib/python3.10/site-packages/flair/embeddings/transformer.py", line 1169, in __setstate__
    self.model.load_state_dict(model_state_dict)
  File "/home/maksim/.local/lib/python3.10/site-packages/torch/nn/modules/module.py", line 2152, in load_state_dict
    raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
RuntimeError: Error(s) in loading state_dict for XLMRobertaModel:
	Unexpected key(s) in state_dict: "embeddings.position_ids".

Screenshots

No response

Additional Context

No response

Environment

Versions:

Flair

0.12.2

Pytorch

2.1.0+cu121

Transformers

4.34.1

GPU

False

brestok-1 avatar Oct 22 '23 09:10 brestok-1

Hi @brestok-1 this sounds to me like an issue that was solved in https://github.com/flairNLP/flair/issues/3346 and works on the current master branch. Can you please verify this?

helpmefindaname avatar Oct 23 '23 08:10 helpmefindaname

Still not working for me. Installing the packages as described here solves it for me: https://github.com/flairNLP/flair/issues/3346#issuecomment-1773557057

semlommers avatar Oct 24 '23 12:10 semlommers