Attention and Position
David I. Inouye
Dynamic Content · Last Updated: October 30, 2026
Self-attention removes the sequential chain within a layer
| \(\boldsymbol{h}_1\rightarrow\boldsymbol{h}_2\rightarrow\cdots\rightarrow\boldsymbol{h}_L\) |
\(X\rightarrow\{\boldsymbol{y}_1,\ldots,\boldsymbol{y}_L\}\) together |
| Distant information travels through successive updates |
Each output directly combines all input positions |
| Position \(\ell\) waits for the preceding state |
Outputs share the same available input collection |
Shorter paths for learning; parallel operations across positions during training.
Column vectors become transposed rows in a sequence matrix
\[X=\begin{bmatrix}\boldsymbol{x}_1^{\mathsf T}\\\vdots\\\boldsymbol{x}_L^{\mathsf T}\end{bmatrix},\qquad
\boldsymbol{q}_\ell=W_Q\boldsymbol{x}_\ell,\qquad Q=XW_Q^{\mathsf T}\]
Likewise \(K=XW_K^{\mathsf T}\) and \(V=XW_V^{\mathsf T}\). Example: four tokens, three features per vector.
![Q, K and V are each four by three. Each colored row contains the transpose of a column query, key or value vector and its three scalar entries.]()
Each query–key inner product fills one score-matrix entry
![A four-by-three Q with query2 highlighted multiplies a three-by-four K transpose with key3 highlighted. The four-by-four product shows all sixteen q_i transpose k_j entries; entry2,3 is highlighted and expanded into three scalar products.]()
\[S=\frac{QK^{\mathsf T}}{\sqrt{d_k}},\qquad
A=\operatorname{softmax}_{\rm row}(S),\qquad
a_{\ell j}=\frac{e^{s_{\ell j}}}{\sum_{r=1}^{L}e^{s_{\ell r}}}\]
Dot products give scores; row softmax gives weights.
Attention is a soft lookup in a key–value table
Dictionary lookup: an exact key selects one stored value.
Attention: a query scores every key, then blends the paired values.
![Queries, keys and values enter from below; the weighted output is above. Query compares with every key; input-dependent weights multiply the paired values and sum them into one output. The illustrated weights are calculated on the following numeric example.]()
Query: features to match. Key: matching features. Value: features to combine.
Softmax turns matching scores into relative combination weights
\[s_{\ell j}=\frac{\boldsymbol{q}_\ell^{\mathsf T}\boldsymbol{k}_j}{\sqrt{d_k}},
\qquad
a_{\ell j}=\frac{\exp(s_{\ell j}-m_\ell)}{\sum_{k=1}^{L}\exp(s_{\ell k}-m_\ell)},
\qquad m_\ell=\max_{k} s_{\ell k}\]
- A larger dot product gives a larger weight relative to the other scores.
- Subtracting the row maximum preserves the weights and avoids large exponentials.
These weights are not probabilities that a token is true or important.
Next: calculate one complete weighted combination.
One weighted combination uses values, not token IDs
Synthetic one-feature query \(\boldsymbol{q}=[1]\); the query is at the.
please |
\([0]\) |
\(0\) |
\(1/6\) |
\([1,0]^{\mathsf T}\) |
send |
\([\log2]\) |
\(\log2\) |
\(2/6\) |
\([0,2]^{\mathsf T}\) |
the |
\([\log3]\) |
\(\log3\) |
\(3/6\) |
\([2,2]^{\mathsf T}\) |
\[\boldsymbol{y}=\tfrac16[1,0]^{\mathsf T}+\tfrac26[0,2]^{\mathsf T}+\tfrac36[2,2]^{\mathsf T}=[7/6,5/3]^{\mathsf T}\]
This is a context feature vector. A later vocabulary head predicts the next token.
Matrix multiplication computes all output states together
Stack token representations as rows of \(X\in\mathbb{R}^{L\times d}\).
\[S=QK^{\mathsf T}/\sqrt{d_k},\qquad A=\operatorname{softmax}_{\rm row}(S),\qquad Y=AV\]
| \(Q,K\) |
\(L\times d_k\) |
Query/key features per token |
| \(QK^{\mathsf T}\) |
\(L\times L\) |
Query rows × key columns |
| \(A\) |
\(L\times L\) |
One normalized weight row per query |
| \(Y=AV\) |
\(L\times d_v\) |
Combined value features per token |
One row of \(A\) gives the input-dependent weights for one output row of \(Y\).
Divide by \(\sqrt{d_k}\) to keep score fluctuations comparable
Assume independent query/key entries with mean \(0\) and variance \(1\).
\[z=\boldsymbol{q}^{\mathsf T}\boldsymbol{k}=\sum_{i=1}^{d_k}q_i k_i,
\qquad \mathbb{E}[z]=0,\qquad \operatorname{Var}(z)=d_k\]
Positive and negative terms partly cancel. Variances add; standard deviations grow as \(\sqrt{d_k}\).
Dividing by \(\sqrt{d_k}\) keeps the score standard deviation at \(1\), for any width.
| \([0,\sqrt{20}]\) |
\([1.1\%,98.9\%]\) |
| Divide by \(\sqrt{20}\): \([0,1]\) |
\([26.9\%,73.1\%]\) |
Avoid near-one-hot attention caused merely by width: saturated softmax has small gradients.
Cross-attention gets queries and context from different sequences
Query-side inputs \(X\in\mathbb{R}^{L_q\times d_x}\); context inputs \(C\in\mathbb{R}^{L_c\times d_c}\).
\[Q=XW_Q^{\mathsf T},\qquad K=CW_K^{\mathsf T},\qquad V=CW_V^{\mathsf T}\]
\[\operatorname{CA}(X,C)=
\operatorname{softmax}_{\rm row}\!\left(\frac{(XW_Q^{\mathsf T})(CW_K^{\mathsf T})^{\mathsf T}}{\sqrt{d_k}}\right)(CW_V^{\mathsf T})\]
- Example: question tokens query representations of a passage.
- Weight matrix: \(L_q\times L_c\). Output: one row per query, \(L_q\times d_v\).
Self-attention is the case \(C=X\). The same soft-lookup mechanism applies.
Next-token prediction determines which context is available
At position \(\ell\), we predict \(w_{\ell+1}\).
Which input tokens may this output attend to?
A. Past and current tokens: \(w_0,\ldots,w_\ell\)
B. Later tokens: \(w_{\ell+1},w_{\ell+2},\ldots\)
C. Both earlier and later tokens
A. Later inputs include the answer we are trying to predict.
We need an autoregressive (causal) mask to enforce that restriction.
A causal mask removes future positions before softmax
![Five by five additive causal mask: zeros on and below the diagonal, negative infinity above it. Rows are queries and columns are keys.]()
\[A=\operatorname{softmax}_{\rm row}(S+M),\qquad Y=AV\]
Self-attention without positions is permutation equivariant
For any permutation matrix \(P\), with no mask or positional encoding:
\[\operatorname{SA}(PX)=P\operatorname{SA}(X)\]
The outputs are the same, just reordered.
| Item A · Item B · Item C |
\(\boldsymbol{y}_A^{\mathsf T}\) · \(\boldsymbol{y}_B^{\mathsf T}\) · \(\boldsymbol{y}_C^{\mathsf T}\) |
| Item C · Item A · Item B |
\(\boldsymbol{y}_C^{\mathsf T}\) · \(\boldsymbol{y}_A^{\mathsf T}\) · \(\boldsymbol{y}_B^{\mathsf T}\) |
Useful for an unordered set: changing the storage order should not change what each item means.
A sequence needs order beyond an unordered collection
| Send the report before the slides. |
Report first, slides second. |
| Send the slides before the report. |
Slides first, report second. |
Focus on report: its relationship to before has changed.
Without a mask or positions, self-attention gives report the same output vector in both sentences—just in a different row.
We want token representations to reflect order-dependent relationships.
Causal masks restrict access; position signals describe location
A fixed causal mask breaks arbitrary permutation equivariance: reordering tokens changes which context they may read.
| Causal mask |
Which earlier/current inputs a query may access |
| Absolute position signal |
Where a token occurs in the sequence |
| Relative position signal |
How far apart two token positions are |
The mask does not explicitly encode token indices or pairwise offsets.
Next: add position features, or rotate query/key features using their positions.
Additive positions combine token features and location features
Look up the token’s embedding, then add a vector describing its position:
\[\boldsymbol{x}_\ell=E[w_\ell,:]^{\mathsf T}+\boldsymbol{p}_\ell,\qquad \boldsymbol{x}_\ell,\boldsymbol{p}_\ell\in\mathbb{R}^{d}\]
Fixed sinusoids: \(p_{\ell,2i}=\sin(\ell\omega_i)\), \(p_{\ell,2i+1}=\cos(\ell\omega_i)\), \(\omega_i=10000^{-2i/d}\).
Pair index \(i=0,\ldots,d/2-1\); even width \(d\).
![Four sinusoidal features for d=4. Vertical slices at positions 0, 2 and 5 give the complete position vectors printed beside the curves.]()
Same token “the” at positions 0 and 2: \(E[\text{the},:]^{\mathsf T}+\boldsymbol{p}_0\ne E[\text{the},:]^{\mathsf T}+\boldsymbol{p}_2\).
RoPE rotates query and key pairs by their positions
Split each query/key into 2-D feature pairs; rotate every pair by a position-dependent angle.
\[R(\alpha)=\begin{bmatrix}\cos\alpha&-\sin\alpha\\\sin\alpha&\cos\alpha\end{bmatrix},\qquad
\widetilde{\boldsymbol{q}}_\ell^{(i)}=R(\ell\theta_i)\boldsymbol{q}_\ell^{(i)}\]
\[[\underbrace{q_{\ell,0},q_{\ell,1}}_{\text{pair 0: }\ell\theta_0},\quad
\underbrace{q_{\ell,2},q_{\ell,3}}_{\text{pair 1: }\ell\theta_1},\quad\ldots]^{\mathsf T},
\qquad \theta_i=10000^{-2i/d_k}\]
Fast and slow pairs cover different distance scales. Keys use the same rotations.
![One illustrative feature pair: query/key positions 1 and 3 versus 2 and 4. Rotating both by an extra 30 degrees preserves the 60-degree angle difference.]()
RoPE compares positions through angle differences
Moving both tokens one step rotates both arrows by the same extra angle: their angle difference stays the same.
\[s\theta_i-\ell\theta_i=(s-\ell)\theta_i,\qquad
(\widetilde{\boldsymbol{q}}_\ell^{(i)})^{\mathsf T}\widetilde{\boldsymbol{k}}_s^{(i)}
=(\boldsymbol{q}_\ell^{(i)})^{\mathsf T}R((s-\ell)\theta_i)\boldsymbol{k}_s^{(i)}\]
The full dot product adds the matches from all feature pairs.
| Relative offset enters the query–key match directly |
Same separation gives the same positional comparison, with content held fixed. |
| Fixed rotations, no learned position table |
Few extra operations; no separate learned vector for every index. |
Used in Llama 2: RoPE is itself a positional encoding, an alternative to adding absolute position vectors.
Multiple heads learn different ways to combine context
![The input at the bottom feeds three separate learned projection and attention heads. Their outputs concatenate along features and pass through an output projection.]()
\[Y=\operatorname{Concat}(Y_1,\ldots,Y_b)W_O^{\mathsf T}\]
Each head has its own query/key/value projections; every head obeys the allowed context.
Next: combine attention with residual paths, normalization and a token-wise MLP.
Implementation detail: padding masks exclude absent tokens
Two messages share a batch:
| Message A |
please · send · the · report |
| Message B |
send · report · <PAD> · <PAD> |
- Attention key mask: padded positions cannot supply values to real queries.
- Loss mask: padded targets contribute no training loss.
A causal mask still applies within each real prefix.