recurrentlens 0.1.0.post1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- recurrentlens-0.1.0.post1/.github/workflows/ci.yml +51 -0
- recurrentlens-0.1.0.post1/.gitignore +66 -0
- recurrentlens-0.1.0.post1/.pre-commit-config.yaml +14 -0
- recurrentlens-0.1.0.post1/CHANGELOG.md +88 -0
- recurrentlens-0.1.0.post1/CITATION.cff +29 -0
- recurrentlens-0.1.0.post1/CONTRIBUTING.md +47 -0
- recurrentlens-0.1.0.post1/LICENSE +216 -0
- recurrentlens-0.1.0.post1/PKG-INFO +148 -0
- recurrentlens-0.1.0.post1/README.md +109 -0
- recurrentlens-0.1.0.post1/SECURITY.md +63 -0
- recurrentlens-0.1.0.post1/notebooks/01_quickstart.ipynb +83 -0
- recurrentlens-0.1.0.post1/notebooks/02_explore_pretrained.ipynb +144 -0
- recurrentlens-0.1.0.post1/notebooks/03_train_mamba130m_sae.ipynb +128 -0
- recurrentlens-0.1.0.post1/pyproject.toml +76 -0
- recurrentlens-0.1.0.post1/scripts/check_no_secrets.sh +37 -0
- recurrentlens-0.1.0.post1/scripts/train_mamba130m_smoke.py +63 -0
- recurrentlens-0.1.0.post1/src/recurrentlens/__init__.py +81 -0
- recurrentlens-0.1.0.post1/src/recurrentlens/_version.py +1 -0
- recurrentlens-0.1.0.post1/src/recurrentlens/bench/__init__.py +5 -0
- recurrentlens-0.1.0.post1/src/recurrentlens/bench/evaluate.py +131 -0
- recurrentlens-0.1.0.post1/src/recurrentlens/core/__init__.py +1 -0
- recurrentlens-0.1.0.post1/src/recurrentlens/core/model.py +158 -0
- recurrentlens-0.1.0.post1/src/recurrentlens/core/protocols.py +50 -0
- recurrentlens-0.1.0.post1/src/recurrentlens/core/types.py +20 -0
- recurrentlens-0.1.0.post1/src/recurrentlens/features/__init__.py +6 -0
- recurrentlens-0.1.0.post1/src/recurrentlens/features/ablate.py +52 -0
- recurrentlens-0.1.0.post1/src/recurrentlens/features/steer.py +42 -0
- recurrentlens-0.1.0.post1/src/recurrentlens/hooks/__init__.py +10 -0
- recurrentlens-0.1.0.post1/src/recurrentlens/hooks/registry.py +162 -0
- recurrentlens-0.1.0.post1/src/recurrentlens/hub/__init__.py +5 -0
- recurrentlens-0.1.0.post1/src/recurrentlens/hub/io.py +64 -0
- recurrentlens-0.1.0.post1/src/recurrentlens/sae/__init__.py +14 -0
- recurrentlens-0.1.0.post1/src/recurrentlens/sae/base.py +164 -0
- recurrentlens-0.1.0.post1/src/recurrentlens/sae/cache.py +118 -0
- recurrentlens-0.1.0.post1/src/recurrentlens/sae/datastream.py +102 -0
- recurrentlens-0.1.0.post1/src/recurrentlens/sae/train.py +167 -0
- recurrentlens-0.1.0.post1/src/recurrentlens/sae/variants.py +126 -0
- recurrentlens-0.1.0.post1/src/recurrentlens/viz/__init__.py +5 -0
- recurrentlens-0.1.0.post1/src/recurrentlens/viz/feature_explorer.py +154 -0
- recurrentlens-0.1.0.post1/tests/__init__.py +0 -0
- recurrentlens-0.1.0.post1/tests/test_features.py +93 -0
- recurrentlens-0.1.0.post1/tests/test_hooks_unit.py +118 -0
- recurrentlens-0.1.0.post1/tests/test_model_unit.py +80 -0
- recurrentlens-0.1.0.post1/tests/test_sae_unit.py +219 -0
- recurrentlens-0.1.0.post1/tests/test_smoke.py +43 -0
- recurrentlens-0.1.0.post1/tests/test_train_smoke.py +77 -0
- recurrentlens-0.1.0.post1/tests/test_viz_bench.py +109 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [main]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
lint:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
steps:
|
|
13
|
+
- uses: actions/checkout@v4
|
|
14
|
+
- uses: actions/setup-python@v5
|
|
15
|
+
with:
|
|
16
|
+
python-version: '3.12'
|
|
17
|
+
- name: Install uv
|
|
18
|
+
run: pip install uv
|
|
19
|
+
- name: Install ruff
|
|
20
|
+
run: uv pip install --system ruff
|
|
21
|
+
- name: Ruff format check
|
|
22
|
+
run: ruff format --check src tests
|
|
23
|
+
- name: Ruff lint
|
|
24
|
+
run: ruff check src tests
|
|
25
|
+
|
|
26
|
+
no-secrets:
|
|
27
|
+
runs-on: ubuntu-latest
|
|
28
|
+
steps:
|
|
29
|
+
- uses: actions/checkout@v4
|
|
30
|
+
- name: Grep for token-shaped strings
|
|
31
|
+
run: bash scripts/check_no_secrets.sh
|
|
32
|
+
|
|
33
|
+
unit-cpu:
|
|
34
|
+
runs-on: ubuntu-latest
|
|
35
|
+
strategy:
|
|
36
|
+
matrix:
|
|
37
|
+
python: ['3.11', '3.12']
|
|
38
|
+
steps:
|
|
39
|
+
- uses: actions/checkout@v4
|
|
40
|
+
- uses: actions/setup-python@v5
|
|
41
|
+
with:
|
|
42
|
+
python-version: ${{ matrix.python }}
|
|
43
|
+
- name: Install uv
|
|
44
|
+
run: pip install uv
|
|
45
|
+
- name: Install package + dev deps (CPU)
|
|
46
|
+
run: |
|
|
47
|
+
uv pip install --system -e ".[dev]"
|
|
48
|
+
- name: Pytest (smoke + unit, CPU)
|
|
49
|
+
env:
|
|
50
|
+
RECURRENTLENS_SKIP_GPU_TESTS: '1'
|
|
51
|
+
run: pytest -ra
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
*.so
|
|
6
|
+
.Python
|
|
7
|
+
build/
|
|
8
|
+
develop-eggs/
|
|
9
|
+
dist/
|
|
10
|
+
downloads/
|
|
11
|
+
eggs/
|
|
12
|
+
.eggs/
|
|
13
|
+
lib/
|
|
14
|
+
lib64/
|
|
15
|
+
parts/
|
|
16
|
+
sdist/
|
|
17
|
+
var/
|
|
18
|
+
wheels/
|
|
19
|
+
*.egg-info/
|
|
20
|
+
.installed.cfg
|
|
21
|
+
*.egg
|
|
22
|
+
MANIFEST
|
|
23
|
+
|
|
24
|
+
# venv
|
|
25
|
+
.venv/
|
|
26
|
+
venv/
|
|
27
|
+
ENV/
|
|
28
|
+
env/
|
|
29
|
+
.env
|
|
30
|
+
|
|
31
|
+
# Testing / coverage
|
|
32
|
+
.coverage
|
|
33
|
+
.coverage.*
|
|
34
|
+
.pytest_cache/
|
|
35
|
+
htmlcov/
|
|
36
|
+
.tox/
|
|
37
|
+
.nox/
|
|
38
|
+
*.cover
|
|
39
|
+
coverage.xml
|
|
40
|
+
|
|
41
|
+
# IDE
|
|
42
|
+
.vscode/
|
|
43
|
+
.idea/
|
|
44
|
+
*.swp
|
|
45
|
+
*.swo
|
|
46
|
+
.DS_Store
|
|
47
|
+
|
|
48
|
+
# Type checkers / linters
|
|
49
|
+
.mypy_cache/
|
|
50
|
+
.pyright/
|
|
51
|
+
.ruff_cache/
|
|
52
|
+
|
|
53
|
+
# Notebooks
|
|
54
|
+
.ipynb_checkpoints/
|
|
55
|
+
|
|
56
|
+
# uv
|
|
57
|
+
uv.lock
|
|
58
|
+
|
|
59
|
+
# Project-specific
|
|
60
|
+
artifacts/
|
|
61
|
+
activations_cache/
|
|
62
|
+
experiments/_wip/
|
|
63
|
+
*.zarr/
|
|
64
|
+
*.safetensors
|
|
65
|
+
*.pt
|
|
66
|
+
*.html
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
repos:
|
|
2
|
+
- repo: https://github.com/astral-sh/ruff-pre-commit
|
|
3
|
+
rev: v0.6.9
|
|
4
|
+
hooks:
|
|
5
|
+
- id: ruff
|
|
6
|
+
args: [--fix]
|
|
7
|
+
- id: ruff-format
|
|
8
|
+
- repo: local
|
|
9
|
+
hooks:
|
|
10
|
+
- id: no-secrets
|
|
11
|
+
name: Grep for token-shaped strings
|
|
12
|
+
entry: bash scripts/check_no_secrets.sh
|
|
13
|
+
language: system
|
|
14
|
+
pass_filenames: false
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes follow [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
|
|
4
|
+
and the project version follows [PEP 440](https://peps.python.org/pep-0440/)
|
|
5
|
+
(SemVer-compatible, with `post` releases reserved for correctness-only patches).
|
|
6
|
+
|
|
7
|
+
## [0.1.0.post1] — 2026-05-23
|
|
8
|
+
|
|
9
|
+
Correctness hotfix on top of v0.1.0. No new features, no API additions, no
|
|
10
|
+
breaking changes to the eight public functions. The release fixes silent
|
|
11
|
+
correctness bugs uncovered in a post-release multi-agent audit.
|
|
12
|
+
|
|
13
|
+
### Fixed
|
|
14
|
+
- **JumpReLU SAE — straight-through estimator** (`sae/variants.py`).
|
|
15
|
+
The forward gate is now `z = z_pre * gate`, the canonical STE form. The
|
|
16
|
+
v0.1.0 implementation multiplied the surrogate gradient by `0.0`, which
|
|
17
|
+
silently severed the backward path. `JumpReLUSAE` is now trainable.
|
|
18
|
+
- **`bench.evaluate` — removed `ce_recovery_proxy`** (`bench/evaluate.py`).
|
|
19
|
+
The v0.1.0 ratio `ce_clean / ce_sae` was not the standard CE-recovery
|
|
20
|
+
formula and produced numbers that could not be compared against SAELens,
|
|
21
|
+
Anthropic, or DeepMind reports. The metric has been replaced by
|
|
22
|
+
`ce_delta = ce_sae − ce_clean` (lower is better, 0 is perfect). The full
|
|
23
|
+
zero-ablation baseline ships in v0.1.1.
|
|
24
|
+
- **`viz.feature_explorer` — HTML escape**.
|
|
25
|
+
All user-controlled fields (`context`, `model_id`, `hook_site`, `variant`)
|
|
26
|
+
are now passed through `html.escape`. This closes an XSS vector for
|
|
27
|
+
feature pages rendered from Hub-loaded SAEs.
|
|
28
|
+
- **`BaseSAE.load` — metadata validation** (`sae/base.py`).
|
|
29
|
+
Refuse to construct an SAE when the safetensors metadata is inconsistent
|
|
30
|
+
with the stored tensors (e.g., a lying `d_sae` field) or names an unknown
|
|
31
|
+
variant. Closes a supply-chain footgun for `hub.load_sae`.
|
|
32
|
+
- **L0 metric — signed nonzero count** (`sae/base.py`, `bench/evaluate.py`).
|
|
33
|
+
`(z != 0)` instead of `(z > 0)`. Matches the standard SAE convention so
|
|
34
|
+
negative activations (JumpReLU / Vanilla) are counted correctly.
|
|
35
|
+
- **`ssm_h_t` hook — warn once per module** (`hooks/registry.py`).
|
|
36
|
+
The proxy warning previously fired on every `_resolve_target` call (i.e.
|
|
37
|
+
every forward pass for ablate/steer/eval). It now fires once per layer
|
|
38
|
+
module.
|
|
39
|
+
- **`train._cosine_lr` off-by-one** (`sae/train.py`).
|
|
40
|
+
The final training step no longer collapses to `LR=0`; the schedule floors
|
|
41
|
+
at 1% of `base_lr` at progress=1.
|
|
42
|
+
- **`features.ablate` — `torch.no_grad()`** (`features/ablate.py`).
|
|
43
|
+
SAE encode/decode inside the intervention hook no longer builds an
|
|
44
|
+
autograd graph.
|
|
45
|
+
- **`ActivationCache(mmap)` — overwrite warning** (`sae/cache.py`).
|
|
46
|
+
Truncating an existing `.memmap` file now emits a `UserWarning` instead
|
|
47
|
+
of silently destroying data.
|
|
48
|
+
- **`hub.push_sae` — `tempfile.TemporaryDirectory`** (`hub/io.py`).
|
|
49
|
+
No more stale `/tmp/<repo_id>/...` directories left behind after upload.
|
|
50
|
+
|
|
51
|
+
### Changed
|
|
52
|
+
- **`hooks.resolve_target`** is now a public symbol (`recurrentlens.hooks`).
|
|
53
|
+
The previous `_resolve_target` private name is kept as a back-compat alias
|
|
54
|
+
until v0.2.
|
|
55
|
+
- **README `Quick look`**: `n_tokens=200_000` (CPU smoke). The 100M-token
|
|
56
|
+
research-grade target is documented as the v0.1.1 pretrained-artifact path.
|
|
57
|
+
- **README SAELens characterization**: factual link to
|
|
58
|
+
[decoderesearch/SAELens#311](https://github.com/decoderesearch/SAELens/issues/311)
|
|
59
|
+
with the open-since date; removed editorial "broken" wording.
|
|
60
|
+
- **README** now carries an `Alpha` status badge near the top.
|
|
61
|
+
- **`notebooks/02_explore_pretrained.ipynb`**: `REPO_ID = None` by default,
|
|
62
|
+
falls back to an untrained `TopKSAE` for the API demo. Pretrained
|
|
63
|
+
Hub artifacts ship in v0.1.1.
|
|
64
|
+
|
|
65
|
+
### Added
|
|
66
|
+
- `tests/test_sae_unit.py::test_jumprelu_gradient_flows_to_encoder`
|
|
67
|
+
- `tests/test_sae_unit.py::test_l0_counts_signed_nonzero`
|
|
68
|
+
- `tests/test_sae_unit.py::test_base_sae_load_rejects_inconsistent_meta`
|
|
69
|
+
- `tests/test_sae_unit.py::test_base_sae_load_rejects_unknown_variant`
|
|
70
|
+
- `tests/test_viz_bench.py::test_feature_explorer_escapes_xss`
|
|
71
|
+
- `tests/test_viz_bench.py::test_feature_explorer_no_acts_emits_warning_banner`
|
|
72
|
+
- `tests/test_viz_bench.py::test_evaluate_does_not_emit_ce_recovery_proxy`
|
|
73
|
+
- `tests/test_hooks_unit.py::test_ssm_h_t_warning_is_emitted_once_per_module`
|
|
74
|
+
- `tests/test_train_smoke.py::test_cosine_lr_final_step_above_zero`
|
|
75
|
+
- `tests/test_features.py::test_ablate_zero_removes_feature_contribution`
|
|
76
|
+
upgraded from a shape-only tautology to an analytic-delta assertion.
|
|
77
|
+
|
|
78
|
+
### Deferred to v0.1.1
|
|
79
|
+
- Pretrained Mamba-130M SAEs at layers {2, 6, 10} via Colab T4.
|
|
80
|
+
- TopK auxiliary-K dead-feature loss (Gao et al. 2024 §3.2).
|
|
81
|
+
- True CE recovery formula with zero-ablation baseline.
|
|
82
|
+
- Tangent-projected decoder gradient + checkpoint save in `train_sae_full`.
|
|
83
|
+
- nnsight backend for exact `h_t` capture.
|
|
84
|
+
|
|
85
|
+
## [0.1.0] — 2026-05-23
|
|
86
|
+
|
|
87
|
+
Initial public release. SSM-first SAE + feature-viz + Hub registry framework
|
|
88
|
+
for Mamba and Mamba-2. Apache-2.0, 38 unit tests, 3 Colab notebooks.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
cff-version: 1.2.0
|
|
2
|
+
message: "If you use recurrentlens in academic work, please cite it as below."
|
|
3
|
+
title: "recurrentlens: mechanistic interpretability for State-Space Models"
|
|
4
|
+
authors:
|
|
5
|
+
- family-names: "hinanohart"
|
|
6
|
+
version: 0.1.0.post1
|
|
7
|
+
date-released: 2026-05-23
|
|
8
|
+
license: Apache-2.0
|
|
9
|
+
repository-code: "https://github.com/hinanohart/recurrentlens"
|
|
10
|
+
keywords:
|
|
11
|
+
- mechanistic-interpretability
|
|
12
|
+
- sparse-autoencoder
|
|
13
|
+
- state-space-models
|
|
14
|
+
- mamba
|
|
15
|
+
notes: |
|
|
16
|
+
v0.1.0.post1 ships a framework + scaffold + smoke + 3 Colab notebooks.
|
|
17
|
+
Pretrained Mamba-130M SAE artifacts land in v0.1.1 (community Colab T4).
|
|
18
|
+
|
|
19
|
+
The shipped train_sae default is n_tokens=200_000, sized for a CPU smoke
|
|
20
|
+
run. The research-grade design target is 100M+ tokens; v0.1.1 pretrained
|
|
21
|
+
artifacts will be trained at that scale.
|
|
22
|
+
|
|
23
|
+
v0.1.0.post1 is a correctness hotfix on top of v0.1.0:
|
|
24
|
+
- JumpReLU straight-through estimator (was a `* 0.0` no-op in v0.1.0).
|
|
25
|
+
- bench.evaluate: removed the ce_recovery_proxy ratio (added ce_delta).
|
|
26
|
+
- viz.feature_explorer: HTML-escape user-controlled fields (XSS guard).
|
|
27
|
+
- hub.load_sae: validate safetensors metadata before construction.
|
|
28
|
+
- L0 metric: count (z != 0), matching the standard SAE convention.
|
|
29
|
+
See CHANGELOG.md for the full list.
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# Contributing
|
|
2
|
+
|
|
3
|
+
Thanks for your interest in `recurrentlens`. The library is in early alpha (v0.1.0); contributions are very welcome.
|
|
4
|
+
|
|
5
|
+
## Setup
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
git clone https://github.com/hinanohart/recurrentlens.git
|
|
9
|
+
cd recurrentlens
|
|
10
|
+
uv venv
|
|
11
|
+
source .venv/bin/activate
|
|
12
|
+
uv pip install -e ".[dev]"
|
|
13
|
+
pre-commit install
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Running tests
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
pytest -ra # full suite
|
|
20
|
+
pytest -ra -k smoke # smoke only
|
|
21
|
+
RECURRENTLENS_SKIP_GPU_TESTS=1 pytest -ra # CPU-only (CI default)
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Style
|
|
25
|
+
|
|
26
|
+
- `ruff format`
|
|
27
|
+
- `ruff check --fix`
|
|
28
|
+
- `pyright` (standard mode)
|
|
29
|
+
|
|
30
|
+
Pre-commit hooks enforce all three plus a `no-secrets` grep gate.
|
|
31
|
+
|
|
32
|
+
## High-leverage contributions for v0.1.0 → v0.1.1
|
|
33
|
+
|
|
34
|
+
1. **Pretrained SAE artifacts**: run `notebooks/03_train_mamba130m_sae.ipynb` on Colab T4, then `recurrentlens.hub.push_sae(...)` to `hinanohart/recurrentlens-mamba130m-L{2,6,10}-sae`. PR adding the Hub IDs to README.
|
|
35
|
+
2. **Hook sites for other SSMs**: RWKV-7, Jamba block-local hooks are v0.2 but design notes in `docs/hooks_design.md` welcome.
|
|
36
|
+
3. **Feature explorer richness**: v0.1.0 ships a minimal HTML table; richer interactivity (highlighting, k-way comparison) for v0.1.x is open.
|
|
37
|
+
|
|
38
|
+
## v0.2 roadmap
|
|
39
|
+
|
|
40
|
+
- `extract_circuit` (recurrent-state-aware patching)
|
|
41
|
+
- `diff.compare(sae_mamba, sae_gpt2)`
|
|
42
|
+
- Falcon-Mamba-7B Hub registry
|
|
43
|
+
- RWKV-7 / Jamba support
|
|
44
|
+
|
|
45
|
+
## Code of conduct
|
|
46
|
+
|
|
47
|
+
Be kind. Disagree about technical claims with citations.
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
203
|
+
|
|
204
|
+
Copyright 2026 hinanohart
|
|
205
|
+
|
|
206
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
207
|
+
you may not use this file except in compliance with the License.
|
|
208
|
+
You may obtain a copy of the License at
|
|
209
|
+
|
|
210
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
211
|
+
|
|
212
|
+
Unless required by applicable law or agreed to in writing, software
|
|
213
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
214
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
215
|
+
See the License for the specific language governing permissions and
|
|
216
|
+
limitations under the License.
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: recurrentlens
|
|
3
|
+
Version: 0.1.0.post1
|
|
4
|
+
Summary: Mechanistic interpretability for State-Space Models: SAEs, feature visualization, and a Hub registry for Mamba/Mamba-2.
|
|
5
|
+
Project-URL: Homepage, https://github.com/hinanohart/recurrentlens
|
|
6
|
+
Project-URL: Issues, https://github.com/hinanohart/recurrentlens/issues
|
|
7
|
+
Author: hinanohart
|
|
8
|
+
License-Expression: Apache-2.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: interpretability,mamba,mechanistic-interpretability,sparse-autoencoder,ssm,state-space-model
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Science/Research
|
|
13
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
17
|
+
Requires-Python: >=3.11
|
|
18
|
+
Requires-Dist: datasets>=2.20
|
|
19
|
+
Requires-Dist: einops>=0.7
|
|
20
|
+
Requires-Dist: huggingface-hub>=0.26
|
|
21
|
+
Requires-Dist: jinja2>=3.1
|
|
22
|
+
Requires-Dist: numpy>=1.26
|
|
23
|
+
Requires-Dist: pydantic>=2.7
|
|
24
|
+
Requires-Dist: pyyaml>=6.0
|
|
25
|
+
Requires-Dist: safetensors>=0.4
|
|
26
|
+
Requires-Dist: torch>=2.4
|
|
27
|
+
Requires-Dist: tqdm>=4.66
|
|
28
|
+
Requires-Dist: transformers>=4.45
|
|
29
|
+
Requires-Dist: zarr<3.0,>=2.18
|
|
30
|
+
Provides-Extra: dev
|
|
31
|
+
Requires-Dist: ipykernel>=6.29; extra == 'dev'
|
|
32
|
+
Requires-Dist: pyright>=1.1; extra == 'dev'
|
|
33
|
+
Requires-Dist: pytest-cov>=5.0; extra == 'dev'
|
|
34
|
+
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
35
|
+
Requires-Dist: ruff>=0.6; extra == 'dev'
|
|
36
|
+
Provides-Extra: mamba
|
|
37
|
+
Requires-Dist: mamba-ssm>=2.2.2; extra == 'mamba'
|
|
38
|
+
Description-Content-Type: text/markdown
|
|
39
|
+
|
|
40
|
+
# recurrentlens
|
|
41
|
+
|
|
42
|
+
**Mechanistic interpretability for State-Space Models.** Sparse autoencoders, feature visualization, and a Hub registry for Mamba / Mamba-2.
|
|
43
|
+
|
|
44
|
+
[](https://github.com/hinanohart/recurrentlens/actions/workflows/ci.yml)
|
|
45
|
+
[](https://opensource.org/licenses/Apache-2.0)
|
|
46
|
+
[](https://www.python.org/downloads/)
|
|
47
|
+
[](#status)
|
|
48
|
+
|
|
49
|
+
**Colab notebooks:** [Quickstart](https://colab.research.google.com/github/hinanohart/recurrentlens/blob/main/notebooks/01_quickstart.ipynb) · [Explore an SAE](https://colab.research.google.com/github/hinanohart/recurrentlens/blob/main/notebooks/02_explore_pretrained.ipynb) · [Train your own](https://colab.research.google.com/github/hinanohart/recurrentlens/blob/main/notebooks/03_train_mamba130m_sae.ipynb)
|
|
50
|
+
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
## Status
|
|
54
|
+
|
|
55
|
+
**Alpha.** The public API is intentionally small (see [v0.1.0 API](#v010-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](./CHANGELOG.md) for the v0.1.0.post1 correctness hotfix (JumpReLU STE, HTML escape, CE metric cleanup).
|
|
56
|
+
|
|
57
|
+
## ⚠️ v0.1.0.post1 scope disclosure (read first)
|
|
58
|
+
|
|
59
|
+
`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.
|
|
60
|
+
|
|
61
|
+
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.
|
|
62
|
+
|
|
63
|
+
## Why recurrentlens
|
|
64
|
+
|
|
65
|
+
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](https://github.com/decoderesearch/SAELens/issues/311), 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.
|
|
66
|
+
|
|
67
|
+
## Install
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
uv pip install recurrentlens # core (CPU smoke + scaffold)
|
|
71
|
+
uv pip install "recurrentlens[mamba]" # adds mamba-ssm CUDA kernels — Linux + CUDA only
|
|
72
|
+
uv pip install "recurrentlens[dev]" # development
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
> 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).
|
|
76
|
+
|
|
77
|
+
## Quick look
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
import recurrentlens as rl
|
|
81
|
+
|
|
82
|
+
model = rl.load_model("state-spaces/mamba-130m-hf")
|
|
83
|
+
sae = rl.train_sae(
|
|
84
|
+
model,
|
|
85
|
+
hook_site="out_proj_out", # residual-stream analog (default)
|
|
86
|
+
layer=6,
|
|
87
|
+
variant="topk",
|
|
88
|
+
d_sae=16384,
|
|
89
|
+
n_tokens=200_000, # CPU smoke; use 5M-100M+ on a T4 GPU
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
with rl.steer(model, sae, feature_id=42, vector_scale=2.0):
|
|
93
|
+
out = model.generate("The capital of France is", max_new_tokens=20)
|
|
94
|
+
|
|
95
|
+
# Capture activations + contexts first; see notebooks/02 for a runnable example.
|
|
96
|
+
# rl.viz.feature_explorer(sae, feature_id=42, activations=acts, contexts=ctxs).save("feature42.html")
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
> **`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`.
|
|
100
|
+
|
|
101
|
+
See `notebooks/` for end-to-end examples.
|
|
102
|
+
|
|
103
|
+
## v0.1.0 API
|
|
104
|
+
|
|
105
|
+
| Function | Status |
|
|
106
|
+
|---|---|
|
|
107
|
+
| `load_model(model_id, device, dtype)` | scaffold (Phase 2) |
|
|
108
|
+
| `train_sae(model, hook_site, layer, variant, ...)` | scaffold (Phases 4–5) |
|
|
109
|
+
| `ablate(model, sae, feature_id, strength)` | scaffold (Phase 7) |
|
|
110
|
+
| `steer(model, sae, feature_id, vector_scale)` | scaffold (Phase 7) |
|
|
111
|
+
| `viz.feature_explorer(sae, feature_id, ...)` | scaffold (Phase 8) |
|
|
112
|
+
| `hub.load_sae(repo_id)` / `hub.push_sae(sae, repo_id)` | scaffold (Phase 8) |
|
|
113
|
+
| `bench.evaluate(sae, metrics=[...])` | scaffold (Phase 9) |
|
|
114
|
+
|
|
115
|
+
`extract_circuit` and `diff.compare` are scheduled for **v0.2**.
|
|
116
|
+
|
|
117
|
+
## Supported models (v0.1.0)
|
|
118
|
+
|
|
119
|
+
- `state-spaces/mamba-130m-hf` (smoke + Colab training)
|
|
120
|
+
- `state-spaces/mamba-130m` (raw)
|
|
121
|
+
- `state-spaces/mamba2-1.3b` (Colab training only — too large for CI smoke)
|
|
122
|
+
|
|
123
|
+
Falcon-Mamba-7B, RWKV-7, and Jamba are **v0.2+** targets.
|
|
124
|
+
|
|
125
|
+
## Honest assessment
|
|
126
|
+
|
|
127
|
+
- **Novelty**: As of 2026-05, OSS coverage of SSM-specific SAE training is thin; SAELens has Mamba code paths but [Issue #311](https://github.com/decoderesearch/SAELens/issues/311) has been open since 2024-10. `recurrentlens` treats the SSM design (recurrent state, selective scan, `out_proj` residual) as first-class.
|
|
128
|
+
- **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.
|
|
129
|
+
- **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.
|
|
130
|
+
- **Open question**: do Mamba `out_proj_out` activations actually yield monosemantic SAE features? An empirical question the v0.1.1 release will start answering.
|
|
131
|
+
|
|
132
|
+
## Pretrained SAEs
|
|
133
|
+
|
|
134
|
+
| repo_id | model | layer | hook | variant | d_sae | status |
|
|
135
|
+
|---|---|---|---|---|---|---|
|
|
136
|
+
| `hinanohart/recurrentlens-mamba130m-L2-sae` | mamba-130m | 2 | out_proj_out | topk | 16384 | **planned v0.1.1** |
|
|
137
|
+
| `hinanohart/recurrentlens-mamba130m-L6-sae` | mamba-130m | 6 | out_proj_out | topk | 16384 | **planned v0.1.1** |
|
|
138
|
+
| `hinanohart/recurrentlens-mamba130m-L10-sae` | mamba-130m | 10 | out_proj_out | topk | 16384 | **planned v0.1.1** |
|
|
139
|
+
|
|
140
|
+
Want to seed this table? Run notebook 03 on Colab T4 and open a PR. See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
141
|
+
|
|
142
|
+
## Citation
|
|
143
|
+
|
|
144
|
+
See `CITATION.cff`.
|
|
145
|
+
|
|
146
|
+
## License
|
|
147
|
+
|
|
148
|
+
Apache-2.0.
|