Mamba 官方代码链接为:https://github.com/state-spaces/mamba,在原来博客 “Mamba 环境安装踩坑问题汇总及解决方法” 基础上,不绕过selective_scan_cuda,进行 Mamba 环境安装,这样可以获得和 Linux 一样的速度1。
本文涉及到的所有文件均可在:mamba 安装包,包括Windows和Linux下载。
(安装问题 / 资源自取售后 / 论文合作想法请+vx:931744281
)
conda create -n mamba python=3.10
conda activate mamba
conda install cudatoolkit==11.8
pip install torch==2.1.1 torchvision==0.16.1 torchaudio==2.1.1 --index-url https://download.pytorch.org/whl/cu118
pip install setuptools==68.2.2
conda install nvidia/label/cuda-11.8.0::cuda-nvcc_win-64
conda install packaging
pip install triton-2.0.0-cp310-cp310-win_amd64.whl
其中 triton-2.0.0-cp310-cp310-win_amd64.whl
获取参看原来博客 “Mamba 环境安装踩坑问题汇总及解决方法” 。
causal-conv1d
的安装,同原来博客 “Mamba 环境安装踩坑问题汇总及解决方法” ,具体细化为:git clone https://github.com/Dao-AILab/causal-conv1d.git
cd causal-conv1d
git checkout v1.1.1 # 安装最新版的话,此步可省略
set CAUSAL_CONV1D_FORCE_BUILD=TRUE
pip install .
官方没有编译好的适用于Windows版本的 whl,因此需要用上述步骤来手动编译。笔者编译好了 Windows 下的 causal_conv1d-1.1.1-cp310-cp310-win_amd64.whl,亦可直接下载安装(只适用于torch 2.1)。
pip install causal_conv1d-1.1.1-cp310-cp310-win_amd64.whl
成功安装之后,会在相应虚拟环境中(xxx\conda\envs\xxx\Lib\site-packages\
)产生 causal_conv1d_cuda.cp310-win_amd64.pyd
文件,此文件对应 causal_conv1d_cuda 包。
mamba-ssm
环境准备,下载工程文件,即git clone https://github.com/state-spaces/mamba.git
cd mamba
git checkout v1.1.3 # 安装最新版的话,此步可省略
注意,上述过程中,新版 mamba-ssm 需要搭配新版 causal-conv1d,要不然函数不兼容。完成前期工作后进入下一步正式编译。
mamba-ssm
的编译setup.py
修改第41行配置:FORCE_BUILD = os.getenv("MAMBA_FORCE_BUILD", "TRUE") == "TRUE"
csrc/selective_scan/selective_scan_fwd_kernel.cuh
的 void selective_scan_fwd_launch
函数改为void selective_scan_fwd_launch(SSMParamsBase ¶ms, cudaStream_t stream) {
// Only kNRows == 1 is tested for now, which ofc doesn't differ from previously when we had each block
// processing 1 row.
static constexpr int kNRows = 1;
BOOL_SWITCH(params.seqlen % (kNThreads * kNItems) == 0, kIsEvenLen, [&] {
BOOL_SWITCH(params.is_variable_B, kIsVariableB, [&] {
BOOL_SWITCH(params.is_variable_C, kIsVariableC, [&] {
BOOL_SWITCH(params.z_ptr != nullptr , kHasZ, [&] {
using Ktraits = Selective_Scan_fwd_kernel_traits<kNThreads, kNItems, kNRows, kIsEvenLen, kIsVariableB, kIsVariableC, kHasZ, input_t, weight_t>;
// constexpr int kSmemSize = Ktraits::kSmemSize;
static constexpr int kSmemSize = Ktraits::kSmemSize + kNRows * MAX_DSTATE * sizeof(typename Ktraits::scan_t);
// printf("smem_size = %d\n", kSmemSize);
dim3 grid(params.batch, params.dim / kNRows);
auto kernel = &selective_scan_fwd_kernel<Ktraits>;
if (kSmemSize >= 48 * 1024) {
C10_CUDA_CHECK(cudaFuncSetAttribute(
kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, kSmemSize));
}
kernel<<<grid, Ktraits::kNThreads, kSmemSize, stream>>>(params);
C10_CUDA_KERNEL_LAUNCH_CHECK();
});
});
});
});
}
csrc/selective_scan/static_switch.h
的 BOOL_SWITCH
函数改为#define BOOL_SWITCH(COND, CONST_NAME, ...) \
[&] { \
if (COND) { \
static constexpr bool CONST_NAME = true; \
return __VA_ARGS__(); \
} else { \
static constexpr bool CONST_NAME = false; \
return __VA_ARGS__(); \
} \
}()
(这几步是将 constexpr
改为 static constexpr
)
csrc/selective_scan/selective_scan_bwd_kernel.cuh
和 csrc/selective_scan/selective_scan_fwd_kernel.cuh
文件开头加入:#ifndef M_LOG2E
#define M_LOG2E 1.4426950408889634074
#endif
pip install .
一般即可顺利编译,成功安装。pip install mamba_ssm-1.1.3-cp310-cp310-win_amd64.whl
由于此时没有绕过selective_scan_cuda,在虚拟环境中(xxx\conda\envs\xxx\Lib\site-packages\
)产生了 selective_scan_cuda.cp310-win-amd64.pyd 文件,所以运行速度较快。
mamba_ssm
的编译出现的问题及解决(20240714)如果不进行修改利用 `pip install .`` 直接编译源码时会出现如下报错:
subprocess.CalledProcessError: Command '['ninja', '-v']' returned non-zero exit status 1.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/data/xxx/anaconda3/envs/xxx/lib/python3.10/site-packages/torch/utils/cpp_extension.py", line 2116, in _run_ninja_build
raise RuntimeError(message) from e
RuntimeError: Error compiling objects for extension
[end of output]
这是最基本的报错信息,只要编译出错就会输出这些,如果在其上面没有看到具体报错,可在 setup.py
里面,将
cmdclass={"bdist_wheel": CachedWheelsCommand, "build_ext": BuildExtension}
改为
cmdclass={"bdist_wheel": CachedWheelsCommand, "build_ext": BuildExtension.with_options(use_ninja=False)}
pytorch默认使用ninjia作为backend2,禁用掉可以看到具体的报错,但是编译速度实测会变慢,所以解决bug后可以改回来。
注:有的博客将 anaconda环境下的 lib/python3.6/site-packages/torch/utils/cpp_extension.py文件里的[‘ninja’,‘-v’]改成[‘ninja’,‘–v’] 或者[‘ninja’,‘–version’] 是错误的做法,治标不治本。
不少小伙伴在装完 cuda-nvcc
以后,安装 causal-conv1d
时还是会显示CUDA版本不对的错误,这是由于环境中还可能 CUDA_PATH
(Windows)变量指定到错误的位置,此时需要检查:
nvcc -V
python -c "import torch.utils.cpp_extension; print(torch.utils.cpp_extension.CUDA_HOME)"
确保其输出的是正确的版本或位置。尤其是要保证第二句命令输出的位置是正确的。
在 Win 下,则使用 where nvcc
虚拟环境正确的路径(路径到bin之前,不包括 bin\nvcc),把系统环境变量里的 CUDA_PATH
修改为该路径,然后继续安装过程。
在Windows下会出现如下大量报错:
xxx\mamba-1.1.3\csrc\selective_scan\selective_scan_bwd_kernel.cuh(221): error: identifier "M_LOG2E" is undefined
出现这种情况的原因,可参考 issue:
Note for the owners: The reason for needing #define is stated here: https://stackoverflow.com/a/56974843:
“On windows it is using the Microsoft compiler for that. So the Microsoft compiler is correct to disallow VLA, and there is no way to avoid this AFAIK. Your code works on linux, because on linux nvcc uses the g++ host compiler, and it allows (in a non-standard-compliant way) the use of a VLA in C++ host code.”
因此,只需在csrc/selective_scan/cus/selective_scan_bwd_kernel.cuh
和 csrc/selective_scan/cus/selective_scan_fwd_kernel.cuh
文件加入以下代码即可
#ifndef M_LOG2E
#define M_LOG2E 1.4426950408889634074
#endif
在Windows下会出现如下大量报错:
error C2975: “kIsVariableC_”:“Selective_Scan_bwd_kernel_traits”的模板参数无效,应为编译时常量表达式
将csrc/selective_scan/static_switch.h
函数里的 constexpr
改为 static constexpr
,参考 issue。具体步骤参看前一节。
在Windows下会出现如下大量报错:
xxx\mamba-1.1.3\csrc\selective_scan\selective_scan_fwd_kernel.cuh(314): error C2975: “kNRows_”:“Selective_Scan_fwd_kernel_traits”的模板参数无效,应为编译时常量表达式
将csrc/selective_scan/selective_scan_fwd_kernel.cuh
函数 void selective_scan_fwd_launch
里的 constexpr
改为 static constexpr
,参考 issue。具体步骤参看前一节。
有的同学在安装好之后发现出现了以下报错:
ImportError: DLL load failed while importing causal_conv1d_cuda: 找不到指定的程序。
或者是
ImportError: DLL load failed while importing selective_scan_cuda: 找不到指定的程序。
虽然在虚拟环境的相应位置中,已经生成了 causal_conv1d_cuda.cp310-win-amd64.pyd
和 selective_scan_cuda.cp310-win-amd64.pyd
但是还是无法导入调用。
查询这两个包的依赖项,发现
它们均高度依赖于torch的相关dll,因此推测出现报错是由于torch的版本发生冲突。卸载torch 并重新安装发现解决问题。(某同学环境中装了两个不同版本的torch,因此冲突)
pip uninstall torch
pip install torch==2.1.1 torchvision==0.16.1 torchaudio==2.1.1 --index-url https://download.pytorch.org/whl/cu118
由于本人前面的编译的两个whl环境均为 torch 2.1,所以大家安装的环境必须也得是2.1,否则通过whl安装调用相关函数时就会报这个错误。
python,torch 及 cuda 版本必须和本人前面的环境保持一致。
正常安装出现报错:
RuntimeError:CUDA error:no kernel image is available for execution on the device
CUDA kernel errors might be asynchronously reported at some other API call, so ther stacktrace below might be incorrect.
这个问题在本人博客(VMamba 安装教程(无需更改base环境中的cuda版本))已经提到过,是因为GPU算力的问题。causal-conv1d 、 mamba-ssm 以及 vmamba 均暂时不支持算力为6.1及以下的设备,解决方法见这个博客,由于需要重新编译,前述的whl均不再适用。经过复杂的编译过程,得到:
支持低算力版的安装包:
低算力版安装包优惠通道(站外):
triton
的问题由于 triton
官方目前只支持Linux,因此在 Windows 系统运行时,函数中只要涉及到其调用都会出现报错,包括但不限于:
KeyError: 'HOME'
RuntimeError: failed to find C compiler, Please specify via cc environment variable.
终极解决方案参考Windows 下 Mamba / Vim / Vmamba 环境安装终极版:Windows 下Mamba2 / Vim / Vmamba 环境安装问题记录及解决方法终极版(无需绕过triton)
DLL load failed
问题。有环境版本定制化需求请私信vx。Windows Support #12 ↩︎
出现错误“subprocess.CalledProcessError: Command ‘[‘ninja‘, ‘-v‘]‘ returned non-zero exit status 1”解决方法 ↩︎