Inferensys

Glossary

Residual Stream

The central information highway in a transformer architecture where each layer reads from and writes its output back to a shared, accumulating state vector.
Engineer reviewing vector database search results on laptop, embeddings visualization on screen, home office coding session.
TRANSFORMER ARCHITECTURE

What is Residual Stream?

The residual stream is the central information highway in a transformer model, serving as a shared, accumulating state vector that each layer reads from and writes its output back to.

The residual stream is the core communication channel in a transformer architecture, implemented as a vector of a fixed dimension that persists across all layers. Each sub-layer—whether an attention head or an MLP block—reads its input directly from this stream via a residual connection, performs its computation, and then adds its output back into the same stream. This additive update mechanism allows the model to preserve information from earlier layers while incrementally refining the representation.

Mechanistically, the residual stream acts as an accumulating memory that prevents the vanishing gradient problem in deep networks. Because each layer's contribution is summed rather than replacing the previous state, the model learns to compute deltas or corrections to the existing representation. Techniques like the logit lens and tuned lens exploit this property by applying the unembedding matrix directly to intermediate residual stream states, revealing how the model's next-token prediction converges layer by layer.

ARCHITECTURAL FOUNDATIONS

Key Properties of the Residual Stream

The residual stream is the central information highway in a transformer, accumulating outputs from each layer. Understanding its properties is essential for mechanistic interpretability.

01

Linear Accumulation

The residual stream operates on a simple principle: each sub-layer's output is added to the existing stream. This means the final representation is a linear sum of contributions from every attention head and MLP layer.

  • Enables Direct Logit Attribution: the final logit can be decomposed into a sum of independent, layer-wise contributions.
  • Preserves information flow, allowing gradients to propagate directly to early layers.
02

Shared Communication Bus

Unlike feedforward networks where layers process sequentially, the residual stream acts as a shared memory or communication bus. Every component reads its input from and writes its output to this single, expanding vector.

  • Attention heads write vectors that move information between token positions.
  • MLP layers write vectors that process information at the current position.
  • This read/write architecture is the basis for circuits analysis.
03

Expanding Residual Dimension

In many architectures, the residual stream's dimension (d_model) is larger than the output dimension of individual attention heads (d_head). This expansion creates a high-dimensional space where:

  • Features can be stored in superposition, representing more concepts than dimensions.
  • Sparse Autoencoders are used to decompose this dense, polysemantic stream into interpretable, monosemantic feature directions.
04

The Logit Lens

Because the residual stream accumulates the model's current prediction state, applying the unembedding matrix directly to intermediate stream states (the Logit Lens) reveals the model's evolving next-token predictions.

  • Early layers often show nonsensical predictions.
  • Middle layers refine syntactic structure.
  • Final layers converge on the output distribution.
  • The Tuned Lens improves this with learned affine transformations per layer.
05

Causal Intervention Target

The residual stream is the primary target for causal techniques that isolate circuit function.

  • Activation Patching: Replacing a stream value at a specific layer and token position with one from a corrupted run to measure its impact on the output.
  • Path Patching: Isolating the direct effect of a specific computational path (e.g., Head A -> Head B) by patching the stream along that path while freezing others.
06

Steering Vectors

The linear nature of the residual stream allows for direct behavioral modification via Activation Engineering. By adding a pre-computed steering vector to the stream during a forward pass, a model's behavior can be controlled without prompt engineering.

  • A 'happy' vector added to the stream biases the model toward positive sentiment.
  • Function Vectors are a specific type of steering vector extracted from in-context learning examples that can trigger a task.
TERMINOLOGICAL PRECISION

Residual Stream vs. Hidden State vs. Activations

Clarifying the distinct architectural roles of these frequently conflated terms in transformer models.

FeatureResidual StreamHidden StateActivations

Primary Definition

The central, accumulating data highway where each sub-layer reads from and writes its output back to via residual connections.

The output of a specific layer or block, often used interchangeably with the residual stream state at a given depth.

The output values of any neural network layer after applying a non-linear activation function.

Scope

Model-wide, persistent across all layers.

Layer-specific, a snapshot at depth L.

Component-specific, the output of a single function.

Mathematical Role

Sum of all previous layer outputs and the initial token embedding: x_0 + Σ layer_i(x).

The state vector x_L at a specific layer L, serving as input to the next sub-layer.

The result of σ(W·x + b), where σ is a non-linearity like GELU or ReLU.

Persistence

Persistent. It accumulates information additively from layer 0 to layer N.

Transient. It is the stream's value at a single point in the computation graph.

Transient. Exists only as the immediate output of a weight matrix operation.

Analogy

A legal document being drafted, where each lawyer adds clauses without erasing previous ones.

The state of the document after a specific lawyer's edit.

The rough notes a lawyer makes before formalizing a clause.

Causal Intervention Target

Patching the stream at layer L affects all subsequent layers' computations.

Patching the output of layer L isolates that layer's direct contribution.

Patching activations inside an MLP tests the function of a specific neuron or feature.

Decoding Technique

Logit Lens or Tuned Lens applied directly to the stream to read intermediate predictions.

Probing classifiers trained on the hidden state at layer L to detect encoded concepts.

Feature visualization or Sparse Autoencoders applied to decompose polysemantic neurons.

Typical Dimensionality

d_model (e.g., 4096 for Llama-2-7B, 8192 for Llama-3-70B).

d_model, identical to the residual stream dimension.

d_ff (e.g., 11008 for Llama-2-7B), typically 2.5x to 4x larger than d_model.

RESIDUAL STREAM

Frequently Asked Questions

Clear, technically precise answers to the most common questions about the transformer architecture's central information highway.

The residual stream is the primary, additive information highway in a transformer architecture. It is a vector of a fixed dimension d_model that is initialized as the sum of the token embedding and positional encoding. At each layer, the attention and MLP sub-layers read from this stream, perform their computations, and write their output additively back into it. This means the stream's state is not overwritten but continuously accumulated, allowing each layer to inject new information while preserving all previous computations. The final state of the residual stream after the last layer is passed through an unembedding matrix to produce the logits for next-token prediction. This design, inspired by residual connections from computer vision, is the fundamental reason transformers can be scaled to hundreds of layers without degradation.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.