Recommended resources
The freely available resources this lesson will direct you to.
Hello! Welcome back to our journey into the Transformer architecture.
In the last few lessons, we've carefully examined the fundamental building blocks: the powerful multi-head self-attention mechanism and the clever sinusoidal positional encodings that give the model a sense of sequence order. Now, it's time to assemble these components into a complete, functioning unit.
This lesson directly addresses the learning outcome: Describe the architecture of a single Transformer encoder block, including multi-head attention and feed-forward layers. We will see how these pieces, along with two other crucial elements—residual connections and layer normalization—work together to process input sequences and generate rich, contextualized representations.
Let's begin by looking at a high-level roadmap of what we're about to build.

1. The Anatomy of an Encoder Block
As the diagram above illustrates, a Transformer encoder block consists of two primary sub-layers:
- A Multi-Head Self-Attention layer.
- A position-wise Feed-Forward Network.
The input to an encoder block (e.g., the word embeddings plus positional encodings) flows through these two sub-layers sequentially. An essential characteristic of the Transformer is that the output of an encoder block has the exact same dimensions as its input. This design choice is critical because it allows us to stack multiple encoder blocks on top of each other (the original paper used N=6), with the output of one serving as the input for the next.
To understand how this is achieved and why it's important, we need to look at the "glue" that holds the sub-layers together: residual connections and layer normalization.
Let's start with a clear, conceptual overview of the encoder's structure from Jay Alammar's excellent blog post, 'The Illustrated Transformer'.
Please read the section under the heading 'The encoders are all identical in structure'. This will give you a solid mental model of the two main sub-layers and the flow of data.
The reading highlights a key property: while the self-attention layer creates dependencies between all words in the sequence, the feed-forward layer processes each position independently and in parallel. Now, let's explore the components that connect everything.
2. Sub-layer 1: Multi-Head Attention with "Add & Norm"
The first operation within the encoder block is multi-head self-attention, which we've studied previously. It allows the model to weigh the importance of different words in the input sequence when encoding a specific word.

After the attention output is produced, it goes through an "Add & Norm" step. This step is composed of two operations:
- Residual Connection (
Add): The input to the multi-head attention sub-layer is added to the output of the sub-layer. The formula isx_new = LayerNorm(x_input + Attention(x_input)). - Layer Normalization (
Norm): The result of the addition is then normalized.
Why are these two steps so important?
Blowing up the Transformer Encoder!
This video provides an excellent explanation for the motivation behind residual connections and layer normalization.
Watch the segment from 12:44 to 15:35. The speaker clearly explains how residual connections help with gradient flow in deep networks and how layer normalization provides training stability.
As the video explained, these are not just minor details; they are crucial for training deep Transformer models:
- Residual Connections create a direct path for the gradient to flow backwards, mitigating the vanishing gradient problem. They also ensure that the model doesn't "forget" the original information from the input
xas it passes through a layer. - Layer Normalization standardizes the inputs to the next layer, ensuring that the mean is close to 0 and the standard deviation is close to 1. This stabilizes training and often helps the model converge faster.
3. Sub-layer 2: The Feed-Forward Network
The output of the first "Add & Norm" step is then passed to the second sub-layer: a position-wise Feed-Forward Network (FFN).
This is a relatively simple component. It consists of two linear transformations with a ReLU activation function in between. "Position-wise" means that it processes the vector for each token position separately and identically.
The formula is:
An interesting architectural detail is that the first linear layer typically expands the dimensionality of the representation, and the second layer projects it back down. For example, it might transform a 512-dimensional vector into a 2048-dimensional vector, and then back to 512.
L-8 Transformer Encoder: Multi-Head Attention to FFN (Full Math)
The following video explains the structure of the Feed-Forward Network and its role in processing the information learned by the attention mechanism.
Watch the segment from 29:30 to 34:53. The video details the FFN's three-layer structure (Linear, ReLU, Linear), the expansion and reduction of dimensions, and its conceptual role.
The video makes a great distinction:
- Attention tells a token what to look at.
- The FFN tells the token how to process what it has learned from the attention step.
Just like the first sub-layer, the FFN is also wrapped in an Add & Norm step. The input to the FFN is added to its output, and the result is layer-normalized. This completes one full pass through a Transformer encoder block.
4. Assembling the Block: A Code Perspective
Seeing the architecture in code can solidify these concepts. We won't write an implementation from scratch, but we can inspect the forward pass of a well-structured EncoderBlock to see the data flow precisely.
Tutorial 6: Transformers and Multi-Head Attention
This PyTorch tutorial provides a clear implementation of a Transformer EncoderBlock. Focus on the forward method to see exactly how the components are connected.
In this resource, scroll down to the section titled 'Transformer Encoder'. Read the conceptual explanation and then examine the Python class EncoderBlock. Pay close attention to the forward method, as it perfectly mirrors the architecture we've discussed.
Let's trace the steps in the forward method from the provided EncoderBlock class. If x is the input to the block:
-
Attention Part:
attn_out = self.self_attn(x, mask=mask): The inputxis passed through the multi-head attention layer.x = x + self.dropout(attn_out): The residual connection adds the original inputxto the attention output. (Dropout is included for regularization).x = self.norm1(x): The result is layer-normalized. The variablexnow holds the output of the first sub-layer.
-
MLP Part (Feed-Forward Network):
linear_out = self.linear_net(x): The updatedxis passed through the FFN.x = x + self.dropout(linear_out): A second residual connection adds the input of the FFN to its output.x = self.norm2(x): The result is layer-normalized again.
The final x is the output of the encoder block, ready to be passed to the next encoder block or to the decoder.
Conclusion
In this lesson, we have constructed a complete Transformer encoder block from its constituent parts. We saw that it is far more than just an attention mechanism; it is a sophisticated architecture designed for deep, stable, and effective sequence processing.
Key Takeaways:
- A Transformer encoder block is composed of two sub-layers: Multi-Head Self-Attention and a position-wise Feed-Forward Network (FFN).
- Each sub-layer is wrapped with a residual connection (
Add) and Layer Normalization (Norm). - Residual connections are vital for gradient flow and preserving information, enabling the training of very deep models.
- Layer Normalization stabilizes training by ensuring the outputs of each sub-layer have a consistent distribution.
- The input and output of an encoder block have the same dimension, which allows multiple blocks to be stacked sequentially.
Preview of the Next Lesson
Now that we have a solid understanding of the encoder, we are well-prepared to tackle its counterpart: the decoder. In the next lesson, we will describe the architecture of a single Transformer decoder block. We'll find that it shares many similarities with the encoder but includes one crucial addition: a second attention mechanism that allows it to look at the output of the encoder stack.