4.1.3 · D3Transformer Architecture

Worked examples — Query, key, value matrices

2,308 words10 min readBack to topic

This page is a companion to the parent topic. We already know the three recipes Here we stress-test them. We will feed the machinery tricky inputs — all-positive tokens, mixed signs, a token of all zeros, two identical tokens, orthogonal projections — so that when the exam (or real code) throws one at you, you have already seen it.

Before anything, plain-word reminders so no symbol is unearned:

Figure — Query, key, value matrices

In the picture the teal key points nearly the same way as the orange query, so is large and positive (high relevance). The plum key points off to the upper left, away from the query, so its score is small or negative (low relevance). This is the geometric fact every example below relies on.

The scenario matrix

Every case this topic can throw at you falls into one of these cells. The examples below are labelled with the cell they cover.

Cell Scenario What could break / what to notice
A All-positive token, normal Baseline projection, no sign traps
B Mixed-sign token & weights Cancellation — terms fight each other
C Zero token (degenerate / padding) Every projection is ; score
D Two identical tokens Self-score vs cross-score behaviour
E Orthogonal query & key Dot product exactly — the "right-angle" case
F Scaling limit: score before vs after Why we divide, saturation of softmax
G Full softmax over a row (word problem) Turning raw scores into weights that sum to 1
H Exam twist: , output vector shape Values live in a different-size space

Example 1 — Cell A: the clean baseline

Forecast: guess — will and look similar? (They come from the same but different lenses, so no.)

  1. Query, first output slot. Multiply against column 1 of and add: Why this step? A projection's each output number is a dot product of the token with one column of the weight matrix. Column 1 is the recipe for the first new coordinate.
  2. Query, second slot (column 2): Why this step? The second coordinate uses column 2 as its recipe; we repeat the same token-times-column dot product so that both slots of are filled. So .
  3. Key, both slots the same way with : So .

Why they differ: same word, two lenses. says "what I'm hunting for," says "what I advertise." Different matrices, different arrows.

Verify: has one big component () driven mostly by the third feature (). is small in both slots because its columns partly cancel. Shapes: . ✓


Example 2 — Cell B: mixed signs and cancellation

Forecast: with big positive and negative weights, guess whether the result is large or near zero.

  1. Slot 1: Why this step? Same dot-product recipe. Notice the third term dominates and drags the sum negative.
  2. Slot 2: So .

Lesson: mixed signs mean features can cancel or reinforce. Here the big positive feature hits a negative weight and pulls everything down. There is no error — negative key components are normal; they just mean "this arrow points into the negative region."

Verify: flip the sign of the whole third column input intuition — the third feature contributes to both slots, and both slots are indeed negative. ✓


Example 3 — Cell C: the zero (padding) token

Forecast: guess before computing — does a zero token attend to anything?

  1. Every projection is zero. Each output slot is . So and (length ), and (length ). Why this step? Multiplying the zero vector by any matrix gives zero — the arrow has no length, so no lens can stretch it into something.
  2. Self-score: .

This is exactly why real transformers use a mask: a genuine all-zero score would still get a nonzero softmax weight (softmax of zeros is uniform), so we must additively force padding scores to . The degenerate input reveals why masking exists.

Verify: for all four columns; product is the zero vector, dot product . ✓


Example 4 — Cell D: two identical tokens

Forecast: identical tokens — will the two scores be equal?

  1. Query of token 1 (columns of , only slots 1 and 3 survive since ): Why this step? Zeros in kill those rows of ; only the "1" features contribute.
  2. Key of token 2 (same input, ): Since , .
  3. Scores:

Lesson: when tokens are identical, self- and cross-scores are equal — the query of one lands on the same key arrow. Attention here cannot distinguish position from content alone; that is precisely the job of Positional Encoding.

Verify: and use identical inputs, so they are the same vector; hence both dot products equal . ✓


Example 5 — Cell E: orthogonal query and key (score = 0)

Forecast: these arrows look "different." Guess the sign of the score.

  1. Dot product: Why this step? Score is always the dot product of the query and key arrows.
  2. Geometry: a score of exactly means the two arrows are at a right angle (perpendicular / orthogonal). Neither aligned nor opposed — pure neutrality.

The figure below draws exactly this pair. Notice the little square at the origin marking the corner between the orange query and the teal key : when the corner is a perfect right angle, the score collapses to .

Figure — Query, key, value matrices

This is the boundary case between positive (relevant) and negative (anti-relevant) scores. After softmax it still gets a finite weight, but a smaller one than any positive-scoring token in the row.

Verify: ; lengths are , so the angle satisfies . ✓


Example 6 — Cell F: why we scale by

Forecast: which raw score threatens softmax more — the small one or the big one?

  1. Scale factor: . For : . For : . Why this step? Dot products of random-ish terms grow roughly like in magnitude; dividing by cancels that growth so scores stay in a tame range.
  2. Scaled scores:
  3. Danger without scaling: a raw fed into softmax against neighbours near gives a weight near for one token and near for the rest — the gradient there is almost flat (saturation), so Backpropagation gets tiny updates. Scaling to keeps the softmax responsive.

Verify: and . ✓


Example 7 — Cell G: full softmax row (word problem)

Forecast: guess the ranking of the three weights before computing.

  1. Scale: . Why this step? Same scaling as Example 6, applied to the whole row of raw scores before softmax.
  2. Exponentiate: Why this step? Softmax (defined at the top of this page) first raises to each scaled score; exponentials are always positive and make bigger scores dominate smoothly.
  3. Normalise by the sum : Why this step? Dividing by the total is the second half of softmax — it forces the weights to sum to so they read as "fractions of attention."
  4. Read it: "sat" attends most (63%) to "the", then "cat", least to itself.

Verify: weights sum to (up to rounding), all positive, ordering matches score ordering. ✓


Example 8 — Cell H: exam twist, and output shape

Forecast: what length is the output — , , or ?

  1. Weighted sum of values: Why this step? The output of attention is a convex mix of value vectors — weights sum to 1, so it is a weighted average of the information each token offers.
  2. Component-wise: So .
  3. Shape: the output lives in space — not , not . Values, not keys, set the output size.

Twist resolved: the query/key space () only decides how much to blend; the value space () decides what gets blended, and therefore the output length. In Multi-Head Attention these per-head outputs are concatenated and re-projected back to by a final linear layer.

Verify: each output component lies between the corresponding min and max of (a convex average must); e.g. . Weights . ✓


Recall Quick self-test

A padding token produces which query vector? ::: The zero vector — every projection of is , so it must be masked out. An attention score of exactly means the query and key arrows are... ::: perpendicular (orthogonal, ) — neither aligned nor opposed. Why divide raw scores by ? ::: dot products grow like ; dividing keeps softmax out of its flat saturation region so gradients survive. The attention output vector has length equal to... ::: (the value dimension), because it is a weighted average of value vectors.

Related: Scaled Dot-Product Attention, Self-Attention, Cross-Attention, Embedding Layer, Query, key, value matrices.