You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

加载NVIDIA/nv-embed-v2遇ImportError:无法导入MISTRAL_INPUTS_DOCSTRING

问题

尝试使用NVIDIA/nv-embed-v2嵌入模型,代码如下:

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()

当前使用的transformers版本:transformers==4.42.4

运行代码时出现报错:

---------------------------------------------------------------------------
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)

已安装模型卡片指定版本的库,寻求解决办法。

解决方案

这个错误源于transformers 4.42.4版本中,Mistral模块已移除MISTRAL_INPUTS_DOCSTRING常量,但NV-Embed-v2的远程代码仍在尝试导入它。可通过以下方式解决:

  • 升级transformers到兼容版本
    直接升级至4.45.0及以上版本,新版本已适配NV-Embed-v2的远程代码依赖:

    pip install --upgrade transformers>=4.45.0
    
  • 手动修改缓存模型代码(临时方案)
    若无法升级transformers,找到本地缓存的NV-Embed-v2模型代码文件(路径可参考报错信息中的缓存模块路径),将第11行的导入语句:

    from transformers.models.mistral.modeling_mistral import MISTRAL_INPUTS_DOCSTRING
    

    替换为:

    MISTRAL_INPUTS_DOCSTRING = ""
    

    或直接注释该行,该常量仅用于文档字符串,不影响模型运行。

  • 确认依赖完整性
    检查是否安装了模型所需的其他依赖(如torch、accelerate等),确保版本符合模型卡片要求。


内容的提问来源于stack exchange,提问作者6zL

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 18:05:18