Part 1 · Understand LLMs Chapter 3
Model Architecture
From the bottleneck of RNNs to attention, then the complete Transformer block, RoPE, GQA, MoE, long context, and the state space models and hybrids that trade the KV cache for a fixed-size state: the structure of modern LLMs and the reasoning behind each choice.
After this chapter you will be able to
- Say how the bottlenecks of RNNs in sequential computation and long-range dependencies led to attention, and compute scaled dot-product attention by hand.
- Explain why a model needs position information and how positional encoding evolved from sinusoidal encoding to RoPE.
- Draw a Transformer block (LayerNorm, attention, FFN, residual connections) and count its parameters.
- Compare what MQA, GQA, MLA and FlashAttention each save: the KV cache, memory traffic or computation.
- Explain MoE routing and the difference between total and active parameters, and the methods and three costs of long-context extrapolation.
- Explain how linear attention, state space models and hybrids replace the KV cache with a fixed-size state, and what they give up in exact recall.
Almost every mainstream LLM is a variant of the Transformer. This chapter first explains why it appeared (where recurrent networks get stuck), then takes the “standard version” apart down to every matrix multiplication, and then looks at the improvements adopted in recent years, each answering a concrete bottleneck: the KV cache of attention is too large, parameters grow too fast, and the context is too short. The last page returns to the opening question: can the model compress the history into a fixed-size state instead of keeping every token? Linear attention, state space models and hybrids give one family of answers, and their cost can be measured in experiments.
After this chapter you should be able to read the structural fields of a model’s config.json (layers, attention heads, num_key_value_heads, expert counts, RoPE settings) and work out its parameter count and KV-cache size. Every page comes with code you can run, and several pages check these numbers against the real configurations of public models.
To see the trade-offs and experiments of the original papers, read alongside the blog posts Attention Is All You Need, Explained, LLaMA and Switch Transformers; the explainers matching the MoE and long-context pages are Mixtral of Experts, DeepSeek-V3 and YaRN.
Chapter roadmap
- 3.1
From RNNs to Attention: Why the Transformer Appeared
An RNN squeezes history into one fixed-size state and computes it step by step, so gradients pass through a long chain of multiplications. Attention lets every position read every other position directly, in parallel. Experiments measure vanishing gradients, gating and the copy bottleneck.
Prerequisites2.3Neural Network Basics2.2From Language Modeling to Next-Token Prediction
- 3.2
Attention Basics: How a Transformer Reads Context
Understand Q/K/V, scaled dot-product attention and multi-head attention through intuition, a worked numeric example and a minimal code sample.
Prerequisites2.5Tokenizers and Embeddings
- 3.3
Positional Encoding Explained: Absolute, Relative and RoPE
Self-attention cannot see token order, so positional encodings tell the model where a token is or how far apart two tokens are: sinusoidal and learned encodings are added to the input, relative biases and ALiBi act on attention scores, and RoPE rotates queries and keys.
Prerequisites3.2Attention Basics
- 3.4
Transformer Block Explained: Attention, FFN, Residuals and LayerNorm
A Transformer block is multi-head attention plus a feed-forward network (FFN), each wrapped in a residual connection and LayerNorm. This page gives the equations, Pre-Norm versus Post-Norm, a parameter count (about 12d²) and runnable PyTorch code.
Prerequisites3.2Attention Basics3.3Positional Encoding Explained
- 3.5
Modern LLM Architecture: From GPT-2 to Llama, Qwen and DeepSeek
Almost every mainstream open LLM is a refined decoder-only Transformer: Pre-Norm with RMSNorm, a SwiGLU feed-forward network, RoPE positions and GQA attention, and some add mixture of experts (MoE) to grow parameters without growing compute in proportion.
Prerequisites3.4Transformer Block Explained3.3Positional Encoding Explained
- 3.6
Efficient Attention: MQA, GQA, MLA and FlashAttention
Attention has two bottlenecks: the KV cache that decoding rereads at every step, and the N×N score matrix of training and prefill. MQA, GQA and MLA shrink the KV cache; FlashAttention cuts memory traffic with tiling and online softmax, and its output is identical to standard attention.
Prerequisites3.2Attention Basics
- 3.7
Mixture of Experts (MoE): Routing, Load Balancing and Sparse Activation
A mixture-of-experts layer swaps the feed-forward network for many experts and routes each token to only k of them, so total parameters grow while per-token compute stays small. This page computes total and active parameters for Mixtral and DeepSeek-V3 and lets you try load balancing.
Prerequisites3.5Modern LLM Architecture
- 3.8
Long Context: Position Extrapolation, Training Recipes and Cost
Extending a context window from 4K to 128K takes three fixes: RoPE extensions (PI, NTK-aware scaling, YaRN) for unseen positions, staged continued training on long text, and a budget for attention compute and KV cache. This page covers all three with formulas, code and two interactive tools.
Prerequisites3.3Positional Encoding Explained3.6Efficient Attention
- 3.9
State Space Models and Hybrids: Trading the KV Cache for a Fixed-Size State
Linear attention, state space models (Mamba) and the gated delta rule compress history into a fixed-size state: memory stays flat as context grows, but capacity is limited by the state size. Hybrids keep a few attention layers for exact recall; Jamba's KV cache is 1/8 of Mixtral's.
Prerequisites3.2Attention Basics3.1From RNNs to Attention3.6Efficient Attention