win11环境下成功安装mamba

文章目录

  • [1. Mamba环境搭建](#1. Mamba环境搭建)
  • [2. triton安装](#2. triton安装)
  • [3. causal_conv1d安装](#3. causal_conv1d安装)
    • [3.1 下载causal_conv1d工程文件源码](#3.1 下载causal_conv1d工程文件源码)
    • [3.2 修改setup.py文件](#3.2 修改setup.py文件)
    • [3.3 安装 causal_conv1d](#3.3 安装 causal_conv1d)
  • [4. Mamba安装](#4. Mamba安装)
    • [4.1 下载mamba工程文件源码](#4.1 下载mamba工程文件源码)
    • [4.2 修改setup.py文件](#4.2 修改setup.py文件)
    • [4.3 安装 mamba](#4.3 安装 mamba)
  • [5. 查看所有成功安装的库](#5. 查看所有成功安装的库)
  • [6. 测试mamba安装是否成功](#6. 测试mamba安装是否成功)
    • [6.1 测试成功](#6.1 测试成功)
    • [6.2 测试失败:No module named 'causal_conv1d_cuda' 或 'selective_scan_cuda'](#6.2 测试失败:No module named 'causal_conv1d_cuda' 或 'selective_scan_cuda')
    • [6.3 解决方案](#6.3 解决方案)
  • [7. 卸载causal_conv1d和mamba-ssm](#7. 卸载causal_conv1d和mamba-ssm)
  • [8. 下载所需文件](#8. 下载所需文件)

1. Mamba环境搭建

参考:https://blog.csdn.net/yyywxk/article/details/136071016

javascript 复制代码
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

javascript 复制代码
# 激活刚才创建的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/136071016https://blog.csdn.net/m0_59115667/article/details/137794459

将下面的代码

javascript 复制代码
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"

修改为

javascript 复制代码
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

javascript 复制代码
# 激活刚才创建的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中我们要进行以下改动:
将下面的代码

javascript 复制代码
FORCE_BUILD = os.getenv("MAMBA_FORCE_BUILD", "FALSE") == "TRUE"
SKIP_CUDA_BUILD = os.getenv("MAMBA_SKIP_CUDA_BUILD", "FALSE") == "TRUE"

修改为

javascript 复制代码
FORCE_BUILD = os.getenv("MAMBA_FORCE_BUILD", "FALSE") == "FALSE"
SKIP_CUDA_BUILD = os.getenv("MAMBA_SKIP_CUDA_BUILD", "FALSE") == "FALSE"

4.3 安装 mamba

javascript 复制代码
# 激活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安装是否成功

javascript 复制代码
# 激活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 解决方案

  1. 根据报错文件路径,找到causal_conv1d_interface.pyselective_scan_interface.py文件
  2. 用vscode或其他编辑软件打开
  3. 注释或修改报错内容

a. causal_conv1d_interface.py和selective_scan_interface.py注释

javascript 复制代码
文件: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函数

javascript 复制代码
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)

修改为

javascript 复制代码
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函数

javascript 复制代码
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)

修改为

javascript 复制代码
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

上述对你的电脑来说,可能也不会成功,......方便卸载:)再试试别的办法吧~~

javascript 复制代码
pip uninstall causal_conv1d
pip uninstall mamba-ssm

8. 下载所需文件

不想去官网下载的,可以直接网盘下载我已经修改好setup.py文件所有文件。

链接:https://pan.baidu.com/s/1NKoqUPIGd_UexBdDFZxljQ

提取码:3asf

相关推荐
神奇夜光杯15 分钟前
Python酷库之旅-第三方库Pandas(181)
开发语言·人工智能·python·excel·pandas·标准库及第三方库·学习与成长
光明中黑暗16 分钟前
Python 学习笔记
笔记·python·学习
tang138976426 分钟前
Python(包和模块)
开发语言·python
正义的彬彬侠39 分钟前
绘制近似线性可分支持向量机的分类边界和支持向量
人工智能·python·机器学习·支持向量机·分类·svm
纪怽ぅ44 分钟前
LSTM——长短期记忆神经网络
python·深度学习·神经网络·算法·机器学习·lstm
yannan201903131 小时前
【算法】(Python)回溯算法
python·算法
心软且酷丶1 小时前
leetcode:面试题 05.07. 配对交换(python3解法)
python·算法·leetcode
bigbig猩猩1 小时前
基于python的语音识别与蓝牙通信的温控系统毕设项目
python·django·语音识别
B站计算机毕业设计超人1 小时前
计算机毕业设计Python+大模型股票预测系统 股票推荐系统 股票可视化 股票数据分析 量化交易系统 股票爬虫 股票K线图 大数据毕业设计 AI
大数据·爬虫·python·深度学习·数据挖掘·数据分析·课程设计
FreedomLeo11 小时前
Python数据分析NumPy和pandas(十六、文本格式数据的读取与存储:csv、json、xml和html)
xml·python·数据分析·html·json·pandas·csv