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

- URL: https://dmxsf.com/en/wiki/architecture/
- [DMXSF](https://dmxsf.com/en/about/)
- Updated: 2026-11-13

## 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](https://dmxsf.com/en/blog/attention-is-all-you-need/), [LLaMA](https://dmxsf.com/en/blog/llama-open-and-efficient-foundation-language-models/) and [Switch Transformers](https://dmxsf.com/en/blog/switch-transformers-scaling-to-trillion-parameter-models/); the explainers matching the MoE and long-context pages are [Mixtral of Experts](https://dmxsf.com/en/blog/mixtral-of-experts/), [DeepSeek-V3](https://dmxsf.com/en/blog/deepseek-v3-technical-report/) and [YaRN](https://dmxsf.com/en/blog/yarn-efficient-context-window-extension-of-large-language-models/).

[Chapter labs on GitHub](https://github.com/dmxsf/dmxsf/tree/main/labs/architecture)
