Derive an attention-score variance from stated random-variable assumptions and test whether the reasoning survives a change in dependence.
Problem
Context. The fixture below defines four-dimensional query and key vectors using independent random signs, then changes their within-vector dependence.
Task. Find the unscaled and scaled dot-product variances in both cases, showing the assumptions needed at each step. Explain what the result does and does not establish about attention scaling in a learned model.
Constraints. Use the specified zero-mean sign distributions rather than assuming arbitrary learned coordinates are independent. Check covariance terms when independence changes; a familiar formula is not a substitute for the derivation.
Let and . All eight coordinates of and independently take or , each with probability . Find the variance of and of . Then decide whether the same conclusion holds when every query coordinate shares one sign and every key coordinate shares another.
Picture
Each product is another independent sign: mean zero, variance one. Four independent products add variance; halving their sum multiplies variance by one quarter.
An exact four-coordinate distribution
Square-root scaling stabilizes variance only under the stated dependence assumptions
Derivation
Why does dividing the dot product by eight divide its variance by sixty-four?
References: Vaswani et al. and PyTorch scaled-dot-product attention.
Worked example: attention scaling
The decisive identity retains covariance until the assumptions remove it:
Here the covariance terms vanish, so and . The equally likely sign assignments give an exact check:
| Assignment count |
Check also : no scale change is needed. Dividing variance by would incorrectly give two at ; variance squares the multiplier.
Now set every and every , with independent signs . All products equal , so has variance 16. Six pair covariances contribute beyond the four individual variances. Dividing by two leaves variance four.
Change the assumptions or object
1. Change coordinate variances
New task: independent zero-mean query coordinates have variance four; independent key coordinates have variance nine.
Picture and change: retain independence of vectors and products, but each product now has variance . For , the sum has variance 144.
Check: dividing by two leaves variance 36. Dividing by restores one in this stylized distribution.
Use when: testing the unit-variance premise, not claiming exact whitening of trained heads.
2. Average correlated worker errors
New task: average four unit-variance errors with pairwise correlation .
Picture and change: apply the same covariance identity to their average.
Check: independence gives ; perfect correlation gives one.
Use when: shared worker errors invalidate independent-noise averaging.
Transfer: recover a missing identity
You forget the importance-sampling identity, and the proposal misses some target support. What can you derive safely?
Check your answer
Start from the target integral and insert only where . Identify any missing contribution; support mismatch can block equality. Large weights or an effective-sample-size number cannot recover omitted support.
Use the same four-part structure
1. Setup
Define variables, dimensions, probability direction and assumptions. For gradients, state the differentiated variable and row/column convention.
2. Key identity
Expose the decisive step: chain rule, log-ratio expansion, log-sum-exp derivative, Jensen’s inequality or a variance identity. Justify it rather than hiding it behind a name.
3. Sanity checks
Use at least two: dimensions, sign, equality case, scalar case, limit, symmetry or numerical scale.
4. Interpretation
Explain the model or system consequence, then change an assumption and identify what survives.
Coordinate independence is sufficient, not necessary, for the attention result. For independent zero-mean vectors with covariance matrices :
This conditional result does not establish universal softmax saturation or training stability. Learned projections and normalization alter empirical head distributions.
A remembered formula is not a proof
“It prevents large values” does not justify or identify when the variance calculation changes.
Eight proof checkpoints
These are separate derivations connected by a presentation protocol, not one common mathematical proof.
| Prompt | Decisive step and check |
|---|---|
| Softmax cross-entropy | Log-sum-exp differentiation gives for normalized targets; gradients sum to zero; common logit shifts preserve probabilities. |
| KL direction | ; check support, equality and asymmetry. |
| ELBO | , when well-defined; equality uses the exact posterior. |
| L1 versus L2 | $ |
| Geometric attempts | Independent identical success trials counted from one give ; gives one attempt. |
| Averaging | Independent errors give ; equal pair correlation gives . |
| Importance sampling | Insert in the target integral only where the proposal supports the contributing integrand. |
| Attention | Keep covariance terms until assumptions justify removing them; square any scale factor in variance. |
For Bernoulli parameters and , the KL is about nats; reversing direction gives . The unequal values check asymmetry, not an error in the expectation direction. At geometric success probability zero, success never occurs and the expectation is not finite. Importance weights can have infinite variance despite a valid support identity. Effective sample size is a diagnostic, not a finite-variance or support certificate.
Which conclusion survives?
Independence, convexity, asymptotics, unbiasedness and support overlap are different premises. State which identity uses each one. The ELBO gap concerns posterior approximation, not an unspecified reconstruction-plus-regularization slogan.
Evidence in a derivation
Defined notation, justified identities, two checks and explicit error recovery make the reasoning inspectable. No format guarantees a hiring outcome.
Errors to expose
Undefined notation, missing support and unjustified algebra remain errors even when the final formula looks familiar. Stop at the missing step rather than inventing one.
Common follow-up
“You cannot remember the identity needed for the next step. What do you do?”
State what is known, derive the missing piece from a definition or simpler case, and identify the exact unresolved step.
Use the timed math oral with an observer and a changed-assumption follow-up.
Related: derive logistic regression, softmax and cross-entropy, and KL divergence.