Introduction
In our last lesson, we delved into the first major component of a Transformer block, tracing the data's path through the Multi-Head Attention sub-layer. We saw how this mechanism allows tokens to communicate and aggregate information from across the sequence, resulting in a contextually enriched tensor. We also distinguished between the modern Pre-Layer Normalization (Pre-LN) and the original Post-LN architectures.
This lesson completes our exploration of the Transformer block. We will pick up right where we left off, taking the output from the attention sub-layer and tracing its journey through the second and final stage: the Position-wise Feed-Forward Network (FFN). You will learn why this component is indispensable and how it works with its own residual connection and layer normalization to produce the block's final output. By the end of our session, you will have a complete, step-by-step understanding of how an input sequence is processed and transformed by a full Transformer block.
The Role of the Position-wise Feed-Forward Network
After the Multi-Head Attention sub-layer has mixed information across different token positions, the Transformer needs a component to process this new information within each position. This is the job of the Position-wise Feed-Forward Network (FFN).
At first glance, it might seem like just another set of layers. However, its role is critical for two main reasons:
-
Introducing Non-linearity: The attention mechanism, while powerful, is fundamentally a sophisticated weighted averaging operation. In terms of its
Valueinputs, it is a linear transformation. If we were to stack multiple attention layers without any non-linear processing in between, the entire stack would be mathematically equivalent to a single, more complex linear transformation. The FFN introduces essential non-linearity, typically via a GELU or ReLU activation function, which dramatically increases the model's expressive power and allows it to learn far more complex functions. -
Per-Position Transformation: The FFN acts as a small, two-layer Multi-Layer Perceptron (MLP) that is applied independently to each token's vector representation. While the attention layer's job is to handle communication between tokens, the FFN's job is to "think" or "digest" the information gathered for each token individually. You can conceptualize this as a two-step process within each block: first, gather context (attention), then process that context (FFN).
Recent research suggests that the FFN is more than just a source of non-linearity; it may also function as a key-value memory or knowledge store, where the model's factual knowledge is encoded.
To dive deeper into the motivation and architecture of the FFN, let's turn to an excellent explanation.
The Transformer Architecture: A Deep Dive - Imad Dabbura
The article by Imad Dabbura provides a clear and modern perspective on the FFN. It explains why the FFN is necessary and offers the compelling interpretation of it as a knowledge store.
Please read the section Feed-Forward Network. Pay close attention to the separation of concerns between Attention (mixing information) and the FFN (transforming information). Note the "expand, activate, contract" structure and the insight that the FFN accounts for roughly two-thirds of a Transformer's parameters.
Tracing the Data Flow: The Second Sub-layer
Having established the FFN's purpose, let's now trace the data flow from the output of the attention sub-layer, which we'll call , through this second component. Just as with the attention sub-layer, we'll examine both the modern Pre-LN and original Post-LN patterns.
The Pre-LN FFN Sub-layer (Modern Standard)
The flow is consistent with the first sub-layer: Output = Input + Sublayer(LayerNorm(Input)).
- Input: We begin with the tensor produced by the MHA sub-layer. It has the shape
[batch_size, seq_len, d_model]. - Pre-Normalization: The tensor is passed through the block's second
LayerNormmodule. - FFN Processing: The normalized tensor is processed by the FFN. As described in the reading, this involves an expansion, a GELU activation, and a contraction.
The output has the same shape as the input,
[batch_size, seq_len, d_model]. - Residual Connection: The FFN's output is added back to its un-normalized input . A dropout layer is typically applied to for regularization.
The resulting tensor is the final output of the entire Transformer block. Its dimensions are unchanged, allowing it to be fed directly into the next identical block in the stack.
The Post-LN FFN Sub-layer (Original Architecture)
The original paper applied normalization after the residual connection: Output = LayerNorm(Input + Sublayer(Input)).
- Input: The tensor from the MHA sub-layer.
- FFN Processing: The FFN is applied directly to the un-normalized tensor .
- Residual Connection: The output is added to its input .
- Post-Normalization: The result of the sum is passed through the second
LayerNormmodule.
Putting It All Together: The Complete Transformer Block
We have now traced the data flow through both sub-layers. Let's consolidate this into a complete picture of a single Transformer block.
The diagram below shows the FFN sub-layer in context. The input comes from the MHA sub-layer (which we covered last time), gets processed by the FFN, and is then integrated via a residual connection and a final LayerNorm. This specific diagram illustrates the Post-LN flow.

The following diagram shows a complete encoder block, integrating both the Multi-Head Attention and Feed-Forward Network sub-layers.

For a rigorous, mathematical summary of the entire block's data flow, the Jurafsky and Martin textbook provides a step-by-step breakdown.
This excerpt from "Speech and Language Processing" by Jurafsky and Martin formalizes the data flow through a complete Transformer block using a series of equations.
First, quickly review the section on the Feedforward layer to see its formal definition (Eq. 8.21). Next, focus on the section Putting it all together. The equations 8.26 through 8.31 trace the processing of a single token vector. Then, the equations 8.40 through 8.45 show the same process for a full matrix of tokens, which aligns with how you'd implement it in code. Notice how the output of one step becomes the input to the next, clearly mapping the data's journey. These equations formalize the Pre-LN architecture.
Finally, let's see how this entire block is implemented in code. Andrej Karpathy's "Let's build GPT" video provides a hands-on demonstration where he assembles these components into a Block class.
Let's build GPT: from scratch, in code, spelled out.
In this video, Andrej Karpathy builds a GPT-style (decoder-only) Transformer from scratch. We will focus on the sections where he implements the Feed-Forward Network and assembles the full Block.
Watch the following segments: Feed-Forward Network: This part introduces the FeedForward class. Note the structure: a linear layer to expand, a ReLU, and another linear layer to contract. This directly corresponds to the FFN architecture we discussed. Residual Connections: This segment shows the addition of skip connections around the attention and FFN sub-layers. Notice the x = x + ... pattern, which is the hallmark of a residual connection. He also discusses the common 4x expansion factor for the FFN's inner dimension. Layer Normalization: Here, Karpathy adds LayerNorm. Critically, he implements the Pre-LN formulation (LayerNorm is applied before the self-attention and FFN), which he notes is a slight deviation from the original paper but is the modern standard for stability.
Your DevOps experience with CI/CD pipelines provides a useful mental model here. A Transformer model is a data processing pipeline composed of stacked, identical blocks. Each block is a mini-pipeline with two stages (Attention and FFN). The use of residual connections ensures a "main branch" of information flows cleanly through, while each stage contributes modifications. The Pre-LN architecture adds verification steps (normalization) before each critical stage, ensuring the stability and predictability of the entire pipeline, much like running linters and tests before a build or deployment step.
Conclusion
In this lesson, we have completed our detailed examination of the Transformer block by tracing the data flow through its second major component, the Position-wise Feed-Forward Network.
Here are the key takeaways:
- The FFN provides essential non-linearity and acts as a per-token computation/digestion step after the attention mechanism has mixed information across the sequence.
- The FFN typically follows an "expand-activate-contract" architecture, projecting the
d_modelrepresentation to a much larger intermediate dimension (4 * d_model) before projecting it back down. - The data flow through the FFN sub-layer mirrors the attention sub-layer, incorporating Layer Normalization and a residual connection to ensure stable and effective training.
- A complete Transformer block is a self-contained unit that takes a tensor of shape
[batch_size, seq_len, d_model]and produces an output tensor of the exact same shape, making the blocks stackable to create deep networks.
You now possess a complete, end-to-end conceptual and mathematical map of a single Transformer block. With this foundational knowledge, you are well-prepared to understand how these blocks are arranged into the large-scale architectures that power modern LLMs.
In our next lesson, we will zoom out to the architectural level, comparing the original encoder-decoder model from "Attention Is All You Need" with the decoder-only (GPT-style) and encoder-only (BERT-style) variants that have become standards in the field.