文章目录
- 1. Mamba环境搭建
- 2. triton安装
- 3. causal_conv1d安装
- 3.1 下载causal_conv1d工程文件源码
- 3.2 修改setup.py文件
- 3.3 安装 causal_conv1d
- 4. Mamba安装
- 4.1 下载mamba工程文件源码
- 4.2 修改setup.py文件
- 4.3 安装 mamba
- 5. 查看所有成功安装的库
- 6. 测试mamba安装是否成功
- 6.1 测试成功
- 6.2 测试失败:No module named 'causal_conv1d_cuda' 或 'selective_scan_cuda'
- 6.3 解决方案
- 7. 卸载causal_conv1d和mamba-ssm
- 8. 下载所需文件
1. Mamba环境搭建
参考:https://blog.csdn.net/yyywxk/article/details/136071016
conda clean --all
conda create -n mamba_env python=3.10.13
conda activate mamba_env
conda install cudatoolkit==11.8 -c nvidia
pip install torch==2.1.1 torchvision==0.16.1 torchaudio==2.1.1 --index-url https://download.pytorch.org/whl/cu118
conda install -c "nvidia/label/cuda-11.8.0" cuda-nvcc
conda install packaging
2. triton安装
下载triton文件:https://github.com/PrashantSaikia/Triton-for-Windows/tree/main
# 激活刚才创建的mamba环境
conda activate mamba_env
# 安装triton
pip install 【文件路径】\triton-2.0.0-cp310-cp310-win_amd64.whl
# 如:pip install D:\mamba\triton-2.0.0-cp310-cp310-win_amd64.whl
3. causal_conv1d安装
3.1 下载causal_conv1d工程文件源码
下载causal_conv1d工程文件源码:https://github.com/Dao-AILab/causal-conv1d/releases
这里有各个版本的causal_conv1d,找到v1.1.1
进入v1.1.1资源界面:
拉到最后,点击Source code(zip),直接下载
3.2 修改setup.py文件
解压causal-conv1d-1.1.1.zip文件
将里面的源码setup.py进行以下改动:
参考:https://blog.csdn.net/yyywxk/article/details/136071016和https://blog.csdn.net/m0_59115667/article/details/137794459
将下面的代码
FORCE_BUILD = os.getenv("CAUSAL_CONV1D_FORCE_BUILD", "FALSE") == "TRUE"
SKIP_CUDA_BUILD = os.getenv("CAUSAL_CONV1D_SKIP_CUDA_BUILD", "FALSE") == "TRUE"
# For CI, we want the option to build with C++11 ABI since the nvcr images use C++11 ABI
FORCE_CXX11_ABI = os.getenv("CAUSAL_CONV1D_FORCE_CXX11_ABI", "FALSE") == "TRUE"
修改为
FORCE_BUILD = os.getenv("CAUSAL_CONV1D_FORCE_BUILD", "FALSE") == "FALSE"
SKIP_CUDA_BUILD = os.getenv("CAUSAL_CONV1D_SKIP_CUDA_BUILD", "FALSE") == "FALSE"
# For CI, we want the option to build with C++11 ABI since the nvcr images use C++11 ABI
FORCE_CXX11_ABI = os.getenv("CAUSAL_CONV1D_FORCE_CXX11_ABI", "FALSE") == "FALSE"
保存
3.3 安装 causal_conv1d
# 激活刚才创建的mamba环境
conda activate mamba_env
# 打开causal_conv1d所在文件夹
cd/d D:\Anaconda\Mamba\causal-conv1d-1.1.1 # 改成你自己的causal-conv1d-1.1.1文件路径
# 安装
pip install . 或者 python setup.py install
4. Mamba安装
4.1 下载mamba工程文件源码
下载mamba工程文件源码:https://github.com/state-spaces/mamba/releases?page=2
步骤跟causal_conv1d一样,这里有各个版本的mamba,找到v1.1.1
4.2 修改setup.py文件
解压mamba-1.1.1文件
mamba工程文件的源码setup.py中我们要进行以下改动:
将下面的代码
FORCE_BUILD = os.getenv("MAMBA_FORCE_BUILD", "FALSE") == "TRUE"
SKIP_CUDA_BUILD = os.getenv("MAMBA_SKIP_CUDA_BUILD", "FALSE") == "TRUE"
修改为
FORCE_BUILD = os.getenv("MAMBA_FORCE_BUILD", "FALSE") == "FALSE"
SKIP_CUDA_BUILD = os.getenv("MAMBA_SKIP_CUDA_BUILD", "FALSE") == "FALSE"
4.3 安装 mamba
# 激活mamba环境
conda activate mamba_env
# 打开文件夹
cd/d D:\Anaconda\Mamba\mamba-1.1.1 # 改成你自己的mamba-1.1.1文件路径
# 安装
pip install . 或者 python setup.py install
5. 查看所有成功安装的库
6. 测试mamba安装是否成功
# 激活mamba环境
conda activate mamba_env
# 进入python编译
python
# 加载库
import torch
import causal_conv1d
from mamba_ssm import Mamba
# 代码函数
batch, length, dim = 2, 64, 16
x = torch.randn(batch, length, dim).to("cuda")
model = Mamba(
# This module uses roughly 3 * expand * d_model^2 parameters
d_model=dim, # Model dimension d_model
d_state=16, # SSM state expansion factor
d_conv=4, # Local convolution width
expand=2, # Block expansion factor
).to("cuda")
y = model(x)
assert y.shape == x.shape
print('success')
6.1 测试成功
6.2 测试失败:No module named ‘causal_conv1d_cuda’ 或 ‘selective_scan_cuda’
from mamba_ssm import Mamba不成功
报错:ModuleNotFoundError: No module named ‘causal_conv1d_cuda’ || ‘selective_scan_cuda’
6.3 解决方案
- 根据报错文件路径,找到causal_conv1d_interface.py和selective_scan_interface.py文件
- 用vscode或其他编辑软件打开
- 注释或修改报错内容
a. causal_conv1d_interface.py和selective_scan_interface.py注释
文件:causal_conv1d_interface.py
# import causal_conv1d_cuda
文件:selective_scan_interface.py
# import causal_conv1d_cuda
# import selective_scan_cuda
b. 修改causal_conv1d_interface.py中的causal_conv1d_fn函数
def causal_conv1d_fn(x, weight, bias=None, seq_idx=None, activation=None):
"""
x: (batch, dim, seqlen)
weight: (dim, width)
bias: (dim,)
seq_idx: (batch, seqlen)
activation: either None or "silu" or "swish"
out: (batch, dim, seqlen)
"""
return CausalConv1dFn.apply(x, weight, bias, seq_idx, activation)
修改为
def causal_conv1d_fn(x, weight, bias=None, seq_idx=None, activation=None):
"""
x: (batch, dim, seqlen)
weight: (dim, width)
bias: (dim,)
seq_idx: (batch, seqlen)
activation: either None or "silu" or "swish"
out: (batch, dim, seqlen)
"""
return causal_conv1d_ref(x, weight, bias, activation)
c. 修改selective_scan_interface.py中的selective_scan_fn和mamba_inner_fn函数
def selective_scan_fn(u, delta, A, B, C, D=None, z=None, delta_bias=None, delta_softplus=False,
return_last_state=False):
"""if return_last_state is True, returns (out, last_state)
last_state has shape (batch, dim, dstate). Note that the gradient of the last state is
not considered in the backward pass.
"""
return SelectiveScanFn.apply(u, delta, A, B, C, D, z, delta_bias, delta_softplus, return_last_state)
def mamba_inner_fn(
xz, conv1d_weight, conv1d_bias, x_proj_weight, delta_proj_weight,
out_proj_weight, out_proj_bias,
A, B=None, C=None, D=None, delta_bias=None, B_proj_bias=None,
C_proj_bias=None, delta_softplus=True
):
return MambaInnerFn.apply(xz, conv1d_weight, conv1d_bias, x_proj_weight, delta_proj_weight,
out_proj_weight, out_proj_bias,
A, B, C, D, delta_bias, B_proj_bias, C_proj_bias, delta_softplus)
修改为
def selective_scan_fn(u, delta, A, B, C, D=None, z=None, delta_bias=None, delta_softplus=False,
return_last_state=False):
"""if return_last_state is True, returns (out, last_state)
last_state has shape (batch, dim, dstate). Note that the gradient of the last state is
not considered in the backward pass.
"""
return selective_scan_ref(u, delta, A, B, C, D, z, delta_bias, delta_softplus, return_last_state)
def mamba_inner_fn(
xz, conv1d_weight, conv1d_bias, x_proj_weight, delta_proj_weight,
out_proj_weight, out_proj_bias,
A, B=None, C=None, D=None, delta_bias=None, B_proj_bias=None,
C_proj_bias=None, delta_softplus=True
):
return mamba_inner_ref(xz, conv1d_weight, conv1d_bias, x_proj_weight, delta_proj_weight,
out_proj_weight, out_proj_bias,
A, B, C, D, delta_bias, B_proj_bias, C_proj_bias, delta_softplus)
7. 卸载causal_conv1d和mamba-ssm
上述对你的电脑来说,可能也不会成功,……方便卸载:)再试试别的办法吧~~
pip uninstall causal_conv1d
pip uninstall mamba-ssm
8. 下载所需文件
不想去官网下载的,可以直接网盘下载我已经修改好setup.py文件所有文件。
链接:https://pan.baidu.com/s/1NKoqUPIGd_UexBdDFZxljQ
提取码:3asf