Skip to main content

A library for training and deploying Sparse Autoencoders

Project description

Spindle: Sparse Autoencoder Library

Structured Projection INdex for Dense Latent Embeddings

Spindle is a Python library for training and deploying Sparse Autoencoders (SAEs). It provides a simple, flexible API for working with SAEs in PyTorch.

Features

  • Train sparse autoencoders with configurable architectures
  • Analyze feature activations and reconstruction quality
  • Serve SAE models via a FastAPI server
  • Utilities for working with embeddings and weights
  • Visualization tools for interpreting SAE features

Installation

Install from PyPI:

pip install spindle

For additional features:

# For transformer model support
pip install "spindle[transformers]"

# For server components
pip install "spindle[server]"

Or install from source:

git clone https://github.com/wafer-inc/spindle
cd spindle
pip install -e .

Quick Start

Training an SAE

import torch
import numpy as np
from spindle.models.autoencoder import SAE
from spindle.models.trainer import train_sae

# Load your data
embedding_data = np.load('vectors.npy')
data = torch.tensor(embedding_data, dtype=torch.float32)

# Create and train the model
input_dim = data.shape[1]  # Embedding dimension
hidden_dim = 500  # Sparse feature dimension

model = SAE(input_dim, hidden_dim)
train_stats = train_sae(
    model=model,
    data=data,
    epochs=10,
    batch_size=128,
    sparsity_weight=1e-3,
    save_path='sae_model.pt'
)

print(f"Training complete! Final loss: {train_stats['final_loss']}")

Analyzing Features

from spindle.utils.analysis import compute_feature_statistics
from torch.utils.data import DataLoader, TensorDataset

# Load model and data
model = SAE.load('sae_model.pt', input_dim, hidden_dim)
dataset = TensorDataset(data)
loader = DataLoader(dataset, batch_size=128)

# Compute statistics
stats = compute_feature_statistics(model, loader)
print(f"Dead features: {stats['dead_feature_count']} ({stats['dead_feature_ratio']:.2%})")

Running a Server

from transformers import AutoTokenizer, AutoModel
from spindle.utils.server import SaeServer

# Load tokenizer and model
tokenizer = AutoTokenizer.from_pretrained("sentence-transformers/all-MiniLM-L6-v2")
embedding_model = AutoModel.from_pretrained("sentence-transformers/all-MiniLM-L6-v2")

# Create and run server
server = SaeServer(
    encoder_weights="encoder_weights.npz",
    tokenizer=tokenizer,
    embedding_model=embedding_model
)
server.run()

Documentation

For detailed documentation and examples, see the examples directory.

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

spindle_ml-0.1.1.tar.gz (20.1 kB view details)

Uploaded Source

Built Distribution

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

spindle_ml-0.1.1-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for spindle_ml-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d443d0d4884da4ac12de36cb2efe5fc0500e3fccba46c31e8b424a4ee076cab6
MD5 9d29c5293ba1493352993ffedb1a343c
BLAKE2b-256 d9bbacc8d0f9fcfbea7c9efb68e6bbc1b890654962a060bfb0d98fe238ddf138

See more details on using hashes here.

Provenance

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

Publisher: publish.yml on wafer-inc/spindle

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

File details

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

File metadata

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

File hashes

Hashes for spindle_ml-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 25abd110009d611ccc29407f0c53f5a79a9dc481abc37afa4751b8eb101ea84c
MD5 aaea1a79b12f84b9a900019745b65521
BLAKE2b-256 2a5435c580f87f8ae25119bc93130859b6f4f74b62c43a5d4f0440546ac2e78e

See more details on using hashes here.

Provenance

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

Publisher: publish.yml on wafer-inc/spindle

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