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 #
- This file matches the usual mathematical definition of scaled dot-product attention:
softmax(c * Q Kᵀ) V. In PyTorch this corresponds to building the same computation withtorch.matmul+torch.softmax, or using the dedicated helper: https://pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html
Matrix shape for m×d Q/K/V inputs.
Instances For
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
Saved tensors of the attention graph: Kᵀ, logits, scaled logits, probabilities, output.
Instances For
Node 1: Kᵀ.
Instances For
Node 2: logits Q Kᵀ.
Instances For
Node 3: scaled logits c * (Q Kᵀ).
Instances For
Node 4: row-wise softmax probabilities.
Instances For
Node 5: output probs * V.
Instances For
Attention graph prefix through Kᵀ.
Instances For
Attention graph prefix through the logits.
Instances For
Attention graph prefix through the scaled logits.
Instances For
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.