MinishLab / model2vec

Distill a Small Static Model from any Sentence Transformer
MIT License
364 stars 16 forks source link

AttributeError: 'NoneType' object has no attribute 'get' #79

Closed aoezdTchibo closed 1 week ago

aoezdTchibo commented 1 week ago

If I try to use the distilled model with Sentence Transformers, I am getting the following error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[4], line 7
      5 model_distilled = distill(model_name=model_name, pca_dims=256)
      6 model_distilled.save_pretrained("./multilingual-e5-small-distilled")
----> 7 st_model_distilled = SentenceTransformer("multilingual-e5-small-distilled")

File ~/PycharmProjects/product-search-custom-embedding/.venv/lib/python3.10/site-packages/sentence_transformers/SentenceTransformer.py:306, in SentenceTransformer.__init__(self, model_name_or_path, modules, device, prompts, default_prompt_name, similarity_fn_name, cache_folder, trust_remote_code, revision, local_files_only, token, use_auth_token, truncate_dim, model_kwargs, tokenizer_kwargs, config_kwargs, model_card_data)
    294         modules, self.module_kwargs = self._load_sbert_model(
    295             model_name_or_path,
    296             token=token,
   (...)
    303             config_kwargs=config_kwargs,
    304         )
    305     else:
--> 306         modules = self._load_auto_model(
    307             model_name_or_path,
    308             token=token,
    309             cache_folder=cache_folder,
    310             revision=revision,
    311             trust_remote_code=trust_remote_code,
    312             local_files_only=local_files_only,
    313             model_kwargs=model_kwargs,
    314             tokenizer_kwargs=tokenizer_kwargs,
    315             config_kwargs=config_kwargs,
    316         )
    318 if modules is not None and not isinstance(modules, OrderedDict):
    319     modules = OrderedDict([(str(idx), module) for idx, module in enumerate(modules)])

File ~/PycharmProjects/product-search-custom-embedding/.venv/lib/python3.10/site-packages/sentence_transformers/SentenceTransformer.py:1454, in SentenceTransformer._load_auto_model(self, model_name_or_path, token, cache_folder, revision, trust_remote_code, local_files_only, model_kwargs, tokenizer_kwargs, config_kwargs)
   1451 tokenizer_kwargs = shared_kwargs if tokenizer_kwargs is None else {**shared_kwargs, **tokenizer_kwargs}
   1452 config_kwargs = shared_kwargs if config_kwargs is None else {**shared_kwargs, **config_kwargs}
-> 1454 transformer_model = Transformer(
   1455     model_name_or_path,
   1456     cache_dir=cache_folder,
   1457     model_args=model_kwargs,
   1458     tokenizer_args=tokenizer_kwargs,
   1459     config_args=config_kwargs,
   1460 )
   1461 pooling_model = Pooling(transformer_model.get_word_embedding_dimension(), "mean")
   1462 self.model_card_data.set_base_model(model_name_or_path, revision=revision)

File ~/PycharmProjects/product-search-custom-embedding/.venv/lib/python3.10/site-packages/sentence_transformers/models/Transformer.py:56, in Transformer.__init__(self, model_name_or_path, max_seq_length, model_args, tokenizer_args, config_args, cache_dir, do_lower_case, tokenizer_name_or_path)
     53     config_args = {}
     55 config = AutoConfig.from_pretrained(model_name_or_path, **config_args, cache_dir=cache_dir)
---> 56 self._load_model(model_name_or_path, config, cache_dir, **model_args)
     58 if max_seq_length is not None and "model_max_length" not in tokenizer_args:
     59     tokenizer_args["model_max_length"] = max_seq_length

File ~/PycharmProjects/product-search-custom-embedding/.venv/lib/python3.10/site-packages/sentence_transformers/models/Transformer.py:87, in Transformer._load_model(self, model_name_or_path, config, cache_dir, **model_args)
     85     self._load_mt5_model(model_name_or_path, config, cache_dir, **model_args)
     86 else:
---> 87     self.auto_model = AutoModel.from_pretrained(
     88         model_name_or_path, config=config, cache_dir=cache_dir, **model_args
     89     )

File ~/PycharmProjects/product-search-custom-embedding/.venv/lib/python3.10/site-packages/transformers/models/auto/auto_factory.py:564, in _BaseAutoModelClass.from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)
    562 elif type(config) in cls._model_mapping.keys():
    563     model_class = _get_model_class(config, cls._model_mapping)
--> 564     return model_class.from_pretrained(
    565         pretrained_model_name_or_path, *model_args, config=config, **hub_kwargs, **kwargs
    566     )
    567 raise ValueError(
    568     f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"
    569     f"Model type should be one of {', '.join(c.__name__ for c in cls._model_mapping.keys())}."
    570 )

File ~/PycharmProjects/product-search-custom-embedding/.venv/lib/python3.10/site-packages/transformers/modeling_utils.py:3792, in PreTrainedModel.from_pretrained(cls, pretrained_model_name_or_path, config, cache_dir, ignore_mismatched_sizes, force_download, local_files_only, token, revision, use_safetensors, *model_args, **kwargs)
   3789 with safe_open(resolved_archive_file, framework="pt") as f:
   3790     metadata = f.metadata()
-> 3792 if metadata.get("format") == "pt":
   3793     pass
   3794 elif metadata.get("format") == "tf":

AttributeError: 'NoneType' object has no attribute 'get'

Use the following code snippet to reproduce the error:

from model2vec.distill import distill
from sentence_transformers import SentenceTransformer

model_name = "intfloat/multilingual-e5-small"
model_distilled = distill(model_name=model_name, pca_dims=256)
model_distilled.save_pretrained("./multilingual-e5-small-distilled")
st_model_distilled = SentenceTransformer("multilingual-e5-small-distilled")
stephantul commented 1 week ago

Hey, you are unfortunately not able to use model2vec like this. Please use the following snippet:

from sentence_transformers import SentenceTransformer
from sentence_transformers.models import StaticEmbedding

model_name = "intfloat/multilingual-e5-small"
static_embedding = StaticEmbedding.from_distillation(model_name, device="cuda")
model = SentenceTransformer(modules=[static_embedding])

Should you have any further questions, please don't hesitate to reach out.

aoezdTchibo commented 1 week ago

Works with the provided snippet, thank you!