Sparsify transformers with SAEs and transcoders
Project description
Introduction
This library trains k-sparse autoencoders (SAEs) and transcoders on the activations of HuggingFace language models, roughly following the recipe detailed in Scaling and evaluating sparse autoencoders (Gao et al. 2024).
This is a lean, simple library with few configuration options. Unlike most other SAE libraries (e.g. SAELens), it does not cache activations on disk, but rather computes them on-the-fly. This allows us to scale to very large models and datasets with zero storage overhead, but has the downside that trying different hyperparameters for the same model and dataset will be slower than if we cached activations (since activations will be re-computed). We may add caching as an option in the future.
Following Gao et al., we use a TopK activation function which directly enforces a desired level of sparsity in the activations. This is in contrast to other libraries which use an L1 penalty in the loss function. We believe TopK is a Pareto improvement over the L1 approach, and hence do not plan on supporting it.
Loading pretrained SAEs
To load a pretrained SAE from the HuggingFace Hub, you can use the Sae.load_from_hub method as follows:
from sparsify import Sae
sae = Sae.load_from_hub("EleutherAI/sae-llama-3-8b-32x", hookpoint="layers.10")
This will load the SAE for residual stream layer 10 of Llama 3 8B, which was trained with an expansion factor of 32. You can also load the SAEs for all layers at once using Sae.load_many:
saes = Sae.load_many("EleutherAI/sae-llama-3-8b-32x")
saes["layers.10"]
The dictionary returned by load_many is guaranteed to be naturally sorted by the name of the hook point. For the common case where the hook points are named embed_tokens, layers.0, ..., layers.n, this means that the SAEs will be sorted by layer number. We can then gather the SAE activations for a model forward pass as follows:
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Meta-Llama-3-8B")
inputs = tokenizer("Hello, world!", return_tensors="pt")
with torch.inference_mode():
model = AutoModelForCausalLM.from_pretrained("meta-llama/Meta-Llama-3-8B")
outputs = model(**inputs, output_hidden_states=True)
latent_acts = []
for sae, hidden_state in zip(saes.values(), outputs.hidden_states):
# (N, D) input shape expected
hidden_state = hidden_state.flatten(0, 1)
latent_acts.append(sae.encode(hidden_state))
# Do stuff with the latent activations
For use cases beyond collecting residual stream SAE activations, we recommend PyTorch hooks (see examples.)
Training SAEs and transcoders
To train SAEs from the command line, you can use the following command:
python -m sparsify EleutherAI/pythia-160m <optional dataset>
By default, we use the EleutherAI/SmolLM2-135M-10B dataset for training, but you can use any dataset from the HuggingFace Hub, or any local dataset in HuggingFace format (the string is passed to load_dataset from the datasets library).
The CLI supports all of the config options provided by the TrainConfig class. You can see them by running python -m sparsify --help.
Programmatic usage is simple. Here is an example:
import torch
from datasets import load_dataset
from transformers import AutoModelForCausalLM, AutoTokenizer
from sparsify import SaeConfig, Trainer, TrainConfig
from sparsify.data import chunk_and_tokenize
MODEL = "HuggingFaceTB/SmolLM2-135M"
dataset = load_dataset(
"EleutherAI/SmolLM2-135M-10B", split="train",
)
tokenizer = AutoTokenizer.from_pretrained(MODEL)
tokenized = chunk_and_tokenize(dataset, tokenizer)
gpt = AutoModelForCausalLM.from_pretrained(
MODEL,
device_map={"": "cuda"},
torch_dtype=torch.bfloat16,
)
cfg = TrainConfig(SaeConfig(), batch_size=16)
trainer = Trainer(cfg, tokenized, gpt)
trainer.fit()
Finetuning SAEs
To finetune a pretrained SAE, pass its path to the finetune argument.
python -m sparsify EleutherAI/pythia-160m togethercomputer/RedPajama-Data-1T-Sample --finetune EleutherAI/sae-pythia-160m-32x
Custom hookpoints
By default, the SAEs are trained on the residual stream activations of the model. However, you can also train SAEs on the activations of any other submodule(s) by specifying custom hookpoint patterns. These patterns are like standard PyTorch module names (e.g. h.0.ln_1) but also allow Unix pattern matching syntax, including wildcards and character sets. For example, to train SAEs on the output of every attention module and the inner activations of every MLP in GPT-2, you can use the following code:
python -m sparsify gpt2 --hookpoints "h.*.attn" "h.*.mlp.act"
To restrict to the first three layers:
python -m sparsify gpt2 --hookpoints "h.[012].attn" "h.[012].mlp.act"
We currently don't support fine-grained manual control over the learning rate, number of latents, or other hyperparameters on a hookpoint-by-hookpoint basis. By default, the expansion_factor option is used to select the appropriate number of latents for each hookpoint based on the width of that hookpoint's output. The default learning rate for each hookpoint is then set using an inverse square root scaling law based on the number of latents. If you manually set the number of latents or the learning rate, it will be applied to all hookpoints.
Distributed training
We support distributed training via PyTorch's torchrun command. By default we use the Distributed Data Parallel method, which means that the weights of each SAE are replicated on every GPU.
torchrun --nproc_per_node gpu -m sparsify meta-llama/Meta-Llama-3-8B --batch_size 1 --layers 16 24 --k 192 --grad_acc_steps 8 --ctx_len 2048
This is simple, but very memory inefficient. If you want to train SAEs for many layers of a model, we recommend using the --distribute_modules flag, which allocates the SAEs for different layers to different GPUs. Currently, we require that the number of GPUs evenly divides the number of layers you're training SAEs for.
torchrun --nproc_per_node gpu -m sparsify meta-llama/Meta-Llama-3-8B --distribute_modules --batch_size 1 --layer_stride 2 --grad_acc_steps 8 --ctx_len 2048 --k 192 --load_in_8bit --micro_acc_steps 2
The above command trains an SAE for every even layer of Llama 3 8B, using all available GPUs. It accumulates gradients over 8 minibatches, and splits each minibatch into 2 microbatches before feeding them into the SAE encoder, thus saving a lot of memory. It also loads the model in 8-bit precision using bitsandbytes. This command requires no more than 48GB of memory per GPU on an 8 GPU node.
TODO
There are several features that we'd like to add in the near future:
- Support for caching activations
- Evaluate SAEs with KL divergence when grafted into the model
If you'd like to help out with any of these, please feel free to open a PR! You can collaborate with us in the sparse-autoencoders channel of the EleutherAI Discord.
Installation
pip install eai-sparsify
Development
Run pip install -e .[dev] from the sparsify directory.
We use conventional commits for releases.
Experimental features
Linear k decay schedule:
bash python -m sparsify gpt2 --hookpoints "h.*.attn" "h.*.mlp.act" --k_decay_steps 10_000
GroupMax activation function:
bash python -m sparsify gpt2 --hookpoints "h.*.attn" "h.*.mlp.act" --activation groupmax
End-to-end training:
bash python -m sparsify gpt2 --hookpoints "h.*.attn" "h.*.mlp.act" --loss_fn ce
or
bash python -m sparsify gpt2 --hookpoints "h.*.attn" "h.*.mlp.act" --loss_fn kl
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 eai_sparsify-1.2.1.tar.gz.
File metadata
- Download URL: eai_sparsify-1.2.1.tar.gz
- Upload date:
- Size: 28.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5772a26ae0fd4c8290d9e630462b55d7643a28c9e86c1db525b47b7854cbe3c7
|
|
| MD5 |
803f3d7dc08ce06ddb2db903aa5739ee
|
|
| BLAKE2b-256 |
0dabcdc5862a231b1d139c9beeb51f8d0a694b65de00c8b607210c3905db96bf
|
Provenance
The following attestation bundles were made for eai_sparsify-1.2.1.tar.gz:
Publisher:
build.yml on EleutherAI/sparsify
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
eai_sparsify-1.2.1.tar.gz -
Subject digest:
5772a26ae0fd4c8290d9e630462b55d7643a28c9e86c1db525b47b7854cbe3c7 - Sigstore transparency entry: 559818462
- Sigstore integration time:
-
Permalink:
EleutherAI/sparsify@25e880ce33c9f799d3be581dc5f6d6492c405839 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/EleutherAI
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build.yml@25e880ce33c9f799d3be581dc5f6d6492c405839 -
Trigger Event:
push
-
Statement type:
File details
Details for the file eai_sparsify-1.2.1-py3-none-any.whl.
File metadata
- Download URL: eai_sparsify-1.2.1-py3-none-any.whl
- Upload date:
- Size: 31.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
917da73f5fb241f962deca6bd080b3df2474f919ecbf0529d7ab720dd07be291
|
|
| MD5 |
0c9d0e02f06e962abfb45739dc599524
|
|
| BLAKE2b-256 |
c807dbee3db80c4fb4d1fdbd488155318929941b32c184599c4aaf0a3bd2cd46
|
Provenance
The following attestation bundles were made for eai_sparsify-1.2.1-py3-none-any.whl:
Publisher:
build.yml on EleutherAI/sparsify
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
eai_sparsify-1.2.1-py3-none-any.whl -
Subject digest:
917da73f5fb241f962deca6bd080b3df2474f919ecbf0529d7ab720dd07be291 - Sigstore transparency entry: 559818483
- Sigstore integration time:
-
Permalink:
EleutherAI/sparsify@25e880ce33c9f799d3be581dc5f6d6492c405839 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/EleutherAI
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build.yml@25e880ce33c9f799d3be581dc5f6d6492c405839 -
Trigger Event:
push
-
Statement type: