Skip to content

Commit f577286

Browse files
berlinchen7caitWW
andauthored
Make CUDA selective scan install explicit opt-in (#977)
* Made CUDA selective scan optional w/ MAMBA_KEEP_CUDA_BUILD * Bumped python requirement from >=3.9 to >=3.10 --------- Co-authored-by: Caitlin Wang <123908556+caitWW@users.noreply.github.com>
1 parent ed6ce09 commit f577286

8 files changed

Lines changed: 219 additions & 184 deletions

File tree

.github/scripts/build.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ export LD_LIBRARY_PATH=/usr/local/nvidia/lib64:/usr/local/cuda/lib64:$LD_LIBRARY
1212

1313
# Limit MAX_JOBS otherwise the github runner goes OOM
1414
export MAX_JOBS=2
15-
export MAMBA_FORCE_BUILD="TRUE"
16-
export MAMBA_FORCE_CXX11_ABI=$CXX11_ABI
15+
export MAMBA_FORCE_BUILD="TRUE"
16+
export MAMBA_KEEP_CUDA_BUILD="TRUE"
17+
export MAMBA_FORCE_CXX11_ABI=$CXX11_ABI
1718

1819
# 5h timeout since GH allows max 6h and we want some buffer
1920
EXIT_CODE=0

.github/workflows/publish.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,6 @@ jobs:
138138
pip install torch --index-url https://download.pytorch.org/whl/cpu
139139
140140
- name: Build core package
141-
env:
142-
MAMBA_SKIP_CUDA_BUILD: "TRUE"
143141
run: |
144142
python setup.py sdist --dist-dir=dist
145143

README.md

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,42 @@ with an efficient hardware-aware design and implementation in the spirit of [Fla
2525

2626
## Installation
2727

28-
Install PyTorch first, then:
29-
- [Option] `pip install causal-conv1d>=1.4.0 --no-build-isolation`: an efficient implementation of a simple causal Conv1d layer used inside the Mamba block.
30-
- `pip install mamba-ssm --no-build-isolation`: the core Mamba package.
31-
- `pip install mamba-ssm[causal-conv1d] --no-build-isolation`: To install core Mamba package and causal-conv1d.
32-
33-
`--no-build-isolation` is required so that pip uses your existing CUDA-enabled PyTorch instead of installing torch-cpu in an isolated build environment.
34-
35-
NOTE: To use Mamba-3, please install from source `MAMBA_FORCE_BUILD=TRUE pip install --no-cache-dir --force-reinstall git+https://github.com/state-spaces/mamba.git --no-build-isolation`.
36-
37-
Other requirements:
28+
Install PyTorch first. By default, `mamba-ssm` installs the core package without compiling the
29+
`selective_scan_cuda` extension and without downloading cached CUDA-enabled wheels.
30+
31+
| Install mode | Command | Behavior |
32+
| --- | --- | --- |
33+
| Core package | `pip install mamba-ssm --no-build-isolation` | Installs `mamba-ssm` without `selective_scan_cuda` and does not compile CUDA extensions. |
34+
| Core package plus causal-conv1d | `pip install "mamba-ssm[causal-conv1d]" --no-build-isolation` | Installs the core package and the `causal-conv1d` extra, still without `selective_scan_cuda`. |
35+
| Force local core package build | `MAMBA_FORCE_BUILD=TRUE pip install mamba-ssm --no-build-isolation` | Builds the default pure Python wheel locally, still without `selective_scan_cuda`. |
36+
| CUDA selective scan opt-in | `MAMBA_KEEP_CUDA_BUILD=TRUE pip install mamba-ssm --no-build-isolation` | Installs `selective_scan_cuda`; pip first tries a matching prebuilt CUDA/HIP wheel, then compiles locally if no wheel is available. |
37+
| Force local CUDA selective scan build | `MAMBA_FORCE_BUILD=TRUE MAMBA_KEEP_CUDA_BUILD=TRUE pip install mamba-ssm --no-build-isolation` | Skips cached wheels and compiles `selective_scan_cuda` locally. |
38+
39+
`--no-build-isolation` is required for CUDA builds so that pip uses your existing CUDA-enabled
40+
PyTorch instead of installing torch-cpu in an isolated build environment.
41+
42+
Source installs use the same defaults and opt-in flags:
43+
44+
| Install mode | Command |
45+
| --- | --- |
46+
| Source default, no `selective_scan_cuda` | `pip install . --no-build-isolation` |
47+
| Source default from GitHub, no `selective_scan_cuda` | `pip install git+https://github.com/state-spaces/mamba.git --no-build-isolation` |
48+
| Source forced local core build, no `selective_scan_cuda` | `MAMBA_FORCE_BUILD=TRUE pip install . --no-build-isolation` |
49+
| Source CUDA selective scan opt-in | `MAMBA_KEEP_CUDA_BUILD=TRUE pip install . --no-build-isolation` |
50+
| Source forced local CUDA selective scan build | `MAMBA_FORCE_BUILD=TRUE MAMBA_KEEP_CUDA_BUILD=TRUE pip install . --no-build-isolation` |
51+
52+
NOTE: To use Mamba-3 from the latest source tree, install from source. For the default source
53+
install without `selective_scan_cuda`, run `pip install git+https://github.com/state-spaces/mamba.git --no-build-isolation`.
54+
For the CUDA selective scan extension, add `MAMBA_KEEP_CUDA_BUILD=TRUE`; add
55+
`MAMBA_FORCE_BUILD=TRUE` as well to force local CUDA compilation.
56+
57+
Core requirements:
3858
- Linux
39-
- NVIDIA GPU
59+
- Python 3.10+
4060
- PyTorch 1.12+
61+
62+
Additional requirements for CUDA `selective_scan_cuda` builds and GPU execution:
63+
- NVIDIA GPU
4164
- CUDA 11.6+
4265

4366
For AMD cards, see additional prerequisites below.

mamba_ssm/ops/selective_scan_interface.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,26 @@
1717

1818
from mamba_ssm.ops.triton.layer_norm import _layer_norm_fwd
1919

20-
import selective_scan_cuda
20+
try:
21+
import selective_scan_cuda
22+
except ImportError:
23+
selective_scan_cuda = None
24+
25+
26+
def _check_selective_scan_cuda():
27+
if selective_scan_cuda is None:
28+
raise RuntimeError(
29+
"selective_scan_cuda is not installed. To use Mamba-1, reinstall with:\n"
30+
" MAMBA_KEEP_CUDA_BUILD=TRUE pip install mamba-ssm --no-build-isolation"
31+
)
2132

2233

2334
class SelectiveScanFn(torch.autograd.Function):
2435

2536
@staticmethod
2637
def forward(ctx, u, delta, A, B, C, D=None, z=None, delta_bias=None, delta_softplus=False,
2738
return_last_state=False):
39+
_check_selective_scan_cuda()
2840
if u.stride(-1) != 1:
2941
u = u.contiguous()
3042
if delta.stride(-1) != 1:
@@ -192,6 +204,7 @@ def forward(ctx, xz, conv1d_weight, conv1d_bias, x_proj_weight, delta_proj_weigh
192204
"""
193205
xz: (batch, dim, seqlen)
194206
"""
207+
_check_selective_scan_cuda()
195208
assert causal_conv1d_fwd_function is not None, "causal_conv1d_cuda is not available. Please install causal-conv1d."
196209
assert checkpoint_lvl in [0, 1]
197210
L = xz.shape[-1]

0 commit comments

Comments
 (0)