4.1.3 · D1Transformer Architecture

Foundations — Query, key, value matrices

1,968 words9 min readBack to topic

This page builds every symbol used in the parent topic starting from a smart-12-year-old's blank slate. We never use a symbol before you can see it. By the end you own the alphabet the whole topic is written in.


1. A number, then a list of numbers (a vector)

Before matrices, before attention, there is a single number — like . A number alone tells us almost nothing about a word.

So we bundle numbers into a list, written across in brackets:

The picture: think of a vector as an arrow starting at the origin. Each component says how far to move along one axis. With 2 components you can literally draw the arrow on paper.

Figure — Query, key, value matrices

WHY the topic needs it: every word (token) in a transformer is represented by such a list of numbers — that is how a computer "holds" a word. See Embedding Layer for where these lists come from.


2. The dimension — how long is the list?

We keep asking "how many numbers are in this list?" That count gets its own symbol.

The parent note uses several dimensions with subscripts (little labels below the letter):

The subscript is just a name tag: means "the that belongs to keys". Nothing more mysterious than that.


3. The set symbol — "a real list of length "

The parent writes things like . Let us earn every piece.

  • (the blackboard R) means the real numbers — all ordinary numbers like , , .
  • means a list of real numbers. So is "any 4-number list."
  • is the word belongs to. Read as " is a 4-number list of real values."

WHY the topic needs it: it is the shorthand that tells you the shape of every object at a glance — before you compute anything, you know how big it is.


4. Subscripts and index , — labelling the tokens

A sentence has many words. We line them up and give each a position number.

  • ::: the sequence length — how many words (tokens) we have.
  • ::: two roaming positions. When we say "token attends to token ", is the one asking and is the one being looked at.

The picture: a row of numbered boxes, one per word. and are two fingers pointing at boxes — sometimes the same box, sometimes different ones.


5. The matrix — a stack of vectors

If one word is a list (a vector), a whole sentence is a stack of lists: a grid of numbers.

The parent's is exactly this: rows (one per word) and columns (the numbers describing each word).

Figure — Query, key, value matrices

WHY the topic needs it: stacking lets us process every word at once with one operation instead of a loop — the reason transformers are fast. Built by the Embedding Layer and Positional Encoding.


6. Matrix multiplication — the "transform" operation

The parent writes . That is a matrix multiplication. This is the single most important operation on the page, so we build it slowly.

Start with the simplest case: a row-vector times a column of a matrix. That is a dot product.

The picture: slide one list on top of the other, multiply where they line up, sum the pile.

Figure — Query, key, value matrices

Now, matrix multiplication is just many dot products arranged in a grid. To get the entry in row , column of the result, take the dot product of row of the left matrix with column of the right matrix.

WHY the topic needs it: takes each word's -list and turns it into a shorter -list — this is the query transformation. Same machine, three different weight grids, gives Q, K, and V.


7. The weight matrices — learned lenses

The parent uses three:

  • — turns a word into its query.
  • — turns a word into its key.
  • — turns a word into its value.

The superscript , , is just a name tag saying which job the lens does. Each of these is one linear layer — that is exactly what "a learned times your input" means.


8. Transpose — flipping rows and columns

The attention formula contains and the parent also writes .

WHY it appears: to dot-product every query against every key we need shapes to line up. is and is ; flipping to makes it , so has shape — one relevance number for every pair of tokens. That grid of numbers is the whole point.


9. Softmax and the weight symbols

The parent's final formula uses and the output uses .

  • ::: the attention weight — the share of attention token gives to token . Because softmax makes them sum to 1, the row is like slicing a pie among all tokens.
  • ::: the sum sign. It means "add up the following as runs from 1 to ." So = "mix all value vectors, each weighted by its attention share."

The scaling ( is the square root) shrinks scores before softmax so they don't blow up when is large. Full mechanics live in Scaled Dot-Product Attention.


10. How it all connects

Number

Vector = list of numbers

Dimension d

Set notation R to the d

Matrix = stack of vectors X

Dot product

Matrix multiplication X times W

Weight matrices WQ WK WV

Q K V matrices

Transpose

Q times K transpose relevance grid

Softmax gives attention weights alpha

Weighted sum of values = output

Query Key Value Attention

Every foundation above flows down into the parent topic. If any arrow is unfamiliar, revisit that node before reading the parent's derivation.


Equipment checklist

Test yourself — say the answer out loud, then reveal.

What is a vector, in plain words?
An ordered list of numbers; each entry is a component.
What does mean?
The length of a word's original embedding list (number of features per token).
Read aloud.
" is a real-number list whose length is ."
What do , , and stand for?
= number of tokens; = the querying token; = the token being looked at.
What is a matrix, and what is 's shape?
A rectangle of numbers; is (rows = words, columns = features).
Compute .
.
State the shape rule for .
; inner dimensions must match and cancel.
Are fixed or learned?
Learned — trained by backpropagation, starting from random.
Why do we transpose in ?
To align shapes so every query dots with every key, giving an relevance grid.
What does softmax do to a list of scores?
Turns them into positive numbers that sum to 1 — the attention shares .