4.1.12 · D3Transformer Architecture

Worked examples — The original - Attention is All You Need - architecture

2,270 words10 min readBack to topic

Every symbol below is built from the ground up. If you have never seen a matrix, read it as a grid of numbers: a row is a horizontal strip, a column a vertical strip, and the entry at "row , column " sits where strip crosses strip .


The scenario matrix

Here = the size of each key/query vector (how many numbers describe one token in an attention head). = number of tokens. "Score" = the raw dot product before softmax.

# Case class What makes it special Covered by
A Standard forward pass full attention, all positions Ex 1
B Scaling sign & size why divide by ; large vs small Ex 2
C Softmax saturation (limiting) scores , gradient Ex 3
D Degenerate input: single token () attention becomes identity Ex 4
E Causal mask, every row future = , all triangle rows Ex 5
F Cross-attention (asymmetric vs ) from decoder, from encoder Ex 6
G Positional encoding: all four sign quadrants positive & negative Ex 7
H Residual + LayerNorm edge (zero variance) input, why exists Ex 8
I Real-world word problem translation, count the FLOPs-ish shape Ex 9
J Exam twist: multi-head splitting , concat back Ex 10

We now hit every cell.


Example 1 — Cell A: the standard forward pass

Forecast: Guess — will token 1's output lean toward itself or toward token 2? Write your guess.

Figure — The original  - Attention is All You Need -  architecture
  1. Scores . Row , col = dot product of row of with row of . Why this step? The dot product measures overlap: identical directions give , perpendicular give . Each token here is perpendicular to the other, so off-diagonal is .

  2. Scale by . Why this step? Keeps the numbers small so softmax stays in its sensitive region (see Ex 3).

  3. Softmax row 1 over . Exponentiate: , , sum . Why this step? Softmax turns scores into a probability row that sums to — "how much attention token 1 pays to each token."

  4. Weighted sum with : output for token 1 . Why this step? The output is a blend of value vectors weighted by attention.

Verify: Row weights sum to ✓. Token 1 leans toward itself () because it matched itself with score — matching the geometry in the figure (red arrow longest onto itself).


Example 2 — Cell B: why the sign and size matter

Forecast: Does scaling make the two dimensions give the same number? Guess yes/no.

  1. Raw score = . So gives ; gives . Why this step? A dot product of two all-ones vectors is just the count of dimensions — it grows linearly with .

  2. Scaled score . For : . For : . Why this step? Scaling by turns linear growth into square-root growth — much gentler.

Verify: Unscaled ratio ; scaled ratio ✓. Scaling shrinks the blow-up from down to . See Scaled Dot-Product Attention — this is exactly the "variance control" argument.


Example 3 — Cell C: the saturation limit (why big scores kill gradients)

Forecast: As grows huge, does attention become , , or ?

Figure — The original  - Attention is All You Need -  architecture
  1. Weight on token 1 (the sigmoid). Why this step? Divide top and bottom by to get the standard logistic form.

  2. Limit : , weight . Limit : , weight . Why this step? Covers both signs of — the two flat tails of the curve.

  3. Slope of the curve is . At weight or the slope . Why this step? Zero slope = zero gradient = the network stops learning. That is saturation, and it's exactly why we scale (Ex 2) to keep moderate.

Verify (numeric): at , weight and slope ✓. Nearly dead gradient — the accent-red flat region in the figure.


Example 4 — Cell D: degenerate single token ()

Forecast: Does attention change the token at all?

  1. Score matrix is : just , one number. Why this step? With there is nothing else to compare to.

  2. Softmax of a single number is always (since ). Why this step? A probability row of length one must sum to , so it is .

  3. Output . Attention is the identity. Why this step? The weighted sum has one term with weight .

Verify: For any score , exactly ✓. This is the degenerate edge: attention cannot mix information when there is only one token — it just passes it through. (In a real model the FFN and residual still transform it; see Residual Connections.)


Example 5 — Cell E: causal mask, every triangle row

Forecast: Row 1 attends to how many tokens? Row 3?

Figure — The original  - Attention is All You Need -  architecture
  1. Mask rule: entry becomes if (future), else stays. Why this step? Prevents a token from peeking at tokens that come after it — the core of Attention Masking and autoregressive generation.

  2. Row 1 = . Softmax: , . Weights . Why this step? Token 1 has no past, so it attends only to itself.

  3. Row 2 = . Weights . Why this step? Token 2 splits equally across positions 1–2.

  4. Row 3 = . Weights . Why this step? Token 3 sees all three past-or-present positions equally.

Verify: each row sums to : , , ✓. The lower-triangular red region in the figure is exactly the set of allowed attentions.


Example 6 — Cell F: cross-attention (asymmetric shapes)

Forecast: Is the score matrix , , or ?

  1. comes from the decoder ( rows), from the encoder ( rows). See Encoder-Decoder Architecture. Why this step? Cross-attention lets each output token query the whole source sentence.

  2. Score has shape . Why this step? Matrix-product shapes: inner dims cancel, outer dims survive.

  3. Softmax runs across the encoder columns (one distribution per decoder token). Why this step? Each decoder token must decide how to split its attention over the source tokens; the row must sum to .

Verify: shape check ✓; each of the rows sums to over entries. Output shape , back to decoder length ✓.


Example 7 — Cell G: positional encoding across all sign quadrants

Forecast: At which position does first go negative?

Figure — The original  - Attention is All You Need -  architecture
  1. — positive, rising (quadrant I).
  2. — positive, past the peak, falling (quadrant II).
  3. — negative, falling (quadrant III).
  4. — negative, near trough (quadrant IV).

Why this step? Positional Encoding uses precisely because they take both signs and repeat smoothly, so the model can read a position's phase. Covering all four quadrants shows the encoding is never "stuck" positive.

Verify: signs are ; first negative at (since , crosses zero just after pos ) ✓.


Example 8 — Cell H: LayerNorm at the zero-variance edge

Forecast: What is here? What breaks if ?

  1. Mean . So . Why this step? A constant vector has all entries equal to its mean.

  2. Variance . Without we'd divide by — undefined. Why this step? This is the degenerate corner; guarantees the denominator is never zero.

  3. With : output for every entry. Why this step? The numerator is already , so the output is a clean zero vector, not a NaN.

Verify: ; ✓. The turned a "divide-by-zero crash" into a safe .


Example 9 — Cell I: real-world translation word problem

Forecast: Guess the per-head dimension before computing.

  1. (a) Per-head dim . Why this step? Multi-Head Self-Attention splits the features evenly across heads.

  2. (b) Score matrices per layer = one per head = . Across encoder layers: . Why this step? Each layer runs independent attention operations, each producing its own score matrix ( here).

  3. Sanity on shape: each score matrix is (source attends to source), consistent with Teacher Forcing feeding the full target only on the decoder side, not here. Why this step? Encoder self-attention is symmetric over the source tokens.

Verify: ✓; ✓.


Example 10 — Cell J: the exam twist (split, attend, concat)

Forecast: Does concat give or ?

  1. Concat along the feature axis: stack the two head outputs side by side → . Why this step? Concatenation glues columns so each token regains all features. Row count is unchanged.

  2. mixes the concatenated heads: . Why this step? Without the heads never talk; blends their specialised views into one representation.

  3. Check total head width . Why this step? Confirms the split-and-rejoin conserves dimension — a favourite exam gotcha.

Verify: concat shape ✓; after : ✓; ✓.


Recall Self-test (reveal after answering)

Single-token self-attention output ::: the token's own value vector (attention weight ). Decoder row 2 causal weights on 3 tokens with equal scores ::: . Cross-attention score shape for 2 decoder tokens and 3 encoder tokens ::: . Per-head dimension for ::: . Why does LayerNorm need ? ::: to avoid dividing by zero when the input has zero variance.