AI Compass
Compass

The transformer architecture

The blueprint behind almost every current model: blocks, position encoding, the parameter calculation, and what has actually changed since 2017.

·2 min read·By Fachredaktion Technik
DETAIL
3 sections

The idea

A transformer is a stack of identical blocks. Each block does two things: it lets every token look at all the others, and then it processes each token on its own. That repeats twenty to a hundred times.

At the end comes a prediction about the next token, that is a probability for every entry in the vocabulary.

The structure of one block

  1. 01

    Normalise

    Bring the values into a usable range.

  2. 02

    Attention

    Every token mixes in information from all previous ones.

  3. 03

    Add

    The result is added to the input, not substituted for it.

  4. 04

    Feed-forward

    A small network processes each token individually, again with normalisation and addition.

The parameter calculation

Parameters of a decoder transformer

P ≈ L · ( 4h² + 2·h·f ) + V · h

Per block, four attention projections plus the feed-forward layer, plus the embedding table.

L
number of blocks
h
model width
f
feed-forward width, usually 4h or 8h/3
V
vocabulary size

Worked through for L = 32, h = 4096, f = 11008, V = 128,000:

ItemCalculationParameters
Attention, 32 blocks32 × 4 × 4096²2.15 bn
Feed-forward, 32 blocks32 × 2 × 4096 × 110082.88 bn
Embedding128,000 × 40960.52 bn
Total5.55 bn

With a tied output layer it stays there; otherwise another 0.52 billion is added. The calculation also shows why the feed-forward layer is the largest item, and why mixture of experts targets exactly it.

Position encoding

MethodPrincipleBehaviour on longer context
Learned absoluteOne table entry per positionBreaks beyond the training length
SinusoidalA fixed function of positionGeneralises somewhat better
RoPERotates query and key by positionCan be extended by scaling
ALiBiPenalises long distances in the scoreGeneralises best

RoPE prevailed because it encodes relative distances directly in the dot product and can be stretched afterwards to extend context.

RoPE

Rotary position embedding

q̃_m = R_m · q_m, k̃_n = R_n · k_n q̃_m · k̃_n depends only on (m − n) θᵢ = b^(−2i/d)

Query and key are rotated pairwise by a position-dependent angle; the dot product then depends only on the difference of positions.

q_m
the query at position m
R_m
a rotation matrix depending on position m
θᵢ
the frequency of the i-th dimension pair
b
the base, usually 10,000

That is the decisive property: the model learns relative distances rather than absolute positions. Extending context then works by stretching the frequencies, for instance by raising b or scaling the position.

What changed since 2017

ThenNowReason
Post-normPre-normStability at depth
LayerNormRMSNormFaster, equally good
ReLU in the feed-forwardSwiGLUMeasurably better quality
Absolute positionsRoPE or ALiBiLonger context possible
Multi-head attentionGrouped queriesKV cache 4 to 8 times smaller
Encoder-decoderDecoder onlySimpler, scales better
Dense feed-forwardPartly mixture of expertsMore capacity at the same cost

The basic structure, a stack of identical attention plus feed-forward blocks with residuals, is unchanged. Every change improves stability, efficiency and context length, not the principle.

Grouped queries

Rather than computing separate keys and values per head, several query heads share one key-value head.

KV cache with grouped queries

g = n_q / n_kv ⇒ KV cache smaller by factor g

The cache shrinks by exactly the factor by which query heads share a key-value head.

n_q
number of query heads
n_kv
number of key-value heads
g
group size

With 32 query heads and 8 key-value heads, g = 4 and the cache is a quarter of the size. The measured quality loss is a few tenths of a point of perplexity; the gain in possible context length is a factor of four. See Memory and bandwidth.

Related courses and sources

PaperFreeEN

Attention Is All You Need

The 2017 paper that introduced the transformer. Everything called a language model today rests on these eight pages.

The eight pages everything called a language model today rests on.

PaperFreeEN

BERT

Pre-training on masked text, then fine-tuning per task. The pattern that shaped language processing before the large models.

For understanding what shaped language processing before the large models.

CoursePartly free7200 minEN

Deep Learning Specialization

Five courses from the basics of neural networks to sequence models. Thorough, with programming exercises, and in places older than current practice.

For anyone who can program and wants to work through the field completely.

DeepLearning.AIGo to offer
CourseFree1500 minEN

Hugging Face NLP course

Tokenisation, transformers, fine-tuning and deployment, with code throughout. Assumes Python, and in exchange you end up working with real models.

For development with language models once it has to go beyond calling an interface.

Hugging FaceGo to offer
BookFreeEN

Understanding Deep Learning

A modern textbook with unusually clear figures that already covers transformers and diffusion models in full. Free as a PDF.

For a modern entry point; it already covers transformers and diffusion in full.

Was this page helpful?
The transformer architecture