pip install sentence_transformers报错的解决
可以直接看 ‘解决方法’ 这块

系统配置

我的系统是 win10专业版Python 3.11.9

问题过程

pip install sentence_transformers安装成功
但是运行代码后报错:

(.venv) PS D:\embeddings> & d:/embeddings/.venv/Scripts/python.exe d:/embeddings/gte-Qwen2-7B-instruct.py
Traceback (most recent call last):
  File "d:\embeddings\gte-Qwen2-7B-instruct.py", line 9, in <module>
    model = SentenceTransformer("Alibaba-NLP/gte-Qwen2-7B-instruct", trust_remote_code=True)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\embeddings\.venv\Lib\site-packages\sentence_transformers\SentenceTransformer.py", line 287, in __init__ 
    modules = self._load_sbert_model(
              ^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\embeddings\.venv\Lib\site-packages\sentence_transformers\SentenceTransformer.py", line 1487, in _load_sbert_model
    module = Transformer(model_name_or_path, cache_dir=cache_folder, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\embeddings\.venv\Lib\site-packages\sentence_transformers\models\Transformer.py", line 54, in __init__   
    self._load_model(model_name_or_path, config, cache_dir, **model_args)
  File "D:\embeddings\.venv\Lib\site-packages\sentence_transformers\models\Transformer.py", line 85, in _load_model    self.auto_model = AutoModel.from_pretrained(
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\embeddings\.venv\Lib\site-packages\transformers\models\auto\auto_factory.py", line 551, in from_pretrained
    model_class = get_class_from_dynamic_module(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ed
    model_class = get_class_from_dynamic_module(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    model_class = get_class_from_dynamic_module(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\embeddings\.venv\Lib\site-packages\transformers\dynamic_module_utils.py", line 502, in get_class_from_dy  File "D:\embeddings\.venv\Lib\site-packages\transformers\dynamic_module_utils.py", line 502, in get_class_from_dynamic_module
namic_module
                   ^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\embeddings\.venv\Lib\site-packages\transformers\dynamic_module_utils.py", line 327, in get_cached_module_file
    modules_needed = check_imports(resolved_module_file)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\embeddings\.venv\Lib\site-packages\transformers\dynamic_module_utils.py", line 182, in check_imports    
    raise ImportError(
ImportError: This modeling file requires the following packages that were not found in your environment: flash_attn. Run `pip install flash_attn`

可以看到terminal提示需要 pip install flash_attn
于是我继续pip install flash_attn
报错
ModuleNotFoundError: No module named 'torch'

(.venv) PS D:\embeddings> pip install flash_attn
Collecting flash_attn
  Using cached flash_attn-2.6.1.tar.gz (2.6 MB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
  error: subprocess-exited-with-error

  × Getting requirements to build wheel did not run successfully.
  │ exit code: 1
  ╰─> [20 lines of output]
      Traceback (most recent call last):
        File "D:\embeddings\.venv\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 353, in <module>
          main()
        File "D:\embeddings\.venv\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 335, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "D:\embeddings\.venv\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 118, in get_requires_for_build_wheel
          return hook(config_settings)
                 ^^^^^^^^^^^^^^^^^^^^^
        File "C:\Users\Administrator\AppData\Local\Temp\pip-build-env-7h0x5mak\overlay\Lib\site-packages\setuptools\build_meta.py", line 327, in get_requires_for_build_wheel
          return self._get_build_requires(config_settings, requirements=[])
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "C:\Users\Administrator\AppData\Local\Temp\pip-build-env-7h0x5mak\overlay\Lib\site-packages\setuptools\build_meta.py", line 297, in _get_build_requires
          self.run_setup()
        File "C:\Users\Administrator\AppData\Local\Temp\pip-build-env-7h0x5mak\overlay\Lib\site-packages\setuptools\build_meta.py", line 497, in run_setup
          super().run_setup(setup_script=setup_script)
        File "C:\Users\Administrator\AppData\Local\Temp\pip-build-env-7h0x5mak\overlay\Lib\site-packages\setuptools\build_meta.py", line 313, in run_setup
          exec(code, locals())
        File "<string>", line 19, in <module>
      ModuleNotFoundError: No module named 'torch'
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.

解决方法

步骤一:
安装CUDA Toolkit以及cuda:
cuda官网的下载页面选择匹配自己系统的CUDA Toolkit进行无脑安装
步骤二:
指定 sentence-transformers版本安装
pip install sentence-transformers==2.2.0

GitHub 加速计划 / tra / transformers
130.24 K
25.88 K
下载
huggingface/transformers: 是一个基于 Python 的自然语言处理库,它使用了 PostgreSQL 数据库存储数据。适合用于自然语言处理任务的开发和实现,特别是对于需要使用 Python 和 PostgreSQL 数据库的场景。特点是自然语言处理库、Python、PostgreSQL 数据库。
最近提交(Master分支:3 个月前 )
3a8eb746 * add fix and examples * fix camel case naming 1 天前
54be2d7a small test fix 2 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐