14.5 Matrix calculus problem set: differentials, trace trick, VJPs, SDPA Jacobian
By now you have a scalar autograd engine, Scalar reverse mode: the Value graph, that differentiates one number at a time. It is correct and hopeless at scale: applying one weight to one vector would create a third of a million scalar multiplication nodes. Matrix differentials, the trace trick, and closed-form VJPs replaces it with closed-form vector-Jacobian products, one per operation, that take the upstream gradient as an array and return the input gradients as arrays, and The op library and gradcheck_all in AI Engineering registers them as the ops of the tensor autograd. Each closed form is a derivation to get right on paper first, because a gradient with the right shape and the wrong value trains, slowly and badly, rather than crashing.
Concepts from other sections (30)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- Vector-Jacobian product (VJP): , what one backward pass from leaves in the leaves. Read more: 14.2 Scalar reverse mode: the Value graph
- Upstream gradient: , the gradient arriving at an op’s output. Read more: 14.3 Matrix differentials, the trace trick, and closed-form VJPs
- Trace trick: rewriting with the cyclic property of the trace until it reads . Read more: 14.3 Matrix differentials, the trace trick, and closed-form VJPs
- Cross-entropy: , the average surprise when data from are predicted with . Read more: 17.1 Entropy, cross-entropy, KL, JS, and the k3 estimator
- LayerNorm: per-row normalization by the population mean and variance with inside the square root, then an affine map. Read more: 6.5 Module system and basic layers in AI Engineering
- RMSNorm: divides a vector by its root mean square plus eps (inside the root) and multiplies by a learned gain; no mean subtraction, no bias. Read more: 13.1 Pre-LN and RMSNorm in AI Engineering
- Reverse mode: automatic differentiation that records the computation and walks it backward once from the output, pushing adjoints into inputs. Read more: 14.2 Scalar reverse mode: the Value graph
- FlashAttention: attention computed tile by tile with an online softmax, so the score matrix is never stored. Read more: 22.5 FlashAttention forward in C in AI Engineering
- Chain rule: . Read more: 10.2 Jacobians, the multivariable chain rule, and numeric JVP/VJP
- Product rule / quotient rule: and , the rules for differentiating a product or a ratio of two functions. Read more: Chapter 7: Calculus 1
- scaled dot-product attention: , a weighted average of values with weights from query-key similarity. Read more: 11.1 Scaled dot-product attention (forward and backward) in AI Engineering
- Inner product: . Read more: 9.6 Inner products, projections, cosine similarity, and top-k
- Jacobian: the matrix of partial derivatives of a function . Read more: 10.2 Jacobians, the multivariable chain rule, and numeric JVP/VJP
- Variance: the expected squared distance from the mean; its square root is the standard deviation. Read more: 13.1 Random variables, expectation, variance, and normal draws by Box-Muller
- Forward mode: automatic differentiation that carries tangents alongside values, costing one pass per input direction. Read more: 14.1 Dual numbers and forward-mode autodiff
- dual numbers: forward-mode autodiff values that carry an exact derivative alongside each number. Read more: 6.3 The op library and gradcheck_all in AI Engineering
- Jacobian-vector product (JVP): , the directional derivative of a vector function, computed by one forward-mode pass. Read more: 14.1 Dual numbers and forward-mode autodiff
- training step: one zero-grad, loss, finiteness check, backward, clip, and optimizer update on one batch. Read more: 6.6 Training loop and the autograd bigram in AI Engineering
- logits: unnormalized log-scores that softmax turns into probabilities; here the table stores . Read more: 6.1 Byte bigram: counts, logits, safetensors v0 in AI Engineering
- sigmoid: , mapping the real line into , with . Read more: 7.3 Activation Functions and Their Derivatives
- softplus: , a smooth relu whose derivative is . Read more: 7.3 Activation Functions and Their Derivatives
- Log-sum-exp (LSE): , computed stably as . Read more: 15.2 Stable numerics: logsumexp, softmax, and compensated sums
- op library (
F): the module of differentiable functions, each a NumPy forward plus one closed-form VJP joined byfrom_op. Read more: 6.3 The op library and gradcheck_all in AI Engineering - softmax VJP: with , the backward of a row-wise softmax. Read more: 11.1 Scaled dot-product attention (forward and backward) in AI Engineering
- Log-likelihood: , the log probability of the observed counts under a distribution . Read more: 13.3 MLE, Laplace, absolute discounting
- Shift invariance: softmax is unchanged when the same constant is added to every logit. Read more: 15.2 Stable numerics: logsumexp, softmax, and compensated sums
- FLOPs per token: for the matmul parameters touched plus the parameter-free attention term; three times that for a training step. Read more: 11.1 Counting: parameters, FLOPs, KV bytes, and memory plans
- causal mask: the rule ; each position reads itself and the past. Read more: 11.2 Masks: causal, padding, sliding window, additive in AI Engineering
- Fused cross-entropy: the gradient computed directly from logits. Read more: 14.3 Matrix differentials, the trace trick, and closed-form VJPs
- Automatic differentiation / autodiff: computing exact derivatives of a program by applying the chain rule to each primitive operation it executes, in forward mode (Jacobian-vector products) or reverse mode (vector-Jacobian products); it is how neural networks get their gradients, with no step size and no symbolic formula. Read more: Chapter 14: Matrix Calculus and Autodiff
This problem set drills exactly those derivations: the shape bookkeeping, matrix differentials, the trace trick, the VJPs of softmax, log-softmax, cross-entropy, LayerNorm, and RMSNorm, the cost model of forward and reverse mode, and the attention backward that returns in Scaled dot-product attention (forward and backward) and in FlashAttention. It is a pen-and-paper set with no code to write. Every problem is followed by a collapsed worked answer with the final result, the derivation, and the common wrong answers and why they are wrong; 42 answers in all, counting lettered parts, including four proofs (q16, q20, q28, q34) whose model derivations are also in course/solutions/solve/S-M08/. Try each problem before opening its answer.
The proofs assume the habits of the Discrete math 1 problem set, and the set leans on the gradients and chain rule of the Calculus 3 problem set. The opening of this chapter, Matrix Calculus and Autodiff, gives the wider picture.
Learning objectives
- Give the shape of any gradient and the strides of any row-major view, and turn broadcasting into a backward sum.
- Differentiate matrix expressions with the product rule and read off gradients with the trace trick.
- Compute the VJPs of matmul, softmax, log-softmax, cross-entropy, LayerNorm, and RMSNorm by hand, and check them with their invariances.
- Count the passes, bytes, and FLOPs of forward and reverse mode.
- Derive the backward of scaled dot-product attention from the matmul and softmax rules.
The ideas you will need
Section titled “The ideas you will need”The problems use a small set of symbols, collected here.
| Symbol | Meaning | Type / shape |
|---|---|---|
| a scalar loss | scalar | |
| , | matrices; | |
| upstream gradient, same shape as | ||
| differential: first-order change of | like | |
| trace, | scalar | |
| Frobenius inner product, | scalar | |
| elementwise product | ||
| Jacobian of , | ||
| vector | ||
| 1 if , else 0 | ||
| the all-ones vector |
Shapes, layouts, and strides
Section titled “Shapes, layouts, and strides”The convention in this book (and in PyTorch) is the denominator layout: has the shape of , entry by entry . A row-major array of shape stores index at element offset with strides . A view reinterprets the same memory with other strides: swapping two axes swaps their strides and copies nothing. Broadcasting reuses one value along an axis; its backward is a sum over that axis, which the unbroadcast of the tensor engine in Tensor, broadcasting backward, and no_grad performs. A Jacobian of a map from to is , which for a layer is enormous; reverse mode never builds it.
Differentials
Section titled “Differentials”With shapes settled, the next tool is the differential. The differential of at is the linear part of . It obeys the rules you know for scalars, with the order of factors kept: , , , , and (q16). For the result is . For an elementwise function , , so its Jacobian is diagonal.
The trace trick
Section titled “The trace trick”A differential becomes a gradient through the trace. For a scalar of a matrix , . So once is written as , the gradient is . The tool for getting there is the cyclic property: whenever the products are defined (a cyclic shift, never an arbitrary swap), together with . A scalar is its own trace, so any scalar expression may be wrapped in one.
VJPs of the ops the library registers
Section titled “VJPs of the ops the library registers”A vector-Jacobian product maps an upstream gradient to without forming . The table lists the ones implemented in Matrix differentials, the trace trick, and closed-form VJPs.
| Op | Forward | VJP |
|---|---|---|
| matmul | , | |
| softmax | ||
| log-softmax | ||
| cross-entropy | ; a mean over kept positions divides by | |
| RMSNorm | , | |
| LayerNorm | , | with : |
Here is the mean of the entries of , the mean of , , and the one-hot vector of the target. LayerNorm and RMSNorm are functions of a whole row: LayerNorm centers and scales it to mean 0 and variance 1, RMSNorm only scales it to root-mean-square 1, and both then apply a learned gain. Because each normalizes by a statistic of its own input, the gradient has a correction term that a “treat the statistic as constant” derivation misses. Two checks catch it: LayerNorm’s input gradient sums to zero (shifting changes nothing), and RMSNorm’s is orthogonal to when (scaling changes nothing).
Forward versus reverse mode
Section titled “Forward versus reverse mode”Which mode to run is a question of counting. For , forward mode (the dual numbers of Dual numbers and forward-mode autodiff) computes one Jacobian-vector product per pass, so the full Jacobian takes passes. Reverse mode computes one per pass, so it takes . A loss has : one backward pass gives the gradient with respect to every parameter, at the price of storing the forward activations it needs. For a matrix product the backward computes two products of the same size as the forward one, so backward costs about twice the forward FLOPs, and a training step about three times: the per token of q13 in the Discrete math 1 problem set.
Attention
Section titled “Attention”Everything above comes together in attention. Scaled dot-product attention (SDPA) for positions computes scores , weights along each row, and output . Its backward chains the three rules above: matmul for , softmax row by row, matmul again for (q34).
Two examples worked by hand
Section titled “Two examples worked by hand”Before the set itself, two warm-ups. They are siblings of q21 and q24, not problems of the set.
Softmax VJP. Let , so , and let the upstream gradient be . Then , so and .
Check against the Jacobian: for two classes , and . The entries sum to 0, as every softmax input gradient must.
Cross-entropy. Logits , target class 0. Then , , and : lower the wrong logit, raise the right one, by how far the prediction is from the target.
The problems
Section titled “The problems”Vectors are flat lists; matrices are lists of rows. A gradient answer has the variable’s shape. Answers are exact (, ), not decimals. Lettered parts are answered separately. The tag after each problem names the form the answer takes.
Layouts and shapes
Section titled “Layouts and shapes”The first four problems are bookkeeping: before any derivative, you need to know what shape each gradient has and how arrays sit in memory.
A linear layer computes for a batch of shape , stored row-major in float32, with of shape and of shape . is a scalar loss.
q1. Give the shape of (a) , (b) , (c) . [vector]
Answer
(a) . (b) . (c) . (a) The last axis is contracted against of shape , leaving . (b) A gradient has its variable’s shape, so is like . (c) Likewise like : the forward pass broadcast over and , so the backward sums those axes away (q3). Common mistakes: for (a), the input width; for (b), the transpose; for (c), forgetting to sum over the broadcast axes.
q2. (a) At which element offset (counting from 0) is stored? [number] (b) Give the strides of in elements, one per axis. [vector] (c) Give the strides of the view that swaps the last two axes of (shape , no copy). [vector]
Answer
(a) . (b) . (c) . (b) Row-major strides are the products of the trailing dimensions: . (a) Offset . (c) A view that swaps the last two axes swaps their strides and copies nothing: , now non-contiguous. Common mistakes: for (a), the column-major offset ; for (b), strides in bytes rather than elements; , column-major strides; for (c), the strides a fresh contiguous copy of shape would have.
q3. is broadcast over the batch and time axes. How many entries of are summed into each entry of ? [number]
Answer
. is added to for every , entries, so : six terms. Common mistakes: , every entry of ; , summing over the batch axis only.
q4. Flatten and row-major into vectors. What is the shape of the Jacobian (rows, columns)? [vector]
Answer
. A Jacobian has one row per output and one column per input: has entries and has . Nobody builds this matrix (for a real layer it is enormous and mostly zeros); VJPs exist so that you never do. Common mistake: , (inputs, outputs).
Differentials of matrix expressions
Section titled “Differentials of matrix expressions”The next group practices the differential rules on small expressions, ending with the proof of the inverse rule that q8 uses.
q5. with and . Give . [vector in x1, x2]
Answer
. , so , and . Common mistakes: , the formula that holds only for symmetric ; , dropping one of the two terms of the product rule.
q6. with . Give . [matrix]
Answer
. , so the trace trick reads off . Entry by entry: , so . Common mistake: itself.
q7. with , . Give at . [vector]
Answer
. With , , , so . At : , , , times 2. Common mistakes: , dropping the 2 from the square; , forgetting the that the chain rule brings in.
q8. . Give at . [matrix]
Answer
. Use (q16) with , , and . Then , times gives , and the minus sign flips it. Common mistakes: the same matrix with the sign lost; , as if were .
q9. Give at . [matrix]
Answer
. , so the gradient is . Here and ; transpose it. Common mistake: , not transposed (the two differ whenever is not symmetric).
q10. with . Give at . [matrix]
Answer
. , so and . At this is with . Common mistake: , dropping the 2.
q11. Give the derivative of the sigmoid . [expr in z]
Answer
. Chain rule on : . The second form follows from , and is the one The op library and gradcheck_all uses because it reuses the forward output. Common mistakes: itself; the same fraction with a minus sign, losing the sign of the inner derivative .
q12. Give the derivative of softplus, . [expr in z]
Answer
. ; divide top and bottom by . Softplus is the integral of the sigmoid. Common mistake: .
q13. Give the gradient of . [vector in x1, x2]
Answer
. of is . The gradient of log-sum-exp is softmax, which Stable numerics: logsumexp, softmax, and compensated sums and Losses with a fused backward rest on. Common mistake: , forgetting the derivative of the outer log.
q14. Give the gradient of at . [vector]
Answer
. , so : the unit vector, with . Common mistake: , the gradient of the squared norm.
q15. Give the Jacobian of elementwise at . [matrix]
Answer
. An elementwise function has a diagonal Jacobian with on the diagonal; is 0 for negative inputs and 1 for positive ones. Common mistakes: , the outputs on the diagonal instead of the slopes; a matrix of ones, as if each output depended on every input.
q16. Prove for an invertible square matrix . [proof]
Answer
Claim. For an invertible matrix , .
Proof. The differential obeys the product rule (expand and drop the second-order ), with the order of the factors kept because matrices do not commute. for every invertible near the current one, and , so Hence , and multiplying on the left by , For this is . Entry by entry, .
A complete derivation starts from , uses the product rule and , and keeps the non-commuting order when solving. Model derivation also in q16.md.
The trace trick
Section titled “The trace trick”These problems turn the differential into a gradient with the cyclic property, ending with the proof of the matmul VJP.
q17. For conformable matrices: (a) is always? (b) Is always? [bool]
Answer
(a) Yes. (b) No. (a) is a cyclic shift of , and with , gives it. (b) swaps two factors without rotating; for example , , give but . The trace is invariant under cyclic shifts only.
q18. with , , and upstream gradient . Give (a) and (b) . [matrix]
Answer
(a) . (b) . By q20, and when . The matmul_vjp of Matrix differentials, the trace trick, and closed-form VJPs returns exactly these two. Common mistakes: for (a), the factors in the wrong order and untransposed; the answer given for (a); for (b), forgetting the transpose.
q19. The Frobenius inner product is . Give it for , . [number]
Answer
. ; picks out the off-diagonal entries, . Common mistakes: , which is not the inner product; , the sum of ‘s entries.
q20. Let and a scalar with , . Prove and . [proof]
Answer
Claim. If and with , then and .
Proof. For a scalar of a matrix , , so once is written as the gradient is (unique, because the are independent). By the product rule , and by linearity of the trace Second term: , so . First term: by the cyclic property, , so .
Shapes check: is , is , is ; is like , and is like . A complete derivation writes by the product rule, splits the trace, rotates each term into form, and reads off both gradients. Model derivation also in q20.md.
VJPs by hand
Section titled “VJPs by hand”Now the rules the op library registers, each on numbers small enough to check by hand, ending with the proof of the softmax VJP.
q21. and the upstream gradient is . Give . [vector]
Answer
. , , and multiplying by gives the answer. The entries sum to 0, as every softmax input gradient must. Common mistakes: , dropping the term; , which confuses softmax with cross-entropy.
q22. Give the Jacobian of a 2-class softmax at . [matrix]
Answer
. , entry : diagonal , off-diagonal . Rows sum to 0 because softmax ignores a common shift. Common mistakes: , dropping ; all entries , losing the off-diagonal minus.
q23. with and upstream gradient . Give . [vector]
Answer
. The log-softmax VJP is ; , so it is . Common mistakes: , mixing the softmax and log-softmax rules; the same vector with the sign flipped.
q24. Cross-entropy with logits and target class 1 (counting from 0): . (a) Give . [vector] (b) Give . [number]
Answer
(a) . (b) . , sum 5, so . (a) Softmax minus the one-hot of class 1: . (b) . Common mistakes: , forgetting the one-hot; , one-hot minus softmax, which is the gradient of the log-likelihood and makes descent climb the loss; , the minus sign missing (a negative loss); , the probability rather than the loss.
q25. A batch has 4 positions; one has the target ignore_index and the loss is the mean over the other positions. By what factor is each kept position’s per-position gradient scaled? [number]
Answer
. The mean is over the kept positions, so each kept per-position gradient is divided by 3, and the ignored position contributes nothing. Common mistake: , dividing by all positions including the ignored one, which scales the gradient down by the padding fraction.
q26. RMSNorm with : , . (a) For , , and upstream , give . [vector] (b) Is for every and every ? [bool]
Answer
(a) . (b) Yes. (a) , , , and . First term: . Correction: . Subtract. (b) , and when , so the two terms cancel for every and . This is scale invariance: for , so the derivative along is 0; check (a): . Common mistakes: , treating as a constant; , keeping only the -dependent term.
q27. LayerNorm with , , : with the mean and . For and upstream , give . [vector]
Answer
. , , so and . With : and , so . Then ; multiply by . The entries sum to 0 (shift invariance) and the gradient is orthogonal to (scale invariance). Common mistakes: , treating both and as constants; , keeping the mean’s dependence but dropping the variance’s.
q28. Prove the softmax VJP: if and , then . [proof]
Answer
Claim. For and , .
Proof. Let , so . By the quotient rule, By the chain rule, In vector form, . It costs time and memory and never forms the Jacobian . Its entries sum to , as they must, since softmax is unchanged by adding a constant to every .
A complete derivation gets the Jacobian from the quotient rule, contracts it with , and states the vector form and its cost. Model derivation also in q28.md.
Forward versus reverse mode
Section titled “Forward versus reverse mode”Four short problems on what each mode costs, in passes, bytes, and FLOPs.
q29. A loss has parameters. How many (a) Jacobian-vector products (forward mode) and (b) vector-Jacobian products (reverse mode) give the full gradient? [number]
Answer
(a) . (b) . Forward mode gives one column per pass, so the Jacobian needs one pass per parameter. Reverse mode gives one row per pass, and a scalar loss has one row. This is why training uses reverse mode.
q30. You need the full Jacobian of . Which mode needs fewer passes? (a) forward (b) reverse [choice]
Answer
(a) forward. Forward mode needs one pass per input (3); reverse mode needs one per output (1000). The rule: pick the mode whose count of passes is the smaller dimension.
q31. Backward of a 16-layer MLP stores each layer’s input: a batch of 32 rows of width 1024 in float32. How many bytes of activations does it keep? [number]
Answer
bytes (2 MiB). . Memory that grows with depth and batch is what the checkpointing of Hessian-vector products and the recompute-versus-memory schedule trades for recompute. Common mistake: , counting floats rather than bytes.
q32. with of shape and of shape . (a) How many FLOPs does backward take to compute both and ? [expr in m, k, n] (b) What is the ratio (forward + backward) / forward? [number]
Answer
(a) . (b) . Forward: costs FLOPs ( outputs, multiply-adds each). Backward computes two products of the same size: ( times ) and ( times ), each. (b) : the FLOPs per token of q13 in the Discrete math 1 problem set. Common mistakes: , counting only one of the two products; , counting multiply-adds as one FLOP; for (b), the backward-to-forward ratio rather than the total.
The SDPA Jacobian
Section titled “The SDPA Jacobian”The set closes by chaining the matmul and softmax rules through scaled dot-product attention.
Scaled dot-product attention: , row by row, . Take positions, , , , , no mask.
q33. (a) Give . [matrix] With upstream gradient , give (b) and (c) . [matrix]
Answer
(a) . (b) . (c) . (a) , so row 0 of is and row 1 is . : row 0 is , row 1 is . (b) , which with is row 0 of as a column. (c) . Row 0 through the softmax VJP: , so and that ; row 1 is 0. picks column 1 of (since ): . Common mistakes: for (a), the softmax weights put on the wrong keys; , argmax instead of softmax; for (b), instead of ; for (c), with the softmax VJP skipped; the right magnitude with the sign lost.
q34. Prove the SDPA backward rules: with , , , , , and . [proof]
Answer
Claim. For , by rows, , and : , , , , .
Shapes: are , is , and are , and are .
Proof. Reverse mode through the three steps, each a rule already proved.
- is a matrix product. By q20 with , : (, like ) and (, like ).
- Row of is the softmax of row of , and rows do not interact. By q28 on row with upstream : .
- is a scaled product. By q20 with , , and the constant carried through: (, like ), and , so (, like ).
Every gradient has its variable’s shape. A causal mask sets masked to , so there and step 2 gives : no gradient flows through masked entries. FlashAttention’s backward recomputes tile by tile and applies these rules, with precomputed per row.
A complete derivation gets and from the matmul rule, applies the softmax VJP row by row, gets and from the scaled product, and checks every shape. Model derivation also in q34.md.
Common mistakes across the set
Section titled “Common mistakes across the set”The wrong answers listed under each problem cluster into a handful of recurring errors. The table collects them, with the problems where each one shows up.
| Mistake | Symptom | The fix |
|---|---|---|
| Gradient transposed relative to its variable | shape error in the optimizer, or a silently wrong square gradient | give every gradient its variable’s shape (q1 b); , not (q9) |
| Not summing over broadcast axes | bias gradient of shape (B, T, d) | sum over and , six terms per entry (q1 c, q3) |
| Counting bytes as strides, or using a contiguous copy’s strides for a view | wrong element read through a view | strides in elements; a transpose view swaps strides (q2) |
| for a non-symmetric | wrong gradient for asymmetric forms | from both terms of the product rule (q5) |
| Dropping the factor 2 of a square | gradients half as large; learning rates tuned to hide it | keep the 2 from (q7, q10) |
| Swapping a non-cyclic order inside a trace | an identity that holds only for commuting matrices | rotate cyclically only (q17, q20) |
| Softmax VJP without the term | input gradients that do not sum to zero | (q21, q28) |
| One-hot minus softmax | gradient ascent on the loss | softmax minus one-hot (q24 a) |
| Mean over all positions including ignored ones | loss and gradient scaled down by the padding fraction | divide by the kept count (q25) |
| Treating a normalization’s statistics as constants | RMSNorm or LayerNorm gradients without the correction term | differentiate through , , and ; check orthogonality and zero sum (q26, q27) |
| Using for | value gradients from the wrong positions | from the matmul rule (q33 b, q34) |
Where this leads
Section titled “Where this leads”Every derivation here becomes code. The dual numbers of Dual numbers and forward-mode autodiff compute the JVPs of the forward-versus-reverse discussion; Scalar reverse mode: the Value graph is the oracle against which the closed-form VJPs are tested; and Matrix differentials, the trace trick, and closed-form VJPs turns the VJP table into code, gradchecked rule by rule. In AI Engineering, The op library and gradcheck_all registers those VJPs as tensor ops, with the unbroadcast of q1 and q3; Losses with a fused backward implements fused cross-entropy with ignore_index (q24, q25); Pre-LN and RMSNorm puts RMSNorm in the modern block (q26); the attention of the Attention Origins chapter and Scaled dot-product attention (forward and backward) use the backward of q33 and q34; and FlashAttention, whose forward pass is built in FlashAttention forward in C, recomputes in its backward and applies q34’s rules tile by tile.
Further reading:
- Parr and Howard, “The Matrix Calculus You Need for Deep Learning” (2018).
- Minka, “Old and New Matrix Algebra Useful for Statistics” (2000).
- Baydin et al., “Automatic Differentiation in Machine Learning: a Survey” (2018), sections 2 and 3.
- Dao et al., “FlashAttention” (2022), appendix B.
Summary
Section titled “Summary”- A gradient has the shape of its variable, and broadcasting in the forward pass becomes a sum over the broadcast axes in the backward pass (q1, q3).
- Write the differential , push through the product rule, rotate with the cyclic trace, and read off the gradient: , (q18, q20).
- Softmax’s VJP is and cross-entropy’s gradient with respect to logits is , both (q21, q24, q28).
- Normalizations differentiate through their statistics: LayerNorm’s input gradient sums to zero, and RMSNorm’s is orthogonal to (q26, q27).
- Reverse mode costs one pass per output, so one backward pass gives a million-parameter gradient, at about twice the forward FLOPs (q29, q32).