One Sequence Supplies Queries, Keys, and Values
Self-attention projects queries, keys, and values from the same source sequence, while the mask separately controls which positions may be read. Contrast this source rule with cross-attention and keep the three projected roles distinct.
The word self in self-attention describes where queries, keys, and values come from. They are projected from the same input sequence:
It does not mean that a position may attend only to itself. Depending on the mask, a query may read every position, only its causal prefix, or another allowed subset.
The Three Roles Remain Different
Consider a row from . The same row creates three records:
Their jobs are not interchangeable:
- represents what position is looking for;
- represents how position can be matched;
- represents what position can contribute.
The query at position is compared with keys. The resulting weight on column multiplies , not . A high query-key score therefore routes the corresponding value; it does not copy the key into the result.
Self-Attention and Cross-Attention Differ by Source
The following table separates three choices that are often mixed together:
| Operation | Query source | Key/value source | Typical visibility |
|---|---|---|---|
| Bidirectional self-attention | sequence | the same | all unpadded positions |
| Causal self-attention | sequence | the same | current and earlier positions |
| Cross-attention | sequence | another sequence | allowed positions in |
For cross-attention,
The query length and key-value length may now differ. If has positions and has positions, the score matrix has shape . Chapter 5 will place this operation inside an encoder-decoder architecture. Here it is enough to see that self versus cross and causal versus bidirectional answer different questions.
A Two-Position Example
Let
Then
The first query has the same dot product, 1, with both keys. If both positions are visible, softmax assigns equal weights and the reading is
The keys decided that the match was tied. The values decided that the reading contained 1 in its first coordinate and 1.5 in its second. Replacing would change the reading without changing any attention weight.
Q1. Separate source from visibility
A layer forms Q, K, and V from the same sequence, but a mask lets each query read only its causal prefix. Which description is exact?
Select one choice, then check.
Hint
Solution
Use Two Questions When Reading an Architecture
For any attention operation, first identify the source of Q and the source of K/V. Then identify the visibility mask. This two-question method is more reliable than inferring behavior from the word attention alone.