Why context matters #
In the sentences “the crane lifted the beam” and “the crane crossed the marsh,” the same word has two meanings. A useful internal representation of crane should depend on the surrounding words. Self-attention gives a network a learned way to decide which positions in the current sequence contribute information to each other position.
The attention operation #
Each position is projected into a query, a key and a value. Comparing one position’s query with candidate keys produces scores. Turning these scores into weights and using them to combine the corresponding values gives that position a context-sensitive update. Multiple attention heads perform different learned projections, and feed-forward layers transform the representations that the heads help build.
Position information is important. A set of words shuffled into a different order can mean something entirely different. The original Transformer added positional information to its input representations. In its translation decoder, a mask also restricted which output positions could contribute to a prediction, so the model could learn to generate text from the tokens already available.
From translation to language models #
Ashish Vaswani and colleagues introduced the architecture in 2017 in an encoder-decoder system. An encoder represented the input sentence, and a decoder generated its translation. Later systems reused parts of that design for different objectives. BERT learned representations using context on both sides of a masked word. GPT-3 used an autoregressive objective, predicting subsequent tokens from the preceding context.
Where the cost appears #
With ordinary dense self-attention, each of a sequence’s positions can interact with every allowed position. Doubling sequence length roughly quadruples the number of potential pairwise interactions in an unmasked layer. Other parts of the model contribute to total cost as well. This distinction helps explain why the practical length of a context window matters separately from the number of parameters in a model.
What the original translation experiment measured #
The 2017 paper evaluated translations against reference text. On the WMT 2014 English-to-German task the authors reported 28.4 BLEU, and on English-to-French they reported 41.8. BLEU is sensitive to agreement between sequences of words in the candidate translation and reference translations. Its scale belongs to a particular test and evaluation procedure. Two scores from different language pairs do not rank those languages by difficulty.
The translation setting also makes two kinds of attention easy to distinguish. When producing the next German word, the decoder can consult representations of the complete English sentence. It can also consult the German words produced so far. The source sentence is available from the beginning, while the future German words would reveal the answer during training. These different information boundaries explain why one part of the architecture needs a causal mask.
What the accelerator has to move #
Imagine storing every pairwise attention score for a long passage in the accelerator’s main memory and reading that table back to combine the value vectors. Moving the table can be expensive even on a processor capable of fast multiplication. FlashAttention computes the result in blocks small enough to use fast on-chip memory, carrying forward the quantities needed to normalize the weights. It evaluates dense attention without writing the entire table to main memory. Pairwise comparisons still grow quadratically with sequence length. The saving comes from moving less data between levels of memory.
A different constraint appears when generating one new token at a time. A decoder can save keys and values from earlier positions and reuse them at the next step. A longer prompt means a larger cache to read for each new token. Shazeer’s multi-query attention lets different query heads share keys and values, reducing those reads. This changes the architecture itself. Which design helps more depends in part on whether a workload processes many known tokens together or repeatedly produces a single new one.
Sources
- Ashish Vaswani and colleagues, Attention Is All You Need (2017) arxiv.org
- Jacob Devlin and colleagues, BERT (2018) arxiv.org
- Tom Brown and colleagues, Language Models are Few-Shot Learners (2020) arxiv.org
- Tri Dao and colleagues, FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness (2022) arxiv.orgCited in
- Noam Shazeer, Fast Transformer Decoding: One Write-Head Is All You Need (2019) arxiv.orgCited in
Step 1 of 5