Welcome to the first lesson of Module 2: Transformer Architecture Fundamentals.
In our last module, we traced the evolution from RNNs to attention. You learned that attention mechanisms solve the sequential bottleneck of RNNs by processing all tokens in parallel and providing a direct path between any two tokens, overcoming the long-range dependency problem. We concluded by comparing the computational complexity, noting that attention's power comes at the cost of quadratic complexity with respect to sequence length ().
Today, we move from understanding the components in isolation to assembling the whole machine. This lesson will dissect the original Transformer architecture as presented in the seminal paper "Attention is All You Need." Our goal is to understand its high-level encoder-decoder structure and identify the function of each core component. This architecture is the blueprint for the vast majority of modern LLMs, and mastering it is a critical step toward your goal of deep AI expertise.
The Transformer's Blueprint: An Encoder-Decoder Structure
At its heart, the original Transformer is a sequence-to-sequence (seq2seq) model, a concept you're familiar with from RNNs. It's designed for tasks like machine translation, where you map an input sequence (a sentence in one language) to an output sequence (the same sentence in another language).
It consists of two main parts:
- The Encoder: Its job is to read the entire input sequence and build a rich, contextualized numerical representation for each token. It "encodes" the meaning of the input sentence.
- The Decoder: Its job is to take the encoder's representation and generate the output sequence, one token at a time. It's autoregressive, meaning each new token it generates is based on the encoder's output and the tokens it has already produced.
This division of labor is a powerful paradigm. The encoder can focus solely on understanding the input, while the decoder can focus on generating a coherent output based on that understanding.
Let's look at a high-level diagram.

To get a quick walkthrough of this high-level data flow, watch the following short clip.
Illustrated Guide to Transformers Neural Network: A step by step explanation
This video from The AI Hacker provides a clear, conceptual overview of how the encoder maps an input sequence to a representation and how the decoder uses it to generate an output step-by-step.
Watch the segment from 2:43 to 3:26. Focus on the distinct roles of the encoder (mapping the input) and the decoder (generating the output).
Now, let's break down what's inside each of these stacks.
Dissecting the Encoder
The encoder's goal is to create a set of vectors, one for each input token, where each vector is rich with contextual information from the entire input sentence. This process happens in a stack of identical encoder layers (the original paper used N=6).
Here's the journey of an input sentence through the encoder:
- Input Embedding: As with previous models, we start by converting each token into a dense vector using a learned embedding matrix.
- Positional Encoding: The self-attention mechanism, by design, is permutation-invariant—it doesn't have a built-in sense of word order. To fix this, we inject information about each token's position in the sequence. The original Transformer did this by adding a pre-calculated vector based on sine and cosine functions of different frequencies. This gives the model a way to understand the relative and absolute positions of tokens.
- Multi-Head Self-Attention: This is the core of the encoder. For each token, this layer allows it to "look at" every other token in the input sequence and calculate attention scores. These scores determine how much influence each other token should have on its representation. "Multi-head" simply means this process is done in parallel multiple times (e.g., 8 heads), with each "head" learning to focus on different types of relationships (e.g., syntactic, semantic). The results are then combined.
- Add & Norm (Residual Connection + Layer Normalization): The output of the attention layer is added to its input (a residual connection, just like in ResNet), and the result is normalized. Your experience with deep learning will tell you this is crucial for enabling deep networks to train effectively by preventing vanishing gradients and stabilizing layer inputs.
- Position-wise Feed-Forward Network (FFN): This is a simple two-layer fully connected neural network that is applied independently to each token's representation. It provides additional computational depth, allowing the model to learn more complex transformations.
- Another Add & Norm: A second residual connection and layer normalization are applied after the FFN.
The output of one encoder layer becomes the input to the next, allowing the model to build progressively more sophisticated representations.
Dissecting the Decoder
The decoder's goal is to generate the output sequence autoregressively. It also consists of a stack of identical layers (N=6), but each layer has an extra sub-layer compared to the encoder.
Here's the data flow for one generation step:
- Output Embedding & Positional Encoding: Similar to the encoder, the decoder takes the tokens generated so far, embeds them, and adds positional encodings.
- Masked Multi-Head Self-Attention: This layer works just like the self-attention in the encoder, but with a critical difference: it applies a "look-ahead mask." This mask prevents each position from attending to subsequent positions. Without this, the decoder could "cheat" by looking at the word it's supposed to predict. This masking enforces the autoregressive property.
- Add & Norm: A residual connection and layer normalization are applied.
- Encoder-Decoder Attention (Cross-Attention): This is the crucial link between the encoder and decoder. In this layer, the queries () come from the decoder's masked self-attention layer, but the keys () and values () come from the final output of the encoder stack. This allows the decoder to look at the entire input sentence and decide which parts are most relevant for predicting the next output token. For instance, when translating a sentence, this layer helps the decoder focus on the relevant source words.
- Add & Norm: Another residual connection and layer normalization.
- Position-wise Feed-Forward Network: Same as in the encoder.
- Final Add & Norm: The final residual connection and normalization in the decoder block.
After the final decoder layer, a linear transformation followed by a softmax function produces a probability distribution over the entire vocabulary. The token with the highest probability is chosen as the output for that step, and it is then fed back into the decoder as input for the next step. This process repeats until a special <end_of_sentence> token is generated.
This detailed walkthrough can be quite dense. The following video resource provides a comprehensive visual explanation of each component we've just discussed.
Attention is all you need (Transformer) - Model explanation (including math), Inference and Training
The video by Umar Jamil provides a detailed, step-by-step explanation of the entire Transformer architecture, covering the exact components we've outlined. It's a fantastic resource for seeing how these parts fit together.
Use this video to solidify your understanding of the components: High-Level Structure (07:56 - 08:48): Get a quick visual recap of the encoder-decoder connection. Input & Positional Encoding (12:21 - 20:11): Focus on why positional encodings are needed and the intuition behind using sine/cosine functions. Multi-Head Attention (28:21 - 35:27): Understand how multiple heads allow the model to learn different representation subspaces. Layer Normalization (37:46 - 40:28): Grasp its role in stabilizing training. Decoder Components (40:28 - 44:43): Pay close attention to the explanation of Masked Multi-Head Attention and the Cross-Attention mechanism, which uses the encoder's output.
For a more text-based and highly visual alternative, the article "Transformers Illustrated!" is an excellent companion resource. You can refer to it to clarify any points from the video.
Test your understanding!
In the Transformer's decoder, there are two distinct Multi-Head Attention layers. What is the fundamental difference in what each of these layers "attends to"?
- The first attention layer (Masked Multi-Head Self-Attention).
- The second attention layer (Encoder-Decoder Cross-Attention).
Show answer
-
The Masked Multi-Head Self-Attention layer attends to the tokens generated so far in the output sequence. Its purpose is to build a contextual representation of the partial output, while the mask prevents it from looking ahead.
-
The Encoder-Decoder Cross-Attention layer attends to the final representations from the entire input sequence (i.e., the output of the encoder stack). Its purpose is to align the partially generated output with the relevant parts of the source input to inform the next prediction.
Conclusion
In this lesson, we've constructed the original Transformer architecture from the ground up. You now have a mental map of its components and the flow of data through the system.
Key Takeaways:
- Overall Structure: The Transformer uses an encoder-decoder architecture. The encoder processes the full input to create contextual representations, and the decoder generates the output autoregressively.
- Encoder Components: Each encoder layer contains a multi-head self-attention mechanism and a position-wise feed-forward network, glued together with residual connections and layer normalization.
- Decoder Components: Each decoder layer has three sub-layers: masked multi-head self-attention (to look at previous outputs), encoder-decoder cross-attention (to look at the input), and a feed-forward network.
- Key Enablers: Positional encodings provide word order information, while residual connections and layer normalization are critical for stable training of these deep models.
What's Next?
You now understand the "what" and "where" of the Transformer's components. In the next lesson, we will dive deep into the "how." We'll focus on the engine of the Transformer: the scaled dot-product attention mechanism. You will move from description to implementation, coding this mechanism from scratch in PyTorch to gain a practitioner's understanding of how it works.