3 Model Architecture
  1. In this chapter
  2. From RNNs to Attention
  3. Attention Basics
  4. Positional Encoding Explained
  5. Transformer Block Explained
  6. Modern LLM Architecture
  7. Efficient Attention
  8. Mixture of Experts (MoE)
  9. Long Context
  10. State Space Models and Hybrids
All chapters →

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.

9 pages about 4.5 h Chapter labs on GitHub ↗ Beginner → Advanced Updated

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

  1. 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.

    Beginner 31 min read

    Prerequisites2.3Neural Network Basics2.2From Language Modeling to Next-Token Prediction

  2. 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.

    Beginner 14 min read

    Prerequisites2.5Tokenizers and Embeddings

  3. 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.

    Intermediate 26 min read

    Prerequisites3.2Attention Basics

  4. 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.

    Intermediate 25 min read

    Prerequisites3.2Attention Basics3.3Positional Encoding Explained

  5. 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.

    Intermediate 15 min read

    Prerequisites3.4Transformer Block Explained3.3Positional Encoding Explained

  6. 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.

    Intermediate 27 min read

    Prerequisites3.2Attention Basics

  7. 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.

    Intermediate 31 min read

    Prerequisites3.5Modern LLM Architecture

  8. 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.

    Advanced 40 min read

    Prerequisites3.3Positional Encoding Explained3.6Efficient Attention

  9. 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.

    Advanced 47 min read

    Prerequisites3.2Attention Basics3.1From RNNs to Attention3.6Efficient Attention

Read first