Inferensys

Glossary

Query, Key, Value (QKV) Vectors

The three learned linear projections of an input embedding that form the basis of attention calculations, where the Query interacts with Keys to determine weights applied to Values.
Engineer reviewing vector database search results on laptop, embeddings visualization on screen, home office coding session.
FOUNDATIONAL ATTENTION MECHANISM

What is Query, Key, Value (QKV) Vectors?

The three learned linear projections that form the basis of all attention calculations in Transformer architectures, where the Query interacts with Keys to determine weights applied to Values.

Query, Key, and Value (QKV) vectors are the three distinct learned linear projections of an input token embedding that enable the scaled dot-product attention mechanism. Every token in a sequence is projected into these three vector spaces using separate, trainable weight matrices. The Query vector represents the token's current focus or 'search intent,' the Key vector acts as an indexable label for matching, and the Value vector holds the actual information to be aggregated.

During attention computation, the dot product between a Query and all Keys produces a compatibility score, which is scaled and normalized via softmax into attention weights. These weights are then used to compute a weighted sum of the corresponding Value vectors, producing the output representation. This QKV abstraction, originating from information retrieval systems, allows the model to dynamically retrieve and aggregate relevant context from the entire input sequence.

THE ATTENTION PROJECTIONS

Core Characteristics of QKV Vectors

The three learned linear projections that form the basis of all attention calculations, transforming an input embedding into distinct roles for information retrieval and aggregation.

01

Learned Linear Projections

QKV vectors are not separate inputs but learned projections of the same input embedding. Each token's representation is multiplied by three distinct weight matrices—W_Q, W_K, and W_V—learned during training. This allows the model to dynamically specialize what information a token seeks (Query), what it offers (Key), and what it transmits (Value).

  • Weight matrices are shared across all tokens in a sequence
  • Each attention head has its own unique set of QKV projection weights
  • Projection dimensions are typically smaller than the model's hidden size: d_k = d_model / num_heads
02

The Retrieval Analogy

The QKV mechanism is best understood through a database retrieval analogy. The Query is what you're searching for—a specific lookup request. The Keys are the index tags on every stored item. The Values are the actual content returned.

  • Query: 'Find documents about transformers'
  • Keys: Titles and tags of all stored documents
  • Values: The full document bodies

The attention score is the match quality between Query and each Key, and the output is a weighted sum of Values—retrieving more from highly matched items.

03

Dimensionality and Scaling

The Query and Key vectors must share the same dimensionality d_k to compute dot-product compatibility. The Value vector dimension d_v can differ, though it's typically set equal to d_k in standard implementations.

  • Dot-product variance grows with d_k, pushing softmax into regions of extremely small gradients
  • Scaling factor 1/√d_k counteracts this, keeping variance stable at 1
  • Without scaling, large d_k values cause the softmax to saturate, approximating a one-hot vector and killing gradient flow
04

Asymmetric Roles in Cross-Attention

In self-attention, Q, K, and V all derive from the same sequence. In cross-attention, this symmetry breaks: Queries come from the decoder's current state, while Keys and Values come from the encoder's output.

  • The decoder asks: 'What parts of the input are relevant to my next token?'
  • The encoder provides the contextualized Keys and Values to answer
  • This asymmetry is fundamental to sequence-to-sequence tasks like machine translation
  • The encoder's output is static during decoding, enabling KV caching of the entire source sequence
05

Multi-Head Independence

Each attention head possesses its own independent set of QKV projection matrices, allowing the model to attend to different representation subspaces simultaneously. One head might focus on syntactic dependencies while another tracks long-range semantic relationships.

  • With 8 heads and d_model=512, each head projects to d_k=64
  • Heads operate in parallel with no weight sharing in standard multi-head attention
  • Grouped-Query Attention (GQA) breaks this independence by sharing K and V projections across groups of Query heads, trading some expressivity for significant KV cache reduction
06

Gradient Flow and Training Dynamics

The QKV projections are the primary pathway for gradient flow through the attention mechanism. During backpropagation, gradients flow through the Value-weighted sum, then through the softmax-normalized attention scores, and finally into all three projection matrices.

  • W_V receives direct gradients from the attention output
  • W_Q and W_K receive gradients only through the attention weights—a more indirect path
  • This asymmetry can cause training instability if one projection learns significantly faster than the others
  • Proper initialization (e.g., Xavier uniform) and Layer Normalization placement are critical for balanced convergence
QKV MECHANICS

Frequently Asked Questions

Clear, technical answers to the most common questions about the Query, Key, and Value vectors that form the computational backbone of Transformer attention.

Query (Q), Key (K), and Value (V) vectors are three distinct learned linear projections of an input token's embedding. They are the fundamental operands of the attention mechanism. For every token in a sequence, the input representation x is multiplied by three separate weight matrices—W_Q, W_K, and W_V—to produce the Q, K, and V vectors. The Query represents what the current token is 'looking for'; the Key represents what a token 'has to offer' as a label for matching; and the Value represents the actual information content that will be aggregated. This projection into distinct subspaces allows the model to separate the task of finding relevant tokens (via Q-K interaction) from the task of extracting useful information from them (via V).

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.