proofs
Provedunbiased-attentionrandomized-algorithmssoftmaxgradient

The true derivative of attention: shifting scores by t·c_k, d/dt of the softmax output at 0 is E_π[c v] − E_π[v]·E_π[c]

For all n∈N\htmlData{mx=1}{n \in \htmlData{mx=0}{\mathbb{N}}} and s,c,v:Fin⁡ n→R\htmlData{mx=5}{s, c, v : \htmlData{mx=2}{\htmlData{mx=0}{\operatorname{Fin}}\,\htmlData{mx=1}{n}} \mathrel{\htmlData{mx=4}{\to}} \htmlData{mx=3}{\mathbb{R}}}, if 0<∑kes(k)0 \mathrel{\htmlData{mx=5}{<}} \htmlData{mx=4}{\sum_{k} \htmlData{mx=3}{e^{\htmlData{mx=2}{\htmlData{mx=0}{s}(\htmlData{mx=1}{k})}}}}, then

HasDerivAt⁡ t↦∑kes(k)+t⋅c(k)⋅v(k)∑kes(k)+t⋅c(k) ∑kes(k)⋅c(k)⋅v(k)∑kes(k)−∑kes(k)⋅v(k)∑kes(k)⋅(∑kes(k)⋅c(k)∑kes(k)) 0\htmlData{mx=80}{\htmlData{mx=0}{\operatorname{HasDerivAt}}\,\htmlData{mx=28}{t \mapsto \htmlData{mx=27}{\frac{\htmlData{mx=15}{\sum_{k} \htmlData{mx=14}{\htmlData{mx=10}{e^{\htmlData{mx=9}{\htmlData{mx=3}{\htmlData{mx=1}{s}(\htmlData{mx=2}{k})} + \htmlData{mx=8}{\htmlData{mx=4}{t} \cdot \htmlData{mx=7}{\htmlData{mx=5}{c}(\htmlData{mx=6}{k})}}}}} \cdot \htmlData{mx=13}{\htmlData{mx=11}{v}(\htmlData{mx=12}{k})}}}}{\htmlData{mx=26}{\sum_{k} \htmlData{mx=25}{e^{\htmlData{mx=24}{\htmlData{mx=18}{\htmlData{mx=16}{s}(\htmlData{mx=17}{k})} + \htmlData{mx=23}{\htmlData{mx=19}{t} \cdot \htmlData{mx=22}{\htmlData{mx=20}{c}(\htmlData{mx=21}{k})}}}}}}}}}\,\htmlData{mx=79}{\htmlData{mx=47}{\frac{\htmlData{mx=41}{\sum_{k} \htmlData{mx=40}{\htmlData{mx=36}{\htmlData{mx=32}{e^{\htmlData{mx=31}{\htmlData{mx=29}{s}(\htmlData{mx=30}{k})}}} \cdot \htmlData{mx=35}{\htmlData{mx=33}{c}(\htmlData{mx=34}{k})}} \cdot \htmlData{mx=39}{\htmlData{mx=37}{v}(\htmlData{mx=38}{k})}}}}{\htmlData{mx=46}{\sum_{k} \htmlData{mx=45}{e^{\htmlData{mx=44}{\htmlData{mx=42}{s}(\htmlData{mx=43}{k})}}}}}} - \htmlData{mx=78}{\htmlData{mx=62}{\frac{\htmlData{mx=56}{\sum_{k} \htmlData{mx=55}{\htmlData{mx=51}{e^{\htmlData{mx=50}{\htmlData{mx=48}{s}(\htmlData{mx=49}{k})}}} \cdot \htmlData{mx=54}{\htmlData{mx=52}{v}(\htmlData{mx=53}{k})}}}}{\htmlData{mx=61}{\sum_{k} \htmlData{mx=60}{e^{\htmlData{mx=59}{\htmlData{mx=57}{s}(\htmlData{mx=58}{k})}}}}}} \cdot \left(\htmlData{mx=77}{\frac{\htmlData{mx=71}{\sum_{k} \htmlData{mx=70}{\htmlData{mx=66}{e^{\htmlData{mx=65}{\htmlData{mx=63}{s}(\htmlData{mx=64}{k})}}} \cdot \htmlData{mx=69}{\htmlData{mx=67}{c}(\htmlData{mx=68}{k})}}}}{\htmlData{mx=76}{\sum_{k} \htmlData{mx=75}{e^{\htmlData{mx=74}{\htmlData{mx=72}{s}(\htmlData{mx=73}{k})}}}}}}\right)}}\,0}
What the notation means · or point at any symbol
NNatural numbers
The natural numbers 0, 1, 2, …. In Lean they start at 0. (ℕ)
Fin
Fin: a definition from the library, not special notation. Its name is its meaning; open it in Lean to see how it is defined. (Fin)
f(x)Function application
Fin applied to n. Lean writes f x; papers write f(x). (Fin n)
⟹Implies
If Fin(n), then ℝ. (Between types, A → B is the type of functions from A to B.) (Fin n → ℝ)
x : TVariables
s, c, v range over Fin n → ℝ: the statement must hold for every choice. ((s c v : Fin n → ℝ))
<Less than
0 is strictly smaller than ∑ k, exp(s(k)). (0 < ∑ k, Real.exp (s k))
HasDerivAt
HasDerivAt: a definition from the library, not special notation. Its name is its meaning; open it in Lean to see how it is defined. (HasDerivAt)
∑Summation
Add up exp(s(k)) for every k. (∑ k, Real.exp (s k))
RReal numbers
The real numbers: every point on the number line. (ℝ)
eˣExponential function
e raised to the power s(k), where e ≈ 2.718. (Real.exp (s k))
x ↦ f(x)Function (λ-abstraction)
The function sending t to (∑ k, exp(s(k) + t · c(k)) · v(k))/(∑ k, exp(s(k) + t · c(…. Lean writes fun x => …; papers write x ↦ …. (fun t => (∑ k, Real.exp (s k + t * c k)…)
a/bDivision
∑ k, exp(s(k) + t · c(k)) · v(k) divided by ∑ k, exp(s(k) + t · c(k)). In Lean, dividing by 0 gives 0. ((∑ k, Real.exp (s k + t * c k) * v k) /…)
+Addition
s(k) plus t · c(k). (s k + t * c k)
·Multiplication
t times c(k). (t * c k)
-Subtraction
(∑ k, exp(s(k)) · c(k) · v(k))/(∑ k, exp(s(k))) minus (∑ k, exp(s(k)) · v(k))/(∑ k, exp(s(k))) · ((∑ k, exp(s(k)…. On ℕ, Lean's subtraction stops at 0: 2 − 5 = 0. ((∑ k, Real.exp (s k) * c k * v k) / (∑ …)

A HasDerivAt statement, not an asserted formula: for s_k = ⟨q, k_k⟩ and a query perturbation along coordinate a (c_k = k_{k,a}), the derivative of Σ e^{s_k} v_k / Σ e^{s_k} is the covariance of c and v under the softmax row. This grounds the gradient lemmas (covariance form, product estimates, gradient gadget) in calculus. Part of 'Unbiased randomized attention: cost x variance thresholds', round 9 (true derivative, whole-instance reduction, compositions).

How it was proved

Proved
4 October 2026
By
Ajax, with the agent “Baguette math & proofs (Claude Code, 2026-10-03)”
Checked by
Lean's kernel (Lean 4.33.1, with Mathlib); the proof is signed and replays offline
Axioms
Classical.choice, Quot.sound, propext
Proof
30 lines of Lean
Sign in to see the proofThe proof, its signed report and its history.

More proved statements