When Queries and Keys Speak Different Languages
Dimension mismatch between queries and keys distorts the attention score distribution — specifically inflating its variance — and the sqrt(d_k) normalization in scaled dot-product attention exists precisely to keep that distribution stable regardless of dimension.
A complete interactive classroom, not just a preview.
Start when you are ready to enter this Stage's 9 scenes and explore, respond, and learn as you go.
What happens to attention behavior when query and key vectors live in different dimensional spaces?
Attention scores collapse when query and key dimensions don't line up — and a single scaling rule explains why every modern transformer uses it.
We assume dot-product attention scores are well-behaved. But what if the query lives in 512 dimensions and the key only in 64? The score distribution, gradient flow, and softmax sharpness all silently change.
A direct comparison of attention score distributions across matching, mismatched, and rescaled dimensions, followed by a manipulable simulator where learners change dimension ratio and observe softmax behavior.
Dimension mismatch breaks the assumption that attention scores are scale-invariant — and the sqrt(d_k) scaling is the minimal fix that restores a stable, interpretable distribution.
A plausible first guess is that attention still works fine, because the softmax normalizes everything to a probability — so dimension differences shouldn't really matter.
- Full multi-head attention architecture
- Specific transformer variants (BERT vs. GPT vs. LLaMA)
- Positional encodings and their effect on attention
- KV-cache and inference-time attention optimizations
- Linear attention and kernel-based alternatives
- 01Two Vectors, Two Different LengthsslideQuestion
Open with the concrete scenario: a query vector of dimension 512, a key vector of dimension 64. Frame the driving question — can we still compute a meaningful attention score, and what changes about its behavior?
- Set up the asymmetric setup: Q ∈ R^512, K ∈ R^64
- Pose the question: do scores still behave normally?
- Hint that this is not just theoretical — every transformer scales by sqrt(d_k) for this reason
- 02Your First IntuitionquizPrediction
Ask the learner to commit to an initial prediction before any evidence is shown — what they think happens to the attention distribution when dimension increases.
- Force a single explicit commitment before evidence
- Surface the most common naive assumption explicitly
- 03Watch the Score Distribution ExplodeinteractiveEvidence
A simulation where the learner drags a slider for key dimension d_k and observes a live histogram of dot-product scores Q·K sampled from unit-variance vectors, plus the resulting softmax output. As d_k grows, the distribution widens and softmax saturates.
- Observe score variance scale linearly with d_k
- See softmax collapse toward one-hot as variance grows
- Compare multiple d_k values side by side
- Notice gradient-signal loss in saturated regions
- 04The Variance Math, Made VisibleslideEvidence
Pin down the math behind what the simulator showed: if each component is zero-mean with variance 1, the dot-product has variance d_k. Show three concrete numbers — d_k = 16, 64, 512 — and the corresponding score standard deviations.
- Var(Q·K) = d_k when components are unit-variance and zero-mean
- Standard deviation grows as sqrt(d_k)
- Larger variance → sharper softmax → less informative gradients
- 05Why Softmax Hates Big NumbersslideExplanation
Explain the mechanism: softmax exponentials amplify differences. A standard deviation of 22 means typical differences between scores are tens — so one key gets probability ~0.999 and all others get dust. Backprop through saturated softmax gives near-zero gradients.
- Softmax(x_i) = exp(x_i) / Σ exp(x_j)
- Large σ makes one exp dominate the sum
- Saturated softmax → near-zero gradient on all but the winning key
- Result: attention becomes 'pick one and ignore everything else'
- 06The sqrt(d_k) FixslideExplanation
Show the resolution: dividing the score by sqrt(d_k) restores unit variance regardless of dimension. This is exactly why 'Scaled Dot-Product Attention' in the original transformer paper divides by sqrt(d_k).
- Var(Q·K / sqrt(d_k)) = 1, independent of d_k
- Softmax receives a well-behaved distribution at every scale
- Gradients flow, training is stable
- This is the 'scaled' in scaled dot-product attention
- 07What If We Skip the Scaling?interactiveBoundary
Let the learner toggle the sqrt(d_k) factor on and off across d_k = 16, 64, 256 and observe the softmax output for the same set of keys. The boundary case: with very small d_k, scaling barely matters; with large d_k, omitting scaling breaks training entirely.
- Quantify when scaling matters and when it doesn't
- See the boundary: below d_k ≈ 16 the difference is cosmetic
- Above d_k ≈ 64 the difference is catastrophic
- Modern transformers all live on the 'matters' side
- 08Cross-Dimensional QueriesslideTransfer
Transfer to the practical case: cross-modal attention where text queries attend over image keys (or vice versa), or retrieval where query and document embeddings have different sizes. The same variance argument applies — and is why projection layers normalize dimensions before attention.
- Cross-modal and cross-domain attention are the real-world version of this
- Projection heads map both sides to a shared d_k
- Scaling rule still applies after projection
- Failure mode in production: mismatched embeddings before projection
- 09The Answer in One SentenceslideResolution
Resolve the driving question directly: dimension mismatch changes attention behavior by inflating score variance, saturating softmax, and killing gradients. The sqrt(d_k) scaling is the minimal correction that makes attention dimension-invariant.
- Restate the driving question and answer it plainly
- Tie together variance, softmax saturation, gradient flow, and the scaling fix
- Point forward: this is one reason modern architectures are careful about d_k
Discussion threads for a Stage aren't available yet.
This path ends here.