TutorialsUnderstanding Transformers › How X, Q, K, V Evolve Layer by Layer

Understanding Transformers · Part 12 of 16

How X, Q, K, V Evolve Layer by Layer

A token doesn't carry one fixed query, key, and value through the whole model. It builds new ones at every layer.

The intuition: the token's understanding keeps updating

It's easy to imagine a token computing its Query, Key, and Value once and reusing them all the way up the stack. That's not what happens. Every layer receives the current hidden representation X and builds fresh Q, K, and V from it, using that layer's own learned matrices:

Q(l) = X(l-1) WQ(l)    K(l) = X(l-1) WK(l)    V(l) = X(l-1) WV(l)

The superscript l marks the layer. Both the input representation and the projection matrices differ from layer to layer, so the resulting Q, K, and V differ too.

What changes, and why

WQ, WK, and WV are different learned parameters in every layer, and usually in every head.

A layer-by-layer story: the word “bank”

StageRepresentation of “bank”Attention might…
Embeddinggeneric, dictionary-like — no context yethave integrated little or nothing
Early layernudged by nearby words like “river” or “muddy”focus on local, lexical clues
Middle layerthe geographic sense is taking holdconnect modifiers and related entities
Late layerhighly contextual, shaped for the prediction taskgather what's needed to pick the next token

The token's identity stays “bank” throughout. What changes is its vector — a steadily richer description of what “bank” means here, what role it plays, and what it should send or seek.

Persistent vs. temporary. X is the evolving state of each token, handed from one layer to the next. Q, K, and V are temporary views of that state, built for one attention operation in one layer and then discarded.