Transformer Architecture
The transformer is a neural network architecture based on self-attention mechanisms that processes input sequences in parallel, enabling the training of models with billions of parameters on massive datasets.
What It Really Means
Before transformers, the dominant architectures for sequence processing were RNNs (Recurrent Neural Networks) and LSTMs (Long Short-Term Memory). These process tokens one at a time, left to right. This sequential processing created two problems: training was slow (no parallelism) and long-range dependencies were hard to learn (information decayed over many time steps).
The transformer, introduced in the 2017 paper "Attention Is All You Need" by Vaswani et al., replaced recurrence with self-attention. Instead of processing tokens sequentially, the transformer looks at all tokens simultaneously and learns which tokens should attend to which other tokens. This enables massive parallelism during training and captures long-range dependencies effectively.
Every major LLM — GPT-4, Claude, Llama, Gemini — is built on the transformer architecture. Understanding how transformers work is fundamental to understanding LLM serving, embedding models, prompt engineering, and practically everything in modern AI.
How It Works in Practice
High-Level Architecture
The original transformer has two halves:
- Encoder: Processes the input sequence and builds a contextual representation
- Decoder: Generates the output sequence token by token, attending to the encoder's output
Modern LLMs typically use decoder-only architectures (GPT, Llama, Claude). Encoder-only models (BERT) are used for classification and embeddings. Encoder-decoder models (T5, BART) are used for translation and summarization.
Core Components
1. Token Embedding + Positional Encoding
Input text is tokenized and each token is mapped to a dense vector (embedding). Since self-attention has no inherent notion of position, positional encodings are added to tell the model where each token appears in the sequence.
2. Multi-Head Self-Attention
The key innovation. For each token, the model computes:
- Query (Q): "What am I looking for?"
- Key (K): "What do I contain?"
- Value (V): "What information do I provide?"
Attention score = softmax(QK^T / sqrt(d_k)) * V*
Multiple attention "heads" run in parallel, each learning different relationship patterns (syntactic, semantic, positional). See attention mechanism for a deep dive.
3. Feed-Forward Network
After attention, each token passes through a position-wise feed-forward network (two linear layers with a ReLU/GELU activation). This adds non-linearity and additional processing capacity.
4. Layer Normalization + Residual Connections
Each sub-layer (attention, feed-forward) is wrapped with a residual connection and layer normalization. This stabilizes training and enables very deep networks (GPT-4 has ~120 layers).
5. Output Head
The final layer projects the hidden state to a vocabulary-sized vector. A softmax converts this to a probability distribution over the next token.
Forward Pass Example
Input: "The cat sat on the"
- Tokenize: ["The", "cat", "sat", "on", "the"]
- Embed: Each token becomes a vector (e.g., 4096-dimensional)
- Add positional encoding: Position information injected
- Self-attention (x N layers): Each token builds a contextual representation by attending to all other tokens
- After 96 layers: The representation for position 5 ("the") encodes that this is an article following a preposition, in a sentence about a cat sitting
- Output head: Probability distribution over vocabulary, highest probability: "mat"