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

[Question]: SequenceTagger.load fails with TypeError: not a string when loading a fine-tuned model #3574

Closed
pganesh opened this issue Dec 3, 2024 · 3 comments
Labels
question Further information is requested

Comments

@pganesh
Copy link

pganesh commented Dec 3, 2024

Question

For fine-tuning, I used the google/flan-t5-large as the transformer like this:

4. initialize fine-tuneable transformer embeddings WITH document context

embeddings = TransformerWordEmbeddings(model='google/flan-t5-large',
layers="-1",
subtoken_pooling="first",
fine_tune=True,
use_context=True,
)
The training was succesful and the model was saved correctly as a final-model.pt
However, when I try to use it as follows:

load the trained model

model = SequenceTagger.load('/content/resources/taggers/sota-ner-flert/final-model.pt')

I get an error saying that "TypeError: not a string"
I suspect this is due to using the flan-t5-large - everything works great if I used xlm-roberta-large for example
I need some help on how to use the saved/fine-tuned model if the original embedding was based on flan-t5
thanks!

@pganesh pganesh added the question Further information is requested label Dec 3, 2024
@pganesh
Copy link
Author

pganesh commented Dec 3, 2024

HEre's the error trace:

TypeError Traceback (most recent call last)
in <cell line: 2>()
1 # Model name points to the best-model.pt or final-model.pt path
----> 2 model = SequenceTagger.load(final_model_name)
3
4 # Use corpus.dev or corpus.test
5 result = model.evaluate(corpus.dev, gold_label_type="ner", mini_batch_size=8)

19 frames
/usr/local/lib/python3.10/dist-packages/flair/models/sequence_tagger_model.py in load(cls, model_path)
923 from typing import cast
924
--> 925 return cast("SequenceTagger", super().load(model_path=model_path))

/usr/local/lib/python3.10/dist-packages/flair/nn/model.py in load(cls, model_path)
562 from typing import cast
563
--> 564 return cast("Classifier", super().load(model_path=model_path))
565
566

/usr/local/lib/python3.10/dist-packages/flair/nn/model.py in load(cls, model_path)
195 state.pop("cls")
196
--> 197 model = cls._init_model_with_state_dict(state)
198
199 if "model_card" in state:

/usr/local/lib/python3.10/dist-packages/flair/models/sequence_tagger_model.py in _init_model_with_state_dict(cls, state, **kwargs)
622 del state["state_dict"]["transitions"]
623
--> 624 return super()._init_model_with_state_dict(
625 state,
626 embeddings=state.get("embeddings"),

/usr/local/lib/python3.10/dist-packages/flair/nn/model.py in _init_model_with_state_dict(cls, state, **kwargs)
98 embeddings = kwargs.pop("embeddings")
99 if isinstance(embeddings, dict):
--> 100 embeddings = load_embeddings(embeddings)
101 kwargs["embeddings"] = embeddings
102

/usr/local/lib/python3.10/dist-packages/flair/embeddings/base.py in load_embeddings(params)
229 cls_name = params.pop("cls")
230 cls = EMBEDDING_CLASSES[cls_name]
--> 231 return cls.load_embedding(params)

/usr/local/lib/python3.10/dist-packages/flair/embeddings/base.py in load_embedding(cls, params)
95 state_dict = params.pop("state_dict", None)
96
---> 97 embedding = cls.from_params(params)
98 if state_dict is not None:
99 embedding.load_state_dict(state_dict)

/usr/local/lib/python3.10/dist-packages/flair/embeddings/transformer.py in from_params(cls, params)
1326 config_class = CONFIG_MAPPING[model_type]
1327 config = config_class.from_dict(config_state_dict)
-> 1328 return cls.create_from_state(saved_config=config, **params)
1329
1330 def to_params(self):

/usr/local/lib/python3.10/dist-packages/flair/embeddings/token.py in create_from_state(cls, **state)
58 # this parameter is fixed
59 del state["is_token_embedding"]
---> 60 return cls(**state)
61
62

/usr/local/lib/python3.10/dist-packages/flair/embeddings/token.py in init(self, model, is_document_embedding, allow_long_sentences, **kwargs)
45 **kwargs: Arguments propagated to :meth:flair.embeddings.transformer.TransformerEmbeddings.__init__
46 """
---> 47 TransformerEmbeddings.init(
48 self,
49 model=model,

/usr/local/lib/python3.10/dist-packages/flair/embeddings/transformer.py in init(self, model, fine_tune, layers, layer_mean, subtoken_pooling, cls_pooling, is_token_embedding, is_document_embedding, allow_long_sentences, use_context, respect_document_boundaries, context_dropout, saved_config, tokenizer_data, feature_extractor_data, name, force_max_length, needs_manual_ocr, use_context_separator, transformers_tokenizer_kwargs, transformers_config_kwargs, transformers_model_kwargs, peft_config, peft_gradient_checkpointing_kwargs, **kwargs)
1062 else:
1063 # load tokenizer from inmemory zip-file
-> 1064 self.tokenizer = self._tokenizer_from_bytes(tokenizer_data)
1065 if feature_extractor_data is not None:
1066 self.feature_extractor = self._feature_extractor_from_bytes(feature_extractor_data)

/usr/local/lib/python3.10/dist-packages/flair/embeddings/transformer.py in _tokenizer_from_bytes(cls, zip_data)
428 with tempfile.TemporaryDirectory() as temp_dir:
429 zip_obj.extractall(temp_dir)
--> 430 return AutoTokenizer.from_pretrained(temp_dir, add_prefix_space=True)
431
432 @classmethod

/usr/local/lib/python3.10/dist-packages/transformers/models/auto/tokenization_auto.py in from_pretrained(cls, pretrained_model_name_or_path, *inputs, **kwargs)
918 f"Tokenizer class {tokenizer_class_candidate} does not exist or is not currently imported."
919 )
--> 920 return tokenizer_class.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs)
921
922 # Otherwise we have to be creative.

/usr/local/lib/python3.10/dist-packages/transformers/tokenization_utils_base.py in from_pretrained(cls, pretrained_model_name_or_path, cache_dir, force_download, local_files_only, token, revision, trust_remote_code, *init_inputs, **kwargs)
2211 logger.info(f"loading file {file_path} from cache at {resolved_vocab_files[file_id]}")
2212
-> 2213 return cls._from_pretrained(
2214 resolved_vocab_files,
2215 pretrained_model_name_or_path,

/usr/local/lib/python3.10/dist-packages/transformers/tokenization_utils_base.py in _from_pretrained(cls, resolved_vocab_files, pretrained_model_name_or_path, init_configuration, token, cache_dir, local_files_only, _commit_hash, _is_local, trust_remote_code, *init_inputs, **kwargs)
2445 # Instantiate the tokenizer.
2446 try:
-> 2447 tokenizer = cls(*init_inputs, **init_kwargs)
2448 except import_protobuf_decode_error():
2449 logger.info(

/usr/local/lib/python3.10/dist-packages/transformers/models/t5/tokenization_t5_fast.py in init(self, vocab_file, tokenizer_file, eos_token, unk_token, pad_token, extra_ids, additional_special_tokens, add_prefix_space, **kwargs)
117 kwargs["from_slow"] = True
118
--> 119 super().init(
120 vocab_file,
121 tokenizer_file=tokenizer_file,

/usr/local/lib/python3.10/dist-packages/transformers/tokenization_utils_fast.py in init(self, *args, **kwargs)
130 elif self.slow_tokenizer_class is not None and slow_tokenizer is not False:
131 # We need to create and convert a slow tokenizer to build the backend
--> 132 slow_tokenizer = self.slow_tokenizer_class(*args, **kwargs)
133 fast_tokenizer = convert_slow_tokenizer(slow_tokenizer)
134 elif not slow_tokenizer:

/usr/local/lib/python3.10/dist-packages/transformers/models/t5/tokenization_t5.py in init(self, vocab_file, eos_token, unk_token, pad_token, extra_ids, additional_special_tokens, sp_model_kwargs, legacy, add_prefix_space, **kwargs)
148
149 self.sp_model = spm.SentencePieceProcessor(**self.sp_model_kwargs)
--> 150 self.sp_model.Load(vocab_file)
151
152 if additional_special_tokens is not None:

/usr/local/lib/python3.10/dist-packages/sentencepiece/init.py in Load(self, model_file, model_proto)
959 if model_proto:
960 return self.LoadFromSerializedProto(model_proto)
--> 961 return self.LoadFromFile(model_file)
962
963

/usr/local/lib/python3.10/dist-packages/sentencepiece/init.py in LoadFromFile(self, arg)
314
315 def LoadFromFile(self, arg):
--> 316 return _sentencepiece.SentencePieceProcessor_LoadFromFile(self, arg)
317
318 def _EncodeAsIds(self, text, enable_sampling, nbest_size, alpha, add_bos, add_eos, reverse, emit_unk_piece):

TypeError: not a string

@helpmefindaname
Copy link
Collaborator

Hi @pganesh
I am sorry, but I don't want to read so much text with bad formatting. Besides that, I think there is some default information missing, as this isn't a question but a bug report.

I am closing this for now, please create an issue using the bug-report template, fill out all fields there and verify afterwards that the markdown is readable.

@pganesh
Copy link
Author

pganesh commented Dec 7, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants