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.2.tar.gz (20.3 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.2-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: spindle_ml-0.1.2.tar.gz
  • Upload date:
  • Size: 20.3 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.2.tar.gz
Algorithm Hash digest
SHA256 6e59c77d78e8fd815ef03c88315934a8bae6b323dfabcb05b654ed7ff23903c4
MD5 24849de916821dbe4e5dd777f808ad0f
BLAKE2b-256 f6a3832cc7b592c655b2426dd898ca5e411b9e82b5fb63e9bae7a469c01d89c8

See more details on using hashes here.

Provenance

The following attestation bundles were made for spindle_ml-0.1.2.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.2-py3-none-any.whl.

File metadata

  • Download URL: spindle_ml-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 13.0 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 519e3d0dd9a64a8ac6512558331502b03e913dd280075e173aaa07e61faca98b
MD5 50fe7e0188d61249abc3f2c60b171ed6
BLAKE2b-256 d7e63aa65b3db9e0ddb4ba5de4508e582a745dab43a0a06c6749c9df928d1415

See more details on using hashes here.

Provenance

The following attestation bundles were made for spindle_ml-0.1.2-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