Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Can't load the flair/ner-multi-fast model #3572

Open
orkhank opened this issue Dec 2, 2024 · 1 comment
Open

[Bug]: Can't load the flair/ner-multi-fast model #3572

orkhank opened this issue Dec 2, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@orkhank
Copy link

orkhank commented Dec 2, 2024

Describe the bug

Trying to load the flair/ner-multi-fast model raises the following error:

AttributeError: 'LSTM' object has no attribute '_flat_weights'

To Reproduce

from flair.models import SequenceTagger

# load tagger
tagger = SequenceTagger.load("flair/ner-multi-fast")

Expected behavior

The code given above is part of the following example, taken from the model card of the model. The full code is given below.

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

# load tagger
tagger = SequenceTagger.load("flair/ner-multi-fast")

# make example sentence in any of the four languages
sentence = Sentence("George Washington ging nach 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)

It is stated that this should yield the following output:

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

however, instead, we get an error when loading the model.

Logs and Stack traces

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/dev/app/.venv/lib/python3.9/site-packages/flair/models/sequence_tagger_model.py", line 925, in load
    return cast("SequenceTagger", super().load(model_path=model_path))
  File "/home/user/dev/app/.venv/lib/python3.9/site-packages/flair/nn/model.py", line 564, in load
    return cast("Classifier", super().load(model_path=model_path))
  File "/home/user/dev/app/.venv/lib/python3.9/site-packages/flair/nn/model.py", line 190, in load
    state = load_torch_state(model_file)
  File "/home/user/dev/app/.venv/lib/python3.9/site-packages/flair/file_utils.py", line 384, in load_torch_state
    return torch.load(f, map_location="cpu")
  File "/home/user/dev/app/.venv/lib/python3.9/site-packages/torch/serialization.py", line 1040, in load
    return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
  File "/home/user/dev/app/.venv/lib/python3.9/site-packages/torch/serialization.py", line 1272, in _legacy_load
    result = unpickler.load()
  File "/home/user/dev/app/.venv/lib/python3.9/site-packages/torch/nn/modules/rnn.py", line 353, in __setstate__
    for w in self._flat_weights]
  File "/home/user/dev/app/.venv/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1709, in __getattr__
    raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'")
AttributeError: 'LSTM' object has no attribute '_flat_weights'

Screenshots

No response

Additional Context

The code snippet is run on WSL2 Ubuntu 22.04 (Windows 11). The environment is created using uv.

Environment

Versions:

Flair

0.14.0

Pytorch

2.3.1+cpu

Transformers

4.46.3

GPU

False

@orkhank orkhank added the bug Something isn't working label Dec 2, 2024
@helpmefindaname
Copy link
Collaborator

Hi @orkhank
this issue is due to that model being stored in an old format. I will check if @alanakbik to update it.
As a quickfix you can:

  • install an older version of torch like discribed here
  • load the model and save it again
  • upgrade torch again
  • load your saved model instead of the one on the huggingface hub

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants