Form scaled query-key scores, mask the strict upper triangle before softmax, then use prefix-only weights to mix values.
Problem trace
Future positions receive exactly zero attention weight
Read it this way:The current query mixes information from earlier and current positions. Its future input is present, but its path is closed. Make that future value ten times larger: the blocked path still adds nothing, so the output stays unchanged. If the future scalar grows from one hundred to one thousand, does its masked contribution change?
Sources and provenance
Full interview answer 12 min read
Compute attention outputs for a sequence while preventing every position from using information from future positions.
Problem
Input. Float64 arrays query and key with shape (n, d), and value with shape (n, dv).
Output. Return an (n, dv) array containing one scaled dot-product attention result per query position.
Scores use dot products divided by the square root of d, normalized over permitted keys.
Rules. Query position i may use positions 0 through i, including itself, but no later position.
Require positive n and d, aligned sequence positions, and representable finite scores.
Preserve the inputs. Repeated values and positions’ content are allowed.
Example. With two all-zero query/key rows and value rows [[2], [6]], return [[2], [4]].
The first position sees only 2; the second averages both values because their scores are equal.
Picture
With Q=K=[[1,0],[0,1]], scores are the identity divided by 2.
Query zero sees only key zero; query one sees both keys.
For V=[[10,0],[0,20]], output zero is [10,0]; output one is approximately [3.3024,13.3952].
Problem trace
Separate coded fixture: mask scores before normalization and value mixing
Input and goalCompute one attention head where each token can read only itself and earlier tokens.
Multiply queries by keysUse Q=K=[[1,0],[0,1]] and V=[[10,0],[0,20]]. QK^T is the 2 by 2 identity score matrix.
1001
axesquery rows x key rows
axes
query rows x key rows
column 0 / row 0
1
column 1 / row 0
0
Scale by square root of widthKey width is 2, so divide by sqrt(2). Scores become [[0.7071,0],[0,0.7071]].
0.7071000.7071
scale1 / sqrt(2)
scale
not shown→1 / sqrt(2)
axes
query rows x key rows→not shown
column 0 / row 0
1→0.7071
Mask every future keyThe strict upper triangle is future context. Set score (query 0,key 1) to -infinity before softmax; row 1 may still read keys 0 and 1.
0.7071mask00.7071
rulekey index > query index -> -infinity
rule
not shown→key index > query index -> -infinity
scale
1 / sqrt(2)→not shown
column 1 / row 0
0→mask
Normalize each allowed prefixRow 0 softmax is [1,0]. Row 1 softmax of [0,0.7071] is approximately [0.3302,0.6698]. Masked future weight is exactly zero.
1.0000mask0.33020.6698
rowSums1.0000; 1.0000
row sums
not shown→1.0000; 1.0000
rule
key index > query index -> -infinity→not shown
column 0 / row 0
0.7071→1.0000
Mix value rowsQuery 0 output is 1*V0 = [10,0]. Query 1 output is 0.3302*V0 + 0.6698*V1 = [3.30,13.40].
10.000.003.3013.40
operationweights @ Vresult[[10,0],[3.30,13.40]]
operation
not shown→weights @ V
result
not shown→[[10,0],[3.30,13.40]]
row sums
1.0000; 1.0000→not shown
Recognize it
Use it when each sequence position may aggregate only its own and earlier value rows, requiring future key positions to receive exactly zero normalized weight.
Keep true
Attention row i contains scores for query i against every key; after masking and softmax, entries j>i are zero and allowed prefix weights j<=i sum to one.
Reuse it
For constrained attention, encode allowed information flow as a score-space mask before normalization so forbidden positions receive zero mass, then mix values with the resulting row-stochastic weights.
Use Q=K=[[1,0],[0,1]] and V=[[10,0],[0,20]]. QK^T is the 2 by 2 identity score matrix. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Check: Why can query 0 not use V1 even if that future key would have a high score?
Reference drawing
Problem trace
Future positions receive exactly zero attention weight
Execution trace
Input and goalCompute one attention head where each token can read only itself and earlier tokens.
Multiply queries by keysUse Q=K=[[1,0],[0,1]] and V=[[10,0],[0,20]]. QK^T is the 2 by 2 identity score matrix.
1001
axesquery rows x key rows
axes
query rows x key rows
column 0 / row 0
1
column 1 / row 0
0
Scale by square root of widthKey width is 2, so divide by sqrt(2). Scores become [[0.7071,0],[0,0.7071]].
0.7071000.7071
scale1 / sqrt(2)
scale
not shown→1 / sqrt(2)
axes
query rows x key rows→not shown
column 0 / row 0
1→0.7071
Mask every future keyThe strict upper triangle is future context. Set score (query 0,key 1) to -infinity before softmax; row 1 may still read keys 0 and 1.
0.7071mask00.7071
rulekey index > query index -> -infinity
rule
not shown→key index > query index -> -infinity
scale
1 / sqrt(2)→not shown
column 1 / row 0
0→mask
Normalize each allowed prefixRow 0 softmax is [1,0]. Row 1 softmax of [0,0.7071] is approximately [0.3302,0.6698]. Masked future weight is exactly zero.
1.0000mask0.33020.6698
rowSums1.0000; 1.0000
row sums
not shown→1.0000; 1.0000
rule
key index > query index -> -infinity→not shown
column 0 / row 0
0.7071→1.0000
Mix value rowsQuery 0 output is 1*V0 = [10,0]. Query 1 output is 0.3302*V0 + 0.6698*V1 = [3.30,13.40].
10.000.003.3013.40
operationweights @ Vresult[[10,0],[3.30,13.40]]
operation
not shown→weights @ V
result
not shown→[[10,0],[3.30,13.40]]
row sums
1.0000; 1.0000→not shown
Recognize it
Use it when each sequence position may aggregate only its own and earlier value rows, requiring future key positions to receive exactly zero normalized weight.
Keep true
Attention row i contains scores for query i against every key; after masking and softmax, entries j>i are zero and allowed prefix weights j<=i sum to one.
Reuse it
For constrained attention, encode allowed information flow as a score-space mask before normalization so forbidden positions receive zero mass, then mix values with the resulting row-stochastic weights.
Use Q=K=[[1,0],[0,1]] and V=[[10,0],[0,20]]. QK^T is the 2 by 2 identity score matrix. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Check: Why can query 0 not use V1 even if that future key would have a high score?
Read it this way:The current query mixes information from earlier and current positions. Its future input is present, but its path is closed. Make that future value ten times larger: the blocked path still adds nothing, so the output stays unchanged. If the future scalar grows from one hundred to one thousand, does its masked contribution change?
The attention owner explains the weighted mixture.
Here the extra contract is visibility: a strict upper triangle contributes no exponential mass before rowwise normalization.
Check: One query at position 2 can see cached keys at positions 0,1; both receive nonzero weights.
Use when: Local row zero is not absolute sequence position zero.
Costs become O(nqnk(d+dv)) arithmetic and O(nqnk) score workspace.
Reconstruct
Why does a one-row upper-triangular mask fail during cached decoding?
Reasoned answer
It treats the query as position zero and hides every key column after zero, even when those keys are historical.
Build visibility from absolute positions and separately exclude invalid cached entries when padding is present.
An all-false row is rejected rather than evaluated as negative-infinity minus negative-infinity.