Multi-head attention with a direct reshape split #
End-to-end fderiv/backprop correctness for a multi-head attention variant with a direct
reshape split, decomposed into proven tape nodes:
- linear projections via
matmul, - head split/merge via
reshapeand coordinate reindexing, - attention core via batched
matmul, coordinate reindexing, scaling, and row-wise softmax.
This is a graph-level result over ℝ. It is a corollary of the general graph theorem once each
node used by the graph has a NodeFDerivCorrect instance.
The Q/K/V split here directly reshapes [n, numHeads * headDim] to [numHeads, n, headDim],
preserving flat coordinate order. In contrast, Spec.splitHeadsSpec first reshapes to
[n, numHeads, headDim] and swaps the token/head axes; the executable attention paths use that
permutation. For n = numHeads = headDim = 2, flat projection entries 0,1,2,3,4,5,6,7 stay in
that order in this graph, while the spec split produces 0,1,4,5,2,3,6,7.
The theorem below proves the derivative of this declared graph. It does not establish equality
with Spec.MultiHeadAttention.forward or the executable attention implementation.
PyTorch correspondence / citations #
- The construction has the “project → split heads → scaled dot-product attention → concat heads →
output projection” stages of
torch.nn.MultiheadAttention, with the different split layout described above. https://pytorch.org/docs/stable/generated/torch.nn.MultiheadAttention.html - The core attention step corresponds to
torch.nn.functional.scaled_dot_product_attention. https://pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html
Sequence input shape n×dModel.
Instances For
Concatenated-head representation n×(numHeads*headDim).
Instances For
Split-head representation (numHeads)×n×headDim.
Instances For
Key-transposed shape (numHeads)×headDim×n used for Q Kᵀ.
Instances For
Attention scores shape (numHeads)×n×n.
Instances For
Intermediate shape after swapping axes for concatenation n×numHeads×headDim.
Instances For
Intermediate node output shapes (tape “saved tensors”) for the MHA graph.
Instances For
Saved tensors produced while projecting the input into query, key, and value heads.
Instances For
Saved tensors through scaled query-key scores, before softmax is applied.
Instances For
Saved tensors through the head-wise attention output, before heads are merged.
Instances For
Projection weight shape dModel×(numHeads*headDim) (used for Q/K/V).
Instances For
Output projection weight shape (numHeads*headDim)×dModel.
Instances For
Input context shapes: [x, Wq, Wk, Wv, Wo].
Instances For
Context index of the sequence input x in ΓMHA.
Instances For
Context index of Wq in ΓMHA.
Instances For
Context index of Wk in ΓMHA.
Instances For
Context index of Wv in ΓMHA.
Instances For
Context index of Wo in ΓMHA.
Instances For
Reshaping [n, numHeads * headDim] directly to [numHeads, n, headDim] preserves the
element count.
The graph's reshape node preserves flat coordinate order. This count equality does not provide
the token/head axis permutation used by Spec.splitHeadsSpec.
The same count equality in the direction used when concatenating heads back together.
Append a row-wise softmax node for the most recently saved batched matrix.
Instances For
Project the sequence input into query, transposed-key, and value head tensors.
Instances For
Form and scale the query-key score matrices for every head.
Instances For
Normalize the score rows and combine the resulting probabilities with the value heads.
Instances For
Multi-head attention with a direct reshape split as a proof-carrying graph.
This implements
x Wq Wk Wv Wo ↦ concat_heads (softmax(c * (Q Kᵀ)) V) * Wo, with Q, K, and V
projected from x. Each projection is reshaped directly to [numHeads, n, headDim] without
permuting its flat entries. The output merge swaps the head/token axes before flattening. These
are the graph's declared operations; its input split differs from Spec.splitHeadsSpec.
Instances For
Corollary of the general DAG theorem: backprop equals (fderiv eval)† for the MHA graph.
This is the formal “VJP correctness” statement for the full MHA computation (as laid out by
mhaDGraph).