■ Change #91 · wiki.edited
The transformer architecture: Normalize Markdown to canonical form
body
## Architecture− A transformer maps a sequence of $n$ token vectors $X\in\mathbb R^{n\times d_{\mathrm{model}}}$ to a sequence of the same shape through a stack of identical blocks. Each block has a *self-attention* layer and a position-wise feed-forward network, each wrapped in a residual connection and normalisation. In one attention head, learned projections give queries, keys and values $Q,K,V\in\mathbb R^{n\times d}$, and the output is− $$\mathrm{Att}(Q,K,V)=\mathrm{softmax}\!\Big(\tfrac{QK^{\top}}{\sqrt d}\Big)V,$$+ A transformer maps a sequence of $n$ token vectors $X\in\mathbb R^{n\times d_{\mathrm{model}}}$ to a sequence of the same shape through a stack of identical blocks. Each block has a _self-attention_ layer and a position-wise feed-forward network, each wrapped in a residual connection and normalisation. In one attention head, learned projections give queries, keys and values $Q,K,V\in\mathbb R^{n\times d}$, and the output is+ $\mathrm{Att}(Q,K,V)=\mathrm{softmax}\!\Big(\tfrac{QK^{\top}}{\sqrt d}\Big)V,$where softmax acts on each row. Several heads run in parallel and their outputs are concatenated. In a decoder-only language model a causal mask forbids attending to later positions and the model predicts the next token. Vaswani et al. (2017) introduced the design for machine translation, replacing the recurrent networks that had dominated sequence modelling.## CostForming $QK^{\top}$ takes $O(n^2 d)$ time and the attention matrix has $n^2$ entries, so naive attention is quadratic in context length. The feed-forward layers cost $O(n\,d_{\mathrm{model}}^2)$, which dominates at short context. During autoregressive generation the keys and values of all previous tokens are cached, so each new token costs $O(nd)$ time but requires reading the whole cache from memory, which makes decoding memory-bound on current accelerators. Training uses reverse-mode automatic differentiation and is parallel across positions, which is a main reason transformers displaced recurrent networks.## Making attention cheaperThere are three families of responses.**Exact but IO-aware.** FlashAttention (Dao et al., 2022) computes exactly the same output but tiles the computation so the $n\times n$ matrix is never written to slow memory, cutting memory traffic and making memory use linear in $n$. The arithmetic stays quadratic.**Approximate or sparse.** Sparse, low-rank, kernel and hashing-based methods approximate the attention matrix. Linear attention (Katharopoulos et al., 2020) replaces the softmax by a kernel feature map so that attention can be computed as a recurrence with a fixed-size state.**Recurrent and state-space layers.** Models such as Mamba (Gu and Dao, 2023) use selective state-space recurrences with cost linear in $n$, and hybrids interleave them with a few full-attention layers. Their hidden state has fixed size, which limits how much of a long context they can retrieve exactly; how much this matters in practice has been studied mainly by experiment, not theory.## What theory saysAlman and Song (2023) proved a sharp threshold for computing attention to within $1/\mathrm{poly}(n)$ additive error: when entries of $Q,K,V$ are bounded by $o(\sqrt{\log n})$ there is an $n^{1+o(1)}$ algorithm, and when they reach $\Theta(\sqrt{\log n})$ no truly subquadratic algorithm exists unless the Strong Exponential Time Hypothesis fails (for head dimension $d=\Theta(\log n)$). Standard transformers also face expressivity limits: with saturated attention (Merrill, Sabharwal and Smith, TACL 2022) or with logarithmic numerical precision (Merrill and Sabharwal, TACL 2023), constant-depth transformers compute only functions in the circuit class $\mathsf{TC}^0$, which limits what they can do in a single forward pass, without long chains of generated reasoning tokens.## Open questionsWhether a subquadratic layer can match full attention on the tasks that matter, and why transformer loss improves predictably with scale, are treated in the two open problems attached to this entry.
explainer.student
**Architecture.** A transformer maps a sequence of $n$ token vectors (each of size $d_{\text{model}}$) to a sequence of the same shape through a stack of identical blocks. Each block has a **self-attention** layer and a position-wise feed-forward network, each wrapped in a residual connection and normalisation. Vaswani et al. (2017) introduced it for machine translation, replacing recurrent networks.− In one attention head, learned linear maps produce *queries* $Q$, *keys* $K$ and *values* $V$ for every token. The output is+ In one attention head, learned linear maps produce _queries_ $Q$, _keys_ $K$ and _values_ $V$ for every token. The output is− $$\mathrm{Att}(Q,K,V)=\mathrm{softmax}\!\Big(\tfrac{QK^{\top}}{\sqrt d}\Big)V.$$+ $\mathrm{Att}(Q,K,V)=\mathrm{softmax}\!\Big(\tfrac{QK^{\top}}{\sqrt d}\Big)V.$− In words: each token's query is compared with every token's key to get scores; softmax turns each row of scores into weights that add up to 1; the output for each token is the weighted average of the values. Several heads run in parallel and are concatenated. In a decoder-only language model a *causal mask* stops each position from seeing later ones, and the model predicts the next token.+ In words: each token's query is compared with every token's key to get scores; softmax turns each row of scores into weights that add up to 1; the output for each token is the weighted average of the values. Several heads run in parallel and are concatenated. In a decoder-only language model a _causal mask_ stops each position from seeing later ones, and the model predicts the next token.− **Cost.** Forming $QK^{\top}$ takes time proportional to $n^2d$ and the attention matrix has $n^2$ entries, so naive attention is *quadratic* in context length. The feed-forward layers cost about $n\,d_{\text{model}}^2$, which dominates at short contexts. During generation, the keys and values of earlier tokens are stored in a *KV cache*, so each new token costs time proportional to $n\,d$ but must read the whole cache from memory, which makes decoding limited by memory bandwidth on current accelerators. Training uses reverse-mode automatic differentiation and is parallel across positions, a main reason transformers displaced recurrent networks.+ **Cost.** Forming $QK^{\top}$ takes time proportional to $n^2d$ and the attention matrix has $n^2$ entries, so naive attention is _quadratic_ in context length. The feed-forward layers cost about $n\,d_{\text{model}}^2$, which dominates at short contexts. During generation, the keys and values of earlier tokens are stored in a _KV cache_, so each new token costs time proportional to $n\,d$ but must read the whole cache from memory, which makes decoding limited by memory bandwidth on current accelerators. Training uses reverse-mode automatic differentiation and is parallel across positions, a main reason transformers displaced recurrent networks.**Making attention cheaper.** Three families of responses:− - *Exact but IO-aware.* FlashAttention (Dao et al., 2022) computes exactly the same output but tiles the computation so the $n\times n$ matrix is never written to slow memory, cutting memory traffic.− - *Approximate.* Sparse patterns, low-rank sketches or hashing reduce the cost by accepting a small error.− - *Recurrent or state-space alternatives.* Replace attention with a fixed-size running state to get linear cost, at some loss of recall of exact earlier details.+ - _Exact but IO-aware._ FlashAttention (Dao et al., 2022) computes exactly the same output but tiles the computation so the $n\times n$ matrix is never written to slow memory, cutting memory traffic.+ - _Approximate._ Sparse patterns, low-rank sketches or hashing reduce the cost by accepting a small error.+ - _Recurrent or state-space alternatives._ Replace attention with a fixed-size running state to get linear cost, at some loss of recall of exact earlier details.**Theory questions.** What can a transformer of given depth, width and precision compute? How many layers does a task like following a chain of references need? What do scaling laws, the smooth improvement of loss with more data and compute, look like theoretically? Some answers are known. With limited numerical precision, constant-depth transformers can only compute functions in the circuit class $\mathsf{TC}^0$ in one forward pass (Merrill and Sabharwal). And computing attention to good accuracy in truly subquadratic time is impossible for large entries unless SETH fails, while it can be done almost linearly for small entries (Alman and Song, 2023). Most other questions, including why scaling laws hold, are open.
hash 7eb04ad8d759142ffec2b7196358e4ac9f3ac45bf2be31cc9e22347f21f86dbe
previous 4d304bae78bc5731f5420b5bc19a2095081219e9ed3b3b0517db96b8da9023ee