要安装Bagel试试效果需要安装flash_attnwindows上flash_attn编译非常慢都是用网络上别人编译好的.whl文件Bagel要求的flash_attn2.5.8 torch2.5.1但网上找不到要求的版本就很难受其实不用完全符合要求的自己稍微适配一下也能用但我想试着编译编译做个记录所以就自己捣鼓捣鼓试试Microsoft Visual Studio 2022 版本17.13.5anaconda 版本2024.10-1python 版本3.10Microsoft Visual Studio里关于c的依赖我也不知道需要什么就都安装首先创建虚拟环境conda create -n flash_attn python3.10 -y conda activate flash_attn安装cuda-toolkitconda install cuda-toolkit12.4 -y -c conda-forgecuda-toolkit是虚拟环境内的cuda和本地安装的cuda不冲突使用nvcc -V可以看到版本我本机用的是11.7后来有些算法需要其他版本的cuda懒得搞多版本切换就使用虚拟环境的cuda了安装完后变成12.4了安装完后可以使用Get-Command nvcc | Select-Object -ExpandProperty Source查询路径我的路径是C:\python\envs\flash_attn\Library\bin\nvcc.exe这个路径一会儿要用先放着接下来安装torchBagel的torch的版本是2.5.1pip install torch2.5.1 torchvision0.20.1 --index-url https://download.pytorch.org/whl/cu124接下来创建一个python文件写上代码import os os.environ[CUDA_PATH] rC:\python\envs\flash_attn\Library def run(): os.system(pip install flash_attn2.5.8) if __name__ __main__: run()代码很简单把cuda的环境变量变成虚拟环境的再用子进程进行安装为什么要这么麻烦因为正常安装flash_attn时他会读取环境变量的cuda路径下面开始编译flash_attn编译要很久可以先看我遇到什么问题再自己试试也可以跟着我一步一步试错第一次运行这里有个前提网络必须能访问GitHub否则报错Guessing wheel URL:GitHub上whl的下载地址python main.py报错LINK : fatal error LNK1181: 无法打开输入文件“cudart.lib” error: command C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.43.34808\\bin\\HostX86\\x64\\link.exe failed with exit code 1181打开我的cuda11.7文件夹通过查找找到cudart.lib文件在lib\x64文件夹下而C:\python\envs\flash_attn\Library\lib\x64文件下只有一个cmake文件夹再通过查找发现cudart.lib文件在C:\python\envs\flash_attn\Library\lib文件夹下将C:\python\envs\flash_attn\Library\lib文件夹下的除了文件夹的所有文件都复制到C:\python\envs\flash_attn\Library\lib\x64文件夹下也可以试试缺什么复制什么我嫌多次编译太费时间就都复制过来了再次执行python main.py编译完成有一个问题这次没遇到我就说一下在编译的时候会报cuda_fp16.h文件内找不到nv/target可以进入到C:\python\envs\flash_attn\Library\include\targets\x64文件夹内复制nv文件夹到C:\python\envs\flash_attn\Library\include文件下没报这个错误可以不用管接下来试试源码编译git clone --branch v2.7.4 https://github.com/Dao-AILab/flash-attention.git cd flash-attention选择v2.7.4是因为最新版cuda要12.8新建个python文件思路一样修改环境变量import os os.environ[CUDA_PATH] rC:\python\envs\flash_attn\Library def run(): os.system(python setup.py install) if __name__ __main__: run()报错Traceback (most recent call last): File K:\python\PycharmProjects\flash_attn\flash-attention\setup.py, line 150, in module subprocess.run([git, submodule, update, --init, csrc/composable_kernel], checkTrue) File C:\python\envs\flash_attn\lib\subprocess.py, line 526, in run raise CalledProcessError(retcode, process.args, subprocess.CalledProcessError: Command [git, submodule, update, --init, csrc/composable_kernel] returned non-zero exit status 1.手动执行一下命令行git submodule update --init csrc/composable_kernel再重新执行python main.py经过漫长的等待别人都要求安装对应的cuda什么的我这属于偷懒直接用虚拟环境的cuda为了不重复编译可以编译成whl文件import os os.environ[CUDA_PATH] rC:\python\envs\flash_attn\Library def run(): os.system(python setup.py bdist_wheel) if __name__ __main__: run()