Windows 下 VMamba 安装教程(无需更改base环境中的cuda版本且可加速)

导航

背景

最近有不少小伙伴在 Linux 上安装 Vmamba (VMamba: Visual State Space Model)时,按照官方的安装命令会遇到各种各样的错误,Linux 版本的问题可参考本人之前的博客——Vmamba 安装教程(无需更改base环境中的cuda版本),本篇主要聚焦于 Windows 下 Vmamba 环境的安装,主要参考 issue。(安装问题 / 资源自取 / 论文合作想法请加主页wx)注意,Mamba 和 Vmamba 的环境安装步骤是不同的,请加以区分。

Windows 下环境准备

  1. 运行之前需要下载好 Vmamba 官方的源码,用 git 下载方式如下:
git clone https://github.com/MzeroMiko/VMamba.git
cd VMamba
  1. 前期环境准备,同原来博客 “Window 下Mamba 环境安装踩坑问题汇总及解决方法 (无需绕过selective_scan_cuda)” ,具体为:
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  # 安装 mamba 必须,Vmamba可不用

其中 triton 包是安装 mamba 必须,Vmamba可不用,Windows下triton 2.0.0 的whl下载链接为:hugging-face | 国内镜像,triton 2.1.0 版本的下载链接

  1. 安装 Vmamba 其他必须包,只留最后一步编译
pip install -r requirements.txt
cd kernels/selective_scan

Windows 下 selective_scan 的编译

  • kernels/selective_scan/csrc/selective_scan/static_switch.hBOOL_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

  • 在如下文件
    kernels/selective_scan/csrc/selective_scan/cus/selective_scan_bwd_kernel.cuh
    kernels/selective_scan/csrc/selective_scan/cus/selective_scan_fwd_kernel.cuh
    kernels/selective_scan/csrc/selective_scan/cusoflex/selective_scan_bwd_kernel_oflex.cuh
    kernels/selective_scan/csrc/selective_scan/cusoflex/selective_scan_fwd_kernel_oflex.cuh
    开头加入:
#ifndef M_LOG2E
#define M_LOG2E 1.4426950408889634074
#endif
  • 完成上述修改后,执行 pip install . 一般即可顺利编译,成功安装。
  • 本人编译好的Windows 下的whl 也有:selective-scan-0.0.2 ,可直接下载安装或联系主页vx自取。利用 whl 安装命令为:
pip install selective-scan-0.0.2-cp310-cp310-win-amd64.whl

Windows 下 selective_scan 的编译出现的问题和解决(20240714)

0. 基本报错

当编译中遇到任意错误时,均会输出下图所示的提示:
在这里插入图片描述
内容上即:

Building wheels for collected packages: selective-scan
	Building wheel for selective-scan(setup.py)... error
	error:subprocess-exited-with-error
	
	python setup.py bdist_wheel did not run successfully.
	exit code:1
		[172 lines of output]
		No CUDA runtime is found, using CUDA_HOME='E:\Anaconda\vmamba'

		torch._version=2.1.1+cu118
		
		CUDA HOME = E:\Anaconda\vmamba
		
		CUDA version :11.8
		running bdist_wheel
		running build
		running build_ext
		E: \Anaconda\vmamballib\site-packages(torch\utilscpp_extension.py:383: UserWarning: Error checking compiler versioforcl:[WinError2]系统找不到指定的文件。
			warnings.warn(f'Error checking compiler version for {compiler}: {error}')
		building 'selective scan cudaoflex'extension
		creating E:\Desktop\ToDo\VMambalkernels\selective_scan\build
		creating E:\Desktop\ToDo\VMamba\kernels\selective_scan\build\temp.win-amd64-cpython-310

注意:此处的报错信息不包含有价值的信息,应该往下找真正的报错 error (非 warning)。

1. error: identifier “M_LOG2E” is undefined

在Windows下会出现如下报错:
在这里插入图片描述
出现这种情况的原因,可参考 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.”

因此,只需在四个文件下加入以下代码即可

#ifndef M_LOG2E
#define M_LOG2E 1.4426950408889634074
#endif

具体文件和步骤参看前一节。

2. error C2975: “kDeltaSoftplus_”

在Windows下会出现如下大量报错:

error C2975: “kDeltaSoftplus_”:“Selective_Scan_bwd_kernel_traits”的模板参数无效,应为编译时常量表达式

如下图所示:
在这里插入图片描述
只需把static_switch.h 函数里的 constexpr 改为 static constexpr,参考 issue。具体步骤参看前一节。

20240715 更新

3. fatal error C1083: 无法打开包括文件:“cuda_runtime.h”

如果遇到下面的错误:
在这里插入图片描述
显示为 fatal error C1083: 无法打开包括文件:“cuda_runtime.h”: No such file or directory

E:\VNambalkernels\selective _scan csrc selective _scan cusoflex selective_scan_core_bwd.cu: fatal error C1083:无法打开包括文件:“cuda_runtime.h”: No such file or directory

参考本人之前博客:VMamba 安装教程(无需更改base环境中的cuda版本),解决方案为:

conda install nvidia/label/cuda-11.8.0::cuda-cudart-dev  # 根据CUDA版本

4. fatal error C1189: # error: unsupported Microsoft Visual Studio version!

如遇到以下错误:
在这里插入图片描述
具体为fatal error C1189: #error: -- unsupported Microsoft Visual Studio version!

E:\Anaconda\vmambalincludecrt/host_config.h(153): fatal error C1189: #error:	-- unsupported Microsoft Visual Studio version! Only the versions between 2017 and 2022 (inclusive) aresupported! The nvcc flag '-allow-unsupported-compiler' can be used to override this version check; however, using an unsupported host compiler may cause compilation failure or incorrect run time execution. Use at your own risk.

此类报错只是一连串报错的结果,不包含有用的报错信息,应该在其前面寻找真正的报错原因。

5. fatal error C1083: 无法打开包括文件: “cusparse.h”

如果遇到以下报错:
在这里插入图片描述
显示为 fatal error C1083: 无法打开包括文件: “cusparse.h”: No such file or directory

E:\Anaconda\vmanba liblsite-packagesitorch include Aen/cuda/cuDAcontext.h(6): fatal eror C1083: 无法打开包括文件:“cusparse,h”; No such file or directory

这个问题同Linux下的错误,可参考本人之前博客:VMamba 安装教程(无需更改base环境中的cuda版本)

conda install nvidia/label/cuda-11.8.0::libcusparse-dev   # 根据CUDA版本

类似地,如果遭遇到

  • fatal error C1083: 无法打开包括文件: “cublas_v2.h”: No such file or directory
  • fatal error C1083: 无法打开包括文件: “cusolverDn.h”: No such file or directory
  • fatal error C1083: 无法打开包括文件: “crypt.h”: No such file or directory
    等报错,均可参考本人之前博客:VMamba 安装教程(无需更改base环境中的cuda版本)

6. 正常安装 Vmamba 找不到找不到 selective_scan_cuda 模块

VMamba 中遇到selective_scan_cuda 模块

  • No module named 'selective_scan_cuda'

VMamba 的代码和 Mamba 是独立的,如果想有 selective_scan_cuda 包,则需要安装 mamba_ssm,参考之前博客:

注意,mamba 的这个包在Vmamba里面不是必须的。

7. 无法导入selective_scan_cuda_oflex和selective_scan_cuda_core模块

这是因为安装的是Mamba 而不是 VMamba,Mamba 安装成功后环境中本来就没有selective_scan_cuda_oflex和selective_scan_cuda_core模块,请按Vmamba 正确的顺序安装 Vmamba的环境。

8. 正常安装 Vmamba 找不到 selective_scan_cuda_core 模块

除了selective_scan_cuda_core 模块,还有可能找不到 selective_scan_cuda_nrow 模块,这是因为在编译 selective_scan 时,在 setup.py 第40行选用的模式只有 [‘oflex’]

MODES = ["oflex"]
# MODES = ["core", "ndstate", "oflex"]
# MODES = ["core", "ndstate", "oflex", "nrow"]

改为:

MODES = ["core", "oflex"]
# MODES = ["core", "ndstate", "oflex"]
# MODES = ["core", "ndstate", "oflex", "nrow"]

修改此处再编译即可。注意,该模块也不是必须的。而ndstate 在最新代码中源文件已经被删除,默认模式即可,不需要修改,不做深入研究无必要安装这个包。

Win 下面编译好的 whl 为:selective-scan-0.0.2-cp310-cp310-win-amd64.whl (包含core),selective_scan_cuda_core 模块为:selective-scan-cuda-core.cp310-win-amd64.pyd

  • 23
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值