4.1.11 · D2Transformer Architecture

Visual walkthrough — Masked attention for autoregression

1,749 words8 min readBack to topic

We will build up the machinery of self-attention just far enough to see where a leak happens, then plug it — one step, one picture at a time.


Step 1 — What "attending" even means: a grid of glances

WHAT. A sequence is just a row of tokens. Give each token a position number — this is the same numbering that Positional Encoding injects into the vectors. When token number "reads" the sentence, it decides how much attention to pay to every other token . Collect all those glances into a square grid.

WHY a grid? Because "every token looks at every token" is exactly a table with one row per looker and one column per looked-at. Call it . The entry in row , column is one number: how strongly glances at .

PICTURE. Look at the grid. The looker sits on the row, the target sits on the column. The red cell is "token 3 glancing at token 4" — a glance into the future.

Figure — Masked attention for autoregression

Step 2 — Where the score comes from: query · key

WHAT. Each token produces two little vectors: a query ("what am I looking for?") and a key ("what do I offer?"). The glance score is their dot product.

WHY the dot product and not something else? The dot product is large when the two arrows point the same way and small when they point apart. It is the cheapest possible "how aligned are these two?" meter — exactly the question "does what token wants match what token offers?". That is why attention uses it rather than, say, a distance.

PICTURE. Two arrows: a query and a key. When they line up (small angle) the shaded overlap — the dot product — is big and red; when they are perpendicular it collapses to zero.

Figure — Masked attention for autoregression

Step 3 — Taming the size: divide by

WHAT. Divide every score by , where is the length of the query/key vectors (how many numbers each holds).

WHY divide, and why that number? A dot product of two -long random vectors grows roughly like . If we let scores balloon, the next step (softmax) turns tiny score gaps into all-or-nothing spikes and the gradients die. Dividing by cancels that growth exactly — this is the whole point of Attention Score Scaling.

PICTURE. Left: tall spiky bars (unscaled). Right: the same bars gently flattened after dividing — the red bar is still the tallest, but nothing is off the chart.

Figure — Masked attention for autoregression

Step 4 — Spotting the leak

WHAT. Look again at the full scaled grid . Split it along the diagonal. Everything above the diagonal () is a token peeking at something that comes later in the sentence.

WHY this is fatal. At generation time we produce tokens left-to-right; when we are predicting token , tokens do not exist yet. If training lets row read those future columns, the model learns the shortcut "copy the answer sitting in column ", then falls apart at inference. This is the train–test mismatch the parent note warns about, and it is exactly why Teacher Forcing alone is not enough.

PICTURE. The grid, diagonal drawn in bold. The red upper-triangle is labelled "THE FUTURE — must not be read". The lower part is labelled "past + present — allowed".

Figure — Masked attention for autoregression

Step 5 — The mask: add to the future

WHAT. Build a second grid , the same size, filled with everywhere we allow and everywhere we forbid. Add it to the scores before softmax.

WHY and not "multiply by zero"? Because the very next step exponentiates: , cleanly and automatically. If instead we zeroed weights after softmax, the surviving weights would no longer add up to and we'd have to renormalise by hand (the parent's Mistake 1). Adding before softmax makes the renormalisation happen for free.

WHY only the strict upper triangle? The diagonal () stays — a token is allowed to look at itself (Mistake 3 in the parent). We only kill .

PICTURE. Two stacked grids: the score grid, a big red "+", and the mask grid ( in the lower triangle, glowing red in the upper triangle).

Figure — Masked attention for autoregression

Step 6 — Softmax turns into a clean zero

WHAT. Apply softmax across each row. Softmax takes a row of numbers and returns positive weights that sum to :

WHY softmax? We need the glances to become a probability distribution — a set of positive numbers adding to — so the output is a proper weighted average. Exponentiating makes everything positive; dividing by the row sum makes it total .

WHAT IT LOOKS LIKE. The masked cells had , so : they contribute nothing to the sum and receive weight . The allowed cells share all the probability among themselves.

PICTURE. A single row before and after softmax. The two future cells (red) collapse to a flat ; the past+self cells rise into bars that together fill the bar of height .

Figure — Masked attention for autoregression

Step 7 — The output only mixes the past

WHAT. Each token's output is the weighted blend of the value vectors , using those weights:

WHY this proves causality. Every with is , so those drop out. The sum genuinely stops at . Token 's new representation is built only from tokens — the leak is sealed.

WHAT IT LOOKS LIKE. The final grid is lower-triangular: solid weights below the diagonal, blank white above it — a perfect staircase. This same staircase is what lets KV Caching reuse past keys/values, and it is the defining feature of the Decoder Architecture (versus the full grid used inside Encoder-Decoder Models).

Figure — Masked attention for autoregression

The one-picture summary

Here is the whole journey on one canvas: raw scores add mask softmax lower-triangular weights output from the past only.

Figure — Masked attention for autoregression
Recall Feynman retelling — say it back in plain words

Every token asks a question (its query) and every token wears a label (its key). We measure how well each question matches each label with a dot product, giving a grid of glance scores. We shrink the scores by so the next step doesn't explode. Now the danger: a token could glance forward at words that haven't been generated yet, so it would learn to copy instead of predict. To stop this we add a mask that dumps onto every forward-looking cell. Softmax then exponentiates, and , so those cells politely become zero and the remaining weights re-sum to one for free. The final blend of value vectors therefore uses only the current token and everything before it. The picture is a clean staircase — filled below the diagonal, empty above it — and that staircase is masked attention.

Related reveals:

Why add instead of multiplying by 0 after softmax?
Adding before softmax makes those cells exponentiate to 0 and keeps the surviving weights normalised to sum to 1 automatically.
Is the diagonal masked?
No — a token may attend to itself; only the strict upper triangle (future, ) is masked.
Why divide by ?
Dot products grow like ; dividing cancels that so softmax stays smooth and gradients survive.

Parent: Masked attention for autoregression · Hinglish: 4.1.11 Masked attention for autoregression (Hinglish)