AI Glossary / Natural language and speech / Advanced
Residual stream
Definition
The residual stream is the sequence of vector representations passed through a transformer's residual connections. At each token position, attention and feedforward components contribute updates to the running representation. In a common pre-normalization architecture, each update is added to the existing vector, allowing information from earlier stages to remain available to later computation.
What it means
A transformer does not simply replace a token's representation with an unrelated vector at every step. Residual additions provide a path that carries the current representation forward while learned components modify it. The stream is an activation produced during processing, rather than a separate database or a list of stored sentences.
Each token position has a representation of the model's fixed hidden width. Attention allows information from permitted positions to influence another position's update. The feedforward component processes each position's current representation using parameters shared across positions.
The word stream describes progression through network depth. It does not mean that the model is necessarily receiving live data or generating visible text continuously. The vectors can be examined even when a complete input sequence is processed together.
How it works
For a simplified pre-normalization block, write the incoming representation as x. The attention branch reads normalized representations and produces an update a. The intermediate residual is y=x+a. A feedforward branch reads a normalized version of y and produces update m. The block's outgoing residual is z=y+m.
The additions require matching dimensions. Internal attention and feedforward calculations can use other widths, but their outputs must fit the representation they update. The components producing a and m are not generally linear functions of the input just because the final combination uses addition.
Architecture details matter. The original transformer placed layer normalization after residual additions, while a pre-normalization arrangement places it before the branch computation. A simple sum-of-updates account should therefore identify which arrangement and measurement points it describes.
A final language-model readout converts the processed representation into vocabulary scores, with any normalization required by that model. Those scores can then be converted to probabilities. The residual vector itself is neither a probability distribution nor a guaranteed human-readable explanation.
Three practical examples
- A researcher records representations before and after a block to inspect how a controlled wording change affects processing.
- An engineer checks that a replacement component returns the correct hidden width before adding its output to the residual path.
- An interpretability experiment changes a selected activation and measures resulting outputs, distinguishing observed changes from claims about a complete explanation.
Think of it this way
Think of a running numerical worksheet that several specialist calculations update. Each calculation reads the available values and contributes adjustments. The worksheet preserves a route through the computation, but its columns need not correspond to familiar labels such as truth or politeness. Additions can increase, decrease, or cancel earlier values.
Worked example
Consider a hypothetical calculation at one token position with hidden width three. Let its incoming residual vector be x=[1,2,-1]. Suppose the attention branch, after all its internal computations, produces a=[0.5,-1,2]. Treat these as supplied values rather than a full simulation of attention.
The intermediate residual is y=x+a=[1.5,1,1]. Suppose the feedforward branch evaluated on its appropriate normalized input produces m=[-0.5,2,-1]. Adding this update gives z=y+m=[1,3,0]. Every vector still has three coordinates; two updates have changed values without expanding the hidden width.
The total change is z-x=[0,1,1], which equals a+m. The first coordinate receives +0.5 and then -0.5, so those contributions cancel. The third coordinate moves from -1 to 1 and then to zero. Adding an update does not mean every coordinate must grow, or that each component's earlier contribution remains separately readable from the final vector alone.
For a simplified linear readout, omit final normalization and bias, and define score A as z1+z3 and score B as z2-z3. The final scores are A=1 and B=3. Softmax gives A probability 1/(1+exp(2)), approximately 0.1192, and B probability approximately 0.8808. These toy probabilities are not calibrated confidence estimates.
The same readout applied to x gives scores [0,3]. The attention update contributes [2.5,-3], and the feedforward update contributes [-1.5,3]. Adding these three score vectors gives [1,3], matching the direct readout of z. This equality follows from the chosen linear readout and fixed supplied updates.
It does not imply that removing the attention branch from an actual network would leave m unchanged. The feedforward branch reads the intermediate state, so changing y can change its output. Later layers can change too. A bookkeeping decomposition of one forward computation therefore differs from a new intervention that reruns the model.
The coordinates have no assigned semantic meaning. Real transformers require their actual component operations.
Illustrative case study
Fictional example: VectorScope compares a model's answers to two slightly different instructions. Its team sees that a middle-layer residual representation changes and initially describes that change as the location of the model's decision.
Reviewers ask for a more limited conclusion. The team records the exact token position and layer, tests a controlled activation replacement, and measures downstream behavior across several examples. It reports the tested effect without claiming that one vector explains all decisions. This fictional scenario illustrates investigation design.
Common misconceptions and mistakes
Residual addition does not make the entire transformer linear. Updates depend on learned computations, and normalization placement changes the algebra that applies. A fixed hidden width also does not mean the representation contains a fixed list of independently readable facts.
Another mistake is confusing an activation with model parameters or permanent conversational memory. A large coordinate change is not automatically an important causal change. Conversely, a small change may matter to a later computation. Interpretations require tests appropriate to the claim.
Business use
Understanding residual streams helps teams ask precise questions about debugging and model explanations. Request the model version, layer, token position, and measurement method behind an analysis. Evaluate proposed changes using task outcomes as well as internal measurements; a visually interesting activation pattern alone does not establish better performance or reliability.
People also ask
Is the residual stream the attention matrix?
No; attention contributes to its updates.
Does adding vectors increase their width?
No, matching coordinates are added.
Is it long-term memory?
Not by itself; it is a running activation representation.
Check your understanding
Answer: What is the example's final vector? [1,3,0].
Why does its first coordinate return to one?
Answer: The two updates cancel there.
Why is branch removal different from subtracting a saved update?
Answer: Downstream computations may change.
Related terms
- Self-attention
- Unembedding
- Residual connection
- transformer block
- hidden state
- feedforward network
- layer normalization
- activation patching
Learn next
Trace the residual additions in one model architecture. Then compare a saved-update decomposition with an intervention that recomputes downstream layers.
More in Natural language and speech
- Relative frequency - Relative frequency is the number of times an outcome occurs divided by the total number of relevant observations.
- Relevance - Relevance is the property of bearing on a particular question, task, or choice of action.
- Reporting events - Reporting events are acts of communicating information, such as saying, stating, or reporting something.
- Rhetorical structure theory - Rhetorical structure theory, or RST, is a framework for describing how parts of a text contribute to its overall organization and coherence.
- Role-filler extraction - Role-filler extraction identifies the text spans or entities that occupy specified roles in an event or situation.
- Round-trip translation - Round-trip translation translates text from one language into another and then translates the result back into the original language.
Sources
Jurafsky and Martin, Speech and Language Processing, chapter 7, transformer blocks and residual streams: https://web.stanford.edu/~jurafsky/slp3/7.pdf Vaswani and colleagues, Attention Is All You Need, residual connections and normalization in the original transformer: https://arxiv.org/html/1706.03762v7
Previous: Residual Neural Network Next: Residual variance