Flair: RuntimeError: storage has wrong size when loading en-sentiment

Created on 2 Jan 2019  路  10Comments  路  Source: flairNLP/flair

When trying to load the en-sentiment model, I get the error:

RuntimeError: storage has wrong size: expected -1862414276 got 22700

I am able to load other models such as de-offensive-language and the ner SequenceTagger.

Code:

from flair.models import TextClassifier
from flair.data import Sentence
classifier = TextClassifier.load('en-sentiment')

Possibly related to issue https://github.com/zalandoresearch/flair/issues/172 ?
But the fix for that was merged back in November? https://github.com/zalandoresearch/flair/pull/194

Have tried deleting and redownloading the model.

Platform: Windows 10, Torch 1.0 (cpu), Python 3.6.4 (Anaconda)

bug

Most helpful comment

Yes, we are working on this at the moment. We will push an updated version with the upcoming 0.4.1 release, within the next weeks.

All 10 comments

Hi @rookie32 - yes it is the same bug as reported in #194, namely that very large models (over 2GB) cannot be de-serialized in certain windows and mac setups. This is a known issue in the pickle library which is used by pytorch to serialize.

We currently have a workaround in place in which we make sure that models do not go over 2GB in size, but overlooked this with the en-sentiment model. We will fix this for the next release!

Here also the link to the corresponding pickle issue.

I am facing the same issue on my windows machine (windows 10, 8 GB RAM). Do we have a fix for this issue?

Yes, we are working on this at the moment. We will push an updated version with the upcoming 0.4.1 release, within the next weeks.

It's pretty weird to me that this hasn't been solved, as the discussion about this bug in pickle was reported in 2015.

Anyways, I created a temporary workaround.

Disclaimer: this will use a big amount of RAM. If there's a more memory efficient solution that doesn't change the way Torch serializes the models (i.e. doesn't change serialization.py), let us know

Step 1:
Create a text_classifier.py file, which should be a copy of flair/models/text_classification_model.py

Step 2:
Replace the _load_state function with the following two functions:
https://gist.github.com/highway11git/1fa64621ea1b04e6494bf6672a750b24

Step 3:
Instead of using from flair.models import TextClassifier in your project, import the TextClassifier class from your newly created file. Like so: from text_classifier import TextClassifier

Hi @highway11git thanks very much for this! We'll test it and see if we can directly integrate it. Perhaps we could add a special flag to the model loader to allow users to specify which method they want to use to load a model, with the default being the current one.

I've updated the gist:

  1. load_big_file now uses mmap instead of BytesIO, which makes it _much more memory efficient_
  2. _load_state now also uses the load_big_file function if the user doesn't use cuda

Thanks for testing it, and keep us up to date on any progress.

Cool, thanks! We'll test the solution and let you know!

@highway11git @pdsing @rookie32 I've added the fix proposed by @highway11git into a pull request.

Since we could not reproduce the error on our setups, could you check if this fixed it for you?

Using the newest Flair and torch on Win 10 I still get this error for (other models worked):
tagger = SequenceTagger.load('frame-fast')
or
tagger = SequenceTagger.load('frame')


NotImplementedError Traceback (most recent call last)
in
----> 1 tagger = SequenceTagger.load('frame-fast')
2
3 sentence_1 = Sentence('George returned to Berlin to return his hat .')
4 sentence_2 = Sentence('He had a look at different hats .')
5

c:\program files\python37\lib\site-packages\flair\nn.py in load(cls, model)
84 # see https://github.com/zalandoresearch/flair/issues/351
85 f = file_utils.load_big_file(str(model_file))
---> 86 state = torch.load(f, map_location=flair.device)
87
88 model = cls._init_model_with_state_dict(state)

c:\program files\python37\lib\site-packages\torchserialization.py in load(f, map_location, pickle_module, *pickle_load_args)
527 with _open_zipfile_reader(f) as opened_zipfile:
528 return _load(opened_zipfile, map_location, pickle_module, *
pickle_load_args)
--> 529 return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
530
531

c:\program files\python37\lib\site-packages\torchserialization.py in _legacy_load(f, map_location, pickle_module, *pickle_load_args)
700 unpickler = pickle_module.Unpickler(f, *
pickle_load_args)
701 unpickler.persistent_load = persistent_load
--> 702 result = unpickler.load()
703
704 deserialized_storage_keys = pickle_module.load(f, **pickle_load_args)

c:\program files\python37\lib\pathlib.py in __new__(cls, args, *kwargs)
1023 if not self._flavour.is_supported:
1024 raise NotImplementedError("cannot instantiate %r on your system"
-> 1025 % (cls.__name__,))
1026 self._init()
1027 return self

NotImplementedError: cannot instantiate 'PosixPath' on your system

Was this page helpful?
0 / 5 - 0 ratings

Related issues

happypanda5 picture happypanda5  路  3Comments

isanvicente picture isanvicente  路  3Comments

ChessMateK picture ChessMateK  路  3Comments

davidsbatista picture davidsbatista  路  3Comments

inyukwo1 picture inyukwo1  路  3Comments