The Full Transformer Architecture
Complete specification of the transformer: encoder and decoder stacks, the role of each sublayer (attention, FFN, normalization), residual streams, encoder-decoder cross-attention, and architectural variants (encoder-only, decoder-only).
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- The Encoder Block
- The Decoder Block
- Cross-Attention Mechanism
- The Feed-Forward Network
- The Residual Stream
- Encoder-Only Models (BERT)
- Decoder-Only Models (GPT)
- Parameter Count Analysis
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Describe the full encoder-decoder transformer architecture from Vaswani et al. (2017).
- Explain the function of each sublayer and the order of operations.
- Derive the causal mask for autoregressive decoding.
- Compute the total parameter count as a function of , , , .
- Compare encoder-only, decoder-only, and encoder-decoder variants.
Notation
- — model dimension
- — feed-forward hidden dimension (typically )
- — number of attention heads
- — number of layers (blocks)
- — sequence length
- — vocabulary size
Core Intuition
The transformer processes sequences through alternating attention (mixing information across positions) and feed-forward (transforming each position independently) layers. Residual connections and layer normalization enable training very deep stacks. The architecture's power comes from attention's ability to model arbitrary pairwise interactions in depth.
Transformer Block
Layer 1The Encoder Block
Each encoder layer applies two sublayers:
The encoder uses bidirectional self-attention — every position can attend to every other position. After layers, the encoder output encodes the full input context.
The Decoder Block
Each decoder layer applies three sublayers:
Masked self-attention prevents position from attending to future positions .
Causal mask. The attention scores are masked:
Adding before softmax ensures those positions receive zero attention weight.
Cross-Attention Mechanism
In cross-attention, queries come from the decoder and keys/values from the encoder:
This allows each decoder position to attend to all encoder positions — the mechanism by which the decoder "reads" the input.
The Feed-Forward Network
Applied identically and independently to each position:
where , , and is the activation (ReLU in original, GELU in GPT/BERT).
Parameters per FFN layer: (for ).
Interpretation: The FFN acts as a position-wise "memory lookup" — it stores and retrieves factual knowledge. Attention routes information; FFN processes it.
The Residual Stream
The residual stream interpretation (Elhage et al., 2021): The hidden state is a "residual stream" that accumulates contributions from each sublayer:
Each attention and FFN layer reads from and writes to this stream. This view makes gradient flow transparent: the gradient flows directly from loss to any layer through the identity path.
Encoder-Only Models (BERT)
- Remove the decoder entirely.
- Bidirectional attention over the full input.
- Pre-trained with Masked Language Modeling (MLM): predict randomly masked tokens.
- Used for: classification, NER, sentence similarity, retrieval.
Decoder-Only Models (GPT)
- Remove the encoder and cross-attention.
- Causal (masked) self-attention only.
- Pre-trained with next-token prediction: .
- Used for: text generation, in-context learning, reasoning.
- Most modern LLMs (GPT-4, LLaMA, Claude) are decoder-only.
Why decoder-only dominates: Simpler architecture; scales better; naturally supports generation; in-context learning emerges at scale.
Parameter Count Analysis
For a decoder-only transformer:
Per layer:
- Self-attention: (Q, K, V, O projections)
- FFN:
- LayerNorm: (negligible)
- Total per layer:
Full model:
- Embedding:
- layers:
- Final LN + output head: (often tied with embedding)
Example (GPT-3 175B): . This gives .
Common Pitfalls
Pitfall 1. Thinking the FFN is unimportant. It contains of all parameters and stores most learned "knowledge." Attention just routes information.
Pitfall 2. Not using the causal mask for generation. Without it, the model sees future tokens during training — the loss becomes trivially low but the model cannot generate.
Pitfall 3. Confusing Pre-Norm with Post-Norm. Modern models use Pre-Norm ( before attention/FFN) for training stability. The original paper used Post-Norm.
Summary
- The transformer alternates multi-head attention (inter-position mixing) and FFN (per-position transformation).
- Residual connections + LayerNorm enable training deep stacks.
- Causal masking enforces autoregressive structure for generation.
- Cross-attention connects encoder outputs to decoder queries.
- Decoder-only is the dominant architecture for modern LLMs.
- Total parameters scale as .
Exercises
Exercise 1. For a transformer with , compute the total parameter count.
Exercise 2. Derive the causal mask and show that position 's output depends only on positions .
Exercise 3. Prove that the encoder-decoder cross-attention computes the same function as a retrieval operation: the decoder "queries" the encoder "database."
Exercise 4. Compute the FLOPs for one forward pass through a single transformer layer with sequence length and model dimension .
Exercise 5. Explain why weight tying (sharing embedding and output projection matrices) is valid and compute the parameter savings.