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

相关推荐
hunter2062069 分钟前
用opencv生成视频流,然后用rtsp进行拉流显示
人工智能·python·opencv
Johaden2 小时前
EXCEL+Python搞定数据处理(第一部分:Python入门-第2章:开发环境)
开发语言·vscode·python·conda·excel
小虎牙^O^3 小时前
2024春秋杯密码题第一、二天WP
python·密码学
梦魇梦狸º4 小时前
mac 配置 python 环境变量
chrome·python·macos
查理零世4 小时前
算法竞赛之差分进阶——等差数列差分 python
python·算法·差分
查士丁尼·绵6 小时前
面试-字符串1
python
小兜全糖(xdqt)7 小时前
python中单例模式
开发语言·python·单例模式
Python数据分析与机器学习7 小时前
python高级加密算法AES对信息进行加密和解密
开发语言·python
noravinsc7 小时前
python md5加密
前端·javascript·python
唯余木叶下弦声7 小时前
PySpark之金融数据分析(Spark RDD、SQL练习题)
大数据·python·sql·数据分析·spark·pyspark