Qkv Attention

LLMs interview question · Medium difficulty

What are Query, Key, and Value in attention?medium

Status
✓
Type
conceptual
Topic
qkv-attention
Frequency
common
Tags
attention, query-key-value, transformers
Answer

Every token is projected into three learned vectors: Query (what this token is looking for), Key (what this token offers for matching), and Value (the actual information it carries). Attention scores come from Q·K similarity, and the result is used to decide how much of each token's Value to pull in.

Explanation

A useful analogy is library search: your search query is matched against book indexes (keys), and strong matches return the book content (values). For the word "it" in "The cat sat because it was tired," the Query for "it" may look for the entity being described; the Key of "cat" matches strongly, so the model pulls the Value of "cat" to understand the sentence. Formally, attention scores are softmax(Q·Kᵀ / √d_k), then multiplied by V — high Q-K similarity means the model attends heavily to that token's Value. This is the mechanism by which context relationships, like pronoun resolution or subject-object binding, actually get captured inside a transformer layer.

Follow-upWhy does attention divide by √d_k before the softmax?
Follow-upWhat would happen to attention if Query and Key were the same vector as Value?
Follow-upHow does this relate to multi-head attention?