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