4.1.11 · D1Transformer Architecture

Foundations — Masked attention for autoregression

1,894 words9 min readBack to topic

Before you can read the parent note, you must own every symbol it throws at you. Below, each symbol is built from nothing: plain words → the picture → why the topic needs it. They are ordered so each one leans only on the ones above it.


1. A sequence and its positions

Picture a row of numbered boxes:

Figure — Masked attention for autoregression
  • = the position doing the looking (the "query" position).
  • = the position being looked at (the "key" position).
  • = the total number of tokens (the length of the row).

2. Vectors: , , and the dimension

Every token is turned into three such vectors. The parent note stacks them as rows of three matrices:

Symbol Plain words Picture
(Query) "What am I looking for?" an arrow each position points out with
(Key) "What do I offer?" an arrow each position advertises
(Value) "What do I hand over if chosen?" the actual content passed along

The matrix is : rows (one per token) and columns (the numbers in each arrow). Same shape for and .


3. The dot product — measuring "how aligned?"

Figure — Masked attention for autoregression

So is a grid where = (query ) (key ) = alignment of position with position .


4. Scaling by


5. Exponential and softmax

Figure — Masked attention for autoregression

This is the hinge of the whole topic: because , if we push a score down to before softmax, that position gets weight exactly — it is completely ignored, and the remaining weights still sum to automatically.


6. and the mask matrix

Figure — Masked attention for autoregression

7. The attention weight and the output


8. Autoregression and the product


Prerequisite map

Tokens and positions i j T

Vectors Q K V dim d_k

Dot product QK transpose

Scale by sqrt d_k

Softmax via exponential

Neg infinity gives weight zero

Causal mask M lower triangular

Attention weights alpha

Masked attention for autoregression

Product of conditionals

Every arrow feeds into the parent topic. Adjacent tools you will meet next: Positional Encoding, KV Caching, Encoder-Decoder Models.


Equipment checklist

Cover the right side and check you can answer each before moving on.

What does the index mean vs ?
is the position doing the looking (query); is the position being looked at (key).
What is a vector, in one phrase?
An ordered list of numbers, pictured as an arrow in space.
What are , , in the library analogy?
Query = your search, Key = book label, Value = book contents.
What single question does a dot product answer?
How much do these two arrows point the same way (their alignment)?
What does the superscript in do?
Transpose — flips rows into columns so every query meets every key.
Why divide scores by ?
To cancel the natural growth of dot-product size with dimension, keeping scores tame.
What two facts about do we exploit?
and .
What does softmax guarantee about a row of weights?
All positive and they sum to exactly 1.
Why add before softmax instead of multiplying by 0 after?
Because zeros the weight AND the remaining weights still sum to 1 with no renormalizing.
What shape does the allowed region of form and why?
Lower-triangular — position may see positions (past and self) but not the future.
Is the diagonal () masked?
No — a position may attend to itself; only (the future) is forbidden.
What does say in words?
The sentence probability is the product, over each position, of that word's probability given all earlier words.