All courses

The residual stream as a shared channel

Explain that every sublayer reads from and adds into one d-dimensional stream, so the final state is the embedding plus all sublayer outputs. Explain how later components can use what earlier ones wrote.

Builds on: The pre-norm transformer block, Near-orthogonality in high dimensions

▶ Watch this lesson Start the whole course

What the animation says

No wire from block 3 to block 25

Block twenty-five of a thirty-two block model needs something that block three's attention figured out. But there is no wire between them. Each sublayer only sees the one vector it is handed at its position. So when block four runs, what happens to block three's result?

One shared vector everyone writes into

Block four adds. It never overwrites. Each token position owns one vector of d numbers, called the residual stream. Every sublayer reads the stream through a norm, computes, and adds its result back. So a write stays in the sum, unless a later sublayer adds its exact opposite. Any later reader at that position sees every earlier write. Here are three positions side by side. Tap the sublayer that can bring in information from another position.

Writing and reading in four numbers

Let's make it concrete, with just four numbers. The embedding starts as one, zero, zero, zero. Attention one adds two in the second slot. Then the FFN adds three in the last slot. The stream is now one, two, zero, three. A later head reads the second direction with a dot product and gets two, exactly attention one's write. Reading the first direction still gives the embedding's one. Now a later sublayer adds minus two in the second slot. Read the second direction again: what comes out?

Unrolling the additions

Zero. Adding the opposite is the only way to erase a write. Each block makes two writes: attention's output, then the FFN's output on the updated stream. Unroll all the blocks, and the final stream is the embedding, plus every attention write, plus every FFN write. A later reader applies a norm, then a matrix, like the query weights. The norm only divides by one number and applies a fixed linear map. So the read is still a sum, with one piece per earlier write.

Many writes, one space — and a check

Real streams have thousands of dimensions, and writes aren't on neat axes. Random directions in high dimensions are nearly orthogonal, so a reader picks up only about one over root d of each other write. So many writes share the same space with little crosstalk. Here's a check in NumPy. Stand-in sublayers read the normalized stream and record their writes. At the end it prints True: final equals embedding plus all writes. Tap the line that makes that true.

How does layer 25 hear what layer 3 said?

Block 25 of a 32-block model needs something that block 3's attention figured out. There is no dedicated wire between them: each sublayer only sees the vector $r$ it is handed. So how does information travel across many layers? And what exactly is the model's final state at a position made of?

One shared vector everyone writes into

Each token position owns one $d$-dimensional vector, the **residual stream**. Every sublayer reads it (through Norm), computes, and **adds** its result back. Nothing is overwritten. A write stays in the sum unless a later sublayer adds its opposite. So the stream is a shared channel: any later reader at that position sees every earlier write. Only attention moves information between positions. FFNs work within one position.

Unrolling the additions

Block $\ell$ makes two writes: $a^{(\ell)}=\text{Attn}(\text{Norm}(r^{(\ell-1)}))$, then $m^{(\ell)}=\text{FFN}(\text{Norm}(r^{(\ell-1)}+a^{(\ell)}))$. Unrolling all $L$ blocks gives $$r^{(L)} = r^{(0)} + \sum_{\ell=1}^{L}\big(a^{(\ell)} + m^{(\ell)}\big)$$ A later reader computes $\text{Norm}(r)\,W$ (e.g. $W=W_Q$). Norm divides the row by one number and then applies a fixed linear map (the gain, plus mean-centering for LayerNorm). So the read is still a sum of $r^{(0)}$'s part plus each earlier write's part, all scaled by that one number.

Writing and reading in d = 4

Start with $r^{(0)}=[1,0,0,0]$. Attn₁ writes $[0,2,0,0]$ and FFN₁ writes $[0,0,0,3]$. The stream is now $[1,2,0,3]$. A later head reads direction $u=[0,1,0,0]$: $[1,2,0,3]\cdot u=2$. That is exactly Attn₁'s write. Reading $[1,0,0,0]$ still gives the embedding's 1. To erase Attn₁'s write, a later sublayer adds $[0,-2,0,0]$. In large $d$, random-like directions are nearly orthogonal, so a reader picks up only about $\pm1/\sqrt d$ per unit of each other write: small crosstalk.

Check: final state = embedding + all writes

This uses stand-in sublayers that each read the normalized stream and add an output. The final stream equals the starting embedding plus the sum of every write.
import numpy as np
rng = np.random.default_rng(0)
T, d, L = 3, 8, 4
norm = lambda r: r / np.sqrt((r**2).mean(-1, keepdims=True) + 1e-6)
Ws = [rng.normal(size=(d, d)) / np.sqrt(d) for _ in range(2 * L)]
r0 = rng.normal(size=(T, d)); r = r0.copy(); writes = []
for W in Ws:                       # stand-ins for Attn/FFN sublayers
    out = np.tanh(norm(r) @ W)     # read the whole stream
    writes.append(out)
    r = r + out                    # add, never overwrite
print(np.allclose(r, r0 + sum(writes)))  # True

Remember this

Every sublayer reads the whole stream and adds into it. So the final state is the embedding plus every sublayer's output. Any later component at the same position can pick up an earlier write with a linear projection, unless someone cancelled it. Other positions' writes arrive only through attention. Nearly-orthogonal directions let many writes share the same $d$ dimensions with little crosstalk.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.