TorchLean API

NN.Proofs.Autograd.Tape.Ops.Attention.SpecBridge

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:

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
    @[reducible, inline]
    noncomputable abbrev Proofs.Autograd.Attention.ctxQ {m d : ℕ} (xV : CtxVec (ΓQKV m d)) :

    The Q tensor stored in a vectorized attention context.

    Instances For
      @[reducible, inline]
      noncomputable abbrev Proofs.Autograd.Attention.ctxK {m d : ℕ} (xV : CtxVec (ΓQKV m d)) :

      The K tensor stored in a vectorized attention context.

      Instances For
        @[reducible, inline]
        noncomputable abbrev Proofs.Autograd.Attention.ctxV {m d : ℕ} (xV : CtxVec (ΓQKV m d)) :

        The V tensor stored in a vectorized attention context.

        Instances For

          Tensor-level attention forward pass with an explicit scale factor c.

          Instances For

            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.

              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.