TorchLean API

NN.Proofs.Autograd.Tape.Ops.Attention.ScaledDotProduct

ScaledDotProduct #

End-to-end fderiv/backprop correctness for a scaled dot-product attention graph, built out of the proven tape nodes (matmul, matrixTranspose, scale, softmaxLast).

This is spec-level over . It is a corollary of the general graph theorem once each node used by the graph has a NodeFDerivCorrect instance.

PyTorch correspondence / citations #

@[reducible, inline]

Matrix shape for m×d Q/K/V inputs.

Instances For
    @[reducible, inline]

    Input context shapes: [Q, K, V], each m×d.

    Instances For

      Context index of Q in ΓQKV.

      Instances For

        Context index of K in ΓQKV.

        Instances For

          Context index of V in ΓQKV.

          Instances For
            @[reducible, inline]

            Saved tensors of the attention graph: Kᵀ, logits, scaled logits, probabilities, output.

            Instances For
              @[reducible, inline]

              Node 1: Kᵀ.

              Instances For
                @[reducible, inline]

                Node 2: logits Q Kᵀ.

                Instances For
                  @[reducible, inline]

                  Node 3: scaled logits c * (Q Kᵀ).

                  Instances For
                    @[reducible, inline]

                    Attention graph prefix through Kᵀ.

                    Instances For
                      @[reducible, inline]

                      Attention graph prefix through the logits.

                      Instances For
                        @[reducible, inline]

                        Attention graph prefix through the scaled logits.

                        Instances For
                          @[reducible, inline]

                          Attention graph prefix through the softmax probabilities.

                          Instances For

                            Scaled dot-product attention as an explicit tape graph.

                            Computes Q K V ↦ softmax(c * (Q * Kᵀ)) * V and records intermediate values needed by backprop. The graph is a plain Graph.snoc chain so that Graph.evalVec can be computed node by node when relating the tape to the specification forward pass.

                            Instances For

                              Scaled dot-product attention as a proved-correct DGraph.

                              Every node of scaledDotProductGraph carries its NodeFDerivCorrect certificate.

                              Instances For

                                Corollary of the general DAG theorem: backprop equals (fderiv eval)† for the attention graph.

                                This is the formal statement that the tape reverse pass computes the VJP for the full attention computation.