Self-attention: Difference between revisions
No edit summary |
No edit summary |
||
| Line 34: | Line 34: | ||
== Complexity == | == Complexity == | ||
The | The complexity of standard self-attention | ||
* Time $\propto O(T^2 d)$ | |||
* Memory $\propto O(T^2)$ | |||
where $O$ is the [[o(x) and o(x)|big-O notation]]. | |||
{{Hidden proof}} | |||
Since | |||
$$ | |||
\mathbf{Q}\in\mathbb{R}^{T\times d},\, | |||
\mathbf{K}\in\mathbb{R}^{T\times d} | |||
$$ | |||
so | |||
$$ | |||
\mathbf{Q}\mathbf{K}^{\top}\in\mathbb{R}^{T\times T}. | |||
$$ | |||
Computing one entry of $\mathbf{Q}\mathbf{K}^{\top}$ is a dot product of length $d$: | |||
$$ | |||
(\mathbf{Q}\mathbf{K}^{\top})_{ij} | |||
= \sum_{r=1}^{d} Q_{ir}K_{jr}. | |||
$$ | |||
There are $T \times T = T^2$ such entries, and each costs $O(d)$ work. Therefore: | |||
$$ | |||
\text{Time} \propto O(T^2 \cdot d) | |||
$$ | |||
time to form the score matrix. | |||
After that, attention also computes | |||
$$ | |||
\mathrm{softmax}(\mathbf{Q}\mathbf{K}^{\top})\mathbf{V}, | |||
$$ | |||
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 | |||
$$ | |||
\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. | |||
{{End hidden}} | |||
Generating a sequence of tokens in [[large language model (LLM)#Inference|inference]] recomputes most of this work unless the keys and values computed and cached, such as via [[KV caching]]. | |||
<ref>{{#cite:Q3130}}</ref> | <ref>{{#cite:Q3130}}</ref> | ||
Revision as of 09:59, 12 November 2026
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 stadard 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.
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.