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. The first half of this file 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 #

theorem Proofs.Autograd.val_finProdFinEquiv {m k : } (i : Fin m) (j : Fin k) :
(finProdFinEquiv (i, j)) = j + k * i
theorem Proofs.Autograd.val_idxMN {m n : } (i : Fin m) (j : Fin n) :
(TapeNodes.Matmul.idxMN i j) = j + n * i

The flattened index of entry (i, j) is j + n * i: row-major layout, as in PyTorch.

theorem Proofs.Autograd.exists_idxMN {m n : } (ip : Fin (TapeNodes.Matmul.matSize m n)) :
∃ (i : Fin m) (j : Fin n), TapeNodes.Matmul.idxMN i j = ip

Every flattened matrix index is the image of a row/column pair.

Coordinate idxMN i j of a vectorized matrix is the matrix entry A i j.

This is the bridge the whole file is named for. Attention is proved in the flat Vec world, where Mathlib's calculus lives, and stated about Tensor ℝ [m, n]; this lemma is what connects the two without ever unfolding the flattening.

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 Qᵀ 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 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.

              The adjoint of block projection is block injection.

              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.