Juan Simancas

Artículo

The Functional Triad: Queries, Keys, and Values

26 de octubre de 2026

The Functional Triad: Queries, Keys, and Values

In traditional sequence processing, a single hidden state is forced to fulfill multiple competing responsibilities: it summarizes the history of preceding words, encodes the identity of the current token, and prepares the vector that will be matched against neighboring positions. Compressing these distinct communicative duties into an unspecialized representation creates an informational compromise. A token cannot easily express what it is searching for without diluting the description of what it currently contains.

To resolve this bottleneck, the Transformer architecture introduced by Vaswani et al. (2017, arXiv:1706.03762) decomposes the representation of each token into three distinct functional projections: a query (qq), a key (kk), and a value (vv). Rather than relying on a single vector to perform search, indexing, and information delivery simultaneously, the model projects the input through three separate linear transformations.

Suppose an input sequence contains token vectors xi∈RCx_i \in \mathbb{R}^C, where CC represents the channel embedding dimension (denoted dmodeld_{\text{model}} in the original paper). At each position ii, the model computes three specialized vectors:

qi=xiWQ,ki=xiWK,vi=xiWVq_i = x_i W_Q, \qquad k_i = x_i W_K, \qquad v_i = x_i W_V

where WQ,WK∈RC×dkW_Q, W_K \in \mathbb{R}^{C \times d_k} and WV∈RC×dvW_V \in \mathbb{R}^{C \times d_v} are learned parameter matrices. In the base model of Vaswani et al., the projection dimensions are set to dk=dv=dmodel/h=64d_k = d_v = d_{\text{model}} / h = 64, where h=8h = 8 denotes the number of parallel attention heads.

Each of these three vectors fulfills a specialized operational role within the attention mechanism:

The query vector qiq_i represents the inquiry formulated by position ii. It specifies the informational attributes that the current token requires from other positions in the sequence to resolve its syntactic dependencies or semantic ambiguity.

The key vector kjk_j functions as an indexing descriptor for position jj. It advertises the properties of the token residing at that position, providing an addressable profile against which incoming queries can be matched.

The value vector vjv_j contains the substantive content that position jj will contribute to the rest of the network if its key matches an active query.

This division of labor establishes the foundational principle of self-attention: the mechanism never evaluates compatibility between the representations that are actually transported. It compares queries with keys to determine routing weights, and then applies those weights strictly over the values. This separation allows a position to advertise one set of properties during indexing while transmitting an entirely different set of features during aggregation.

To evaluate how strongly position ii should attend to position jj, the model measures the directional alignment between the query qiq_i and the key kjk_j using an inner product:

qiTkj=∑m=1dkqimkjmq_i^T k_j = \sum_{m=1}^{d_k} q_{im} k_{jm}

When the query vector and the key vector point in similar directions in their shared dkd_k-dimensional subspace, their inner product is positive and large. When they are orthogonal, their inner product is zero. When they point in opposite directions, it is negative. This inner product does not measure superficial orthographic or dictionary similarity; it reflects alignment within an abstract representational space learned through gradient descent to minimize task loss.

However, calculating compatibility solely through raw dot products introduces a numerical instability that worsens as the channel dimension dkd_k increases. To analyze this behavior, Vaswani et al. examine the statistical properties of the dot product under standard initialization conditions. Assume that the components of qiq_i and kjk_j are independent random variables with mean zero and variance one:

E[qim]=0,Var(qim)=1,E[kjm]=0,Var(kjm)=1\mathbb{E}[q_{im}] = 0, \quad \text{Var}(q_{im}) = 1, \qquad \mathbb{E}[k_{jm}] = 0, \quad \text{Var}(k_{jm}) = 1

The product of two independent, zero-mean unit-variance variables has a mean of zero and a variance of one:

E[qimkjm]=0,Var(qimkjm)=E[(qimkjm)2]−(E[qimkjm])2=1\mathbb{E}[q_{im} k_{jm}] = 0, \qquad \text{Var}(q_{im} k_{jm}) = \mathbb{E}[(q_{im} k_{jm})^2] - (\mathbb{E}[q_{im} k_{jm}])^2 = 1

Because the dot product qiTkjq_i^T k_j is the sum of dkd_k such independent terms, its mean remains zero, but its variance scales linearly with the dimensionality:

Var(qiTkj)=∑m=1dkVar(qimkjm)=dk\text{Var}(q_i^T k_j) = \sum_{m=1}^{d_k} \text{Var}(q_{im} k_{jm}) = d_k

The standard deviation of the raw dot product is therefore dk\sqrt{d_k}. When dkd_k is small, this dispersion is manageable. But when dkd_k reaches typical architectural scales such as 6464, the standard deviation reaches 88; for dk=512d_k = 512, it reaches approximately 22.622.6.

Under such large standard deviations, dot products frequently produce large positive or negative values. When these extreme logits are fed into the softmax function, exponentiation amplifies the gap between the largest score and the remaining scores. The softmax distribution collapses into a near-deterministic one-hot vector, placing almost all probability mass on a single position.

In this saturated regime, the gradients of the softmax function with respect to its inputs approach zero:

∂αi∂sj=αi(δij−αj)≈0\frac{\partial \alpha_i}{\partial s_j} = \alpha_i (\delta_{ij} - \alpha_j) \approx 0

This vanishing gradient impedes backpropagation, preventing the projection matrices WQW_Q and WKW_K from learning effectively. To neutralize this dimensional growth, Vaswani et al. introduce scaled dot-product attention, dividing the inner product by the square root of the projection dimension:

sij=qiTkjdks_{ij} = \frac{q_i^T k_j}{\sqrt{d_k}}

Dividing by dk\sqrt{d_k} normalizes the variance of the compatibility scores back to one, preserving a smooth distribution over sequence positions and maintaining non-vanishing gradients throughout training.

Once calculated, the scores for each query position ii are converted into attention weights using the softmax function across all available key positions j∈{1,…,T}j \in \{1, \ldots, T\}:

αij=exp⁡(sij)∑m=1Texp⁡(sim)\alpha_{ij} = \frac{\exp(s_{ij})}{\sum_{m=1}^T \exp(s_{im})}

The scalar αij\alpha_{ij} represents the proportion of attention that token ii assigns to token jj. Because the softmax operates along the sequence dimension, the weights form a valid probability distribution:

αij≥0,∑j=1Tαij=1for every query i\alpha_{ij} \ge 0, \qquad \sum_{j=1}^T \alpha_{ij} = 1 \quad \text{for every query } i

The final representation for position ii is obtained by computing the convex combination of all value vectors according to these weights:

yi=∑j=1Tαijvjy_i = \sum_{j=1}^T \alpha_{ij} v_j

The resulting vector yiy_i is a context-dependent mixture of values. It contains information drawn from tokens across the sentence, weighted by how well their keys satisfied the inquiry formulated by query qiq_i.

A frequent source of confusion when analyzing this pipeline is conflating the role of the projection dimension dkd_k with the sequence length TT. The channel dimension dkd_k determines the capacity of the comparison space; it is along this dimension that inner products are calculated. In contrast, the sequence length TT determines the cardinality of the competition pool; it is across these TT positions that the softmax normalizes the scores. The score sijs_{ij} is not an absolute threshold; it is relative to the other scores in row ii. A position with a positive compatibility score may still receive negligible attention weight if another token in the sentence produces a significantly larger alignment.

It is also important to demystify the retrieval terminology. Although "query", "key", and "value" are intuitive metaphors inherited from database systems, they do not denote discrete symbols or natural language questions. They are continuous, differentiable coordinate vectors produced by linear projections. Their internal geometric structure is not manually authored; it emerges entirely from the interaction between gradient descent, the loss objective, and the architectural constraints of the network.

Finally, relying on a single set of projection matrices would force the model to average all semantic and syntactic relationships into a single attention distribution. In natural language, a verb may simultaneously need to identify its grammatical subject, bind its direct object, and adjust for adverbial tense. To allow the model to track multiple relational types in parallel, Vaswani et al. replicate this functional triad across hh independent heads, projecting queries, keys, and values into separate representational subspaces. Within each subspace, the triad executes its scaled dot-product calculus, producing specialized mixtures that are concatenated and projected to form the final multi-head output.