Transformer Architecture and Its Variants
After tokenization, the model finally receives a sequence of ids. A frontier dense model turns those ids into predictions with a stack of nearly identical transformer blocks, and that block has barely changed since 2023 (Touvron et al. 2023; Bai et al. 2023). Almost everything inside it is a question that has been answered and closed: how to normalize, how to activate, how to encode position. One question stays open, and it is not in the math of the forward pass at all. It is a cost that lives in memory during generation, the size of the key-value cache, and it is the main reason an architect still redraws a working block. The useful reading is therefore double: the block as a settled forward pass, and the same block as the single open cost that drives attention variants from MHA through MQA and GQA to MLA.
One block, two jobs
A transformer block has to do two distinct jobs and do them in a way that survives being stacked a hundred times deep. It must let tokens exchange information, since a language model is useless if position cannot see position , and it must transform each token's features, which is where most of the model's knowledge lives. Stacking those two operations naively does not work: a deep residual network is hard to keep trainable, the activation and normalization choices decide whether the gradient (the signal that says how to adjust each parameter to lower the loss) survives to the bottom layer, and attention, the one operation that mixes across positions, costs in compute and leaves behind a cache that grows without bound during generation.
The right way to read a block is not "attention, then MLP." It is two sublayers that each read a shared residual stream, compute a delta, and write it back:
The residual stream is the model's working memory, a vector of width carried from the embedding to the final layer. Each block gets one pass to read it and add to it. Depth is the number of such read-modify-write passes; width is how much the stream can hold at once. This framing makes the rest of the design fall out. Figure 8.1 draws one pre-norm block this way: the residual stream runs straight down as an identity path, and each sublayer branches off it through a norm, computes a delta, and adds the delta back.
The first sublayer is attention, the only place tokens exchange information, and the subject of the second half of this chapter. The second is a position-wise MLP, also called the feed-forward network or FFN, applied to each token independently, and it holds most of the parameters: it expands to roughly four times and projects back, costing about per block, dwarfing attention's projections. Knowledge lives mostly in the MLP; mixing lives entirely in attention.
Three questions the field has closed
Three choices keep a deep stack of these blocks trainable, and all three are settled. They are not live debates; they are the record of how the consensus stack was assembled, one near-free win at a time.
Normalization has two orthogonal axes. The form is LayerNorm (Ba et al. 2016), which re-centers and re-scales with a learned gain and bias, versus root mean square layer normalization (RMSNorm) (Zhang and Sennrich 2019), which only re-scales, dropping the mean subtraction and the bias. Mean-centering buys little, so RMSNorm is cheaper and now the default. The placement is post-norm, the original design with the norm after the residual add (Vaswani et al. 2017), versus pre-norm, with the norm inside the branch and an identity path straight down the residual stream. Pre-norm keeps a clean gradient highway from the top of the stack to the bottom, and it is what lets a deep model train without elaborate warmup (slowly ramping the learning rate up over the first steps of training) (Xiong et al. 2020).
Activation has a clear lineage: ReLU, simple but prone to dead units, then GeLU (Hendrycks and Gimpel 2016), the smooth BERT and GPT-2 default, then gated linear units, of which Swish-gated linear unit (SwiGLU) is the modern winner (Shazeer 2020). SwiGLU computes , three matrices rather than two. The easy-to-miss detail: the hidden dimension is scaled by about so the gated FFN keeps the same parameter count as a plain FFN, which is what makes a like-for-like comparison fair.
Position is the third. Attention is permutation-invariant on its own, so the block must be told where tokens sit. rotary position embedding (RoPE) rotates the query and key vectors by an angle proportional to absolute position, in two-dimensional subspace pairs, so that the dot product depends only on the relative offset (Su et al. 2021). A base-frequency parameter, theta, sets how fast the rotation turns and quietly governs how far context can later be extended. ALiBi takes the other route: it adds a static, head-specific linear penalty to the pre-softmax scores, with no learned positional parameters at all (Press et al. 2022). The contrast is rotations on vectors versus biases on scores. RoPE became the dense default.
One bracket choice frames the stack from outside. Embedding and unembedding sit at the two ends: the input embedding maps token ids to vectors, the LM head maps the final residual state to vocabulary logits (the raw per-token scores the model produces before softmax turns them into probabilities). Weight tying shares one matrix for both (Press and Wolf 2017). This is a genuine tension, not a default. Tying saves parameters and regularizes small models, but once the vocabulary is large the saving is marginal, and most large modern models leave the two untied.
These choices, assembled, are the whole settled block. Vaswani et al. shipped the original transformer with post-norm and a plain FFN (Vaswani et al. 2017). Because post-norm diverges at depth without heavy warmup, pre-norm replaced it (Xiong et al. 2020); RMSNorm then shed LayerNorm's mean subtraction at no measured quality cost (Zhang and Sennrich 2019). SwiGLU edged out GeLU under the rescaling (Shazeer 2020), and on the positional axis RoPE displaced ALiBi as the default (Su et al. 2021). By the time of LLaMA and Qwen, the consensus had set: pre-norm, RMSNorm, a SwiGLU FFN, RoPE, and no bias terms anywhere (Touvron et al. 2023; Bai et al. 2023). Strikingly little has changed in the dense block since 2023; the action moved to data, scale, and post-training.
The one cost that stays open
Now attention itself, the sublayer that mixes across positions and the source of the chapter's live constraint. Scaled dot-product attention projects each token to a query, key, and value, then computes (Vaswani et al. 2017)
Softmax turns each row of scores into positive weights that sum to one; the divisor keeps the logits from growing with dimension and saturating it. A causal mask zeros the upper triangle so a token attends only to the past. The cost is in both compute and the score matrix, which is the pressure behind every downstream efficiency technique.
Multi-head attention splits into heads so attention can attend to several subspaces at once, concatenating their outputs and projecting once more (Vaswani et al. 2017). Heads are cheap in parameters but not free at inference, because each head carries its own keys and values, and those must be kept.
Keeping them is not a detail; it is what the rest of the chapter costs. To see why, follow how the block actually turns a prompt into text, because a language model generates one token at a time and does it in two passes with very different shapes. The first pass is prefill: it reads the entire prompt at once, every position attending to every earlier one, and computes the keys and values for all prompt tokens together. The second pass is decode: it emits the answer one token at a time, feeding each token just produced back in to produce the next. Decode is autoregressive: the output of one step is the input of the next, so the sequence grows by one position per step.
Decode is where keeping the keys and values pays off. A token's key and value never change once computed, so rather than recompute attention over the whole sequence at every step, which would be quadratic work repeated for every token, the model caches them and each step does the attention math for only the single new token against the cache of all the past ones. The cache is the price of not recomputing. That is why these vectors must be kept, and why their cost is measured in memory rather than compute. The serving consequences of the prefill-decode split, the latencies it sets and the scheduling it forces, are the subject of Chapter 31; here it is enough that decode is what gives the cache a reason to exist.
That cost is the spine of the chapter. The cache size is
It grows linearly with context length and batch size, and at long context it overtakes the model weights themselves. Figure 8.4 draws that linear growth and the crossover with a flat weights line, with the slope of each variant set by how many KV heads it keeps. This single fact is what explains MQA, GQA, and MLA. The question each answers is: how much of the KV cache can you delete before quality breaks?
Try this for a 7B-class model: plot each variant's KV cache against context and read off where it overtakes the weights. Change n_kv to see how sharing heads pushes the crossover out.
import numpy as np
import matplotlib.pyplot as plt
layers, d_head, dtype, params = 32, 128, 2, 7e9 # fp16 bytes, 7B weights
weights_gb = params * dtype / 1e9
seq = np.linspace(0, 200_000, 400)
for name, n_kv in [("MHA", 32), ("GQA", 8), ("MQA", 1)]:
per_tok = 2 * layers * n_kv * d_head * dtype # bytes per token
cache_gb = per_tok * seq / 1e9
crossover = weights_gb * 1e9 / per_tok
print(f"{name:3s}: {per_tok/1024:6.0f} KB/token, crosses weights at {crossover:>7.0f} tokens")
plt.plot(seq / 1000, cache_gb, label=name)
plt.axhline(weights_gb, ls="--", color="gray", label=f"weights ({weights_gb:.0f} GB)")
plt.xlabel("context length (1000s of tokens)"); plt.ylabel("KV cache (GB)")
plt.legend(); plt.title("KV cache vs context, batch=1"); plt.show()
The first answer cuts the most. Multi-query attention shares a single key/value head across all query heads, cutting the cache by a factor of (Shazeer 2019). The cut is dramatic, but collapsing to one KV head can destabilize training and drop quality non-gracefully. The second answer interpolates. Grouped-query attention shares K and V within groups of query heads, recovering MHA () at one extreme and MQA () at the other, and it can be uptrained cheaply from an existing MHA checkpoint (Ainslie et al. 2023). GQA recovers most of the quality at most of the savings and became the default in the LLaMA and Qwen families (Touvron et al. 2023; Bai et al. 2023). Figure 8.5 shows the mechanism: the cache holds one K and V per distinct KV head, so the savings come entirely from how many query heads are made to share each one.
Multi-head latent attention, introduced in DeepSeek-V2, attacks the same cost from a different angle (DeepSeek-AI 2024). Instead of sharing heads, it projects K and V down into a low-rank latent vector that is cached in place of the full per-head K and V, decompressing on the fly, with a decoupled RoPE component carried alongside. The pitch is GQA-class cache savings, or better, at MHA-class quality. It is the second branch of the cache-compression frontier that Figure 8.6 lays out.
The third branch is younger and attacks a different term. Sharing and compressing both shrink what each cached token costs, but every past token is still attended, so the score computation survives untouched. Sparse attention drops that premise: each query attends to a selected subset of past tokens rather than to all of them, cutting attention compute and long-context cost together. Bolting a sparsity pattern onto a model trained with full attention has historically cost quality, so the current designs learn the selection during training. DeepSeek's native sparse attention (NSA) trains a hierarchical token selection jointly with the rest of the model (Yuan et al. 2025), and its successor, DeepSeek sparse attention (DSA), shipped in the production model DeepSeek-V3.2-Exp in September 2025, uses a lightweight indexer to pick which tokens each query reads (DeepSeek-AI 2025). What sparsity does to serving and to long-context behavior is Chapter 35's territory; here it matters as the frontier's third branch, the first to attack the quadratic itself.
FlashAttention belongs to this story only as a reminder of what these variants do not solve. It keeps attention exact and makes the score matrix affordable by never writing it to high-bandwidth memory, but it does not shrink the KV cache, which is a memory footprint, not a compute kernel (Dao et al. 2022). The serving-time mechanics of that kernel and of cache paging belong to Chapter 33 and Chapter 32.
The key-value cache size motivates an attention variant. MHA's cache grows with the number of heads, and at long context and large batch it overtakes the weights, so an architect trades head independence for a smaller cache: MQA shares one KV head, GQA shares within groups, MLA compresses to a latent. None of these change what attention computes in principle. They exist because a downstream cost, the memory a decoding server must hold per sequence, reaches up and reshapes the block. The serving pressure in Chapter 31 is what makes a working block worth touching at all.
Whether MLA actually breaks the quality-versus-cache trade is not settled. The DeepSeek position is that latent compression delivers cache savings at MHA-class quality, dominating GQA on the frontier (DeepSeek-AI 2024). The conservative position is that GQA, which is simpler, uptrains from an existing MHA checkpoint, and has been validated across many shipped models (Ainslie et al. 2023), remains the safe default, and that MLA's advantage is entangled with the rest of the DeepSeek recipe and harder to transfer than a single comparison suggests. A third position now holds that head arithmetic is the wrong axis entirely: trained sparsity cuts the quadratic compute and the cache together, and with DeepSeek-V3.2-Exp, DSA is already serving in production (DeepSeek-AI 2025). The three coexist today: most open models use GQA, DeepSeek uses MLA with DSA on top. Treat the choice as open, not as a settled win for any of them.
Architecture knobs that carry cost
Every choice above is a balance with a knee. Set side by side, they are the dials an architect actually turns.
- Norm placement, pre versus post. Post-norm can reach slightly better final loss when it trains at all, but it is unstable at depth without heavy warmup and careful initialization. Pre-norm trades a sliver of peak quality for a robustly trainable gradient highway, and wins in practice (Xiong et al. 2020).
- Norm form, LayerNorm versus RMSNorm. Mean-centering buys little. RMSNorm drops it for lower cost and one fewer parameter set, a near-free win, now default (Zhang and Sennrich 2019).
- Activation choice. SwiGLU's gain is small but consistent, and effectively free under the hidden-dimension rescaling (Shazeer 2020). The cost is a third weight matrix and slightly more irregular shapes for kernels and parallelism.
- Positional scheme as a one-way door. The base scheme is chosen at run start and is expensive to change. It constrains how far context can later be extended and how well the model extrapolates. RoPE's theta base is a small knob with large long-context consequences, the mechanisms for which are Chapter 35.
- Weight tying. A real tension: parameter saving and small-model regularization against the freedom and marginal cost at scale that lead large models to untie (Press and Wolf 2017).
- Depth versus width. At a fixed parameter budget, deeper means more sequential composition but harder training and parallelism, with pipeline bubbles and serial latency, while wider means more per-step compute, friendlier to tensor parallelism, and easier to keep stable. This is co-designed with the systems layer in Chapter 10, not resolved in isolation, and the quality-per-FLOP side of it is set by Chapter 5.
- KV cache versus quality, the dominant axis. MHA gives full quality at full cache, GQA most quality at a fraction of cache, MQA the smallest cache at a quality and stability risk, and MLA claims to break the trade through latent compression. The right point depends on target context length and batch, since the cache scales with both.
Building the block, and how it breaks
Assembling the consensus dense block is mostly bookkeeping once the choices above are made: pre-norm with RMSNorm on each sublayer's input, a SwiGLU FFN with the hidden dimension rescaled by , RoPE applied to queries and keys before the dot product, GQA grouping for the KV heads, and no bias terms anywhere. Since LLaMA, nearly every open dense model carries this shape (Touvron et al. 2023).
The failure modes are worth naming because each bites at a different time. Post-norm at depth blows up or stalls early in training, the classic symptom pre-norm was invented to fix (Xiong et al. 2020). A positional scheme or a too-small RoPE theta chosen for short context caps long-context behavior later, and the cost surfaces only at the first context-window bump. Forgetting the rescaling when switching to a gated FFN silently inflates or shrinks the parameter budget and breaks like-for-like comparisons. Untied, unregularized output logits can drift without bound, which is why z-loss guards them, with mechanics in Chapter 5. And collapsing too far toward MQA can destabilize training and drop quality non-gracefully, which is the reason GQA exists (Ainslie et al. 2023): the cheapest cache is not safely the best one.
Further reading
- Vaswani et al., “Attention Is All You Need,” 2017. arXiv:1706.03762
- Ba et al., “Layer Normalization,” 2016. arXiv:1607.06450Layer normalization computes normalization statistics across all hidden units within a single layer and training case, eliminating batch-size constraints and stabilizing recurrent neural network training.
- Zhang & Sennrich, “Root Mean Square Layer Normalization” (RMSNorm), 2019. arXiv:1910.07467RMSNorm replaces LayerNorm's mean-and-variance normalization with RMS-only scaling, achieving comparable accuracy while reducing per-step runtime by 7 to 64 percent.
- Xiong et al., “On Layer Normalization in the Transformer Architecture,” 2020. arXiv:2002.04745This paper uses mean field theory to show that placing layer normalization inside residual blocks (Pre-LN) yields well-behaved gradients at initialization, allowing Transformer training without learning rate warm-up.
- Hendrycks & Gimpel, “Gaussian Error Linear Units (GELUs),” 2016. arXiv:1606.08415This paper introduces GELU, an activation function defined as x times the Gaussian CDF, which outperforms ReLU and ELU across vision, NLP, and speech tasks.
- Shazeer, “GLU Variants Improve Transformer” (SwiGLU), 2020. arXiv:2002.05202This paper proposes GLU variants such as SwiGLU and GEGLU as replacements for the ReLU activation in the Transformer FFN sublayer, finding they improve perplexity and downstream task quality.
- Su et al., “RoFormer: Enhanced Transformer with Rotary Position Embedding” (RoPE), 2021. arXiv:2104.09864RoFormer introduces RoPE, which encodes token positions as rotation matrices in self-attention, giving sequence-length flexibility and decaying inter-token dependency with distance.
- Press et al., “Train Short, Test Long: Attention with Linear Biases Enables Input Length Extrapolation” (ALiBi), 2022. arXiv:2108.12409ALiBi replaces positional embeddings with per-head linear distance penalties on attention scores, enabling transformers trained on short sequences to extrapolate to longer ones at inference time with no added runtime cost.
- Press & Wolf, “Using the Output Embedding to Improve Language Models” (weight tying), 2017. arXiv:1608.05859Tying the input and output embedding matrices in neural language models reduces perplexity and can cut translation model parameter count to less than half with no performance loss.
- Shazeer, “Fast Transformer Decoding: One Write-Head is All You Need” (MQA), 2019. arXiv:1911.02150Multi-query attention (MQA) shares keys and values across all attention heads, cutting memory bandwidth for incremental decoding with only minor quality loss.
- Ainslie et al., “GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints,” 2023. arXiv:2305.13245
- Dao et al., “FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness,” 2022. arXiv:2205.14135
- DeepSeek-AI, “DeepSeek-V2: A Strong, Economical, and Efficient Mixture-of-Experts Language Model” (introduces multi-head latent attention (MLA)), 2024. arXiv:2405.04434
- Yuan et al., “Native Sparse Attention: Hardware-Aligned and Natively Trainable Sparse Attention” (NSA, trainable sparse attention), 2025. arXiv:2502.11089NSA makes sparse attention end-to-end trainable rather than a post-hoc mask: a hierarchical compress-select-window design that speeds up long-context training and decoding while matching full-attention quality.
- DeepSeek-AI, “DeepSeek-V3.2: Pushing the Frontier of Open Large Language Models” (DeepSeek sparse attention (DSA) in production), 2025. arXiv:2512.02556Introduces DeepSeek Sparse Attention (DSA), a fine-grained sparse attention driven by a lightning indexer, first shipped in DeepSeek-V3.2-Exp, cutting long-context attention cost from O(L^2) toward O(Lk) at near-identical output quality.
- Touvron et al., “LLaMA: Open and Efficient Foundation Language Models,” 2023. arXiv:2302.13971LLaMA introduces a family of open foundation language models (7B to 65B parameters) trained exclusively on public data, where smaller models trained on more tokens match or outperform larger proprietary models at inference.
- Bai et al., “Qwen Technical Report,” 2023. arXiv:2309.16609Qwen is a family of large language models trained on up to 3 trillion tokens, covering base pretrained models, RLHF-aligned chat models, and specialized coding and mathematics variants.
- Yang et al., “Qwen2 Technical Report,” 2024. arXiv:2407.10671Qwen2 is a series of open-weight LLMs (0.5B to 72B, plus a 57B-A14B MoE) trained on 7T tokens with GQA, RoPE, SwiGLU, DPO, and strong multilingual coverage across 30 languages.
- Yang et al., “Qwen2.5 Technical Report,” 2024. arXiv:2412.15115Qwen2.5 is a family of LLMs trained on 18 trillion tokens with SFT, DPO, and GRPO post-training, spanning dense models from 0.5B to 72B and MoE variants Qwen2.5-Turbo and Qwen2.5-Plus.
Comments
Log in to comment