The Transformer Block: Residual Stream, MLP, and What Each Part Learns
How attention and MLP sublayers share a residual stream, what factual knowledge each stores, and why this view changes how you debug and fine-tune LLMs.
A production system was giving wrong answers about a specific country's capital. The team added retrieval, added examples, tweaked the prompt — nothing changed. A colleague finally ran an ablation: muting the MLP neurons in layers 12–18 of the 7B model restored the correct answer. The model had an incorrect capital cached in its MLP weights from training data noise, and it was overriding the retrieved context. That's the residual stream view made concrete. If you don't know where in the architecture knowledge lives, you can't fix it systematically.
The residual stream: the thread that connects all layers
Every transformer layer in every production model from GPT-2 onward uses the same basic motif: a residual connection around each sublayer. The formal expression is x_out = x_in + sublayer(LN(x_in)). But the more useful mental model is the residual stream.
At the first block, the embedding table produces a vector of d_model dimensions — 4,096 for a 7B model, 8,192 for a 70B. That vector is the initial state of the residual stream for that token. From there it travels through every block in the network. In each block, the attention sublayer reads the current state of the stream, computes its contribution, and adds that contribution to the stream. Then the MLP sublayer does the same: read, compute, add.
Nothing gets overwritten. The stream from the first embedding is still in there at layer 80, underneath 160 additive updates. This is why the operation is called "residual" — the input is preserved as a residual, and the sublayer only computes the difference between input and output.
This architecture choice was initially motivated by training stability: skip connections let gradients flow directly from loss to early layers, solving the vanishing gradient problem that killed deep networks before 2015. But the residual stream view is more interesting than that. It means information deposited by early layers is available to all later layers through the direct path. An attention head in layer 40 can read a representation that was written by a layer-2 attention head, carried through 38 layers without being erased.
flowchart LR
EMB["Token\nembedding"] --> B1["Block 1\n+ attention delta\n+ MLP delta"]
B1 --> B2["Block 2\n+ attention delta\n+ MLP delta"]
B2 --> DOTS["..."]
DOTS --> BN["Block N\n+ attention delta\n+ MLP delta"]
BN --> HEAD["Unembedding\nhead → logits"]
style EMB fill:#00e5ff,color:#0a0a0f
style B1 fill:#0e7490,color:#fff
style B2 fill:#0e7490,color:#fff
style BN fill:#0e7490,color:#fff
style HEAD fill:#a855f7,color:#fff
The practical implication: not all layers carry equal weight. The first few layers tend to handle syntax and local context; middle layers build semantic structure; late layers specialize for the output task. But they all share the same stream, and a bug or bias deposited early propagates to the final output unless a later layer corrects it. That country-capital story above? An MLP in an early-to-mid layer had confidently written a wrong fact into the stream, and no later layer was strong enough to override it.
The attention sublayer: routing information across tokens
The attention sublayer is covered in depth in the self-attention article, so this section focuses on its role within the block structure specifically.
Attention reads the residual stream at every token position, computes how much each position should "attend" to every other position, and produces a weighted blend of Value vectors that represents "what this token should know from the context." That blended result is then added back to the stream.
What's easy to miss: attention is not deciding what the final answer is. It's deciding which parts of the context to aggregate for this position. The aggregated information gets written into the residual stream, where the MLP (and subsequent blocks) can act on it.
Multi-head attention runs this process in parallel for H different "heads" — each with its own Q, K, V projection matrices — allowing different heads to attend for different reasons simultaneously. Some heads track syntactic structure; others track coreference ("it" → "the model"). The diversity is the point. Knock out the coreference heads on a long document and pronoun resolution degrades; knock out syntactic heads and subject-verb agreement breaks.
Grouped-Query Attention (GQA), now universal in production models (Llama 3, Gemma, Qwen 2.5, Mistral), shares K and V matrices across groups of Q heads. Instead of H separate KV projections, you have H/G projections shared by G heads each. The gain is KV cache size at inference, at near-zero quality cost. At 128K context, this matters. A hypothetical full-MHA 70B model (80 layers, d_model 8192) needs 2 × 80 × 8192 × 2 bytes ≈ 2.6 MB of fp16 KV cache per token — about 340 GB at 128K. GQA with 8 KV head groups cuts that 8×, to ~43 GB. The context windows and KV cache article has the full memory math.
The MLP sublayer: the knowledge store
The MLP sublayer is often treated as the boring part — "just a feed-forward network." It is not boring. By parameter count, it's the majority of the model.
The structure is a two-stage projection with a nonlinearity:
up_proj: (d_model) → (d_ff) # expand
gate_proj: (d_model) → (d_ff) # gating signal (SwiGLU only)
activation: SiLU(gate) × up # pointwise gating
down_proj: (d_ff) → (d_model) # project back
d_ff is typically 4 × d_model for ReLU-based MLPs. SwiGLU requires a third matrix (the gate projection), so to keep total parameters roughly constant, models shrink the intermediate dimension to roughly 3.5 × d_model in practice (Llama 3 70B uses 28,672 / 8,192 ≈ 3.5). SwiGLU (used in Llama 3, Gemma, and most production models since 2023) consistently outperforms ReLU and GeLU at the same parameter count, with gains in the 5–10% range on standard benchmarks.
Critically: the MLP has no cross-token interaction. Each token's stream vector is processed independently through the same set of weights. There is no attention-style mixing; the MLP cannot "look at" other token positions. What it can do is transform the representation that the preceding attention sublayer already gathered — turning the "what I should know from context" signal into "what I should do with that knowledge."
Where factual knowledge lives
The mechanistic interpretability community has spent considerable effort on this. The current evidence is that factual associations are disproportionately stored in MLP weights. The cleanest result: ROME — Rank-One Model Editing (Meng et al., 2022) — used causal tracing to localize factual associations like "The Eiffel Tower is in Paris" to MLP modules in early-to-middle layers, at the subject's final token, and then changed the fact by editing those weight matrices directly. The neuron-level story comes from adjacent work: Geva et al. (2021) framed feed-forward layers as key-value memories, and Dai et al. (2022) identified "knowledge neurons" whose ablation degraded a specific fact while leaving unrelated facts intact.
The intuition: the MLP can be thought of as a key-value store, where the first projection (up/gate) computes keys and the second (down) returns values. Trained on enough data, it learns to store "if representation looks like {entity about Eiffel Tower}, output direction toward {Paris}." The attention sublayer has already handled the routing — which parts of context matter for this token — and the MLP does the factual lookup.
This is why fine-tuning on new factual data is unreliable for injecting knowledge. You're trying to overwrite specific neurons in the MLP key-value store across a sea of related parameters. It sometimes works, but it also corrupts adjacent associations. The reliable path for factual grounding is retrieval, as the hallucination mechanistic view covers in depth.
flowchart TD
STREAM["Residual stream\n(token i, layer L)"] --> UP["up_proj + gate_proj\n(expand to d_ff = ~4× d_model)"]
UP --> ACT["SwiGLU activation\nSiLU(gate) ⊙ up"]
ACT --> DOWN["down_proj\n(compress back to d_model)"]
DOWN --> DELTA["MLP delta\n(added to residual stream)"]
style STREAM fill:#00e5ff,color:#0a0a0f
style ACT fill:#a855f7,color:#fff
style DELTA fill:#0e7490,color:#fff
LayerNorm: the gatekeeper before each sublayer
Pre-normalization is the universal standard in production models. The rule is: normalize the residual stream before passing it to a sublayer, not after.
One naming note first: every model discussed here — Llama 3, Gemma 2, Qwen, OLMo 2 — actually uses RMSNorm, a stripped-down LayerNorm that drops mean-centering and the bias term. It's cheaper and empirically equivalent, which is why a Llama config exposes rms_norm_eps and no LayerNorm anywhere. Everything below about norm placement applies identically to both.
Post-norm (normalize after adding the residual) was the original Transformer design. It has a pathology: putting the norm on the main path produces large, ill-conditioned gradients near the output at initialization (Xiong et al., 2020), so deep post-norm models diverge unless you baby them through long learning-rate warm-up schedules.
Pre-norm inverts this. The sublayer sees a normalized input regardless of the current scale of the residual stream. The stream itself can grow with depth — and it does, roughly proportional to the square root of the number of layers under certain conditions — but that growth is contained to the stream and doesn't destabilize the sublayer's computation.
The trade-off is that pre-norm transformers can accumulate large activation magnitudes in the residual stream, causing numerical precision issues and instability in deep models. Peri-normalization addresses this directly by normalizing the sublayer output before it joins the stream, and it ships in two flavors. Gemma 2 sandwiches each sublayer — norm on the input and again on the output:
x_out = x_in + Norm_post(sublayer(Norm_pre(x_in)))
OLMo 2 goes further and drops the input norm entirely, normalizing only the output: x_out = x_in + Norm(sublayer(x_in)). Either way the cost is small — an extra (or relocated) norm op per sublayer — and the payoff is fewer loss spikes during training at depth. Expect output-normalized variants to keep spreading through major open-weight releases in 2026.
Circuits: what blocks compose into
The residual stream view leads naturally to mechanistic interpretability. If every sublayer is an additive contributor to the stream, then specific computations can be decomposed into "circuits" — subsets of attention heads and MLP neurons that together implement an identifiable algorithm.
Induction heads are the best-documented example. An induction head is a pair of attention heads across two adjacent layers that together implement in-context pattern completion. If the sequence contains A B ... A, the second A's induction head attends back to B and amplifies B as the next prediction. This is the mechanistic explanation for why few-shot examples work: the model pattern-matches via induction circuits rather than reasoning about the examples.
Copy suppression heads are another example: heads that actively reduce the probability of a token being repeated verbatim, which explains why LLMs don't simply copy their input back unchanged.
The practical value of knowing this: when a model fails at few-shot prompting in a specific format, it's sometimes because the token spacing or structure is disrupting the induction head's pattern match. When a model starts repeating itself, copy suppression may have been overwhelmed by attention sink phenomena (certain tokens accumulating disproportionate attention mass regardless of content, documented in 2026 research as contributing to context retrieval failures).
These circuits show up in real production debugging, which is the only reason to care about them here.
Pre-norm vs peri-norm: numbers on what changes
The shift from post-norm to pre-norm to peri-norm is driven by measurable training outcomes.
Training stability comparison (illustrative, order-of-magnitude):
Post-norm (original Transformer):
Max stable depth at standard LR: ~12 layers without warm-up surgery
Required LR warm-up: thousands of steps (4,000 was a typical floor)
Gradient behavior at init: large and ill-conditioned near the output —
deep post-norm runs routinely diverged
Pre-norm (GPT-3 → Llama 3):
Stable to 96+ layers at standard LR
Minimal warm-up needed
Residual stream magnitudes: grow with depth, leaving room for occasional
activation and loss spikes late in long training runs
Peri-norm (Gemma 2, OLMo 2 variants):
Stable to 96+ layers
Loss spikes during training: markedly reduced — the OLMo 2 report credits
its norm reordering (with QK-norm) for much of its improved stability
Per-block overhead: an extra or relocated norm op per sublayer
(~negligible vs block compute)
Reported quality delta vs pre-norm: roughly neutral; the win is stability
The takeaway for practitioners: peri-norm exists to keep deep-model training from blowing up, and downstream it means fewer numerical edge cases in the models you inherit. When reading model cards, norm placement is a specific architectural detail worth checking.
The interaction between attention and MLP within one block
It's tempting to think of attention as "early work" and MLP as "late work" within a block, but they actually compose tightly. The attention sublayer produces a delta that enriches the residual stream with context-aware information — "token at position 3 is related to token at position 1 in this way." The MLP then reads this enriched stream and applies its factual lookup on top of it.
Here's a concrete trace:
Prompt: "The capital of France is"
At layer 15 (mid-depth, attention sublayer):
Attention head 7 identifies "capital of France" as a factual question pattern
and routes information from the "France" token toward the prediction position.
→ Residual stream at prediction position now contains: {France-country, capital-query}
At layer 15 (MLP sublayer):
Enriched representation activates neurons in the MLP that store {France → Paris}
→ MLP delta pushes residual stream toward Paris representation
Subsequent layers refine and the unembedding head maps to "Paris" token.
The sequence is: attention routes, MLP retrieves. But it's the same block, and both are writing to the same stream. If the attention head fails to correctly route the "France" country context (e.g., because the prompt is ambiguous), the MLP will do a factual lookup on the wrong feature set and produce a wrong answer with high confidence.
What breaks
Attention-MLP interference at long context
At very long contexts, attention heads in early-to-mid layers can attend to semantically irrelevant tokens due to the attention sink phenomenon — specific structural tokens (BOS, period) accumulate disproportionate attention mass that "wastes" capacity. When this happens, the context-aware enrichment of the residual stream is degraded before the MLP ever sees it. The MLP then does a lookup on a poorly-formed query and either returns the wrong fact or falls back to something generic.
The fix at inference time is limited — you can use attention masks or Flash Attention's more numerically stable softmax, but you can't fully compensate for a model that was trained on short sequences and then deployed on long ones. This is one reason the context window reality article argues that effective recall degrades well before the marketed limit.
Fine-tuning overwriting the MLP key-value store
Full fine-tuning of an MLP on new factual data has a consistency problem: the MLP neurons that encode related facts are not cleanly partitioned. Teaching "the capital of France is Lyon" (incorrect, for illustration) tends to shift the representations of nearby concepts as well, because the MLP's representation is distributed — there is no explicit per-fact entry to overwrite.
This is why LoRA and PEFT approaches work well for behavioral fine-tuning (style, format, instruction following) but poorly for factual injection. LoRA on MLP projections adjusts the residual delta without fully rewriting the weights — it adds a low-rank delta that shifts outputs in a small subspace. For factual grounding in production, retrieval is the right answer.
Gradient flow failure in very deep models
Even with pre-norm, gradients can fail to propagate effectively through 80+ layers if initialization is wrong. The standard fix is careful weight initialization (the depth-scaled 1/sqrt(N_layers) factor for attention output and MLP down projections in models like Llama) that prevents early layers from contributing too much initially and crowding out gradient signal from later layers. If you're training from scratch and seeing loss spikes or plateaus past 48 layers, check your initialization before assuming architecture bugs.
Induction heads disrupted by tokenization artifacts
The induction head circuit operates on exact token sequences. If your few-shot examples have inconsistent spacing, different tokenizations for the same apparent text, or structural markers (newlines, pipes) that break the pattern match, the induction head may not fire correctly. This shows up as mysteriously worse few-shot performance on inputs that "look" similar but tokenize differently — a concrete reason to test your prompts using the tokenization article's guidance before concluding the model is unreliable.
The decision in practice: what the block view changes for you
You don't need to implement attention from scratch to benefit from the residual stream view. Here are four decisions where it matters directly.
Choosing between RAG and fine-tuning for facts. The MLP-as-key-value-store model says: don't try to overwrite it with fine-tuning for factual corrections. Use retrieval. Fine-tune for behavior. The decision framework article has the full breakdown, but the mechanistic view is the "why."
Diagnosing factual errors. When a model gives a confidently wrong factual answer despite correct information in the prompt, the issue is likely one of: (a) attention failed to route the correct context into the prediction position's stream, or (b) a strong MLP prior in mid-depth layers is overriding the retrieved context. Both are diagnosable with activation-steering experiments if you have model access. Without model access, structuring the retrieved fact as close to the prediction position as possible in the prompt reduces the routing problem.
Using LoRA efficiently. If your fine-tuning goal is style/format/instruction-following, targeting the attention Q/O projections is often sufficient and cheaper. If you need to adjust factual behavior, also targeting the MLP gate/up/down projections helps. The residual identity path is preserved either way — you're just adjusting the delta.
Picking pre-trained models for long-context tasks. Architecture details — norm placement, attention sink handling, RoPE scaling — shape how gracefully a model degrades near the high end of its claimed context window. Check the model card before assuming all 128K-context models behave equivalently.
The transformer block is two sublayers sharing a stream. The stream is the model's working memory; the attention is how it reads across context; the MLP is how it applies what it knows. When something breaks in production, those three facts are where the debugging starts.
Covered in sibling articles: how attention scores are computed in detail (self-attention); how the KV cache is allocated from these blocks (context windows); how fine-tuning targets these weights (LoRA and PEFT); why the MLP-stored facts can surface as hallucinations (why models hallucinate).
Frequently asked questions
▸What is the residual stream in a transformer?
The residual stream is a single vector per token that persists across all layers of the transformer. Both the attention sublayer and the MLP sublayer read from it and add small updates to it — neither sublayer replaces it. This additive structure means earlier layers can influence the final output even through many subsequent layers, and it is why information is described as "flowing" through the network rather than being overwritten at each step.
▸What does the MLP (feed-forward) layer in a transformer actually learn?
The MLP sublayer is thought to store factual associations — entity-to-attribute mappings like "Paris is the capital of France." Ablation studies show that removing specific MLP neurons demonstrably degrades factual recall while leaving syntactic processing relatively intact. The MLP expands d_model by ~3.5× in SwiGLU-based models like Llama 3 (or ~4× in older ReLU/GeLU models), processes each token independently, then projects back down — it has no cross-token interaction, which is what distinguishes it from attention.
▸What is the difference between pre-norm and post-norm transformers?
Post-norm (the original Transformer) applies LayerNorm after adding the residual — this constrains the residual stream scale but caused training instability at depth. Pre-norm applies LayerNorm before each sublayer, normalizing only the sublayer inputs while leaving the residual stream free to grow. All major production models (GPT-3 onward, Llama, Gemma, Qwen) use pre-norm because it trains stably without warm-up tricks. The trade-off is that activation magnitudes in the residual stream grow with depth, which is addressed by peri-normalization in Gemma 2 and OLMo 2.
▸What is peri-normalization and which models use it?
Peri-normalization normalizes the output of each sublayer before it is added to the residual stream, so no single sublayer can dump an outsized update into the stream. Gemma 2 sandwiches each sublayer — RMSNorm on the input and again on the output — while OLMo 2 moves the norm to the sublayer output only, dropping the input norm. Both adopted their variants in 2024-2025, reporting fewer loss spikes and more stable training at depth than plain pre-norm.
▸Why does LoRA target attention and MLP projection matrices specifically?
LoRA injects low-rank updates to the Q, K, V, O projection matrices in attention and the gate/up/down projections in the MLP — precisely because those are where task-specific computation happens most efficiently. The residual identity path carries the pre-trained knowledge forward; LoRA only needs to nudge the delta applied at each sublayer. Targeting the residual addition itself would corrupt the pre-trained behavior, and targeting LayerNorm parameters captures far less expressive capacity per parameter.
▸What are induction heads and why do they matter practically?
Induction heads are pairs of attention heads across two adjacent layers that together implement in-context pattern completion — if the sequence contains "A B ... A", an induction head attends from the second A to B and boosts B as the next token. They are one of the clearest examples of a "circuit" identified by mechanistic interpretability research. Practically, they explain why few-shot examples in the prompt work so reliably: the model is not "reasoning" about them, it is pattern-matching via induction heads across layers.
You may also like
Mixture of Experts: why every frontier model went sparse
How mixture of experts works — routers, top-k selection, shared experts — and the VRAM and batching math that decides when sparse models save you money.
Reading LLM Benchmarks Without Getting Fooled
LLM leaderboard scores are routinely inflated by contamination and benchmark saturation. Learn how to read them skeptically — and what to do instead.
RLHF, Reward Models, and Constitutional AI
How reinforcement learning from human feedback actually works: the reward model, PPO loop, RLAIF, and Constitutional AI — the baseline before DPO shortcuts.