Query, Key, Value › Dimensions and Shapes
Michael BrenndoerferUpdated June 4, 20252 min read
Language AI HandbookAll chapters Part 13 of 67Self-Attention
Query, Key, Value › Dimensions and ShapesOne section · Sections in this chapter4 / 11 ↓
Sections in this chapter11
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Understanding the tensor shapes in self-attention is important for implementation and debugging. Let's summarize the key dimensions:
| Tensor | Shape | Description |
|---|---|---|
| Input | tokens, each with -dimensional embedding | |
| Query projection, maps | ||
| Key projection, maps | ||
| Value projection, maps | ||
| Queries | Query vectors for all tokens | |
| Keys | Key vectors for all tokens | |
| Values | Value vectors for all tokens | |
| Scores | All pairwise attention scores | |
| Weights | Softmax-normalized attention | |
| Output | Context-enriched representations |
A few important constraints:
- Query and key dimensions must match () because we compute their dot product
- Value dimension can differ () since values are aggregated, not compared
- Output dimension equals value dimension () because the output is a weighted sum of values
In practice, most implementations set , where is the embedding dimension and is the number of attention heads. This choice ensures that the total computation across all heads remains comparable to a single-head attention with full dimension. We'll explore multi-head attention in a later chapter.