Overview
DeepLens is a library for mechanistic interpretability. It includes a full set of tools that allow end-to-end interpretability pipelines: from feature extraction, to feature steering. The library includes Sparse Autoencoders (TopK and L1), feature extractors, feature dataset modules, and intervention modules.
Quick How To
Installation
Before installing any dependency, I recommend creating a new virtual envoronment to avoid library conflicts.
conda create -n deeplens python=3.11
conda activate deeplens
The following command should install the necessary dependencies and tools:
For Windows (CUDA support):
pip install -e .
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu128
For Mac (no CUDA support):
pip install -e .
pip3 install torch torchvision
If any errors arise, you may alternatively use the manual installation:
pip install -r requirements.txt
pip install -e .
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu128
# OR FOR MAC
pip3 install torch torchvision
1. MLP Feature Extraction
from deeplens.extractor import FromHuggingFace
extractor = FromHuggingFace(
model="gpt2",
layer=3,
dataset_name="HuggingFaceFW/fineweb", # uses dataset streaming!
num_samples=500,
seq_length=1024,
inference_batch_size=16,
device="auto",
save_features=True
)
features = extractor.extract_features()
2. Training
from deeplens.sae import SparseAutoencoder
from deeplens.train import SAETrainer
from deeplens.utils.dataset import ActivationsDatasetBuilder
import torch
dataset = ActivationsDatasetBuilder(
activations="YOUR_SAVED_FEATURES_PT_FILE",
splits=[0.8, 0.2],
batch_size=16,
norm=True
)
train, eval = dataset.get_dataloaders()
config = SAETrainer.config_from_yaml('demo/config.yaml')
model = SparseAutoencoder(**config)
optimizer = torch.optim.Adam(
model.parameters(),
lr=0.0003,
betas=(0.9,0.99),
weight_decay=1e-5 # Just when using untied weights! Else set to 0
)
trainer = SAETrainer(
model=model,
model_name="YOUR_GIVEN_MODEL_NAME",
train_dataloader=train,
eval_dataloader=eval,
optim=optimizer,
epochs=3,
bf16=True,
random_seed=42,
save_checkpoints=True,
device="auto",
grad_clip_norm=3.0,
lrs_type='cosine',
eval_steps=1000,
warmup_fraction=0.1,
save_best_only=True,
log_to_wandb=True
)
trainer.train()
3. SAE Feature Extraction
text = "What color is the car next to Mary's house?"
sample = ExtractSingleSample(
model="SAVED_MODEL_DIR",
sample=text,
layer=3,
max_length=512,
device="auto"
)
acts = sample.get_mlp_acts()
4. Feature Intervention
text = "What color is the car next to Mary's house?"
sample = ExtractSingleSample(
model="SAVED_MODEL_DIR",
sample=text,
layer=3,
max_length=512,
device="auto"
)
acts = sample.get_mlp_acts()
Metadata
Release files for deeplens-sae 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| deeplens_sae-0.1.1.tar.gz | 30.2 kB | Without provenance | Details for deeplens_sae-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| deeplens_sae-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for deeplens_sae-0.1.1-py3-none-any.whl |
Total release size: 63.2 kB
Release files / deeplens_sae-0.1.1.tar.gz
| Download URL | deeplens_sae-0.1.1.tar.gz |
|---|---|
| Size | 30.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bf836dc2482e76d0d120c0a30fd1223a3d147a300b3748ea1261e9b166762c39
|
|
BLAKE2b-256 checksum How to use checksums |
71aa108d5111aa03a8e043aeefa0ae98e83ac7cb7ed437d89d322eeb91a25dc4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.7
|
Release files / deeplens_sae-0.1.1-py3-none-any.whl
| Download URL | deeplens_sae-0.1.1-py3-none-any.whl |
|---|---|
| Size | 33.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
49868f016dba9cd85f0e4e8ed78201698fc20f85a16bb51fc57fd16c30674638
|
|
BLAKE2b-256 checksum How to use checksums |
1c3595e610e556b01afe706608645c26323413a4cceea4a6230beb91120ead5e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.7
|
Security for deeplens-sae 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.