# Buggy code
def __setstate__(self, d):
    self.__dict__ = d

    # reload tokenizer to get around serialization issues
    model_name = self.name.split("transformer-word-")[-1]
    try:
        self.tokenizer = AutoTokenizer.from_pretrained(model_name)
    except:
        pass


# Traceback (OSError)
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-5-2f413379aedd> in <module>
6
7 # load the NER tagger
----> 8 tagger = SequenceTagger.load('best-model.pt')
9
10

c:\users\nicod\miniconda3\lib\site-packages\flair\nn.py in load(cls, model)
86             # see https://github.com/zalandoresearch/flair/issues/351
87             f = file_utils.load_big_file(str(model_file))
---> 88             state = torch.load(f, map_location='cpu')
89
90         model = cls._init_model_with_state_dict(state)

c:\users\nicod\miniconda3\lib\site-packages\torch\serialization.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:\users\nicod\miniconda3\lib\site-packages\torch\serialization.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:\users\nicod\miniconda3\lib\site-packages\transformers\tokenization_camembert.py in __setstate__(self, d)
259             raise
260         self.sp_model = spm.SentencePieceProcessor()
--> 261         self.sp_model.Load(self.vocab_file)
262
263     def convert_tokens_to_string(self, tokens):

c:\users\nicod\miniconda3\lib\site-packages\sentencepiece.py in Load(self, model_file, model_proto)
365       if model_proto:
366         return self.LoadFromSerializedProto(model_proto)
--> 367       return self.LoadFromFile(model_file)
368
369

c:\users\nicod\miniconda3\lib\site-packages\sentencepiece.py in LoadFromFile(self, arg)
175
176     def LoadFromFile(self, arg):
--> 177         return _sentencepiece.SentencePieceProcessor_LoadFromFile(self, arg)
178
179     def Init(self,

OSError: Not found: "/home/ubuntu/.cache/torch/transformers/3715e3a4a2de48834619b2a6f48979e13ddff5cabfb1f3409db689f9ce3bb98f.28d30f926f545047fc59da64289371eef0fbdc0764ce9ec56f808a646fcfec59": No such file or directory Error #2

# Fixed code
def __setstate__(self, d):
    self.__dict__ = d

    # necessary for reverse compatibility with Flair <= 0.7
    if "use_scalar_mix" in self.__dict__.keys():
        self.__dict__["layer_mean"] = d["use_scalar_mix"]
    if not "memory_effective_training" in self.__dict__.keys():
        self.__dict__["memory_effective_training"] = True
    if "pooling_operation" in self.__dict__.keys():
        self.__dict__["subtoken_pooling"] = d["pooling_operation"]
    if not "context_length" in self.__dict__.keys():
        self.__dict__["context_length"] = 0
    if "use_context" in self.__dict__.keys():
        self.__dict__["context_length"] = (
            64 if self.__dict__["use_context"] == True else 0
        )

    if not "respect_document_boundaries" in self.__dict__.keys():
        self.__dict__["respect_document_boundaries"] = True
    if not "memory_effective_training" in self.__dict__.keys():
        self.__dict__["memory_effective_training"] = True

    # constructor arguments
    layers = ",".join([str(idx) for idx in self.__dict__["layer_indexes"]])
    subtoken_pooling = self.__dict__["subtoken_pooling"]
    context_length = self.__dict__["context_length"]
    layer_mean = self.__dict__["layer_mean"]
    fine_tune = self.__dict__["fine_tune"]
    allow_long_sentences = self.__dict__["allow_long_sentences"]
    respect_document_boundaries = self.__dict__["respect_document_boundaries"]
    memory_effective_training = self.__dict__["memory_effective_training"]

    model_name = self.__dict__["name"].split("transformer-word-")[-1]

    # special handling for deserializing transformer models
    if "config_state_dict" in d:
        # load transformer model
        config_class = CONFIG_MAPPING[d["config_state_dict"]["model_type"]]
        loaded_config = config_class.from_dict(d["config_state_dict"])

        # re-initialize transformer word embeddings with constructor arguments
        embedding = TransformerWordEmbeddings(
            model_name,
            layers=layers,
            subtoken_pooling=subtoken_pooling,
            use_context=context_length,
            layer_mean=layer_mean,
            fine_tune=fine_tune,
            allow_long_sentences=allow_long_sentences,
            respect_document_boundaries=respect_document_boundaries,
            memory_effective_training=memory_effective_training,
            config=loaded_config,
            state_dict=d["model_state_dict"],
        )

        # I have no idea why this is necessary, but otherwise it doesn't work
        for key in embedding.__dict__.keys():
            self.__dict__[key] = embedding.__dict__[key]

    else:
        # reload tokenizer to get around serialization issues
        try:
            tokenizer = AutoTokenizer.from_pretrained(model_name)

        except:
            pass

        self.tokenizer = tokenizer