Search

Search papers and lessons

Scaling and evaluating sparse autoencoders

Leo Gao, Tom Dupré la Tour, Henk Tillman et al. · 2024

Gao and colleagues train sparse autoencoders with a fixed top-k rule, find clean scaling laws, train a 16 million latent model on GPT-4 activations, and propose new ways to score the features it finds.

On this page

  • Colour = topic mix
  • Discrete math
  • Calculus
  • Probability
  • Neural network basics
  • Linear algebra
  • Losses & regularization
  • ReadReady to readNot yet

TL;DR

The paper trains sparse autoencoders on the internal activations of language models and keeps exactly kk latents active for each input, using a rule called TopK instead of an L1 penalty. With this change, the reconstruction error follows clean scaling laws in the number of latents and the sparsity, and the authors train a 16 million latent autoencoder on GPT-4 activations. They also propose new metrics that test whether the learned features are useful, not just whether they reconstruct well.

Builds on: Autoencoder, Rectified Linear Unit (ReLU), L1 penalty · Leads to: later work on training and evaluating sparse features in large language models

Why it mattered

The method made large sparse autoencoders practical to train. Before it, training wide and sparse autoencoders often produced many latents that never activated again, and the usual sparsity penalty shrank every useful activation. The paper shows that its TopK version avoids the shrinkage and keeps dead latents rare, with 7% of the latents dead in the 16 million latent model.

The paper also states that it releases training code and autoencoders for open-source models, plus a feature visualizer, so other groups could test the same features. Its evaluation ideas, such as downstream loss and explanation-based scores, ask whether a feature matters for the model's predictions, not only whether the autoencoder fits the activations.

History

Sparse coding over an overcomplete dictionary was introduced by Mallat and Zhang in 1993. Olshausen and Field in 1996 proposed learning that dictionary from data without supervision. Hinton and Salakhutdinov proposed the autoencoder for dimensionality reduction in 2006. Combining them, sparse autoencoders with sparsity priors were developed in the years the paper cites, 2007 to 2014 (Lee et al. 2007, Le et al. 2013, Konda et al. 2014).

Makhzani and Frey introduced the kk-sparse autoencoder in 2013, which replaces the penalty with a TopK activation. In 2023, Bricken et al. applied sparse autoencoders to the residual streams of language models, and this paper extends that line of work to much larger models and widths.

It caught on because it gave researchers a concrete way to split a model's internal state into many features, and a way to test them. The paper's contribution is the scaling study and the evaluation metrics, which let later work compare methods on something other than reconstruction alone.

The problem

A language model stores many concepts in a fixed number of numbers. The residual stream, which is the running vector that each layer reads from and writes to, holds the information the model uses for its next prediction. Interpretability researchers want to split that vector into features, each one a human-readable concept such as a language, a topic or a quotation pattern.

A sparse autoencoder tries to do this. It takes an activation vector, encodes it into many latents, keeps only a few of them active, and decodes it back. If the model has many concepts, the autoencoder needs many latents. The paper names three difficulties.

First, the reconstruction and sparsity goals pull against each other, so tuning the penalty weight is hard. Second, many latents die: in the authors' ablations, up to 90% of latents stopped activating during training. Third, reconstruction is not the end goal. A small reconstruction error does not prove that the latents are meaningful.

How it works

The authors' method is a chain of stages. We trace a toy version with three latents and two input numbers. This is an illustration of the mechanics, not a trained model.

flowchart LR
  A["Activation x from the residual stream"] --> B["Encoder: pre-activations W_enc x"]
  B --> C["TopK: keep the k largest, zero the rest"]
  C --> D["Decoder: sum of latent times decoder column"]
  D --> E["Reconstruction x hat and error"]
  1. Start with an activation. Let x=(2,1)x = (2, 1), a two-number vector standing in for one residual stream activation. Check: it has d=2d = 2 entries.
  2. Encode. Apply the encoder rows (1,0)(1, 0), (0,1)(0, 1) and (1,1)(1, 1). The three pre-activations are 22, 11 and 33. Check: the third row gives 1×2+1×1=31 \times 2 + 1 \times 1 = 3.
  3. Keep the top kk. The rule is TopK with k=1k = 1: keep the one largest value and set the others to zero. The largest is 33 in latent 3, so z=(0,0,3)z = (0, 0, 3). Check: exactly one entry is nonzero, as k=1k = 1 requires.
  4. Decode. The decoder columns are (0,1)(0, 1) for latent 1, (0,−1)(0, -1) for latent 2 and (1,0)(1, 0) for latent 3. Only latent 3 is active, so x^=3×(1,0)=(3,0)\hat{x} = 3 \times (1, 0) = (3, 0). Check: the decoder adds one column scaled by its latent value.
  5. Measure the error. The error is x−x^=(2−3,1−0)=(−1,1)x - \hat{x} = (2 - 3, 1 - 0) = (-1, 1). The mean squared error over the two entries is (−1)2+122=1\frac{(-1)^2 + 1^2}{2} = 1. Check: both entries have size 1.
  6. Read the result. One active latent reconstructs the first entry too large and misses the second entirely. A real autoencoder has thousands of latents, each with its own direction, so it can use more than one active latent. The paper studies how the reconstruction error changes as the number of latents and the value of kk change.

Worked example: the TopK rule

Example
Example 1: the TopK rule, tiny

Apply TopK with k=2k = 2 to the pre-activations (0.5,2,−1,1)(0.5, 2, -1, 1).

  1. The two largest values are 22 and 11, at positions 2 and 4.
  2. Keep them: positions 2 and 4 stay 22 and 11.
  3. Set the others to zero: position 1 becomes 00, position 3 becomes 00 (the value −1-1 is not kept).
  4. Result: z=(0,2,0,1)z = (0, 2, 0, 1).
  5. Check: exactly two entries are nonzero, as k=2k = 2 requires.
  6. Interpretation: TopK fixes the number of active latents, so the same kk applies to every input, however large the other pre-activations are.

The math

Definition: TopK sparse autoencoder

Let x∈Rdx \in \mathbb{R}^{d} be an activation, nn the number of latents, and kk the number of latents allowed to be active. With encoder weights Wenc∈Rn×dW_{\text{enc}} \in \mathbb{R}^{n \times d}, decoder weights Wdec∈Rd×nW_{\text{dec}} \in \mathbb{R}^{d \times n}, and a pre-encoder bias bpreb_{\text{pre}}, the TopK autoencoder is

z=TopK(Wenc(x−bpre)),x^=Wdecz+bpre,z = \text{TopK}\left(W_{\text{enc}}(x - b_{\text{pre}})\right), \qquad \hat{x} = W_{\text{dec}} z + b_{\text{pre}},

where TopK\text{TopK} keeps the kk largest entries of its input and sets the others to zero. Training minimises the reconstruction error ∥x−x^∥22\lVert x - \hat{x} \rVert_2^2 alone.

The paper uses the same symbols. Here zz is the vector of latent activations, and x^\hat{x} is the reconstruction. The bias bpreb_{\text{pre}} is initialised to the geometric median of the data.

Step 2 in symbols: the encoder and TopK

The toy example used Wenc=(100111)W_{\text{enc}} = \begin{pmatrix} 1 & 0 \\ 0 & 1 \\ 1 & 1 \end{pmatrix} and bpre=0b_{\text{pre}} = 0, so Wencx=(2,1,3)W_{\text{enc}} x = (2, 1, 3). TopK with k=1k = 1 keeps only the largest, 33. The paper's version uses ReLU as well, so that all kept activations are non-negative. The paper notes that the training curves are indistinguishable from TopK alone, because the kk largest values are almost always positive.

Step 3 in symbols: the decoder

The decoder adds up the decoder columns, weighted by the latent values:

x^=∑i=1nzi di+bpre.\hat{x} = \sum_{i=1}^{n} z_i \, \mathbf{d}_i + b_{\text{pre}}.

The paper normalises each decoder column to unit length after each step. In the toy example, the columns are unit length, so the decoder is a direct sum.

Step 4 in symbols: the loss

The TopK loss is L=∥x−x^∥22\mathcal{L} = \lVert x - \hat{x} \rVert_2^2. The paper compares it with the baseline ReLU autoencoder, whose loss adds an L1 penalty on the latents:

LReLU=∥x−x^∥22+λ∥z∥1.\mathcal{L}_{\text{ReLU}} = \lVert x - \hat{x} \rVert_2^2 + \lambda \lVert z \rVert_1.

The authors argue that the L1 penalty shrinks all active latents toward zero, while TopK removes the need for the penalty and so avoids the shrinkage. They support this with an experiment: refining the TopK activations with a fixed decoder does not increase them on average, while the same refinement does increase the ReLU activations.

Step 5 in symbols: the scaling law

The paper fits the reconstruction error L(n,k)L(n, k) of GPT-4 autoencoders with a joint law:

L(n,k)=exp⁡(α+βklog⁡k+βnlog⁡n+γlog⁡klog⁡n)+exp⁡(ζ+ηlog⁡k),L(n, k) = \exp\left(\alpha + \beta_k \log k + \beta_n \log n + \gamma \log k \log n\right) + \exp\left(\zeta + \eta \log k\right),

with fitted values α=−0.50\alpha = -0.50, βk=0.26\beta_k = 0.26, βn=−0.017\beta_n = -0.017, γ=−0.042\gamma = -0.042, ζ=−1.32\zeta = -1.32 and η=−0.085\eta = -0.085. The paper says γ\gamma is negative, so the law gets steeper as kk grows. It also says η\eta is negative, so the irreducible term decreases with kk.

Token budget to convergence

The number of training tokens needed to converge grows as approximately Θ(n0.6)\Theta(n^{0.6}) for GPT-2 small and Θ(n0.65)\Theta(n^{0.65}) for GPT-4. The learning rate that converges scales with 1/n1/\sqrt{n}. The paper notes that a sublinear token budget would eventually leave each latent with almost no gradient signal, so this trend must break at some scale.

Worked example: the scaling law

Example
Example 2: the scaling-law exponent, standard case

Using the fitted values, compute the first term of L(n,k)L(n, k) when k=32k = 32 and n=217=131072n = 2^{17} = 131072. The paper's α=−0.50\alpha = -0.50, βk=0.26\beta_k = 0.26, βn=−0.017\beta_n = -0.017, γ=−0.042\gamma = -0.042, ζ=−1.32\zeta = -1.32 and η=−0.085\eta = -0.085 are used directly. The paper does not state the logarithm base, so this example assumes natural logarithms.

  1. Write the first term's exponent: α+βklog⁡k+βnlog⁡n+γlog⁡klog⁡n\alpha + \beta_k \log k + \beta_n \log n + \gamma \log k \log n.
  2. Logarithms: log⁡32≈3.466\log 32 \approx 3.466, and log⁡131072≈11.783\log 131072 \approx 11.783.
  3. Terms: βklog⁡k=0.26×3.466≈0.901\beta_k \log k = 0.26 \times 3.466 \approx 0.901; βnlog⁡n=−0.017×11.783≈−0.200\beta_n \log n = -0.017 \times 11.783 \approx -0.200; γlog⁡klog⁡n=−0.042×3.466×11.783≈−1.715\gamma \log k \log n = -0.042 \times 3.466 \times 11.783 \approx -1.715.
  4. Sum with α\alpha: −0.50+0.901−0.200−1.715≈−1.514-0.50 + 0.901 - 0.200 - 1.715 \approx -1.514, so the first term is exp⁡(−1.514)≈0.220\exp(-1.514) \approx 0.220.
  5. Second term: ζ+ηlog⁡k=−1.32−0.085×3.466≈−1.615\zeta + \eta \log k = -1.32 - 0.085 \times 3.466 \approx -1.615, so exp⁡(−1.615)≈0.199\exp(-1.615) \approx 0.199.
  6. Check: the two terms add to 0.220+0.199≈0.420.220 + 0.199 \approx 0.42. This is my arithmetic on the fitted law for these values of nn and kk; the paper does not report this number.
  7. Interpretation: the law is a sum of two exponential terms, and the example shows how the fitted exponents combine to give a predicted loss.

Results

The paper's main findings, with its numbers:

  • Scaling laws. The reconstruction error follows a joint law in the number of latents nn and the sparsity kk. The fitted parameters are given in The math.
  • Dead latents. The 16 million latent autoencoder has 7% dead latents. In the authors' ablations, up to 90% of latents were dead.
  • Scale. The 16 million latent model was trained on GPT-4 activations for 40 billion tokens, and it is the largest model in the paper.
  • Downstream loss. Substituting the 16 million latent autoencoder into GPT-4 gives a loss corresponding to 10% of GPT-4's pretraining compute, and its zero-ablation fidelity is 98.2%.
  • Probes. The authors test 61 binary classification datasets with a one-dimensional logistic probe on each latent. The baseline score from using the residual stream channels directly is 0.600 (Figure 32).
  • Explanations. On GPT-2 small, the TopK models have N2G explanations with recall more than 1.5 times that of the ReLU models with the same nn, and precision only slightly lower (more than 0.9 times).
  • Sparse effects. Latents have (L1/L2)2(L_1 / L_2)^2 values of 10 to 14% for the effect on logits, compared with 60% for ablating residual stream channels. The trend reverses at k=512k = 512.

Limits. The paper says that, because of compute constraints, the 16 million latent model was not trained to the L(N)L(N) token budget, so it is not part of the consistent L(N)L(N) series. The authors also report that a large fraction of the random activations of their features, especially in GPT-4, are not yet adequately monosemantic.

Worked example: measuring fidelity and dead latents

Example
Example 3: measuring feature quality, realistic

A sparse autoencoder with 16 million latents is trained on GPT-4 activations. The authors check how many latents died during training, what fraction of the model's zero-ablation loss its reconstruction recovers, and how much pretraining compute a model with that reconstruction would be worth. Use the paper's reported numbers and reason through what each one says about the autoencoder.

  1. Dead latents: the paper reports that only 7% of the latents in the 16 million latent autoencoder are dead. So about 93% of the 16,000,00016{,}000{,}000 latents still activate in training, which is what the AuxK loss was added to achieve.
  2. Zero-ablation fidelity: replacing the residual stream with zeros is a poor baseline, because it destroys most of the loss. The 16 million latent model recovers 98.2% of the zero-ablation baseline, which is high but partly a consequence of the poor baseline.
  3. Compute-equivalent: the paper says that substituting the autoencoder into GPT-4 gives a language modeling loss matching a model trained with 10% of GPT-4's pretraining compute. This is the more natural measure the authors propose, since it is in units of compute.
  4. Check: the 7% and 98.2% are two different numbers, one for training health and one for fidelity, and neither says that the features are interpretable. Both must be read with the caveats in the Results section.
  5. Interpretation: the autoencoder is useful for analysis, but the paper argues the features also need the explanation and probe metrics, because a high fidelity alone does not show that the features are meaningful.

Try it: an autoencoder has 1000 latents and 3% of them are dead. How many latents are alive?

Answer: 1000×(1−0.03)=1000×0.97=9701000 \times (1 - 0.03) = 1000 \times 0.97 = 970 latents are alive.

Common misreadings

It is tempting to think a lower reconstruction error means a better set of features. In fact the paper argues that reconstruction is not the end goal: a small error does not show that the latents are meaningful, which is why it adds probe, explanation and ablation-sparsity metrics.

It is tempting to think a dead latent is harmless because the model still reconstructs well. In fact a dead latent is a wasted part of the dictionary, so the paper adds an AuxK loss that uses the top kauxk_{\text{aux}} dead latents (typically 512) to reconstruct the error and so trains them.

It is tempting to think a TopK model is the same as a ReLU model with a small L1 penalty. In fact the paper's refinement experiment shows that an L1 penalty shrinks ReLU activations, while TopK activations are not biased toward shrinkage.

It is tempting to think TopK gives a fixed-quality code at every sparsity. In fact a TopK model trained at one kk overfits to that value. Its reconstruction gets worse for larger k′k' at test time, and the paper's Multi-TopK loss, L(k)+L(4k)/8\mathcal{L}(k) + \mathcal{L}(4k)/8, is their fix.

When to use it: when you want a fixed number of active latents per input, a simple tuning knob, and no shrinkage from an L1 term.

When not to use it: the paper names its own limits. TopK uses exactly kk latents for every token, whereas JumpReLU lets the number of active latents vary across tokens. The paper also says a context length of 64 tokens is potentially too few to show the most interesting behaviour. Near k=512k = 512 with dmodel=768d_{\text{model}} = 768, the learned latents become dense and less interpretable.

Alternatives named in the paper: gated sparse autoencoders, ProLU (also called JumpReLU), and per-latent scale fitting, all of which address the L1 shrinkage in different ways.

Reading guide

Check yourself

QuestionWhat does TopK with k=3k = 3 do to the pre-activation vector (5,−2,4,0,1)(5, -2, 4, 0, 1)?Show answer

It keeps the three largest values, which are 55, 44 and 11, and sets the other entries to zero. The result is (5,0,4,0,1)(5, 0, 4, 0, 1), which has exactly three nonzero entries.

QuestionAn autoencoder has 16 million latents and 7% are dead. How many latents are alive?Show answer

16,000,000×(1−0.07)=16,000,000×0.93=14,880,00016{,}000{,}000 \times (1 - 0.07) = 16{,}000{,}000 \times 0.93 = 14{,}880{,}000 latents are alive.

QuestionWhy does the paper argue that a TopK autoencoder avoids the shrinkage of a ReLU autoencoder with an L1 penalty?Show answer

The L1 penalty pushes every active latent toward zero, which shrinks the activations. TopK removes the penalty and fixes the number of active latents directly, so the kept activations are not pulled toward zero. The paper's refinement experiment shows that refining TopK activations does not increase them on average, while it does for ReLU, which is consistent with this.

QuestionWhy does an excellent reconstruction not settle whether a sparse autoencoder has found good features?Show answer

Reconstruction measures how well the activations are rebuilt, not whether each latent means something. The paper therefore adds feature-quality tests: probe scores on known features, explanation scores, and the sparsity of ablation effects on the logits.

Sources

Read the full paper

Leo Gao, Tom Dupré la Tour, Henk Tillman et al., 2024. arXiv:2406.04093