4.1.4 · D1Transformer Architecture

Foundations — Scaled dot-product attention

2,710 words12 min readBack to topic

The parent note opens with one dense line of notation. We will not write it yet — that would use symbols before they are earned. Instead we build every brick from absolute zero, and only at the very end, once each symbol has a picture and a meaning, do we assemble them into the full formula. Nothing is used before it is drawn.


1. A number list is a vector

The smallest brick is a vector: just an ordered list of numbers, like . "Ordered" means position matters — the first slot and the third slot mean different things.

The picture: an arrow from the origin. A 2-number list is an arrow that goes steps right and steps up. Longer lists are arrows in more directions than we can draw, but the idea — a direction and a length — stays.

Figure — Scaled dot-product attention

Why the topic needs it. Each word in a sentence is turned into such a list of numbers (its embedding). That is the raw material attention operates on. See Query-Key-Value Model for where these lists come from.


2. The length (norm) of a vector

Before we combine vectors, we need to say how long an arrow is — its size, ignoring direction.

Why the topic needs it. Length is one of the two things (length and direction) that decide how strongly two vectors align — we use it in the very next step to explain what a dot product really measures.


3. The subscript and summation notation

We keep writing ; there is a compact symbol for that.

Plain example: . That's the whole trick — it is shorthand for "write a plus sign between every slot and add."

Why the topic needs it. Every similarity score is a sum over the slots of two vectors. Without this shorthand the formulas would be pages long.


4. The dot product — measuring "do these point the same way?"

Now we combine two vectors. We want ONE number that says how aligned two arrows are. Both vectors must have the same number of slots; call that count (for Length of the list). Wherever a later section fixes this length to be the query/key length, it will be renamed — but for now it is just "however many slots the two lists share."

Why this tool and not another? We need a similarity score that is (a) a single number, (b) cheap to compute, and (c) smooth so we can later nudge it with Gradient Descent. The dot product is all three. And it has a beautiful geometric meaning that answers exactly our lookup question:

Figure — Scaled dot-product attention

The dot product behaves like a similarity meter:

  • Arrows pointing the same way → large positive number.
  • Arrows at a right angle (perpendicular) → zero.
  • Arrows pointing opposite ways → large negative number.

5. Matrices — stacking many vectors into a grid

We rarely have one vector; we have a whole sentence of them. Stack them into a rectangle.

The picture: a spreadsheet where each row is one word's number-list.

Figure — Scaled dot-product attention

Why the topic needs it. A real sentence is many word-vectors at once, and modern hardware (GPUs) is built to crunch whole grids in one shot rather than one arrow at a time. Bundling every query and every key into a matrix lets us compute all the similarity scores in a single multiplication — the batching the parent note relies on.

Now we can name the three matrices the topic revolves around. Each is defined in the Query-Key-Value Model:

  • — the Query matrix: query vectors (the "questions"), each of length , stacked as rows.
  • — the Key matrix: key vectors (the "labels to match"), each also length .
  • — the Value matrix: value vectors (the "content to retrieve"), each length .

The letters mean: = how many questions, = how many stored items, = length of a query/key list (this is the shared slot-count from the dot product, now given its proper name), = length of a value list.


6. Transpose and matrix multiply — the score grid

To score every query against every key in one stroke, we need two more pieces of notation.

Why flip ? We want the score between query and key to be their dot product. Rows of are queries (length ); we need keys sitting as columns of length so a row-times-column dot product lands. Transposing () puts each key in a column.


7. Softmax — turning raw scores into a set of weights that add to 1

The scores are arbitrary real numbers. We want them to become weights — non-negative and summing to 1 — so we can take a weighted average. That converter is softmax.

Why this tool? We need a smooth, differentiable way to go from "scores" to "a probability distribution." Exponentials guarantee positivity and exaggerate the winner (bigger score → much bigger weight), while the division normalizes. See Softmax Function for the full treatment.

Figure — Scaled dot-product attention

Case check — all cases the reader might hit:

  • All scores equal → equal weights each. (Perfect indecision.)
  • One score huge → weight on it, elsewhere (near one-hot).
  • A very negative score → weight near 0, never exactly 0. Nothing is ever fully ignored unless forced by an Attention Mask.
  • Negative scores are fine: is still positive, so softmax handles any sign.

This softness is precisely why the parent worries about scaling — push scores too big (Example 2) and softmax freezes into a near-one-hot with a flat, near-zero gradient.


8. Variance — why a knob appears at all

The parent's Step 3 argues the scores' spread grows with . One symbol — and one honest assumption — to define.

The picture: a dartboard. Small variance = darts clustered tight at the center; large variance = darts scattered wide.

Why the topic needs it. That spread is exactly what we divide by: keeping softmax in its lively, learnable middle range instead of its frozen peak. The scale factor is a variance-cancelling knob, nothing more mysterious.


9. Putting it together — the full formula, symbol by symbol

Every piece is now earned. Assemble them in the order the data flows:

  1. Score every query against every key (Sections 4–6): .
  2. Calm the spread (Section 8): divide by so the scores have standard deviation .
  3. Turn scores into weights (Section 7): apply softmax to each row, giving a weight grid whose rows each sum to 1.
  4. Blend the values (this last step): multiply the weight grid by the value matrix . This is exactly a matrix multiplication (Section 6) — for query it forms , a weighted average of the value rows using the attention weights. That is the "pull out a blend of the stored content" from our core idea.

You can now read the parent note's formula line by line without meeting a single unexplained symbol. From here you are ready for Self-Attention, Cross-Attention, and Multi-Head Attention, which all reuse this exact machinery.


How these feed the topic

Vector = ordered number list

Norm = length of a vector

Dot product = alignment score

Matrix = stacked vectors

Transpose and matrix multiply

S = QK^T compatibility grid

Variance grows with d_k

Divide by sqrt of d_k

Softmax = scores to weights

Weighted sum of values V

Scaled Dot-Product Attention

Every arrow is a "you must own the tail before the head." Master the leaves (vector, variance) and the trunk (scaled attention) becomes readable line by line.


Equipment checklist

Test yourself — cover the right side and answer out loud.

What is a vector, in plain words?
An ordered list of numbers; pictured as an arrow with a direction and a length.
What does mean?
The single number sitting in slot of the list .
How do you compute the norm of a vector?
Square every slot, add them, take the square root — the Pythagorean length of the arrow.
Read in words.
Multiply matching slots of and , then add all products.
What does the dot product tell you geometrically?
How aligned the two arrows are — positive if same direction, zero if perpendicular, negative if opposite; equals .
What does denote?
The set of all grids (matrices) with rows and columns of real numbers.
Why must we transpose before multiplying?
So keys sit as columns of length , letting row-times-column dot products form the score grid; also so shapes align ( times ).
What is the shape of when is and is ?
.
What does softmax do to a row of scores?
Turns them into non-negative weights that sum to 1, exaggerating larger scores.
Why does softmax use ?
To force positivity and be smooth/differentiable, so bigger scores get exponentially more weight.
What happens to softmax when all scores are equal?
Every weight becomes — a perfectly even split.
Under what assumption does hold?
When each slot is independent with mean 0 and variance 1; then the products' variances add to .
Why divide the scores by ?
To cancel that spread, resetting the scores' standard deviation to about 1 so softmax stays sensitive and its gradients stay healthy.
What is the final step that turns the weight grid into the attention output?
Multiply the softmax weight grid by the value matrix : each output row is , a weighted blend of the values.