Scaled dot-product attention: tape graph versus specification #
NN.Proofs.Autograd.Tape.Ops.Attention.ScaledDotProduct proves backprop = (fderiv eval)† for a
tape graph built from proven nodes. NN.Spec.Layers.Attention defines
Spec.scaledDotProductAttention directly on tensors. This file connects the two:
get_sdpaOutIdx_evalVec: the output block of the graph evaluation is exactly the vectorized specification forward passsoftmax(Q Kᵀ / √d) V;hasFDerivAt_sdpaSpecForwardVec: the specification forward pass is Fréchet-differentiable, with derivative read off the graph;backpropVec_eq_adjoint_fderiv_scaledDotProductAttention: the tape reverse pass seeded on the output block is the vector-Jacobian product ofSpec.scaledDotProductAttention;backpropVec_eq_adjoint_fderiv_scaledDotProductAttention_allTrueMask: the same statement for the masked code path with the all-true Boolean mask.
Blocks of the evaluated graph are read with the Graph.evalVec lemmas of
NN.Proofs.Autograd.Tape.Ops.Norm.CtxVecEval, and matrix coordinates are read through
TapeNodes.Matmul.tensorToVec_idxMN: attention is proved in the flat Vec world where
Mathlib's calculus lives, and stated about Tensor ℝ [m, n]. The first half relates the flattened
node functions (matmulVec, transposeVec, forwardMN) to Spec.matMulSpec,
swapAdjacentAxes, scaleSpec, and Activation.softmaxSpec 1; the second half walks the
attention graph node by node.
Flattened matrices #
Vectorization is homogeneous: scaling a matrix scales its vector. This is what lets the 1/√d
factor in scaled dot-product attention be handled as ordinary scalar multiplication.
Transposition on flat vectors swaps the row and column index, as expected.
Swapping the two axes of a matrix tensor corresponds to the flat transpose.
Attention transposes the key matrix, so without this the Kᵀ in Q Kᵀ would have to be reasoned
about at the tensor level and at the vector level separately.
Reading a vectorized matrix through the Fin m × Fin n product equivalence, after the size
cast, gives the matrix entry. This is TapeNodes.Matmul.tensorToVec_idxMN in the indexing
the softmax development uses.
Row i of a vectorized matrix is the vectorization of row i of the tensor.
Row-wise softmax on tensors agrees with the flat forwardMN softmax on vectors.
The temperature is fixed to 1 because that is the only case attention needs; the scaling is
already folded into the scores by tensorToVec_scaleSpec.
Attention graph evaluation #
Index of the attention output block in the saved context.
Instances For
The Q tensor stored in a vectorized attention context.
Instances For
The K tensor stored in a vectorized attention context.
Instances For
The V tensor stored in a vectorized attention context.
Instances For
Tensor-level attention forward pass with an explicit scale factor c.
Instances For
Unmasked Spec.scaledDotProductAttention is attentionForwardSpec at the scale 1 / √d.
The output block of the attention graph is the vectorized tensor forward pass.
The specification forward pass as a function on vectorized contexts.
Instances For
The specification forward pass is the output projection of the graph evaluation.
The specification attention forward pass is Fréchet-differentiable; its derivative is the output projection of the graph derivative.
Tape backprop seeded on the output block is the VJP of the specification forward pass.
The output block of the attention graph at scale 1 / √d is the vectorized
Spec.scaledDotProductAttention forward pass.
Tape backprop is the vector-Jacobian product of unmasked Spec.scaledDotProductAttention.
With the all-true Boolean mask, the masked code path of Spec.scaledDotProductAttention has
the same vector-Jacobian product, computed by the same tape backprop.