Skip to main content

Mechanistic interpretability for State-Space Models: SAEs, feature visualization, and a Hub registry for Mamba/Mamba-2.

Project description

recurrentlens

Mechanistic interpretability for State-Space Models. Sparse autoencoders, feature visualization, and a Hub registry for Mamba / Mamba-2.

CI License: Apache-2.0 Python 3.11+ Status: Alpha

Colab notebooks: Quickstart · Explore an SAE · Train your own


Status

Alpha. The public API is intentionally small (see v0.1.0 API) and may evolve through v0.1.x. Pretrained SAE artifacts are not yet on the Hub — they ship in v0.1.1 via a community Colab T4 run. See CHANGELOG.md for the v0.1.0.post1 correctness hotfix (JumpReLU STE, HTML escape, CE metric cleanup).

⚠️ v0.1.0.post1 scope disclosure (read first)

recurrentlens ships a framework + smoke-tested API + 3 Colab notebooks. Pretrained SAE artifacts for Mamba-130M/1.3B will land in v0.1.1, trained via the included Colab notebook (T4 GPU is sufficient). The maintainer's build environment is CPU-only; the artifact training is deferred rather than fabricated.

If you have a GPU and want to seed the Hub registry early, run notebooks/03_train_mamba130m_sae.ipynb and recurrentlens.hub.push_sae(...) — pull requests welcome.

Why recurrentlens

SAELens is the canonical SAE training library and supports a wide range of architectures. As of 2026-05, the Mamba path has open issues since 2024-10, and there is room for an SSM-first design that treats recurrent state and selective scan as first-class concerns rather than retrofitting them onto a transformer-shaped abstraction. recurrentlens is that design.

Install

uv pip install recurrentlens                # core (CPU smoke + scaffold)
uv pip install "recurrentlens[mamba]"        # adds mamba-ssm CUDA kernels — Linux + CUDA only
uv pip install "recurrentlens[dev]"          # development

The [mamba] extra pulls mamba-ssm, which requires CUDA and a Linux toolchain. CPU users and macOS users should install just recurrentlens (the core works via HF Transformers' Mamba implementation).

Quick look

import recurrentlens as rl

model = rl.load_model("state-spaces/mamba-130m-hf")
sae = rl.train_sae(
    model,
    hook_site="out_proj_out",   # residual-stream analog (default)
    layer=6,
    variant="topk",
    d_sae=16384,
    n_tokens=200_000,            # CPU smoke; use 5M-100M+ on a T4 GPU
)

with rl.steer(model, sae, feature_id=42, vector_scale=2.0):
    out = model.generate("The capital of France is", max_new_tokens=20)

# Capture activations + contexts first; see notebooks/02 for a runnable example.
# rl.viz.feature_explorer(sae, feature_id=42, activations=acts, contexts=ctxs).save("feature42.html")

n_tokens note. 5M tokens on CPU takes hours; 200k completes in a couple of minutes for a smoke check. For research-grade SAEs the design target is 100M+ tokens, which is what the v0.1.1 pretrained artifacts will be trained on via Colab T4. See notebooks/03_train_mamba130m_sae.ipynb.

See notebooks/ for end-to-end examples.

v0.1.0 API

Function Status
load_model(model_id, device, dtype) scaffold (Phase 2)
train_sae(model, hook_site, layer, variant, ...) scaffold (Phases 4–5)
ablate(model, sae, feature_id, strength) scaffold (Phase 7)
steer(model, sae, feature_id, vector_scale) scaffold (Phase 7)
viz.feature_explorer(sae, feature_id, ...) scaffold (Phase 8)
hub.load_sae(repo_id) / hub.push_sae(sae, repo_id) scaffold (Phase 8)
bench.evaluate(sae, metrics=[...]) scaffold (Phase 9)

extract_circuit and diff.compare are scheduled for v0.2.

Supported models (v0.1.0)

  • state-spaces/mamba-130m-hf (smoke + Colab training)
  • state-spaces/mamba-130m (raw)
  • state-spaces/mamba2-1.3b (Colab training only — too large for CI smoke)

Falcon-Mamba-7B, RWKV-7, and Jamba are v0.2+ targets.

Honest assessment

  • Novelty: As of 2026-05, OSS coverage of SSM-specific SAE training is thin; SAELens has Mamba code paths but Issue #311 has been open since 2024-10. recurrentlens treats the SSM design (recurrent state, selective scan, out_proj residual) as first-class.
  • Practicality: load_model + train_sae + viz.feature_explorer + hub.{load,push}_sae gives users a runnable scaffold today; with v0.1.1 pretrained artifacts the path becomes a 5-minute end-to-end demo.
  • Scope: v0.1.0.post1 has no pretrained SAE artifacts. Real recon-MSE / L0 / CE-recovery numbers will be published with v0.1.1 SAEs (trained via Colab T4). The shipped train_sae default is n_tokens=200_000 for CPU smoke; the research-grade target is 100M+ tokens, which v0.1.1 will use.
  • Open question: do Mamba out_proj_out activations actually yield monosemantic SAE features? An empirical question the v0.1.1 release will start answering.

Pretrained SAEs

repo_id model layer hook variant d_sae status
hinanohart/recurrentlens-mamba130m-L2-sae mamba-130m 2 out_proj_out topk 16384 planned v0.1.1
hinanohart/recurrentlens-mamba130m-L6-sae mamba-130m 6 out_proj_out topk 16384 planned v0.1.1
hinanohart/recurrentlens-mamba130m-L10-sae mamba-130m 10 out_proj_out topk 16384 planned v0.1.1

Want to seed this table? Run notebook 03 on Colab T4 and open a PR. See CONTRIBUTING.md.

Citation

See CITATION.cff.

License

Apache-2.0.

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

recurrentlens-0.1.1.tar.gz (42.5 kB view details)

Uploaded Source

Built Distribution

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

recurrentlens-0.1.1-py3-none-any.whl (33.9 kB view details)

Uploaded Python 3

File details

Details for the file recurrentlens-0.1.1.tar.gz.

File metadata

  • Download URL: recurrentlens-0.1.1.tar.gz
  • Upload date:
  • Size: 42.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for recurrentlens-0.1.1.tar.gz
Algorithm Hash digest
SHA256 35a41ea869f7e55235da2523743362762e0757cd1978e32a9eb39839e373e6ef
MD5 d2eba9ff3140c7351b5ac8e89c67fd82
BLAKE2b-256 735b1d47826d4b8925bf0413826d68828f48626ea54453f18c4894d839e54273

See more details on using hashes here.

Provenance

The following attestation bundles were made for recurrentlens-0.1.1.tar.gz:

Publisher: release.yml on hinanohart/recurrentlens

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

File details

Details for the file recurrentlens-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: recurrentlens-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 33.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for recurrentlens-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6a5a8f200cd09bf15a237cbc13a7a6bad49df88b12a73adc90866207abc0d015
MD5 56ae78df849b69214521e35e0646dbe5
BLAKE2b-256 5f0ddc3e5e8e768fd8d052852691664748426365e4a46401d6a0e15c8143b810

See more details on using hashes here.

Provenance

The following attestation bundles were made for recurrentlens-0.1.1-py3-none-any.whl:

Publisher: release.yml on hinanohart/recurrentlens

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