Views
No views yet
z = Wx + b (encoder)x̂ = W'z_topk + b_dec (decoder)layer_45/
dict_16k_k80/ # 16,384 features, k=80
ae.pt # SAE weights
config.json # Training configuration
feature_labels.json # Natural language feature descriptions
dict_16k_k160/ # 16,384 features, k=160
dict_65k_k80/ # 65,536 features, k=80
dict_65k_k160/ # 65,536 features, k=160
layer_47/
(same structure)
layer_45_mlp/
(same structure)| Layer | Dict Size | k | Activation Dim | Parameters | Sparsity |
|---|---|---|---|---|---|
| 45 | 16,384 | 80 | 5,376 | 176,182,528 | 0.49% |
| 45 | 16,384 | 160 | 5,376 | 176,182,528 | 0.98% |
| 45 | 65,536 | 80 | 5,376 | 704,713,984 | 0.12% |
| 45 | 65,536 | 160 | 5,376 | 704,713,984 | 0.24% |
| 47 | 16,384 | 80 | 5,376 | 176,182,528 | 0.49% |
| 47 | 16,384 | 160 | 5,376 | 176,182,528 | 0.98% |
| 47 | 65,536 | 80 | 5,376 | 704,713,984 | 0.12% |
| 47 | 65,536 | 160 | 5,376 | 704,713,984 | 0.24% |
residual_stream (post-layer activations)pip install torch transformers huggingface_hub1import torch
2from huggingface_hub import hf_hub_download
3
4# Download specific SAE
5ae_path = hf_hub_download(
6 repo_id="uzaymacar/gemma-3-27b-saes",
7 filename="layer_45/dict_16k_k80/ae.pt",
8 subfolder=None,
9)
10
11config_path = hf_hub_download(
12 repo_id="uzaymacar/gemma-3-27b-saes",
13 filename="layer_45/dict_16k_k80/config.json",
14)
15
16# Load SAE
17ae_data = torch.load(ae_path, map_location='cpu')
18with open(config_path, 'r') as f:
19 config = json.load(f)
20
21print(f"Loaded SAE with {config['trainer']['dict_size']} features")
22print(f"Activation dimension: {config['trainer']['activation_dim']}")
23print(f"Top-k: {config['trainer']['k']}")
24
25# SAE weights
26encoder_weight = ae_data['encoder.weight'] # [dict_size, activation_dim]
27encoder_bias = ae_data['encoder.bias'] # [dict_size]
28decoder_weight = ae_data['decoder.weight'] # [activation_dim, dict_size]
29decoder_bias = ae_data['b_dec'] # [activation_dim]
30threshold = ae_data['threshold'] # Learned threshold1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3import torch.nn.functional as F
4
5# Load base model
6model_name = "google/gemma-3-27b-it"
7model = AutoModelForCausalLM.from_pretrained(
8 model_name,
9 torch_dtype=torch.bfloat16,
10 device_map='auto'
11)
12tokenizer = AutoTokenizer.from_pretrained(model_name)
13
14# Get activations from layer 45
15text = "The capital of France is Paris"
16inputs = tokenizer(text, return_tensors="pt").to(model.device)
17
18with torch.no_grad():
19 outputs = model(**inputs, output_hidden_states=True)
20 layer_45_acts = outputs.hidden_states[45] # [batch, seq, activation_dim]
21
22# Encode with SAE
23acts_flat = layer_45_acts.reshape(-1, layer_45_acts.shape[-1]) # [batch*seq, dim]
24
25# Encoder: z = Wx + b
26z = F.linear(acts_flat, encoder_weight, encoder_bias) # [batch*seq, dict_size]
27
28# Top-k selection (per sample, not batch)
29top_k = config['trainer']['k']
30top_values, top_indices = torch.topk(z, k=top_k, dim=-1)
31
32# Create sparse representation
33z_topk = torch.zeros_like(z)
34z_topk.scatter_(-1, top_indices, top_values)
35
36# Decode: x̂ = W'z + b
37reconstructed = F.linear(z_topk, decoder_weight.t(), decoder_bias)
38
39# Compute reconstruction loss
40mse_loss = F.mse_loss(reconstructed, acts_flat)
41print(f"Reconstruction MSE: {mse_loss.item():.6f}")
42
43# Find active features
44active_features = top_indices[0, 0] # First token's active features
45print(f"Active features for first token: {active_features.tolist()}")1import json
2from huggingface_hub import hf_hub_download
3
4# Download feature labels
5labels_path = hf_hub_download(
6 repo_id="uzaymacar/gemma-3-27b-saes",
7 filename="layer_45/dict_16k_k80/feature_labels.json",
8)
9
10with open(labels_path, 'r') as f:
11 labels = json.load(f)
12
13# Examine a specific feature
14feature_id = 1234
15if str(feature_id) in labels:
16 label = labels[str(feature_id)]
17 print(f"Feature {feature_id}:")
18 print(f" Title: {label.get('title', 'N/A')}")
19 print(f" Description: {label.get('description', 'N/A')}")1@software{gemma3_27b_saes,
2 author = {Macar, Uzay},
3 title = {Sparse Autoencoders for Gemma-3-27b-it},
4 year = {2024},
5 url = {https://huggingface.co/uzaymacar/gemma-3-27b-saes}
6}1@software{dictionary_learning,
2 author = {Marks, Samuel and others},
3 title = {Dictionary Learning for Mechanistic Interpretability},
4 year = {2024},
5 url = {https://github.com/saprmarks/dictionary_learning}
6}1@article{gao2024batchTopK,
2 title={Scaling and evaluating sparse autoencoders},
3 author={Gao, Leo and others},
4 journal={arXiv preprint arXiv:2406.04093},
5 year={2024}
6}