Transformer Architecture
How the transformer block works end-to-end — embeddings, self-attention, feed-forward layers, and residual streams.
Introduction
The transformer is the architecture behind every modern large language model, from GPT and Claude to Gemini and Llama. If you understand one transformer block, you understand the whole model, because a large language model is mostly the same block stacked dozens of times with a token embedding at the bottom and a prediction head on top.
In interviews, transformer architecture is the most common opening question in the AI foundations round because it reveals whether a candidate actually understands what an LLM is doing or has only used the API. A strong answer explains the data flow through one block, why residual connections and normalization matter, and how stacking blocks turns token embeddings into a next-token distribution.
This lesson walks the full path: how a sequence of token ids becomes vectors, how self-attention lets each token gather information from other tokens, how the feed-forward network transforms each position, and how the final layer produces logits over the vocabulary. The goal is a mental model precise enough to reason about cost, context length, and quality tradeoffs later in the roadmap.
Where this shows up in production
The transformer block shows up in essentially every production AI system you will design.
- Decoder-only transformers power chat assistants, coding copilots, and answer engines.
- Encoder transformers produce the embeddings that feed vector databases and retrieval.
- The number of layers and the hidden size drive GPU memory, latency, and serving cost.
- Context length, a direct property of the attention mechanism, decides how much retrieved data or conversation history a system can use.
- Understanding residual streams and attention is the basis for interpretability, fine-tuning, and safety work.
Learning Objectives
Trace the full data flow through a decoder-only transformer from token ids to output logits.
Explain the role of token embeddings, positional information, self-attention, and the feed-forward network in one block.
Describe why residual connections and layer normalization make deep transformers trainable.
Explain how stacking many identical blocks builds representational depth without changing the interface.
Connect architectural choices such as layer count, hidden size, and head count to memory, latency, and cost.
Distinguish encoder-only, decoder-only, and encoder-decoder transformers and where each is used.
Theory & Concepts
A language model is one block repeated many times
A decoder-only transformer has three conceptual parts. First, an embedding layer maps each token id to a vector and adds positional information. Second, a stack of identical transformer blocks refines those vectors. Third, a language modeling head projects the final vectors back to a probability distribution over the vocabulary.
The important insight for interviews is uniformity. Every block has the same shape and the same interface: it takes a sequence of hidden vectors and returns a sequence of hidden vectors of the same size. This is why the architecture scales so cleanly. Making a model bigger usually means more blocks, wider hidden vectors, or more attention heads, not a new design.
Self-attention moves information between positions
Inside a block, self-attention is the only component that lets tokens exchange information. Each token produces a query, a key, and a value vector. The attention score between two tokens is the dot product of one token query and another token key, scaled and passed through softmax. Each token then builds a weighted sum of value vectors using those scores.
This is how a pronoun can attend to the noun it refers to, or how a closing bracket can attend to its opening bracket. In a decoder-only model, a causal mask prevents a token from attending to future positions, which is what makes next-token prediction well defined. The attention mechanism lesson goes deeper on the math.
The feed-forward network transforms each position independently
After attention mixes information across positions, the feed-forward network, sometimes called the MLP, transforms each position on its own. It is usually two linear layers with a nonlinearity such as GELU in between, and it expands the hidden size by a factor of about four before projecting back.
A useful intuition is division of labor. Attention decides which other tokens matter, and the feed-forward network decides what to compute given the gathered context. A large fraction of a model parameters live in these feed-forward layers, which is why they dominate compute during the position-wise transformation.
Residual connections and normalization keep deep models trainable
Each sub-layer is wrapped in a residual connection: the output is the input plus the sub-layer result. This creates a residual stream that runs from the embedding all the way to the final layer, and it lets gradients flow through dozens of blocks without vanishing.
Layer normalization stabilizes the scale of activations so training stays numerically healthy. Modern models usually apply normalization before each sub-layer, a pattern called pre-norm, which trains more reliably at depth. The residual stream view is central to interpretability: each block reads from and writes to this shared stream rather than replacing it.
Depth and width create capability
Stacking blocks gives the model depth. Early layers tend to capture local and syntactic structure, while later layers capture more abstract and semantic relationships. Width, meaning the hidden size and the number of attention heads, controls how much information each position can hold and how many distinct relationships attention can track at once.
For interviews, remember the levers. Parameter count grows with both depth and width. Attention cost grows with sequence length squared. Feed-forward cost grows with hidden size. These relationships explain why long context and large models are expensive, a theme that returns in the inference and context window lessons.
Architecture Diagram
The diagram shows the logical flow of one block, not the physical GPU layout. In practice the embedding, all blocks, and the head live on one or more GPUs, and the same block weights are applied at every layer position with independent parameters per layer.
The residual edges are drawn dashed because they carry the unchanged input forward and add it to each sub-layer output. This shared residual stream is why information from the embedding can reach the final layer directly, and why the model can learn to make small, additive edits at each block rather than rebuilding the representation from scratch.
The N stacked blocks node stands for the full depth of the model. A small model might have a dozen blocks, while a frontier model can have many dozens. Every block has the same interface, so the model can be scaled by changing depth and width without redesigning the flow.
Request Flow
- 1
1. Tokenize and look up embeddings
The input text is already converted to token ids by the tokenizer. The embedding layer maps each id to a learned vector by indexing a large embedding matrix. The result is a sequence of hidden vectors, one per token.
- 2
2. Add positional information
Attention alone is order agnostic, so the model injects position. Older models add sinusoidal or learned positional vectors, while most modern models use rotary position embeddings applied inside attention. Either way, the model now knows both what each token is and where it sits.
- 3
3. Compute self-attention with a causal mask
Each token forms query, key, and value vectors. Attention scores are query-key dot products, scaled and softmaxed. A causal mask sets scores for future tokens to negative infinity so each position only attends to itself and earlier positions. Each token output is a weighted sum of value vectors.
- 4
4. Apply the first residual and normalization
The attention output is added back to the block input through a residual connection, and normalization keeps activations well scaled. In a pre-norm model, normalization is applied before the sub-layer, which is the common modern arrangement.
- 5
5. Run the feed-forward network
Each position independently passes through two linear layers with a nonlinearity between them. This is where much of the model per-token computation and many of its parameters live. It transforms the attention-enriched representation into the block output.
- 6
6. Apply the second residual and normalization
The feed-forward output is added back through a second residual connection and normalized. The block now returns a sequence of hidden vectors with the same shape it received, ready for the next block.
- 7
7. Repeat through all blocks
Steps three through six repeat for every block in the stack. Each block reads the residual stream, adds its contribution, and passes it on. Representations become progressively more abstract with depth.
- 8
8. Project to vocabulary logits
The final hidden vector for the last position passes through the language modeling head, a linear projection to vocabulary size, producing a logit for every possible next token. Softmax turns logits into probabilities, and the sampling lesson covers how a concrete token is then chosen.
Deep Dive
Why attention cost grows with the square of sequence length
Self-attention compares every token to every other token, so for a sequence of length n the attention matrix has n times n entries. Doubling the context roughly quadruples attention compute and the memory for attention scores. This quadratic term is the reason long context is expensive and the reason a whole research area exists around efficient and approximate attention.
For interviews, tie this to product limits. A 128K context request does far more attention work than a 4K request, which affects both latency and price. The context window lesson expands on how the key-value cache changes this picture during generation.
Multi-head attention and why more heads help
Instead of one attention computation, the model splits the hidden vector into several heads, each with its own smaller query, key, and value projections. Each head can specialize, for example one head tracking subject-verb agreement while another tracks bracket matching. The head outputs are concatenated and projected back to the hidden size.
The practical tradeoff is that more heads give more distinct relationship channels but each head has a smaller dimension. Head count and head dimension are tuned together, and modern serving optimizations such as grouped-query attention reduce the key-value memory by sharing keys and values across heads.
Parameter budget: where the weights actually live
The largest parameter groups are the token embedding matrix, the attention projection matrices, and the feed-forward layers. Feed-forward layers usually dominate because they expand the hidden size by roughly four times and then project back, at every layer. The embedding and output projection can share weights in some models to save parameters.
Knowing this helps you reason about scaling. Widening the hidden size grows feed-forward and attention weights, while adding layers multiplies the whole block. Both increase quality and cost, which is exactly the tradeoff the model selection lesson formalizes.
Encoder, decoder, and encoder-decoder variants
Encoder-only transformers such as BERT read the whole sequence bidirectionally and are ideal for producing embeddings and classifications. Decoder-only transformers such as GPT use causal masking and are ideal for generation. Encoder-decoder transformers such as the original translation model use an encoder to read the input and a decoder to generate the output.
Most chat and coding models today are decoder-only because a single causal stack is simpler to scale and serve, and it handles both understanding and generation. Embedding models used for retrieval are usually encoder-style, which is why the tokenization and embeddings lesson connects here.
Production Considerations
Memory and layout on the GPU
Serving a transformer means holding the weights and the activations in GPU memory. Weights are fixed, but activations and the key-value cache grow with batch size and sequence length. Large models are sharded across GPUs using tensor and pipeline parallelism, and the block uniformity makes this sharding regular and predictable.
Numerical precision
Production inference commonly uses lower precision such as FP16, BF16, or INT8 quantization to fit larger models and run faster. Normalization layers and attention softmax need care to stay numerically stable in low precision. Precision choices trade a small amount of quality for large gains in memory and throughput.
Architecture versioning and reproducibility
The exact architecture, including layer count, head count, normalization placement, and position encoding, must be recorded with each model version. Mismatched architecture assumptions cause silent quality regressions, so serving stacks pin model configuration alongside weights.
Interview Perspective
What interviewers look for
- ✓A clear end-to-end trace from token ids to output logits without hand waving.
- ✓Correct understanding that attention mixes information across positions and the feed-forward network transforms each position.
- ✓Awareness of residual connections and normalization as the reason deep transformers train.
- ✓Ability to connect architecture to cost, especially the quadratic attention term and where parameters live.
Alternative designs
State-space and recurrent alternatives
Architectures such as state-space models aim for linear scaling with sequence length instead of quadratic. They can be attractive for very long sequences, but transformers remain dominant for general language tasks due to quality and tooling maturity.
Mixture-of-experts feed-forward
Instead of one dense feed-forward network per block, a mixture-of-experts routes each token to a few expert networks. This increases total parameters and capability while keeping per-token compute lower, at the cost of routing complexity and serving challenges.
Likely follow-up questions
Why do transformers need positional information when recurrent networks do not?
Attention treats the input as a set and computes the same scores regardless of order, so without positional information the model cannot tell a sentence from its shuffled version. Recurrent networks process tokens in sequence, so order is implicit. Transformers add positional encodings or rotary embeddings to restore order while keeping the parallelism that makes them fast to train.
What makes the causal mask necessary in a decoder-only model?
Next-token prediction must not let a position peek at future tokens, otherwise training would leak the answer. The causal mask sets attention scores to future positions to negative infinity before softmax, so each token only attends to itself and earlier tokens. This makes the training objective well defined and matches how generation works one token at a time.
How would you reduce the cost of very long context?
Options include sparse or windowed attention that limits how many tokens each position attends to, grouped-query attention to shrink the key-value cache, retrieval so the model reads a short relevant slice instead of everything, and caching of the prompt prefix. Each trades some quality or generality for lower compute and memory, and the right choice depends on the workload.
Common mistakes
- ×Claiming attention understands meaning by itself, when it only computes weighted sums that the feed-forward network then transforms.
- ×Forgetting the causal mask and describing a decoder that can see future tokens.
- ×Ignoring residual connections and normalization, which are the reason deep models train at all.
- ×Assuming making a model bigger means a new architecture rather than more depth and width.
Visual Learning
Transformer family variants
| Variant | Masking | Best for | Example |
|---|---|---|---|
| Encoder-only | Bidirectional, no causal mask | Embeddings and classification | BERT |
| Decoder-only | Causal mask | Text generation and chat | GPT, Claude, Llama |
| Encoder-decoder | Encoder bidirectional, decoder causal | Translation and summarization | T5 |
What each sub-layer does
| Sub-layer | Operates across | Main job | Cost driver |
|---|---|---|---|
| Self-attention | All positions | Gather relevant context | Sequence length squared |
| Feed-forward | Each position alone | Transform the representation | Hidden size |
| Add and Norm | Each position | Stabilize and preserve the residual stream | Small |
Decision guide
How to reason about transformer size
Use a small model with fewer blocks when latency and cost dominate and the task is narrow. It will be cheaper and faster but less capable on complex reasoning.
Use a larger model with more blocks and wider hidden size when quality on hard, open-ended tasks matters more than cost. Expect higher latency and memory.
For long context needs, remember attention scales with the square of length. Prefer retrieval to feed a short relevant slice, or choose a model with efficient attention, before paying for a giant raw context window.
For embeddings and retrieval, reach for an encoder-style model rather than a large decoder, since you only need a good vector, not generation.
Hands-on Examples
Estimate parameters from architecture dimensions
A rough parameter estimate helps you reason about memory before you ever load a model. This estimator counts the dominant feed-forward and attention projection weights across all layers.
param_estimate.py
def estimate_params(hidden, layers, ffn_multiplier=4, vocab=50000):
attn_per_layer = 4 * hidden * hidden
ffn_per_layer = 2 * hidden * (ffn_multiplier * hidden)
per_layer = attn_per_layer + ffn_per_layer
blocks = per_layer * layers
embedding = vocab * hidden
total = blocks + embedding
return {
"per_layer_millions": round(per_layer / 1e6, 1),
"blocks_billions": round(blocks / 1e9, 2),
"total_billions": round(total / 1e9, 2),
}
print(estimate_params(hidden=4096, layers=32))
print(estimate_params(hidden=8192, layers=80))Apply a causal mask to attention scores
The causal mask is a lower-triangular allow pattern. This snippet shows how future positions are set to a very negative value before softmax so they receive near-zero weight.
causal_mask.py
import math
def masked_scores(scores):
n = len(scores)
for i in range(n):
for j in range(n):
if j > i:
scores[i][j] = -math.inf
return scores
example = [[0.2, 0.9, 0.1], [0.5, 0.3, 0.4], [0.1, 0.2, 0.7]]
for row in masked_scores(example):
print(row)Quiz
0/6 answered
1.What is the interface of a single transformer block?
2.Which component allows tokens to exchange information with each other?
3.Why does attention compute scale with the square of sequence length?
4.What is the purpose of residual connections in a transformer?
5.Which transformer variant is the usual choice for text generation and chat?
6.Where do most of a transformer parameters usually live?
Flashcards
Cheat Sheet
Transformer architecture cheat sheet
The stack, top to bottom
- Token ids in.
- Embedding lookup plus positional information.
- N identical blocks, each with attention then feed-forward, both wrapped in residual and normalization.
- LM head projects the final hidden state to vocabulary logits.
- Softmax and sampling pick the next token.
One block
- Self-attention: mixes information across positions using query, key, value.
- Add and Norm: residual connection plus normalization.
- Feed-forward: transforms each position, expands by about four times then projects back.
- Add and Norm again.
Key facts
- Attention cost grows with sequence length squared.
- Feed-forward layers hold most parameters.
- Residual stream runs from embedding to output.
- Causal mask enforces left-to-right prediction in decoder-only models.
Cost levers
- More blocks: more depth, more capability, more latency.
- Wider hidden size: more capacity, larger feed-forward and attention weights.
- More heads: more relationship channels, smaller per-head dimension.
- Longer context: quadratic attention growth.
Variants
- Encoder-only: embeddings and classification.
- Decoder-only: generation and chat.
- Encoder-decoder: translation and summarization.
References
- PaperAttention Is All You Need — Vaswani et al.
- BlogThe Illustrated Transformer — Jay Alammar
- PaperLanguage Models are Few-Shot Learners — Brown et al.
- PaperA Mathematical Framework for Transformer Circuits — Anthropic