Transformer Architecture
Understand how Transformers use attention to process sequences in parallel and power modern LLMs.
What Is Transformer Architecture?
A Transformer is a neural network design for understanding and generating sequences (like text), built around a simple idea: instead of reading tokens one-by-one like a person reading a sentence, it looks at all tokens at once and decides what matters.
Analogy: imagine you’re editing a document with sticky notes. For every word you’re trying to interpret, you can quickly glance at all other words and place sticky notes on the most relevant ones: “this pronoun refers to that noun,” “this adjective modifies that thing,” “this clause changes the meaning of that earlier phrase.” That sticky-note process is attention.
Technically, a Transformer is a stack of layers that repeatedly:
- convert tokens into vectors (embeddings),
- let each token exchange information with other tokens using self-attention,
- refine the result with a small feed-forward network, while using residual connections and normalization for stable learning.
The original Transformer was designed for machine translation with an encoder-decoder structure, but many famous descendants use only one side (encoder-only like BERT, decoder-only like GPT).
Why Does It Matter?
Transformers matter because they solved key bottlenecks in older sequence models (like RNNs/LSTMs):
- Parallelism: Older models processed tokens sequentially, which is slow and hard to scale. Transformers process many tokens in parallel during training, which makes them much faster on modern hardware.
- Long-range relationships: In language, the important clue for a word might be far away (“The book that the professor who the student admired wrote…”). Self-attention gives a direct path between distant tokens.
- General-purpose backbone: The Transformer pattern turned out to work not just for translation, but for summarization, search, code, image understanding (Vision Transformers), speech, and multimodal systems.
If you care about modern AI—LLMs, copilots, retrieval systems, assistants—Transformers are the engine under the hood.
How It Works
Below is the core mechanism in a practical, step-by-step way.
1) Tokenize and embed
Text is split into tokens (words or word pieces). Each token becomes a vector via an embedding table.
- Token:
"cat" - Embedding: a learned vector like
[0.12, -0.03, ...]
Embeddings are the model’s “internal coordinates” for meaning.
2) Add positional information
Attention by itself doesn’t know order. The set {cat, sat, mat} is the same set no matter how you shuffle it.
So Transformers add positional encoding (or learned positional embeddings) so the model can tell “token 3” from “token 30”.
Intuition: you’re giving each token a faint “timestamp” so the model knows where it sits in the sequence.
3) Self-attention: tokens look at other tokens
Self-attention is the signature move.
For each token, the model creates three vectors:
- Query (Q): what this token is looking for
- Key (K): what this token offers
- Value (V): the information this token will provide if chosen
Then each token scores every other token to decide “how much should I pay attention to you?”
Intuition before the formula:
- A query is like a question: “Who does ‘it’ refer to?”
- Keys are like labels on every token: “I’m a noun,” “I’m the subject,” “I’m a date,” etc.
- Values are the actual content you want to blend in once you decide relevance.
The common attention calculation is:
What this means in plain language:
- computes similarity scores between the current token’s query and every token’s key.
- Dividing by keeps scores numerically well-behaved.
softmaxturns scores into weights that sum to 1.- Multiplying by makes a weighted blend of the other tokens’ value vectors.
Concrete example: Sentence: “The cat sat on the mat because it was tired.” When processing “it”, attention can put high weight on “cat” (and low weight on “mat”) so the model learns the reference.
4) Multi-head attention: several “views” at once
Instead of doing attention once, the Transformer does it in multiple parallel channels called heads.
Why? Because different heads can specialize:
- One head focuses on grammatical structure (subject-verb links)
- Another tracks coreference (pronouns → nouns)
- Another focuses on nearby context
- Another notices punctuation or clause boundaries
Then the heads are combined back into one representation.
5) Feed-forward network: local transformation
After attention, each token passes through a small neural network (usually two linear layers with a nonlinearity). This is applied independently to each position.
Intuition: attention mixes information across tokens; the feed-forward network processes the mixed result to create useful features.
6) Residual connections + layer normalization: stability
Each sub-step typically uses:
- Residual connection: add the input back to the output (helps gradients flow, preserves useful signals)
- Layer normalization: keeps activations in a stable range
This is part of why Transformers can be stacked deep without collapsing.
7) Stack the layer many times
One Transformer layer can do simple linking. Many layers let the model build higher-level abstractions:
- early layers: local syntax cues
- middle layers: phrase and sentence structure
- later layers: semantics and task-relevant reasoning patterns
Encoder vs Decoder (original design)
-
Encoder: reads the input and produces contextual representations (great for understanding tasks).
-
Decoder: generates output one token at a time, using:
- masked self-attention (so it can’t “peek” at future tokens),
- and often cross-attention to the encoder outputs (for translation).
Decoder-only Transformers (like many LLMs) drop the encoder and rely on masked self-attention over the prompt.
Key Terminology
- Self-attention: A mechanism where each token computes which other tokens are most relevant and blends information from them.
- Query/Key/Value (Q/K/V): Learned projections used to score relevance (Q·K) and then retrieve content (V).
- Multi-head attention: Multiple attention computations in parallel, allowing different relational patterns to be captured.
- Positional encoding / positional embeddings: Signals added to embeddings so the model knows token order.
- Masked attention: A constraint used in decoders so generation is causal (no future tokens allowed).
Real-World Applications
- Chatbots and LLMs: Decoder-only Transformers generate text, answer questions, write code, and summarize.
- Search and ranking: Encoder-style Transformers create strong text representations for semantic search and re-ranking.
- Machine translation: The original encoder-decoder Transformer maps one language to another.
- Code assistants: Transformers trained on code predict completions, explain functions, and refactor.
- Vision Transformers (ViT): Images are split into patches treated like tokens; attention learns global image relationships.
- Multimodal systems: Combine text tokens with image/audio tokens so attention can align meaning across modalities.
Common Misconceptions
-
“Attention is an explanation of the model’s reasoning.” Attention weights can be informative, but they are not guaranteed to be a faithful explanation of why the model made a decision. They’re part of the computation, not a truth serum.
-
“Transformers read left-to-right only.” Some do (decoder-only, causal models). But encoder models (like BERT-style) can attend to both left and right context during training.
-
“Transformers automatically understand long documents perfectly.” Standard attention has a computational cost that grows roughly with the square of sequence length. Handling very long contexts often needs special tricks (sparse attention, chunking, retrieval, etc.).
Further Reading
- Attention Is All You Need (Vaswani et al., 2017) — the original Transformer paper.
- The Illustrated Transformer (Jay Alammar) — highly visual intuition for attention and the full architecture.
- The Annotated Transformer (Harvard NLP / Rush, 2018) — a readable, line-by-line implementation and explanation.