安装flash-attention踩坑

flash-attn官方仓库

flash-attention的github仓库

pypi上显示的安装方法
https://pypi.org/project/flash-attn/

问题1:

服务器上没有安装git导致的。因为其要去github上去拉取代码

错误如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
(selfragone) [root@IP- flash-attention]# pip install flash-attn==2.3.6 --no-build-isolation
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting flash-attn==2.3.6
Using cached https://pypi.tuna.tsinghua.edu.cn/packages/3c/49/95b86adfc0d90676dcb07fcbef47c71997e6e7c9e71fda51598a962d9148/flash_attn-2.3.6.tar.gz (2.3 MB)
Preparing metadata (setup.py) ... error
error: subprocess-exited-with-error

× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [20 lines of output]
fatal: Not a git repository (or any of the parent directories): .git
/tmp/pip-install-0cmp9saf/flash-attn_baf657cc7596431f9a5503456fb76bc8/setup.py:78: UserWarning: flash_attn was requested, but nvcc was not found. Are you sure your environment has nvcc available? If you're installing within a container from https://hub.docker.com/r/pytorch/pytorch, only images whose names contain 'devel' will provide nvcc.
warnings.warn(
Traceback (most recent call last):
File "<string>", line 2, in <module>
File "<pip-setuptools-caller>", line 34, in <module>
File "/tmp/pip-install-0cmp9saf/flash-attn_baf657cc7596431f9a5503456fb76bc8/setup.py", line 133, in <module>
CUDAExtension(
File "/root/anaconda3/envs/selfragone/lib/python3.9/site-packages/torch/utils/cpp_extension.py", line 1047, in CUDAExtension
library_dirs += library_paths(cuda=True)
File "/root/anaconda3/envs/selfragone/lib/python3.9/site-packages/torch/utils/cpp_extension.py", line 1179, in library_paths
if (not os.path.exists(_join_cuda_home(lib_dir)) and
File "/root/anaconda3/envs/selfragone/lib/python3.9/site-packages/torch/utils/cpp_extension.py", line 2230, in _join_cuda_home
raise EnvironmentError('CUDA_HOME environment variable is not set. '
OSError: CUDA_HOME environment variable is not set. Please set it to your CUDA install root.


torch.__version__ = 1.13.1


[end of output]

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

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

解决方案:

很简单,直接安装git就行

1
sudo yum install -y git

# CentOS7安装Git以及操作

问题2

尝试安装的时候出现错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
(selfragone) [root@IP-210-30-200-60 flash-attention]# python setup.py install


torch.__version__ = 1.13.1


/data/yuanql/flash-attention/setup.py:78: UserWarning: flash_attn was requested, but nvcc was not found. Are you sure your environment has nvcc available? If you're installing within a container from https://hub.docker.com/r/pytorch/pytorch, only images whose names contain 'devel' will provide nvcc.
warnings.warn(
Traceback (most recent call last):
File "/data/yuanql/flash-attention/setup.py", line 133, in <module>
CUDAExtension(
File "/root/anaconda3/envs/selfragone/lib/python3.9/site-packages/torch/utils/cpp_extension.py", line 1047, in CUDAExtension
library_dirs += library_paths(cuda=True)
File "/root/anaconda3/envs/selfragone/lib/python3.9/site-packages/torch/utils/cpp_extension.py", line 1179, in library_paths
if (not os.path.exists(_join_cuda_home(lib_dir)) and
File "/root/anaconda3/envs/selfragone/lib/python3.9/site-packages/torch/utils/cpp_extension.py", line 2230, in _join_cuda_home
raise EnvironmentError('CUDA_HOME environment variable is not set. '
OSError: CUDA_HOME environment variable is not set. Please set it to your CUDA install root.

重要的解决方案参考:

# flash-attention踩坑:使用conda管理CUDA

其中第4步似乎是最重要的

https://anaconda.org/nvidia/cuda-nvcc

根据本机的环境最终选择的方案为:

1
conda install cudatoolkit==11.6 -c nvidia
1
conda install nvidia/label/cuda-11.7.1::cuda-nvcc

最后执行 pip install flash-attn==2.3.6 即可安装成功。


安装flash-attention踩坑
http://yuanql.top/2024/01/04/000_自然语言处理/安装flash-attention踩坑/
作者
Qingli Yuan
发布于
2024年1月4日
许可协议