Intuition The One Core Idea
Masked attention is a way of letting each word in a sentence look back at the words before it — and forbidding it from looking forward — so the model learns to predict the next word instead of copying it. Everything on the parent page is just machinery for drawing an invisible curtain that hides the future from each position.
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.
Definition Token and position index
A token is one chunk of text (a word or word-piece) that the model reads. We line the tokens up left to right and give each one a number called its position index , written i or j .
Picture a row of numbered boxes:
i = the position doing the looking (the "query" position).
j = the position being looked at (the "key" position).
T = the total number of tokens (the length of the row).
Intuition Why two different letters?
Attention always compares a pair: "how much should position i care about position j ?" We need two running numbers because we are filling in a whole grid of pairs ( i , j ) . One letter could never name both ends of a comparison at once.
Definition A vector as a list of numbers
A vector is just an ordered list of numbers, e.g. [ 1 , 2 ] . Geometrically it is an arrow in space; a list of 2 numbers is an arrow on a flat page, a list of d k numbers is an arrow in d k -dimensional space. The count d k is the dimension — how many numbers are in each list.
Every token is turned into three such vectors. The parent note stacks them as rows of three matrices:
Symbol
Plain words
Picture
Q (Query)
"What am I looking for?"
an arrow each position points out with
K (Key)
"What do I offer?"
an arrow each position advertises
V (Value)
"What do I hand over if chosen?"
the actual content passed along
Intuition Why three roles, not one?
Think of a library search. Your search box is the Query , each book's spine label is a Key , and the book's contents are the Value . You match your query against labels, then collect the contents of the best matches. Attention is exactly this library lookup, done with arrows. See Self-Attention Mechanism for how Q , K , V are produced.
The matrix Q is T × d k : T rows (one per token) and d k columns (the numbers in each arrow). Same shape for K and V .
The dot product of two vectors multiplies matching entries and adds them up:
[ a 1 , a 2 ] ⋅ [ b 1 , b 2 ] = a 1 b 1 + a 2 b 2
It answers one question: how much do these two arrows point the same way? Big positive = aligned, near zero = perpendicular (unrelated), negative = opposite.
Intuition Why the dot product and not, say, subtraction?
We want a single number that says "these two directions agree." Subtraction gives another vector, not a score. The dot product is the standard tool that squeezes two arrows into one alignment score — exactly what "how much should i attend to j " needs. When we dot every query with every key we get the whole grid at once, and that grid is written Q K T .
K T
The little T (superscript, not the sequence length) means transpose : flip a matrix so its rows become columns. We flip K so that its columns line up with Q 's rows, which is precisely the arrangement that makes matrix multiplication compute every query-key dot product in one shot.
So A = Q K T is a T × T grid where A ij = (query i ) ⋅ (key j ) = alignment of position i with position j .
Definition Square root and why we divide
d k is the number that, multiplied by itself, gives d k . We divide every score by it:
d k Q K T
Intuition Why this exact tool?
When you add up d k products, the total tends to grow roughly like d k — bigger arrows in more dimensions give bigger dot products just by chance. Dividing by d k cancels that growth, keeping the scores in a tame range so the next step (softmax) behaves. See Attention Score Scaling for the full reason. Without it, scores blow up and gradients vanish.
Definition The exponential
e x
e x is a function that turns any number into a positive number, and turns bigger inputs into much-bigger outputs (it grows explosively). Two facts we lean on:
e 0 = 1
e − ∞ = 0 (as the input plunges to minus infinity, the output collapses to zero)
Softmax turns a row of scores into a row of weights that are all positive and add up to 1 — a probability distribution:
softmax ( s ) j = ∑ k e s k e s j
Exponentiate each score, then divide by the total so the whole row sums to 1 .
Intuition Why softmax and not just "divide by the sum"?
We need every weight to be positive (a negative "amount of attention" is meaningless) and we want big scores to dominate. Plain division fails on negative scores; the exponential fixes both — it forces positivity and sharpens the contrast between high and low scores. The symbol ∑ (capital Greek sigma) just means "add these up over the index shown underneath."
This is the hinge of the whole topic: because e − ∞ = 0 , if we push a score down to − ∞ before softmax, that position gets weight exactly 0 — it is completely ignored, and the remaining weights still sum to 1 automatically.
Definition Negative infinity as "impossible"
− ∞ is not a real number you can hold; here it is a flag meaning "this score is unreachably small." After softmax it becomes weight 0 . We use it as the mathematical way to say "forbidden — pretend this connection does not exist."
Definition The causal mask
M
M is a T × T grid of the same shape as the score grid. Its entries are:
M ij = { 0 − ∞ j ≤ i ( past or self — allowed ) j > i ( future — forbidden )
We add M to the scores. Where M = 0 nothing changes; where M = − ∞ the score is annihilated.
Definition Lower-triangular matrix
A lower-triangular matrix keeps the entries on and below the main diagonal (top-left to bottom-right) and zeros above it. The allowed region of M (the 0 s) forms exactly this shape — a staircase that lets position i see positions 1 … i and nothing beyond.
j ≤ i includes j = i ?
The diagonal (j = i ) is a position looking at itself — that is allowed and useful. We only forbid j > i , the future . (The parent note's "Mistake 3" is exactly this trap.)
α ij (Greek letter alpha) is the final softmax weight: how much of position j 's value ends up in position i 's answer. Each row of α sums to 1.
Definition The product symbol
∏
∏ (capital Greek pi) means "multiply these together," just as ∑ means "add these together." The parent's factorization
P ( y ) = ∏ t = 1 T P ( y t ∣ y < t )
reads: the probability of the whole sentence = multiply, for every position t , the probability of word t given all the words before it.
Definition The "given" bar and
y < t
The vertical bar ∣ means "given" / "conditioned on." y < t means "all y tokens at positions less than t " — i.e. everything already generated. This is the mathematical statement of no peeking ahead , and the mask is what enforces it inside the network. See Decoder Architecture and Teacher Forcing for where this factorization comes from.
Tokens and positions i j T
Neg infinity gives weight zero
Causal mask M lower triangular
Masked attention for autoregression
Every arrow feeds into the parent topic . Adjacent tools you will meet next: Positional Encoding , KV Caching , Encoder-Decoder Models .
Cover the right side and check you can answer each before moving on.
What does the index i mean vs j ? i is the position doing the looking (query); j 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 Q , K , V 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 T in K T do? Transpose — flips rows into columns so every query meets every key.
Why divide scores by d k ? To cancel the natural growth of dot-product size with dimension, keeping scores tame.
What two facts about e x do we exploit? e 0 = 1 and e − ∞ = 0 .
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 e − ∞ = 0 zeros the weight AND the remaining weights still sum to 1 with no renormalizing.
What shape does the allowed region of M form and why? Lower-triangular — position i may see positions 1 … i (past and self) but not the future.
Is the diagonal (j = i ) masked? No — a position may attend to itself; only j > i (the future) is forbidden.
What does ∏ t P ( y t ∣ y < t ) say in words? The sentence probability is the product, over each position, of that word's probability given all earlier words.