# Intervening in the Residual Stream

*This post describes work done at SERI-MATS with mentorship from Neel Nanda. This work has benefitted from discussions with Neel Nanda, Stefan Heimersheim, Kajetan Janiak, Wes Gurnee, Marius Hobbhahn, Aryan Bhatt, Mykyta Baliesnyi, and Laura Vaughn.*

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/419fa4ba54cb3a890c383f38369fa2d3708af0ce99bad713.jpeg)

Executive Summary
=================

*   We demonstrate an interpretable direction in the residual stream of GPT2-small related to the IOI task first studied in [Redwood Research's "Interpretability in the Wild" paper](https://arxiv.org/abs/2211.00593). This direction encodes whether a repeated name is equal to the previous name (ABB), or the one before that (BAB).![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/d3753b9b7ff5cd87397cc69990e7b01f162c16db3d0c3a98.png)
*   This direction is present in the intermediate layers of the transformer, and does not seem to have any relation to the logits of the current token. Most previous work on interpreting the residual stream has focused on using the unembedding directions (the "[logit lens](https://www.lesswrong.com/posts/AcKRB8wDpdaN6v6ru/interpreting-gpt-the-logit-lens)") to study the effect of the residual stream on the logits of the current token.
*   We show that intervening in the model using this direction results in a number of downstream changes consistent with our claims and also with Redwood Research's account of the IOI task. In particular, GPT2 usually completes ABB to ABBA and BAB to BABA; we cause these to flip to ABBB or BABB, respectively.

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

The black line is the decision boundary; our intervention flips the output of GPT2-small from ABBA/BABA to ABBB/BABB

*   As part of this, we develop a general technique (Logistic Regression with Quadratic Programming, or LRQP) for intervening in model activations that seems useful for other interpretability work. Whenever information can be reliably recovered via linear probe, LRQP allows us to intervene in the activations. (This intervention will only be successful in disrupting performance of a particular activity if we have correctly identified where the relevant information is being passed through the network.)

The Indirect Object Identification task
=======================================

We study the IOI task, which was first studied in [this paper](https://arxiv.org/abs/2211.00593) by Redwood Research. To briefly recap, the IOI task is to complete a prompt like the following:

Yesterday, John and Mary went to the store. John bought a drink for _____

with the name "Mary". (Well, actually " Mary", due to tokenization quirks.)

This task is reliably completable by GPT2-small, a 12-layer, 12-heads-per-layer transformer with hidden size 768.

In this post, we actually study a slightly more messy version of this task. The messier version of the task is **harder to work with** than the clean version, and we think it is a more convincing demonstration of the points we wish to make. Specifically, the closer to realistically distributed text our prompts are, the more faith we have that our claims are relevant to the actual behavior of language models. **All claims we make are also true of the base task.**

The messier version is where we prefix the IOI prompts with a sentence from the [openwebtext-10k](https://huggingface.co/datasets/stas/openwebtext-10k) dataset. (These sentences are chosen to contain no names, and to not be more than 50 tokens, but are otherwise random.) It turns out that this does not hurt GPT2-small's ability to perform IOI at all. (But it did break a number of claims that we thought were true before doing this.) Some example prompts:

    The airport is small so it is not hard to find this area, just look for the signs. Then, Kristen and Joshua had a long argument. Afterwards Kristen said to _____

    He realizes that some people believe it is their patriotic duty to show their support for the president and that others are just so tired of the chaos of the campaign that they are desperate to make things seem normal again. Afterwards, Paul and Patrick went to the hospital. Paul gave a bone to _____

Some useful definitions related to IOI
--------------------------------------

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

*   IO is the name that is supposed to be given as an answer. (It is called IO because the name is the indirect object of the last clause.) IO occurs once in the prompt, if you don't count the last token where it is supposed to be the answer.
*   S is the other name, which is repeated twice in the prompt. We call the first occurrence of S, S1, and the second occurence of S, S2.
*   END is the last token, the one right before IO should be predicted
*   We call the prompt an ABBA prompt if IO occurs before S1, and a BABA prompt if S1 occurs before IO.
*   The logit diff is the difference between the logit assigned to IO at the last token and the logit assigned to S at the last token; higher values of the logit diff mean that the network is more certain that the answer is IO as opposed to S. Note that the logit diff is equal to the difference of the log probabilities, or equivalently, is the log of the ratio of the probabilities.

Summary of IOI Circuit Identified by Redwood Research Paper
===========================================================

You can skip this section if you are familiar with the IOI paper.

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

Figure and caption from "Interpretability in the Wild"

To give a very brief and coarse summary of the figure above, there are three classes of heads, the early heads, the S-inhibition heads, and the Name Mover heads. The early heads identify that the S token is repeated at the position of S2, and write this information into the residual stream at the position of S2. The S-inhibition heads pay attention to this value from position of END, and write a value into the residual stream at the position of END that causes the Name Mover heads to pay attention to IO instead of S. The Name Mover heads finish the circuit by reading from the residual stream at the position of IO, which still contains information from the embedding layer about the actual token value at iopos, and writing into the residual stream at the position of END, where it increases the logit of IO when unembedded.

Visualizing Activations
=======================

A surprising fact is that ABBA and BABA examples look very different when probed at the position of S2 for residual stream activations in the middle of the network. In order to visualize these high(=768)-dimensional distributions, we first subtract the mean and then perform the singular value decomposition, and then show the projection of the vectors onto the most significant two dimensions.

Here we show the results at L6.mid, which is after attention layer 6 but before MLP layer 6. ABBA prompts are yellow, while BABA prompts are purple. One can see a very clean, perfect division of the two classes, despite the projection procedure being defined without reference to the two classes. We describe this state of affairs by saying that "ABBA vs. BABA is the primary thing represented by the activations at this point."

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

Here are the corresponding plots for all layers. (All are measured at the position of S2.) The interesting thing is that the distinction is not the primary thing represented until L5.mid, becomes the primary thing represented for several layers, and then stops at L8.mid. My gloss on this is that the middle of th e network is optimized to provide information to later tokens, whereas the later parts of the network start to optimize more for predicting the current token.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Probing
=======

Probing is a method that has been used extensively to test for the presence of specific information in the intermediate activations of a network. Briefly, one builds a supervised classifier that takes as input the intermediate activations at a specific point in the computation of the network, and tries to predict some specific known variable. 

Some rough caveats from the extant literature:

*   One probably wants to use a low-capacity supervised classifier as a probe, because high capacity probes can often extract far too much information. (In the limit, one can imagine using the rest of the model as a probe - of course it will know all sorts of things!)
*   There is a distinction between what can be recovered by a probe and what is actually being used by the network; the latter requires interventions in order to be verified.

In this work, we investigated probing for a number of variables, but found that ABBA vs. BABA was the easiest to probe for. (Some variables like the position of S1 and the position of IO can be recovered via probe in the setting without openwebtext prefixes, but not in the setting with openwebtext prefixes.)

To limit the capacity of the probes, we considered only [logistic regression](https://en.wikipedia.org/wiki/Logistic_regression) models, as [implemented in scikit-learn](https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression).

As would be expected from the above plots, logistic regression is basically a perfect classifier between layers 5 and 8. What is perhaps unexpected is that logistic regression is able to classify ABBA vs. BABA reliably at pretty much all layers after the first attention layer (at the position of S2). We summarize the situation as follows:

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

Intervening with Logistic Regression and Quadratic Programming (LRQP)
=====================================================================

As mentioned in the "Probing" section, there is a distinction between something that is knowable from the activations, and something that the network itself is using to perform its computation. To demonstrate that this phenomenon is a case of the latter, we conducted a number of interventional experiments.

We first give some intuition about the mathematical tools we are using: logistic regression and quadratic programming.

Logistic Regression
-------------------

Our main technique for probing is [logistic regression](https://en.wikipedia.org/wiki/Logistic_regression). In the multinomial setting (more than two classes), one common approach is to minimize the cross-entropy loss under the following model, which is just a softmax over linear functions, one per class:

$$
\mathbb{P}(Y=k)=\frac{e^{\beta_{k}\cdot \mathbf {X}}}{\sum_{j=1}^{K}e^{{{\beta }}_{j}\cdot \mathbf {X}}}
$$

Quadratic Programming
---------------------

Our main technique for intervening is using [quadratic programming](https://en.wikipedia.org/wiki/Quadratic_programming). A quadratic program is an optimization problem of the following form:

$$
\min_x x^\top Q x + c^\top x
$$

subject to linear inequality constraints of the form

$$
 A {x} \preceq {b}
$$

where $y \preceq z$ for two vectors $y$ and $z$ means that $y$ is less than or equal to $z$ in all coordinates. We will now give a little bit of intuition about how quadratic programming works.

The level sets of the objective function are ellipsoids in $\mathbb{R}^n$ (where $n$ is the dimension of $x$). The linear constraints, meanwhile, form a simplex of feasible points. In two dimensions, there are basically three cases: either the optimal point is at a vertex of the feasible simplex; or the optimal point is on one of the edges of the feasible simplex and the line is tangent to the ellipse at that point; or the global minimum of the objective function lies inside the feasible simplex. Here are some pictures, stolen from [here](https://optimization.cbe.cornell.edu/index.php?title=Quadratic_programming), [here](https://ecal.berkeley.edu/files/ce191/CH02-QuadraticProgramming.pdf), and [here](http://hua-zhou.github.io/teaching/biostatm280-2019spring/slides/26-qp/qp.png):

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/9f6d9b0982d052f16a19018f7fcc78b090a521555a82c0fa.jpg)

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

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

In $n$ dimensions, things are a little bit more complex, because the optimal point can lie at the boundary of $k$ constraints for any value of $k$ between $0$ and $n$.

Surgical Interventions via LRQP
-------------------------------

When intervening in an activation, we would like to be "surgical", that is, we would like to change as little as possible about the activation, while still having the desired effect. In this work we operationalize this as follows:

$$
\min_x \left(x - x_0\right)^\top \Sigma^{-1} \left(x - x_0\right)
$$

(where $x_0$ is the initial value of the activations and $\Sigma^{-1}$ is the inverse covariance of the activations) subject to

$$
\mathbb{P}\left[v=k | x\right] \ge \alpha \cdot \mathbb{P} \left[v=\ell | x\right] \quad\mathrm{for }  \ell \ne k
$$

where $\mathbb{P}$ is the probability under the probe, $v$ is the variable being probed for, and $k$ is the value we want the variable to have. (Note that, when intervening counterfactually, $k$ will be different from the true value of $v$.) Here $\alpha$ is an arbitrary value specifying how certain we want the probe to be that the new value has the desired probe behavior; we use $\alpha=\max\left(\frac{\mathbb{P}[v = v|x]}{\mathbb{P}[v=k|x]}, e^{10}\right)$ in the following plots.

The nice thing about this formulation is that, in the case of logistic regression, the constraints become linear constraints. This is because the messy denominator of the softmax function is common to both sides of the constraint and can be canceled out. Since the thing we are maximizing is a (multidimensional) quadratic function, this can thus be solved by Quadratic Programming libraries. We use the ~~proxqp~~ osqp solver in the [qpsolvers](https://scaron.info/doc/qpsolvers/quadratic-programming.html) python package in what follows.

Results
=======

Here, we show a histogram of the logit differences for various interventions. In the following plot, the blue histogram is the clean logit difference (i.e., the logit difference of the original network without any changes); the green histogram is what we get when we intervene to set the ABBA vs. BABA variable to its true value; and the red histogram is what we get when we set the ABBA vs BABA variable to the opposite value. As expected, the blue and green histograms are very close together, and mostly to the right of the decision boundary, while the red histogram is mostly to the left of the decision boundary, indicating that we have (usually) flipped the answer given by the network by intervening.

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

Here is another plot, where the x coordinate of each point is the clean (unmodified) logit difference of the given prompt, and the y-coordinate is the intervened-upon logit difference. Blue is the control (setting ABBA vs. BABA to its true value) and green is the counterfactual (setting ABBA vs. BABA to the opposite of its true value). One interesting thing to note is that the control points mostly stay fairly close to the line $y=x$, indicating that our control intervention doesn't change the logit difference all that much for most prompts.

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

Zooming in more, we can ask how this intervention interacts with the various parts of the IOI circuit identified by Redwood Research in their paper. When we intervene at L6.mid or L7.pre, we are after the "early" heads of the IOI circuit (induction heads, duplicate token heads, previous token heads) and before the S-inhibition heads and the name-mover heads.

Briefly, we find that:

Probing the output of the S-inhibition heads shows that they almost always report the same belief that we have injected into the residual stream. Here are plots for interventions at L6.mid:

*   ![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/cac5d831a1ebb175b86ed477a08981413d47cee6a4ebdf21.png)
*   ![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/2ab8bc6e0482adebc73728cf13206969360e0917cc541b15.png)
*   ![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/7eeeeb57c38588e14523f40f3b7d32265ea29917e0acaf90.png)
*   ![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/d23054ddb279e8f2ac334fe83d3811f58741a68f9ec34bea.png)

Counterfactual intervention changes the attention patterns of the name movers (specifically the Name Movers and the Negative Name Movers); in the clean setting or with the control intervention, they pay attention mostly to position iopos, while in the counterfactual intervention they pay attention mostly to position s1pos. Here are plots for intervening at L6.mid:

*   ![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/41ee0ecd9b85ef83db1c00989bf9f18ec839376241989bb3.png)
*   ![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/96404e34302eccf631a6b5f66cb7713959e722de3a0b1db7.png)
*   ![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/7dbd5c42204f6055027ef68dfd45fa4e2d5cde030f968ebc.png)
*   ![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/682b1edbf34c90315ff5de37e9c6b335e61044153084a60a.png)
*   ![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/ef492bf46a28259cac906df398a7b60133e12a4b5e9d9453.png)

For the Backup Name Movers, this behavior is not so clean, and occasionally the pattern goes the other way. From what I can tell from the Redwood paper, the Backup Name Movers only really kick in when the Name Movers are ablated (which we are not doing here), so this doesn't seem like it really changes the story much. Here are the plots for intervening at L6.mid:

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

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

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

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

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

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

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

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

Conclusion and Open Questions
=============================

We demonstrate that, in one very specific data distribution, there is an interpretable direction in the residual stream. Several works have previously found interpretable directions late in the network using the logit lens, but being able to find interpretable directions in the middle of the network, without using the logit lens, is to our knowledge a novel contribution.

We demonstrate that we can intervene in the residual stream to change downstream behavior using the LRQP method. This method seems like it could plausibly work for a number of similar questions. (Really, the only remaining work at this point is finding distributions of interest for which we know the most relevant variables to probe.)

We provide some evidence to support and (slightly) flesh out the claims made in "Interpretability in the Wild". An interesting future direction would be to intervene with LRQP and then also intervene in the attention patterns of the name movers; this would, if successful (i.e., if flipping the attention patterns flips the answer back to its original value), demonstrate that this effect is implemented primarily via changing the attention patterns of the name movers.

Edit History
============

Edited 12:36 AM Feb 22 2023: redid some plots after switching from the proxqp solver to the osqp solver, which AFAICT is just a better solver.

Edited 8:39 PM Feb 27 2023: edited for readability and better explanations