Fundamentals

Transformers

Last reviewed 30 November 2026

Overview

The transformer is the neural network architecture behind nearly every modern LLM. It was introduced in 2017 for translation and replaced recurrent networks because attention can process a whole sequence in parallel, which makes training on huge datasets practical.

Key concepts

  • Self-attention: each token builds its representation by weighting every other token it can see. Weights come from comparing a token's query with others' keys, then mixing their values.
  • Multi-head attention: several attention patterns run side by side, each able to track a different kind of relationship.
  • Feed-forward layers: applied to each token after attention; hold much of the model's stored knowledge.
  • Residual connections and layer normalisation: keep deep stacks trainable.
  • Positional information: attention alone ignores order, so positions are encoded (originally sinusoidal; today usually rotary embeddings, RoPE).
  • Decoder-only and causal masking: LLMs only let each token attend to earlier tokens, so the model can generate left to right.
  • KV cache: at inference, keys and values for earlier tokens are stored so each new token costs one step instead of reprocessing the whole sequence. Prompt caching in APIs builds on this.
  • Mixture of experts (MoE): the feed-forward part is split into many "experts" and a router activates only a few per token. Total parameters can be huge while compute per token stays modest. Most large 2025–2026 open-weight models are MoE, with active parameters often 2–5% of the total: Mistral Large 4 has 1T total and 49B active, gpt-oss-120b 117B and 5.1B, and GLM-5.3 753B (active count not published).
  • Efficient and hybrid attention: to make long contexts affordable, many recent models mix full attention with cheaper layers: sliding-window attention (Gemma 4), linear-attention layers such as Gated DeltaNet (Qwen3.5 uses three for every full attention layer), or compressed sparse attention that shrinks the KV cache (DeepSeek V4-Pro reports 27% of the per-token FLOPs and 10% of the KV cache of V3.2 at 1M tokens). Qwen3.8-Flash-Next adds Qwen Sparse Attention, which first narrows a long sequence to its most relevant regions, to Gated DeltaNet layers. Sparse attention is spreading beyond DeepSeek: Z.ai's GLM-5.3 declares the model type glm_moe_dsa, an MoE with DeepSeek-style sparse attention (inferred from the name; the card does not explain it).
  • Beyond decoder-only: DeepSeek-V4.1-Flash (2026-09) uses a "causal encoder-decoder" MoE: a 20-layer encoder reads the input and a 20-layer decoder generates, with different active parameters for each (8B and 16B).

How to use it

You rarely work with the architecture directly when using an API, but it explains:

  • why cost and latency grow with context length (attention is roughly quadratic);
  • why repeated prefixes can be cached cheaply;
  • what "total vs active parameters" means in MoE model cards.

Current state

  • 2026-11-30: Z.ai's GLM-5.4 model page confirms the loop details: 28 layers of the GLM-5 base run for two iterations. The second pass needs only one sliding window of extra KV cache rather than a full context cache, and Z.ai says the largest gains are in maths, scientific reasoning, and long context, with 10–14% shorter outputs on GPQA-Diamond and MMLU-Pro. Z.ai also warns of overly long "marathon thinking" loops.

  • 2026-11-24: Looped (recurrent-depth) transformers have reached products. A group of weight-shared layers runs more than once per token, giving depth without more parameters. Z.ai's GLM-5.4 (2026-11-23, Coding Plan only, no open weights so far) and Thinking Machines' Inkling 2 Mini (2026-11-20) both use this design.

  • 2026-11-15: Decoder-only MoE transformers remain the dominant design, and hybrid or sparse attention is in production in leading open models (Qwen3.5, February 2026; DeepSeek V4, April 2026; GLM-5.3, August 2026). DeepSeek-V4.1-Flash (2026-09-10) is a causal encoder-decoder MoE whose KV cache is about a quarter of V4-Flash's, and Qwen3.8-Flash-Next (August 2026), a preview of Qwen4, combines linear and sparse attention. No Qwen4 release had reached the library by 2026-11-15. Closed labs do not publish their architectures.

Open questions

  • GLM-5.4: partly answered (2026-11-30) from Z.ai's model page: two loop iterations over 28 layers, extra cost of one sliding KV-cache window (no figure given). Still open: numeric benchmark gains over GLM-5.3. The page shows them only as charts that could not be read automatically, and the blog post (https://z.ai/blog/glm-5.4) still returns an empty page.

  • Will non-attention or hybrid architectures overtake transformers for long context?

Sources