ROB-GY 6013 · Supplement to Lecture 6
Scaled dot-product attention, forward and backward
One head: $S=QK\T/\sqrt{d_k}$, a row softmax, $O=PV$, and every gradient back.
Assumes: Lecture 6 on reverse mode. Gradients are columns; $Q,K,V$ hold token rows.
Pages 73–86 of the lecture, unchanged.
Mathematics for Robotics · ROB-GY 6013 · NYU Tandon · Jing Zhang · Fall 2026
A weighted combination of values
Each $p_{ij}$ is computed from the inputs, not stored, and $o_i$ lands in the convex hull of the value rows: between the smallest and the largest when the values are scalars.
Scores from inner products
The constant is part of the operation; its derivative contribution is required here, its justification waits.
Positive weights that sum to one
A later mask must leave one valid key in each row, and needs its own derivative policy.
Vote
Which score change leaves weights unchanged?
One row's scores all gain the same $c$. What happens to that row of $P$?
$\dfrac{e^{s_j+c}}{\sum_l e^{s_l+c}}=\dfrac{e^{c}e^{s_j}}{e^{c}\sum_l e^{s_l}}$
A. The shared factor cancels above and below, so with $s=(0,\ln 3)$ the row is $(1/4,3/4)$ for every $c$. B would make it add to $e^{c}$.
The attention output
Board example, carried forward
One query, two keys: $\operatorname{softmax}(0,\ln 3)=(1/4,3/4)$, so with $v=(2,6)$ the output is $\tfrac14\!\cdot\!2+\tfrac34\!\cdot\!6=5$.
Backward through the value mixture
One multiplication, two inputs, two gradients. Each keeps its own input's shape, and that is what catches a dropped transpose.
Vote
Credit to values and weights
Board row: $p=(1/4,3/4)$, $v=(2,6)$, and $g_o=2$ from the loss. Which pair is $(g_v,g_p)$?
Row forms $g_v=g_o\,p$ and $g_p=g_o\,v$, so evaluate $2(1/4,3/4)$ and $2(2,6)$.
A. Derivative in $v_j$ is its weight; in $p_j$ its value. Centred differences in each argument reproduce all four.
A softmax output depends on all scores
Quotient rule: own numerator only at $i=j$, shared denominator at every $j$. No weight has a private score.
Assemble the softmax Jacobian
Finite, unmasked scores, and one $p$ and one $J$ per score row. The two sums agree only because $J$ is symmetric.
Apply the VJP without building a Jacobian
This is $J\T g_p$ rewritten, not approximated. Do not materialise a full attention Jacobian in code: the short form needs $T_k$ numbers, never $T_k^2$.
Vote
A complete softmax backward example
Board row: $p=(1/4,3/4)$, upstream $g_p=(4,12)$. Find $g_s$.
Weighted mean: $p\T g_p=\tfrac14(4)+\tfrac34(12)=10$.
Deviations $(-6,2)$, then scale by $p$.
B. $\tfrac14(-6)$ and $\tfrac34(2)$, which add to $0$: a shift common to all scores moves no weight, so $g_s$ has no part along $\mathbf1$.
Backward through query-key scores
$S$ is linear in $Q$ and in $K$, so each gradient is the other array contracted with $G_S$ and divided by the same constant.
Shared inputs receive several contributions
A variable used three times accumulates three gradients; drop two and a centred difference in $X$ disagrees.
Vote
Reconstruct the complete reverse chain
Which line gives $G_Q$, with $T_q=2$, $T_k=3$, $d_k=4$?
$G_Q$ must be $2\times4$.
$G_S\T$ is $3\times2$, $K$ is $3\times4$: inner $2$ against $3$, so B is no product at these sizes.
A. $(2\times3)(3\times4)$ over $2$. C is off by exactly $2$. Verify by centred differences in all $35$ entries.