LLM & Transformer Fundamentals·Question 3 of 109BasicAI-75

Explain self-attention step by step. What exactly are Q, K, and V?

Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.

Model answer: the part to lead with

Every token's vector is linearly projected three ways: a query ("what am I looking for?"), a key ("what do I contain, for others to match against?"), and a value ("what content do I contribute if attended to?"). Attention is a soft, differentiable key-value lookup: token i's query is compared to every token's key; the resulting weights mix the values.

Full worked answer with diagram

Every token's vector is linearly projected three ways: a query ("what am I looking for?"), a key ("what do I contain, for others to match against?"), and a value ("what content do I contribute if attended to?"). Attention is a soft, differentiable key-value lookup: token i's query is compared to every token's key; the resulting weights mix the values.

Steps, for X of shape (n, d_model):

  1. Q = XW_q, K = XW_k, V = XW_v - three learned projections.
  2. Scores = QKᵀ / √d_k - an (n, n) matrix; entry (i, j) is how relevant token j is to token i.
  3. Apply the causal mask: set scores where j > i to −∞.
  4. Row-wise softmax → each row becomes a probability distribution over previous positions.
  5. Output = weights · V - each token's output is a convex combination of value vectors, then a final output projection W_o maps back to d_model.
import numpy as np
def attention(Q, K, V, causal=True):
    s = Q @ K.T / np.sqrt(K.shape[-1])
    if causal:
        s[np.triu(np.ones_like(s, dtype=bool), k=1)] = -np.inf
    w = np.exp(s - s.max(-1, keepdims=True))
    w /= w.sum(-1, keepdims=True)
    return w @ V

The crucial conceptual point: the weights are computed from the data at runtime, not fixed like convolution kernels. The same layer can route "pronoun → antecedent" in one sentence and "verb → subject" in another. Why separate K and V? Because what makes a token findable (key) and what it should deliver once found (value) are different roles - collapsing them forces one vector to serve both, which is measurably worse.

Worth sketching. It keeps the three projections and the two matmuls straight, which is where people fumble under pressure.

flowchart LR
    X["X, shape n x d_model"] --> Q["Q = X W_q"]
    X --> K["K = X W_k"]
    X --> V["V = X W_v"]
    Q --> S["scores = Q K^T / sqrt(d_k)"]
    K --> S
    S --> M["causal mask, then row-wise softmax"]
    M --> W["weights, shape n x n"]
    W --> O["out = weights . V"]
    V --> O
    O --> P["W_o back to d_model"]

What a good answer covers

  • Every token's vector is linearly projected three ways
  • Attention is a soft, differentiable key-value lookup
  • Q = XW_q, K = XW_k, V = XW_v - three learned projections
  • Scores = QKᵀ / √d_k - an (n, n) matrix; entry (i, j) is how relevant token j is to token i
  • Apply the causal mask: set scores where j > i to −∞

Key terms: query · key · value

Interviewer follow-ups to expect

  1. Why divide by √d_k?

  2. What is the computational complexity of step 2 and what does that imply for long contexts?

  3. In a decoder generating token-by-token, which of Q, K, V get cached and why?

Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep