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:

Q=XWQ,K=XWK,V=XWV.Q=XW_Q,\qquad K=XW_K,\qquad V=XW_V.

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 xix_i from XX. The same row creates three records:

qi=xiWQ,ki=xiWK,vi=xiWV.q_i=x_iW_Q,\qquad k_i=x_iW_K,\qquad v_i=x_iW_V.

Their jobs are not interchangeable:

  • qiq_i represents what position ii is looking for;
  • kik_i represents how position ii can be matched;
  • viv_i represents what position ii can contribute.

The query at position ii is compared with keys. The resulting weight on column jj multiplies vjv_j, not kjk_j. 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:

OperationQuery sourceKey/value sourceTypical visibility
Bidirectional self-attentionsequence XXthe same XXall unpadded positions
Causal self-attentionsequence XXthe same XXcurrent and earlier positions
Cross-attentionsequence XXanother sequence ZZallowed positions in ZZ

For cross-attention,

Q=XWQ,K=ZWK,V=ZWV.Q=XW_Q,\qquad K=ZW_K,\qquad V=ZW_V.

The query length and key-value length may now differ. If XX has TqT_q positions and ZZ has TkT_k positions, the score matrix has shape (Tq,Tk)(T_q,T_k). 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

X=[1001],WQ=I,WK=[1111],WV=[2003].X=\begin{bmatrix}1&0\\0&1\end{bmatrix},\qquad W_Q=I,\qquad W_K=\begin{bmatrix}1&1\\1&-1\end{bmatrix},\qquad W_V=\begin{bmatrix}2&0\\0&3\end{bmatrix}.

Then

Q=[1001],K=[1111],V=[2003].Q=\begin{bmatrix}1&0\\0&1\end{bmatrix},\quad K=\begin{bmatrix}1&1\\1&-1\end{bmatrix},\quad V=\begin{bmatrix}2&0\\0&3\end{bmatrix}.

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

12[2,0]+12[0,3]=[1,1.5].\tfrac12[2,0]+\tfrac12[0,3]=[1,1.5].

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 WVW_V 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?

Choose one

Select one choice, then check.

Hint
Ask two questions: are the source tensors the same, and what does the mask allow?
Solution
It is causal self-attention. Q, K, and V share the same source, while the mask restricts each query to its causal prefix.
Not attempted
Review

Not marked done.

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.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 1 exercises marked done

Review

Not marked done.

LLM PrimerOne Sequence Supplies Queries, Keys, and Valueshttps://llmprimer.com/transformers/from-attention-to-self-attention/one-sequence-supplies-queries-keys-and-values© 2026 LLM Primer