Mamba SSM - State Space Models optimized for Apple Silicon
Project description
Mamba SSM for macOS Apple Silicon
Mamba 1 and Mamba 2 State Space Models for Apple Silicon
Training and inference of Mamba 1 & 2 on Apple Silicon with MPS acceleration. Works without CUDA/Triton.
Installation
pip install mamba-ssm-macos
Prerequisites: macOS 12.3+ with Apple Silicon, Python 3.10+, 8GB+ RAM recommended.
Quickstart
import torch
from transformers import AutoTokenizer
from mamba_ssm import MambaLMHeadModel, generate_text_with_model, get_device
device = get_device()
model_name = "state-spaces/mamba-130m" # or "state-spaces/mamba2-130m"
model = MambaLMHeadModel.from_pretrained(model_name, device=device)
tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neox-20b")
text = generate_text_with_model(model, tokenizer, "Once upon a time", device, max_length=50, temperature=0.8)
print(text)
The model is downloaded from Hugging Face Hub on first run and cached under ~/.cache/huggingface/.
Training
import torch
from torch import nn
from mamba_ssm.modules.mamba2 import Mamba2
model = nn.Sequential(nn.Embedding(1000, 128), *[Mamba2(d_model=128, d_state=64, d_conv=4, expand=2, headdim=64, ngroups=1, chunk_size=256, device='mps') for _ in range(2)], nn.LayerNorm(128), nn.Linear(128, 1000, bias=False)).to('mps')
optimizer = torch.optim.AdamW(model.parameters(), lr=1e-4)
criterion = nn.CrossEntropyLoss()
for input_ids, labels in dataloader:
optimizer.zero_grad()
logits = model(input_ids)
loss = criterion(logits.view(-1, logits.size(-1)), labels.view(-1))
loss.backward()
optimizer.step()
See examples/03_training.py for a complete training example.
Examples
Clone the repo to run examples:
git clone https://github.com/purohit10saurabh/mamba-ssm-macos.git
cd mamba-ssm-macos
uv sync
python -m examples.01_core_modules # Core modules usage
python -m examples.02_text_generation --interactive # Text generation
python -m examples.03_training # Training
Troubleshooting
"MPS not available" — Check with python -c "import torch; print(torch.backends.mps.is_available())". The library falls back to CPU automatically.
Slow first run — Initial from_pretrained downloads ~500MB of weights from Hugging Face Hub. Subsequent runs use the local cache.
Development
git clone https://github.com/purohit10saurabh/mamba-ssm-macos.git
cd mamba-ssm-macos
uv sync --extra dev
make test
Available make targets: test, test-unit, test-integration, test-quick, format, format-check, clean. See the Makefile for details.
Citation
Also available via GitHub's "Cite this repository" button (CITATION.cff).
@software{purohit2026mamba_ssm_macos,
title={Mamba SSM for macOS Apple Silicon},
author={Purohit, Saurabh},
year={2026},
url={https://github.com/purohit10saurabh/mamba-ssm-macos}
}
Original Mamba papers
@article{gu2023mamba,
title={Mamba: Linear-Time Sequence Modeling with Selective State Spaces},
author={Gu, Albert and Dao, Tri},
journal={arXiv preprint arXiv:2312.00752},
year={2023}
}
@article{dao2024transformers,
title={Transformers are SSMs: Generalized Models and Efficient Algorithms Through Structured State Space Duality},
author={Dao, Tri and Gu, Albert},
journal={arXiv preprint arXiv:2405.21060},
year={2024}
}
References
- state-spaces/mamba — Original implementation
- state-spaces/mamba1-130m — Mamba 1 130M pre-trained model
- state-spaces/mamba2-130m — Mamba 2 130M pre-trained model
Contributing
Feel free to open an issue or submit a PR. See Development for the local setup.
License
Apache 2.0 — see LICENSE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mamba_ssm_macos-1.0.2.tar.gz.
File metadata
- Download URL: mamba_ssm_macos-1.0.2.tar.gz
- Upload date:
- Size: 30.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a49f7676a651b3f3ba384f0a68319840abd8f52293194410028065e044798acc
|
|
| MD5 |
6a5e1338e4d8e6f310658621c377eb01
|
|
| BLAKE2b-256 |
be2e2cd4ab4c31e2f3b9f409e6f8d8b2db407acd6903c1f5f3a383ec62f1da4b
|
Provenance
The following attestation bundles were made for mamba_ssm_macos-1.0.2.tar.gz:
Publisher:
publish.yml on purohit10saurabh/mamba-ssm-macos
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mamba_ssm_macos-1.0.2.tar.gz -
Subject digest:
a49f7676a651b3f3ba384f0a68319840abd8f52293194410028065e044798acc - Sigstore transparency entry: 1396983520
- Sigstore integration time:
-
Permalink:
purohit10saurabh/mamba-ssm-macos@8c016e57eed1ba1cd0517d79e4f05ca006fa1b84 -
Branch / Tag:
refs/tags/v1.0.2 - Owner: https://github.com/purohit10saurabh
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8c016e57eed1ba1cd0517d79e4f05ca006fa1b84 -
Trigger Event:
push
-
Statement type:
File details
Details for the file mamba_ssm_macos-1.0.2-py3-none-any.whl.
File metadata
- Download URL: mamba_ssm_macos-1.0.2-py3-none-any.whl
- Upload date:
- Size: 33.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c8deec4f5f5d239e5fa93b6f821b2b2df2776668c01456f0bbe2bd3cd661167d
|
|
| MD5 |
aa6044b3c99721f36d4c00a7fbaff7c2
|
|
| BLAKE2b-256 |
9cfad9e51f826d12d4bc05fc0a4486354b5667b8fa4299b207636760926e641c
|
Provenance
The following attestation bundles were made for mamba_ssm_macos-1.0.2-py3-none-any.whl:
Publisher:
publish.yml on purohit10saurabh/mamba-ssm-macos
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mamba_ssm_macos-1.0.2-py3-none-any.whl -
Subject digest:
c8deec4f5f5d239e5fa93b6f821b2b2df2776668c01456f0bbe2bd3cd661167d - Sigstore transparency entry: 1396983524
- Sigstore integration time:
-
Permalink:
purohit10saurabh/mamba-ssm-macos@8c016e57eed1ba1cd0517d79e4f05ca006fa1b84 -
Branch / Tag:
refs/tags/v1.0.2 - Owner: https://github.com/purohit10saurabh
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8c016e57eed1ba1cd0517d79e4f05ca006fa1b84 -
Trigger Event:
push
-
Statement type: