Skip to main content

LatentSAE: Training and inference for SAEs on embeddings

Project description

latent-sae

Train Sparse Autoencoders on sentence embedding representations. Decomposes dense embedding vectors into sparse, interpretable features.

Fork of EleutherAI/sae, focused on sentence transformer embeddings with fast disk-based data loading, Modal GPU training, and a comprehensive experiment framework.

Published Models

Model Subfolder Embedding Features k Best For
sae-all-MiniLM-L6-v2-FineWeb-RedPajama-Pile-150M 128_4 MiniLM (384D) 1,536 128 Fine-grained classification
128_8 MiniLM (384D) 3,072 128 Retrieval
64_8 MiniLM (384D) 3,072 64 Maximum feature coverage
sae-nomic-text-v1.5-FineWeb-edu-100BT 64_32 nomic-v1.5 (768D) 24,576 64 Legacy (taxonomy)

Quick Start

# pip install latentsae
from latentsae import Sae
from sentence_transformers import SentenceTransformer
import torch

# Load SAE
sae = Sae.load_from_hub("enjalot/sae-all-MiniLM-L6-v2-FineWeb-RedPajama-Pile-150M", "128_4")

# Embed text
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
embeddings = model.encode(["Hello world", "Sparse autoencoders decompose embeddings"],
                          convert_to_tensor=True, normalize_embeddings=True)

# Extract sparse features
features = sae.encode(embeddings)
print(features.top_indices)  # which features activated
print(features.top_acts)     # how strongly

Training

With YAML config (recommended)

# Single run on Modal A10G
modal run train_modal.py --config experiments/configs/minilm_30M_3source.yaml --gpu-type a10g

# Parameter sweep (parallel)
modal run train_modal.py --config experiments/configs/arch_sweep_base.yaml \
  --sweep experiments/configs/arch_sweep_type.yaml --gpu-type a10g

# Local training on M2 Mac
python -m experiments.run_experiment experiments/configs/smoke_test.yaml --device mps

With CLI args (quick experiments)

modal run train_modal.py --batch-size 1024 --k 128 --expansion-factor 4 --gpu-type a10g
python train_local.py --batch_size 512 --k 64 --expansion_factor 8

Experiment Framework

YAML-driven experiment system with config hashing, cartesian sweeps, and WandB integration. See experiments/ for configs and results.

# Dry-run a sweep to see what would execute
python -m experiments.run_experiment experiments/configs/arch_sweep_base.yaml \
  --sweep experiments/configs/arch_sweep_type.yaml --dry-run

# Compare results
python -m experiments.compare_results experiments/results/

# Evaluate a trained SAE
python -m experiments.eval_probes --sae-path checkpoints/sae_topk_128_4.xxx \
  --embedding-model sentence-transformers/all-MiniLM-L6-v2 --suite hard

Evaluation Suite

Task Type What it tests
AG News 4-class classification Coarse topic features
SST-2 2-class classification Sentiment features
BANKING77 77-class classification Fine-grained intent features
CLINC150 150-class classification Very fine-grained (hardest)
STS-B Similarity (spearman) Continuous semantic structure
SciFact Retrieval (nDCG@10) Information preservation
MMCS Feature quality Decoder weight redundancy

Architecture

Supported SAE types: TopK (recommended), Gated, JumpReLU, LISTA.

Training features: auxk dead feature revival, k-annealing, tilted ERM, decoder decorrelation loss, fire rate penalty, mixed precision (AMP), cosine LR schedule.

Key Research Findings

  • Expansion factor 4-8x is optimal for embeddings (not 32x like LLM layers)
  • k=128 dramatically beats k=64 on fine-grained tasks (CLINC150: 79.6% vs 64.6%)
  • 30M diverse samples retains 97% of 150M quality at 6.4x lower cost
  • 3-source data mix matters more than any training regularization
  • A10G at batch_size=1024 is the cost-optimal GPU config ($2.60/100M samples)

Data Preparation

Training data (pre-computed embeddings) is prepared in latent-data-modal. See the Latent Taxonomy methodology for details.

Part of the latent-* ecosystem

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

latentsae-0.2.0.tar.gz (52.3 kB view details)

Uploaded Source

Built Distribution

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

latentsae-0.2.0-py3-none-any.whl (43.8 kB view details)

Uploaded Python 3

File details

Details for the file latentsae-0.2.0.tar.gz.

File metadata

  • Download URL: latentsae-0.2.0.tar.gz
  • Upload date:
  • Size: 52.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for latentsae-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e9b9549686987019d905f1f30b53ec8d8c77ada72d5b2feec7d744f1e8b4f1c8
MD5 32e43bac12664a95bea3f19629238cbd
BLAKE2b-256 74e09f6f535bfe3fef2df1fcf7d0c22c10f1fcfdfc2a3527aa6023deba038974

See more details on using hashes here.

File details

Details for the file latentsae-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: latentsae-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 43.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for latentsae-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 567ca4bcec866042de820d80d99933a769aff814a1280fe74c3c2f39a5aeca66
MD5 399d663de6bce23b115ff9cec717747a
BLAKE2b-256 53a6fde61f9954b104abd758cca73d780419fc4f57a43b16ed50e99ccdd80e7f

See more details on using hashes here.

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