Self-attention
Self-attention is the process through which a Transformer enriches its token embedding vectors with contextual information derived from other tokens in the same token sequence.[1]
Definition
Let $\mathbf{X}\in\mathbb{R}^{T\times d}$ be the vector embedding matrix of token sequence $X$, in which row $t$ is the embedding vector for the token at position $t$.
$$ \mathbf{Q}=\mathbf{X}\mathbf{W}_Q, \qquad \mathbf{K}=\mathbf{X}\mathbf{W}_K, \qquad \mathbf{V}=\mathbf{X}\mathbf{W}_V. $$
where $\mathbf{W}_Q,\mathbf{W}_K,\mathbf{W}_V\in\mathbb{R}^{d\times d_k}$ are weight matrices learnt during training (e.g., via backpropagation).
Then the standard attention score $$ S_\text{attention}=\operatorname{Attention}(\mathbf{Q},\mathbf{K},\mathbf{V}) = \operatorname{row-wise softmax}\left(\frac{\mathbf{Q}\mathbf{K}^{\top}}{\sqrt{d_k}}\right)\mathbf{V}, $$
The softmax is taken row-wise, so the weight matrix
$$ \mathbf{A}=\operatorname{row-wise softmax}\left(\frac{\mathbf{Q}\mathbf{K}^{\top}}{\sqrt{d_k}}\right)\in\mathbb{R}^{T\times T} $$
satisfies $\sum_{j=1}^{T}A_{tj}=1$ for every position $t$: row $t$ is a categorical probability distribution over the $T$ positions that position $t$ reads.
The scaling factor $1/\sqrt{d_k}$ keeps the scores from growing with the dimension.
Both are normalisation functions applied for numerical stability.
Sub-layer map
The residual connection adds the output of a sub-layer to its input, so the self-attention sub-layer must return a matrix of shape $T\times d$, like $\mathbf{X}$. The score above returns $T\times d_k$ instead, so $h$ heads are computed and concatenated, and the result is projected back to width $d$:
$$ \operatorname{head}_i(\mathbf{X}) = \operatorname{row-wise softmax}\left(\frac{\mathbf{X}\mathbf{W}_{Q,i}\left(\mathbf{X}\mathbf{W}_{K,i}\right)^{\top}}{\sqrt{d_k}}\right)\mathbf{X}\mathbf{W}_{V,i} \in\mathbb{R}^{T\times d_k}, \qquad i=1,\cdots,h, $$
$$ \operatorname{SL}_{\text{attn}}(\mathbf{X}) = \left[\operatorname{head}_1(\mathbf{X}),\cdots,\operatorname{head}_h(\mathbf{X})\right]\mathbf{W}_O \in\mathbb{R}^{T\times d}, $$
where $[\cdot,\cdots,\cdot]$ concatenates the heads along the feature axis, $d_k=d/h$, and $\mathbf{W}_O\in\mathbb{R}^{hd_k\times d}$ is the output projection.
The widths close because
$$ h\,d_k=h\,\frac{d}{h}=d, $$
so
$$ \underbrace{\mathbf{X}}_{T\times d} \longrightarrow \underbrace{\left[\operatorname{head}_1(\mathbf{X}),\cdots,\operatorname{head}_h(\mathbf{X})\right]}_{T\times hd_k\;=\;T\times d} \longrightarrow \underbrace{\operatorname{SL}_{\text{attn}}(\mathbf{X})}_{T\times d}, $$
and $\mathbf{X}+\operatorname{SL}_{\text{attn}}(\mathbf{X})$ is defined.
With the base model, $d=512$, $h=8$,
$$ d_k=\frac{512}{8}=64, \qquad \left[\operatorname{head}_1(\mathbf{X}),\cdots,\operatorname{head}_8(\mathbf{X})\right]\in\mathbb{R}^{T\times512}, \qquad \mathbf{W}_O\in\mathbb{R}^{512\times512}. $$
The output projection is kept even for 1 head, where $d_k=d$ already gives width $d$: with $\mathbf{W}_O\in\mathbb{R}^{d\times d}$, $\operatorname{SL}_{\text{attn}}$ is never the bare score. $\mathbf{W}_O$ is also what makes the heads interchangeable, since a permutation of the $h$ blocks of columns of the concatenation is absorbed by the same permutation of the rows of $\mathbf{W}_O$.
$\mathbf{W}_O$ holds $hd_kd=d^{2}$ parameters, so the sub-layer holds $4d^{2}$ in total with the 3 input projections; the split of a block's parameters between this sub-layer and the FFN sub-layer is given there. Because $hd_k=d$, the $h$ heads together cost
$$ h\cdot\mathcal{O}\left(T^{2}d_k\right)=\mathcal{O}\left(T^{2}d\right), $$
the bound derived below.
Complexity
The complexity of standard self-attention
- Time $\propto O(T^2 d)$
- Memory $\propto O(T^2)$
where $O$ is the big-O notation.
Generating a sequence of tokens in inference recomputes most of this work unless the keys and values computed and cached, such as via KV caching.