Interactive transformer architecture laboratory

A transformer layer writes an update into a shared residual stream.

Attention moves information between token positions. The MLP transforms features within each position. Residual connections preserve the existing state while both sublayers contribute updates that later layers can read.

Attention communicates across positionsMLPs transform local featuresResiduals preserve and accumulateNormalization controls scaleLayers refine a shared representationAttention communicates across positionsMLPs transform local featuresResiduals preserve and accumulateNormalization controls scaleLayers refine a shared representation

Watch two sublayers write into one vector stream

Blue bars are the residual vector. Cyan bars are the attention update, pink bars are the MLP update, and yellow gates mark normalization.

residualattention updateMLP updatenormalization
14
local55%
small65%
0.10.70
Current layer0 / 4
Residual norm1.00
Cosine drift0.00
Current write0.00
residual = [...]

The residual stream is shared working memory for every transformer block.

The update pattern

A residual connection adds a sublayer output to the state that entered it. The sublayer can contribute a correction without reconstructing the full representation. Later attention heads and MLPs read the accumulated stream and add their own updates.

x' = x + attention(norm(x))
x'' = x' + MLP(norm(x'))
streamnormattentionaddMLPadd

Why residuals help

The identity path carries information and gradients around each nonlinear transformation. A sublayer can learn a small useful update while the original signal remains available.

Attention writes context

Queries select weighted combinations of value vectors from other positions. The resulting update can copy, route, or combine contextual information.

MLPs write features

The feed-forward network expands, activates, and projects the current position. It can detect combinations of features and write a transformed result back.

Normalization sets scale

LayerNorm or RMSNorm standardizes the input seen by a sublayer. Placement changes optimization behavior and the scale trajectory of the residual stream.

Read the block as data movement

Residual stream

A vector per token position that persists through the network. It is not a single human-readable concept; many features can be represented across directions and magnitudes.

Persistent state

Self-attention

Builds each token's update from other positions. Multiple heads can implement different routing patterns, but their outputs are combined before the residual addition.

Across positions

Feed-forward network

Applies the same learned nonlinear transformation independently at each position. Expansion provides a larger intermediate feature space before projection back to model width.

Within position

Normalization

Pre-norm feeds normalized state into each sublayer while preserving an unnormalized identity stream. Post-norm normalizes after residual addition. Their training dynamics differ.

Scale control
One block, four transitions

Separate reading, computing, writing, and preserving.

Many architecture diagrams hide the residual addition inside arrows. Stepping through the state makes clear which operation changes the stream and which merely prepares an input.

Normalize the reader

In a pre-norm block, normalization creates the attention input without replacing the residual stream itself.

attention_input = norm(residual)

Compute contextual update

Attention reads all relevant token positions and produces one update vector for this position. The update is not yet the new token state.

attention_write = attention(attention_input)

Add, then transform locally

The attention write joins the stream. A second normalized read enters the MLP, whose nonlinear feature update is added in turn.

residual += attention_write
residual += mlp(norm(residual))

Pass the stream onward

The next block receives the accumulated vector. With enough layers, update scale and normalization determine whether features remain stable, drift, or dominate.

Common questions

Is the residual stream the same as model memory?

It is temporary activation state for the current sequence, not durable memory. KV cache stores past attention keys and values; external agent memory lives outside the model.

Does each attention head add separately?

Head outputs are concatenated or combined, projected through an output matrix, and then the resulting vector is added to the residual stream.

Why can cosine drift be small while the model changes behavior?

A small update in a behaviorally important direction can matter even when the overall vector direction changes little. Global geometric summaries cannot identify every feature.

Why compare pre-norm and post-norm?

They place normalization on different paths. Pre-norm often improves gradient flow in deep transformers, while post-norm changes the scale and representation after each addition.

Inspect the state that every agent decision depends on.

Explore SuperPowers workflows that turn explicit goals into visible agent actions, from tool use and computer state to complete website generation.

Explore SuperPowersWebsite-building agentComputer-use cache