Query, Key, Value › Dimensions and Shapes

Michael BrenndoerferUpdated June 4, 20252 min read
Language AI HandbookAll chapters
Part 13 of 67Self-Attention
Chapter 91Query, Key, Value › Dimensions and Shapes
Reading section 4 of 11 · Dimensions and Shapes · 2 minSection 4 of 11 · 2 min

Book contents

Section 4 of 11 · 2 min read

Query, Key, Value › Dimensions and ShapesOne section · Sections in this chapter4 / 11 ↓
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 shapes in self-attention. Query and key dimensions must match for dot product compatibility.
TensorShapeDescription
Input X\mathbf{X}(n,d)(n, d)nn tokens, each with dd-dimensional embedding
WQ\mathbf{W}_Q(d,dk)(d, d_k)Query projection, maps d→dkd \to d_k
WK\mathbf{W}_K(d,dk)(d, d_k)Key projection, maps d→dkd \to d_k
WV\mathbf{W}_V(d,dv)(d, d_v)Value projection, maps d→dvd \to d_v
Queries Q\mathbf{Q}(n,dk)(n, d_k)Query vectors for all tokens
Keys K\mathbf{K}(n,dk)(n, d_k)Key vectors for all tokens
Values V\mathbf{V}(n,dv)(n, d_v)Value vectors for all tokens
Scores QKT\mathbf{Q}\mathbf{K}^T(n,n)(n, n)All pairwise attention scores
Weights α\alpha(n,n)(n, n)Softmax-normalized attention
Output(n,dv)(n, d_v)Context-enriched representations

A few important constraints:

  • Query and key dimensions must match (dkd_k) because we compute their dot product
  • Value dimension can differ (dvd_v) since values are aggregated, not compared
  • Output dimension equals value dimension (dvd_v) because the output is a weighted sum of values

In practice, most implementations set dk=dv=d/hd_k = d_v = d / h, where dd is the embedding dimension and hh 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.