Jump to content

Self-attention: Difference between revisions

From Wikibase
Add sub-layer map section giving SL(X) for the residual connection; AI-assisted (RonzzWikiCowriter) (via update-page on MediaWiki MCP Server)
Tag: Reverted
 
(8 intermediate revisions by 2 users not shown)
Line 2: Line 2:
'''Self-attention''' is the process through which a [[Transformer]] enriches its [[token embedding (machine learning)|token embedding vectors]] with contextual information derived from other tokens in the same token sequence.<ref>{{#cite:Q3130}}</ref>
'''Self-attention''' is the process through which a [[Transformer]] enriches its [[token embedding (machine learning)|token embedding vectors]] with contextual information derived from other tokens in the same token sequence.<ref>{{#cite:Q3130}}</ref>


== Definition ==
== Single-head attention ==


Let $\mathbf{X}\in\mathbb{R}^{T\times d}$ be the '''vector embedding matrix''' of [[token (machine learning)|token sequence]] $X$, in which row $t$ is the embedding vector for the token at position $t$.
Let $\mathbf{X}\in\mathbb{R}^{T\times d}$ be the '''vector embedding matrix''' of [[token (machine learning)|token sequence]] $X$, in which row $t$ is the embedding vector for the token at position $t$.
Line 16: Line 16:
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]]).
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 stadard attention score
Then the standard attention score
$$
$$
S_\text{attention}=\operatorname{Attention}(\mathbf{Q},\mathbf{K},\mathbf{V})
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},
\operatorname{row-wise\ softmax}\left(\frac{\mathbf{Q}\mathbf{K}^{\top}}{\sqrt{d_k}}\right)\mathbf{V},
$$
$$


<blockquote>
<blockquote>
The [[Softmax|softmax]] is taken row-wise so for each embedding vector $\vec x_t$ within $X$, $\sum_{i=1}^{d_k} (S_{\text{attention})_{t,i} =1$.
The [[Softmax|softmax]] is taken row-wise so for each embedding vector $\vec x_t$ within $X$, $\sum_{i=1}^{d_k} (S_{\text{attention}})_{t,i} = 1$.


The scaling factor $1/\sqrt{d_k}$ keeps the scores from growing with the dimension.
The scaling factor $1/\sqrt{d_k}$ keeps the scores from growing with the dimension.
Line 32: Line 32:


<ref>{{#cite:Q3130}}</ref>
<ref>{{#cite:Q3130}}</ref>
== Sub-layer map ==


The [[residual connection (Transformer)|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$:
== Multi-head attention sub-layer and residual ==
 
In a standard [[Transformer]], each attention sub-layer has $h$ attention heads $\{\operatorname{head}_1(\mathbf{X}),\cdots,\operatorname{head}_h(\mathbf{X})\}$.
 
where for each $\operatorname{head}_i$


$$
$$
\operatorname{head}_i(\mathbf{X})
S_\text{attention}(i)=\operatorname{Attention}(\mathbf{Q}_i,\mathbf{K}_i,\mathbf{V}_i).
=
\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,
$$
$$
Define sub-layer '''residual'''


$$
$$
\operatorname{SL}_{\text{attn}}(\mathbf{X})
\operatorname{SL}_{\text{attn}}(\mathbf{X})
=
=
\left[\operatorname{head}_1(\mathbf{X}),\cdots,\operatorname{head}_h(\mathbf{X})\right]\mathbf{W}_O
\left[S_\text{attention}(1),\cdots,S_\text{attention}(h)\right]\mathbf{W}_O
\in\mathbb{R}^{T\times d},
\in\mathbb{R}^{T\times d},
$$
$$
 
where $\mathbf{W}_O\in\mathbb{R}^{hd_k\times d}$ is a '''weight matrix''' learnt during training.
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}.
$$


<blockquote>
<blockquote>
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$.
The sublayer '''residual''' is then fed through [[residual connection (Transformer)|a residual connection mechanism]].
</blockquote>
</blockquote>
$\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 [[Feed-Forward Network (FFN) Sub-Layer (Transformer)|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 ==
== Complexity ==


The complexity of standard self-attention
The complexity of standard multi-head self-attention


* Time $\propto O(T^2 d)$
* Time $\propto O(T^2 d h)$
* Memory $\propto O(T^2)$
* Memory $\propto O(T^2 h)$


where $O$ is the [[o(x) and o(x)|big-O notation]].
where $O$ is the [[o(x) and o(x)|big-O notation]].


{{Hidden proof}}
{{Hidden proof}}
'''Compute time''':
'''Single attention head'''
Since
Since


Line 127: Line 94:


$$
$$
\text{Time} \propto O(T^2 \cdot d)
\text{Time} \propto O(T^2 \cdot d).
$$
$$


time to form the score matrix.
to calculate the score matrix.


After that, attention also computes
After that, attention also computes
Line 140: Line 107:
where $\mathbf{V}\in\mathbb{R}^{T\times d}$. The softmax touches the $T\times T$ score matrix, costing $O(T^2)$
where $\mathbf{V}\in\mathbb{R}^{T\times d}$. The softmax touches the $T\times T$ score matrix, costing $O(T^2)$


Finally, multiplying the $T\times T$ attention matrix by $\mathbf{V}$ costs another $O(T^2 d)$. So the attention cost is  
Then, multiplying the $T\times T$ attention matrix by $\mathbf{V}$ costs another $O(T^2 d)$. So the attention cost per attention head is  


$$
$$
\text{Time} \propto O(T^2 d)+O(T^2)+O(T^2 d)=O(T^2 d)
\text{Time} \propto O(T^2 d)+O(T^2)+O(T^2 d)=O(T^2 d).
$$
$$


Memory is $O(T^2)$ as only the final $T\times T$ score/attention matrix need to be stored in standard attention without additional caching.
'''Multi-head aggregation and calculating residual'''
 
There are $h$ attention heads, for a total of $O(T^2 dh)$.
 
Note in addition to compute '''residual''' there is a $(T \times T)\times (T \times d)$, representing $Td$ dot $T$ dimensional [[vector dot product]]s, for a total of $O(T^2 d)$.
 
Hence the overall sub-layer compute is
 
$$
O(T^2 dh)+O(T^2 d)=O(T^2 dh).
$$
 
'''Memory''':
 
Since in a standard [[Transformer]] $T \gg d_k,d,h$, memory cost is dominated by the storage of $h$ $T \times T$ attention score matrices $S_\text{attention}(i)$, for a total of $O(T^2h)$.
{{End hidden}}
{{End hidden}}



Latest revision as of 11:28, 12 November 2026

Languages: English · français · Esperanto

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]

Single-head attention

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 for each embedding vector $\vec x_t$ within $X$, $\sum_{i=1}^{d_k} (S_{\text{attention}})_{t,i} = 1$.

The scaling factor $1/\sqrt{d_k}$ keeps the scores from growing with the dimension.

Both are normalisation functions applied for numerical stability.

[1]

Multi-head attention sub-layer and residual

In a standard Transformer, each attention sub-layer has $h$ attention heads $\{\operatorname{head}_1(\mathbf{X}),\cdots,\operatorname{head}_h(\mathbf{X})\}$.

where for each $\operatorname{head}_i$

$$ S_\text{attention}(i)=\operatorname{Attention}(\mathbf{Q}_i,\mathbf{K}_i,\mathbf{V}_i). $$

Define sub-layer residual

$$ \operatorname{SL}_{\text{attn}}(\mathbf{X}) = \left[S_\text{attention}(1),\cdots,S_\text{attention}(h)\right]\mathbf{W}_O \in\mathbb{R}^{T\times d}, $$ where $\mathbf{W}_O\in\mathbb{R}^{hd_k\times d}$ is a weight matrix learnt during training.

The sublayer residual is then fed through a residual connection mechanism.

Complexity

The complexity of standard multi-head self-attention

  • Time $\propto O(T^2 d h)$
  • Memory $\propto O(T^2 h)$

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.

[1]