pythonnvidiahuggingface-transformerstransformer-modelword-embedding

(NVIDIA/nv-embed-v2) ImportError: cannot import name 'MISTRAL_INPUTS_DOCSTRING' from 'transformers.models.mistral.modeling_mistral'


My code:

from transformers import AutoTokenizer, AutoModel

model_name = "NVIDIA/nv-embed-v2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModel.from_pretrained(model_name, trust_remote_code=True)
model.eval()

The version of the module I'm currently using:

transformers==4.42.4

The output:


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[12], line 3
      1 model_name = "NVIDIA/nv-embed-v2"
      2 tokenizer = AutoTokenizer.from_pretrained(model_name)
----> 3 model = AutoModel.from_pretrained(model_name, trust_remote_code=True)
      4 model.eval()

File ...\AppData\Local\Programs\Python\Python312\Lib\site-packages\transformers\models\auto\auto_factory.py:582, in _BaseAutoModelClass.from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)
    579 kwargs["adapter_kwargs"] = adapter_kwargs
    581 if has_remote_code and trust_remote_code:
--> 582     model_class = get_class_from_dynamic_module(
    583         class_ref, pretrained_model_name_or_path, code_revision=code_revision, **hub_kwargs, **kwargs
    584     )
    585     _ = hub_kwargs.pop("code_revision", None)
    586     # This block handles the case where the user is loading a model with `trust_remote_code=True`
    587     # but a library model exists with the same name. We don't want to override the autoclass
    588     # mappings in this case, or all future loads of that model will be the remote code model.

File ...\AppData\Local\Programs\Python\Python312\Lib\site-packages\transformers\dynamic_module_utils.py:582, in get_class_from_dynamic_module(class_reference, pretrained_model_name_or_path, cache_dir, force_download, resume_download, proxies, token, revision, local_files_only, repo_type, code_revision, **kwargs)
    569 # And lastly we get the class inside our newly created module
    570 final_module = get_cached_module_file(
    571     repo_id,
    572     module_file + ".py",
   (...)
...
---> 11 from transformers.models.mistral.modeling_mistral import MISTRAL_INPUTS_DOCSTRING
     12 from transformers.modeling_outputs import BaseModelOutputWithPast
     13 from transformers.modeling_attn_mask_utils import _prepare_4d_attention_mask, _prepare_4d_attention_mask_for_sdpa

ImportError: cannot import name 'MISTRAL_INPUTS_DOCSTRING' from 'transformers.models.mistral.modeling_mistral' (...\AppData\Local\Programs\Python\Python312\Lib\site-packages\transformers\models\mistral\modeling_mistral.py)

The error occured when I was trying to use the NVIDIA/nv-embed-v2 embedding model, I have installed the specified version of libraries mentioned in the huggingface model card. Does anybody know the solution?

Link to NVIDIA/nv-embed-v2 huggingface model card


Solution

  • Error shows problem with MISTRAL_INPUTS_DOCSTRING in file modeling_mistral.py

    I checked modeling_mistral.py on GitHub and this variable exists in 4.51.3 (and older versions) but was removes in 4.52.0 and newer versions (the newest version is 4.53.x)

    Maybe you have newer version.
    Or maybe you run different venv with newer version.
    Or maybe you forgot to activate venv.

    You could check trasformers' version directly in running code

    import transformers
    
    print(transformers.__version__)
    

    You could also check Python's version and if you run venv.

    import sys
    
    print(sys.version)
    print(sys.executable)
    

    If you have wrong version then you have to update it
    (maybe to 4.42.2 as suggested on NVIDIA/nv-embed-v2 huggingface model card)

    pip install --update transformers==4.42.4