# Exploring SAE features in LLMs with definition trees and token lists

**TL;DR** A software tool is presented which includes two separate methods to assist in the interpretation of SAE features. Both use a "feature vector" built from the relevant weights. One method builds "definition trees" for "ghost tokens" constructed from the feature vector, the other produces lists of tokens based on a cosine-similarity-based metric.    
  
**Links:** [Github repository](https://github.com/mwatkins1970/SAE_Feature_Interpretability_Tool), [Colab notebook](https://colab.research.google.com/github/mwatkins1970/SAE_Feature_Interpretability_Tool/blob/main/notebooks/SAE_feature_interpretability_multitool.ipynb)

*Thanks to* [*Egg Syntax*](https://www.lesswrong.com/users/eggsyntax) *and* [*Joseph Bloom*](https://www.lesswrong.com/users/joseph-bloom) *for motivating discussions.*

Introduction
============

Objective
---------

The motivation was to adapt techniques I developed for studying LLM embedding spaces (in my series of "Mapping the Semantic Void" posts \[[1](https://www.lesswrong.com/posts/c6uTNm5erRrmyJvvD/mapping-the-semantic-void-strange-goings-on-in-gpt-embedding)\] \[[2](https://www.lesswrong.com/posts/hincdPwgBTfdnBzFf/mapping-the-semantic-void-ii-above-below-and-between-token)\] \[[3](https://www.lesswrong.com/posts/ii3fMmZstDFGdCHQG/mapping-the-semantic-void-iii-exploring-neighbourhoods)\]) to the study of features learned by [sparse autoencoders (SAEs)](https://en.wikipedia.org/wiki/Autoencoder#Sparse_autoencoder) trained on large language models (LLMs). Any feature learned by an autoencoder corresponds to a neuron in its single hidden layer, and to this can be associated a pair of vectors in the LLM's representation space: one built from its encoder layer weights and one from its decoder layer weights.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/7f21880e5b9e24a059fe9470d200ce628c825c86603d0f92.png)

In this toy example we have a 3-dimensional residual stream and five neurons in the hidden layer, each corresponding to a learned feature. As you can see, each of these can be associated with three weights linked to the (red) encoder layer, and to three weights linked to the (green) decoder layer. These can then be thought of as the components of a pair of "feature" vectors in the model's (3-dimensional) representation space.

In my earlier work I used custom embeddings in order to "trick" LLMs into "believing" that there was a token in a tokenless location so I could then prompt the model to define this "ghost token". As this can be done for any point in the model's representation/embedding space, it seemed worth exploring what would happen if SAE feature vectors were used to construct "ghost tokens" and the model were prompted to define these. Would the definitions tell us anything about the learned features?  
  
I focussed my attention on the [four residual stream SAEs trained by Joseph Bloom on the Gemma-2B model](https://huggingface.co/jbloom/Gemma-2b-Residual-Stream-SAEs), since data was available on all of their features via his [Neuronpedia](https://www.neuronpedia.org/) web-interface.

Tool overview
-------------

The first method I explored involved building "definition trees" by iteratively prompting Gemma-2B with the following and collecting the top 5 most probable tokens at each step.

    A typical definition of "<ghost token>" would be "

The data is stored as a nested dictionary structure and can be displayed visually as a tree diagram which, with its branching/"multiversal" structure provides an interestingly nonlinear sense of meaning for the feature (as opposed to an interpretation given via a linear string of text).

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/f42d7bdb151885ac2941feecfb2db38df7c9f8f22e989380.png)

A small part of a typical definition tree, in this case for [SAE layer 6 feature 10](https://www.neuronpedia.org/gemma-2b/6-res-jb/10).

  
In the second method, the model itself is not even used. No forward passes are necessary, all that's needed are the token embeddings. The initial idea is to look at which tokens' embeddings are at the smallest cosine distance from the feature vector in the model's embedding/representation space (those which are "cosine-closest"). The problem with this, as I discovered [when exploring token embedding clusters in GPT-3 (the work which surfaced the so-called glitch tokens)](https://www.lesswrong.com/posts/aPeJE8bSo6rAFoLqg/solidgoldmagikarp-plus-prompt-generation), is that by some quirk of high-dimensional geometry, the token embeddings which are cosine-closest to the mean token embedding (or centroid) are cosine-closest to *everything.* As counterintuitive as this seems, if you make a "top 100 cosine-closest tokens" list for any token in the vocabulary (or any other vector in embedding/representation space), you will keep seeing the same basic list of 100-or-so tokens in pretty much the same order.   
  
In order to filter out these ubiquitously proximate tokens from the lists, it occurred to me to divide the cosine-distance-to-the-vector-in-question by the cosine-distance-to-the-centroid and look for the smallest values. This incentivises a small numerator and a large denominator, thereby tending to exclude the problem tokens which cluster closest to the centroid. And it works quite well. In fact, it's possible to refine this by raising the numerator to a power > 1 (different powers produce different "closest" lists, many topped by a handful of tokens clearly relevant to the feature in question).

| ![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/b6081fed116c3efeba90cb8179153b4919d532410dccf246.png) |
| --- |
| ![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/442be5a68e4f7f91f4edce0b0947d382dedc5589f7213aa1.png) top 100 cosine-closest token lists for decoder-based (above) and encoder-based (below) feature vectors for [layer 0 SAE feature 16](https://www.neuronpedia.org/gemma-2b/6-res-jb/10). Note that "декабря", "ديسمبر" and "grudnia" are Russian, Arabic and Polish for December, respectively. |

Functionality 1: Generating definition trees
============================================

How it works
------------

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/cd71f1ae4a39a6e054aa2ebc6d9362461af47f1d9a6266f1.png)

initial controls for definition tree generation functionality

The user selects from a dropdown one of the four Gemma-2B SAEs available (trained on the residual stream at layers 0, 6, 10 and 12), enters a feature number between 0 and 16383 and chooses between encoder and decoder weights. This immediately produces a feature vector in the model's representation space. We're only interested in this as a *direction* in representation space at this stage, so we normalise it to unit length.

The PCA weighting option can be used to modify the direction in question using the first component in the principal component analysis of the set of all token embeddings. We replace our normalised feature vector with

$\qquad (1 - w) *  \textrm{normalised feature vector} + w  * \textrm{PCA}_1$

so that if $w = 0$ the feature vector is unaffected. The resulting vector is then normalised and scaled by the chosen scaling factor. 

If the "use token centroid offset" checkbox is left in its default (True) setting, the rescaled feature vector is added to the mean token embedding. No useful results have yet been seen without this offset.

The default value of 3.8 for the scaling factor is the approximated distance of Gemma-2B token embeddings from the mean token embedding (centroid), so by using the centroid offset and a value close to 3.8, we end up with something inhabiting a typical location for a Gemma-2B token embedding, and pointing in a direction that's directly tied to the encoder or decoder weights of the SAE feature that was selected.

The embedding of an arbitrary, little-used token is then overwritten with this 2048-d vector (actually a shape-\[2048\] tensor), and this customisation allows the forward passing through the model of prompts including this "ghost token". Prompting for a typical definition of "<ghost token>" and taking the top 5 logits at each iteration allows for the construction of a tree of definitions, encoded as a nested dictionary where each node records a token and a cumulative probability (product of probabilities of all output tokens thus far along that branch).   
  
The "cumulative probability cutoff" parameter determines below which threshold a branch on the definition tree gets terminated. Larger values therefore result in small trees. There is a point below which the tree visualisation output becomes too dense to be visually useful, but the interface allows "trimming" of trees to larger cutoff values:

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/a6bdcef8057f528ff9b5f530803271a689178a97c0a83980.png)

Examples 
---------

### Striking success: Layer 12 SAE feature 121 

This feature is activated not so much by specific words, usages or syntax as the general notion of mental images and insights, as seen in the following top activating text samples found for this feature ([from Neuronpedia](https://www.neuronpedia.org/gemma-2b/12-res-jb/121)):

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/0d083d6d577a20b3319da5cf21b5dc54c3c1484377e98c06.png)

Here's the uppermost part of the definition tree (using cumulative probability cutoff 0.00063, encoder weights, scaling factor 3.8, centroid offset, no PCA)

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/49ecacfcc3ed67ce9803dc8593b40db114c6e422a5a2772c.png)

### Less successful: Layer 6 SAE feature 17

This feature is activated by references to making short statements or brief remarks, as seen in the following top activating text samples found for this feature ([from Neuronpedia](https://www.neuronpedia.org/gemma-2b/6-res-jb/17)):

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/ae9456e20c607fea815183cb8315e862a3bc1be7fbb29a94.png)

Here's the uppermost part of the definition tree (using cumulative probability cutoff 0.00063, encoder weights, scaling factor 3.8, centroid offset, no PCA). There's clearly some limited relevance here, but adjusting the parameters, using decoder weights, etc. didn't seem to be able to improve on this:

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/c9728aafab6146cf030bb2142c7935e3b3137180296e36aa.png)

Functionality 2: Closest token lists
====================================

How it works
------------

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/257e6adf96c0b548c0bb07059539e7cc5ed978016c65dc84.png)

initial controls for the closest token lists functionality

As with functionality 1, the user chooses an SAE, a feature number and whether to use encoder or decoder weights. From these, a feature vector is immediately constructed. It will then be modified by the scaling factor and/or choices to use (or not use) token offset or first PCA direction. The resulting vector in representation space is then used to assemble a ranked list of 100 tokens which produce the smallest value of 

$\qquad(\textrm{cosine distance from feature vector})^m/(\textrm{cosine distance from token centroid})$

where the exponent $m$ is chosen by the user. This trades off the desired "cosine closeness" with the need to filter out those tokens which are cosine-closest to the centroid (and which would otherwise dominate all such lists as mentioned above).

Examples 
---------

### Striking success: Layer 0 SAE feature 0

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/111b9866c1fe57eef7e9a65236acd19f86907e54bb26952f.png)

The feature activates on the two-letter combination "de". The lists generated with various parameter choices are dominated by tokens containing "de" or some variant thereof. See [https://www.neuronpedia.org/gemma-2b/0-res-jb/0](https://www.neuronpedia.org/gemma-2b/0-res-jb/0).

### Another success: Layer 0 SAE feature 7

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/b6f87ac667170486f4f2cca60571dd0a3ef3ef886e2f4285.png)

See [https://www.neuronpedia.org/gemma-2b/0-res-jb/7](https://www.neuronpedia.org/gemma-2b/0-res-jb/7).

###   
A partial success: Layer 10 SAE feature 777

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/07b71bcc1392fd7206c0b7f0651cf6e69bafcffa6c4b7d9b.png)

The feature appear to activate on (numerical) years and ranges of years. The lists generated with various parameter choices often contain tokens like "years", "years", "decades", "decades", "historic", "classic", etc. But the lack of numerical tokens was surprising. See [https://www.neuronpedia.org/gemma-2b/10-res-jb/777](https://www.neuronpedia.org/gemma-2b/10-res-jb/777).

### Observations

When a learned SAE feature is one which seems to activate on the occurrence of a particular word or sequence of letters, the token list method is particularly effective, producing the tokens you would expect. But more nuanced features can also produce relevant lists with some parameter experimentation, as with layer 12 SAE feature 121 seen earlier (concerned with mental states):

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/856c43501257d6618409a754bb87ab3f337520a0c3b84ef3.png)

Discussion and limitations
==========================

One obvious issue here is that producing relevant trees or lists often involves some experimentation with parameters. This makes their use in automated feature interpretation far from straightforward. Possibly generating trees and lists for a range of parameter values and passing the aggregated results to an LLM via API could produce useful results, but tree generation at scale could be quite time-consuming and compute-intensive.

It's not clear why (for example) some features require larger scaling factors to produce relevant trees and/or lists, or why some are more tractable with decoder-based feature vectors than encoder-based feature vectors. This may seem a weakness of the approach, but it may also point to some interesting new questions concerning feature taxonomy.

Future directions
=================

There is significant scope for enhancing and extending this project. Some possibilities include:

**Adaptation to other models**: The code could be easily generalised to work with SAEs trained on other LLMs.  
**Improved control integration**: Merging common controls between the two functionalities for streamlined interaction.  
**Enhanced base prompt customisation**: Allowing customisation of the base prompt for the "ghost token" definitions (currently '''A typical definition of "<ghost token>" would be''').  
**Expanded PCA and linear combination capabilities**: Exploring the impacts of multiple PCA components and linear combinations of encoder- and decoder-based feature vectors.  
**API integration for feature interpretation**: Exporting outputs for further interpretation to LLMs via API, enabling automated analysis.  
**Feature taxonomy and parameter analysis**: Classifying features according to the efficacy of these two tools in capturing the types of text samples they activate on, as well as the typical parameter settings needed to produce relevant effective outputs.

I'm most excited about the following, probably the next direction to be explored:  
  
**Steering vector and clamping applications**: Leveraging feature vectors as "steering vectors" and/or clamping the relevant feature at a high activation in the tree generation functionality to direct the interpretive process, almost certainly enhancing the relevance of the generated trees.

Appendix: Further successful examples
=====================================

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/864a2970ecd99a7317d57f8de1c7bea5fda1385cf3f223ca.png)

see [https://www.neuronpedia.org/gemma-2b/12-res-jb/188](https://www.neuronpedia.org/gemma-2b/12-res-jb/188)

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/d4e7d3552b418e7a6c06b6b6a5503238a1318e6dfdfbc823.png)

see [https://www.neuronpedia.org/gemma-2b/6-res-jb/188](https://www.neuronpedia.org/gemma-2b/6-res-jb/188)

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/d101a011d911d98c43bf8e0b480a72c8aa0391cff799d0ab.png)

see [https://www.neuronpedia.org/gemma-2b/6-res-jb/188](https://www.neuronpedia.org/gemma-2b/6-res-jb/188)

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/3efcdb48474334cd2c2b5c46bf1f2a63e9642d1011704ad1.png)

see [https://www.neuronpedia.org/gemma-2b/6-res-jb/676](https://www.neuronpedia.org/gemma-2b/6-res-jb/676)

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/6d94e3099bb92d78500159173d20ce7a85df9f05b1aa5c48.png)

see [https://www.neuronpedia.org/gemma-2b/6-res-jb/6098](https://www.neuronpedia.org/gemma-2b/6-res-jb/6098)

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/7217e6785e3bd1bee68b68abc2d434f1d7215f2bb500c7ad.png)

see [https://www.neuronpedia.org/gemma-2b/6-res-jb/28](https://www.neuronpedia.org/gemma-2b/6-res-jb/28)

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/4d5a166a662f73dbab0510b53b69ad78b4a4a01f775d7759.png)

see [https://www.neuronpedia.org/gemma-2b/12-res-jb/7777](https://www.neuronpedia.org/gemma-2b/12-res-jb/7777)

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/2ceb734d8e99dec5a097a5d2fad47c63a95ad302b447e0e7.png)

see [https://www.neuronpedia.org/gemma-2b/6-res-jb/10](https://www.neuronpedia.org/gemma-2b/6-res-jb/10)

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/efee8b5379c07e116a40010590f12deb4f178a383bc7ec17.png)

see [https://www.neuronpedia.org/gemma-2b/12-res-jb/7877](https://www.neuronpedia.org/gemma-2b/12-res-jb/7877)

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/150644e9a88f14c47506ad0360a637eb922f55092d6ce6e3.png)

see see [https://www.neuronpedia.org/gemma-2b/6-res-jb/7](https://www.neuronpedia.org/gemma-2b/6-res-jb/7)

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/893bf268a01b3dffb26ab62467c43ba62910c1394068343f.png)

see [https://www.neuronpedia.org/gemma-2b/6-res-jb/33](https://www.neuronpedia.org/gemma-2b/6-res-jb/33)

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/e17fe94f69f6b50386799a4e3aa692bc1b3668c34f78e176.png)

see [https://www.neuronpedia.org/gemma-2b/6-res-jb/47](https://www.neuronpedia.org/gemma-2b/6-res-jb/47)

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/6b82d26d5f8081d02e565fb3b25d21e959d3828404d01216.png)

see [https://www.neuronpedia.org/gemma-2b/6-res-jb/55](https://www.neuronpedia.org/gemma-2b/6-res-jb/55)

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/589da711e5e56d1307c9cee4485f12bdad6d22fdcb01faf4.png)

see [https://www.neuronpedia.org/gemma-2b/6-res-jb/69](https://www.neuronpedia.org/gemma-2b/6-res-jb/69)