What it does
Attention compares a query vector from one position with a key vector from every position, using dot products. The scores are scaled, passed through a softmax to become weights that sum to one, and used to average the value vectors. A position can therefore pull in information from anywhere in the sequence in a single step, and which positions it listens to depends on the content, not on fixed distances. In self-attention, queries, keys and values all come from the same sequence; in cross-attention, queries come from one sequence and keys and values from another. In an exported graph it shows up as MatMul, a scale (Div or Mul), an optional mask Add, Softmax, and a second MatMul.
Formula
Attention(Q, K, V) = softmax(Q KT / √d) Vaij = eqi · kj / √d / Σl eqi · kl / √dyi = Σj aij vjShapes and parameters
- Shapes
- Queries [N, Tq, d], keys [N, Tk, d], values [N, Tk, dv]; the weight matrix is [N, Tq, Tk]; output [N, Tq, dv].
- Parameters
- None in the attention step itself. The learned parameters live in the dense projections that produce Q, K and V (see multi-head attention).
- Used for
- Every transformer, cross-attention in translation decoders, the point-prompt decoder of SlimSAM, and the tracker in VitTrack.
In WeightSpace
Read-only in WeightSpace today. Transformer models in the catalog are edited at their output heads or at a final normalization; the effect of those edits does not change which positions attend to which.
Where it appears
No model in WeightSpace uses Attention yet; it is here as a reference for how the others work.