A transformer is a stack of nearly-identical blocks
Every transformer block has two sublayers: multi-head self-attention and a position-wise feedforward network (a small MLP applied independently to each position). Both sublayers are wrapped in residual connections and layer normalization.
That's it. Stack 12 of these blocks for a small transformer (BERT-base), 96 for a frontier LLM. Add an embedding layer at the input, an LM head or classifier at the output, and a position encoding so the model knows token order.
Position encodings
Self-attention is permutation-invariant on its own. To give the model a sense of order, we add positional information to the token embeddings. Original transformer used sinusoidal encodings; modern LLMs use RoPE (rotary position embeddings) which integrate position into the query/key projections themselves, allowing relative-position reasoning at any context length.
The two flavors of attention you'll see
- Encoder (BERT, ViT) — bidirectional self-attention; every token attends to every other token. Used for classification, retrieval, embedding.
- Decoder (GPT, LLaMA) — causal self-attention with a triangular mask; each token only attends to itself and previous tokens. Used for generation.
- Encoder-decoder (T5, BART) — both, plus cross-attention from decoder to encoder. Used for translation, summarization, text-to-text tasks.