﻿# Queries, keys and values: why attention learns different projections

Queries, keys and values are learned projections with different roles. Follow matching, normalized weights and value aggregation through a concrete attention example.

Author: Retrivium editorial team
Updated: 2026-11-03
Canonical URL: https://retrivium.ai/journal/query-key-value-projection-roles/
Language: en

Attention is often introduced with the names query, key and value. The names become useful when they identify different learned transformations rather than three copies of the same tensor. Matching decides where information comes from, while values determine what is combined.

## Project one representation into different roles

A Transformer layer can obtain queries, keys and values by applying distinct learned projections to its input representations. In self-attention, the source sequence can be shared even though the projections differ. In cross-attention, query and key-value sources may come from different components. The roles should therefore be defined by their use in the calculation, not merely by where a variable was created. Equal vector widths do not imply equal weights, identical representations or interchangeable functions in the layer.

## Matching produces weights rather than the final representation

Query-key interactions yield scores over eligible positions. Scaling, masks and softmax turn those scores into the weights used for aggregation. A strong match does not itself become a semantic answer or a retrieved text fragment. It is part of a learned computation. The attention pattern also controls which positions can participate, so an unavailable key cannot be recovered by assigning it a larger content score. Visibility and preference are separate decisions in the architecture’s information-flow contract.

## Values carry the aggregated content

Consider a small illustrative attention calculation where keys indicate matching characteristics but values contain recognizable payload vectors. Change a value while leaving its key fixed: matching weights can remain unchanged while the output changes. Change the key while preserving its value: the payload remains available but can receive another weight. These interventions isolate the conceptual roles. They are teaching experiments on controlled tensors, not an explanation of a particular trained neuron or proof that natural-language features separate cleanly into human-readable categories.

## Read variations without losing the core distinction

Multi-head attention, shared key-value heads and rotary positions alter parts of this computation while retaining the need to identify matching and aggregation roles. A reliable implementation review traces projection shapes, source sequences, position transforms and masks before inspecting the final output projection. Small controlled inputs can then reveal accidental reuse or permutation of tensors. This role-based understanding supports architecture comparisons: it explains which part changed instead of assuming that every model using the attention name has the same parameter sharing or execution contract.

## Primary research and implementation references

- [Attention Is All You Need](https://arxiv.org/abs/1706.03762).

- [PyTorch: MultiheadAttention](https://docs.pytorch.org/docs/2.15/generated/torch.nn.MultiheadAttention.html).

- [PyTorch: Scaled Dot Product Attention](https://docs.pytorch.org/docs/2.15/generated/torch.nn.functional.scaled_dot_product_attention.html).

## Related engineering context

[Reproducible inference stacks: preserving a GPU pilot’s result](/journal/software-stack-reproducibility/); [A GPU inference benchmark you can trust](/journal/inference-benchmark-method/).
