Skip to main content

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

Apple Silicon Python PyTorch PyPI License

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

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mamba_ssm_macos-1.0.2.tar.gz (30.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mamba_ssm_macos-1.0.2-py3-none-any.whl (33.4 kB view details)

Uploaded Python 3

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

Hashes for mamba_ssm_macos-1.0.2.tar.gz
Algorithm Hash digest
SHA256 a49f7676a651b3f3ba384f0a68319840abd8f52293194410028065e044798acc
MD5 6a5e1338e4d8e6f310658621c377eb01
BLAKE2b-256 be2e2cd4ab4c31e2f3b9f409e6f8d8b2db407acd6903c1f5f3a383ec62f1da4b

See more details on using hashes here.

Provenance

The following attestation bundles were made for mamba_ssm_macos-1.0.2.tar.gz:

Publisher: publish.yml on purohit10saurabh/mamba-ssm-macos

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for mamba_ssm_macos-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c8deec4f5f5d239e5fa93b6f821b2b2df2776668c01456f0bbe2bd3cd661167d
MD5 aa6044b3c99721f36d4c00a7fbaff7c2
BLAKE2b-256 9cfad9e51f826d12d4bc05fc0a4486354b5667b8fa4299b207636760926e641c

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page