Attention Mechanisms
Mechanisms that learn where to focus: multi-head, linear attention, FlashAttention, and more.
Additive Attention (Bahdanau)Additive attention computes "where to look" scores by passing the decoder state and the vector at each input position through a small network with one hidden layer. Proposed by Bahdanau et al. in 2014 for machine translation, it removed the need to pack the input sentence into a single vector, so translation quality no longer degrades as easily on long sentences.
BigBird (Sparse Attention)BigBird is a sparse attention mechanism in which each token attends only to three kinds of tokens: a local window, a few global tokens, and randomly chosen tokens. It keeps the complexity linear in sequence length while provably retaining the same theoretical expressive power as standard attention, enabling BERT-style models to handle documents of 4,096 tokens.
CBAMCBAM (Convolutional Block Attention Module) is a small module that applies channel attention (what to look at) and spatial attention (where to look), in that order, to a CNN feature map to emphasize important features. It extends the SE block with max pooling and spatial attention, and improves accuracy on ImageNet classification and object detection with only a slight increase in computation.
FlashAttentionFlashAttention computes attention by splitting the attention matrix into small tiles and processing them only in the GPU's fast on-chip memory (SRAM), never writing the $N \times N$ matrix to slow memory (HBM). It produces the same result as standard attention while reducing memory reads and writes to run faster, and keeps memory usage linear in sequence length.
FlashAttention-2FlashAttention-2 reworks how FlashAttention partitions its computation: it reduces non-matmul operations, parallelizes along the sequence dimension as well, and changes how work is split among warps inside the GPU, computing the same exact attention about twice as fast. It reaches up to 73% of the theoretical maximum throughput on an A100 and speeds up training of long-context models.
Grouped-Query Attention (GQA)Grouped-query attention (GQA) splits the query heads into several groups, and each group shares one key head and one value head. Sitting between multi-head attention and multi-query attention (MQA), it delivers quality close to multi-head attention while keeping generation speed close to MQA.
Linear AttentionLinear attention replaces the softmax in attention with a dot product of feature maps and reorders the matrix products so that the $N \times N$ weight matrix is never formed. Computation and memory become linear in sequence length, and when generating text or images step by step, it produces each next output in constant time, like an RNN.
LinformerLinformer compresses the keys and values of attention to $k$ along the sequence axis with learnable matrices before computing attention. Because the weight matrix shrinks from $N \times N$ to $N \times k$, computation and memory become linear in sequence length, and inference is about 1.5 times faster even at length 512.
Longformer (Sliding Window Attention)Longformer is a Transformer that combines attention in which each token sees only a fixed-size window around it with a few tokens that interact with the whole document. It makes compute and memory linear in sequence length, letting BERT-style pretrained models read 4,096-token documents at once.
Multi-Head AttentionMulti-head attention projects queries, keys, and values into short vectors with separate linear layers for each head, computes h attentions in parallel, and concatenates the results. It lets each head focus on different relationships while keeping compute about the same as a single attention.
Multi-head Latent Attention (MLA)Multi-head Latent Attention (MLA) is an attention mechanism that compresses keys and values into a single low-dimensional latent vector and stores only that latent vector in the KV cache during inference. Proposed with DeepSeek-V2, it outperformed multi-head attention with a cache about the size of GQA with 2.25 groups.
Multi-Query Attention (MQA)Multi-query attention (MQA) is a form of multi-head attention in which only the keys and values are shared as a single set across all heads. It cuts the keys and values read during token-by-token generation by a factor equal to the number of heads, greatly speeding up generation at the cost of a slight drop in quality.
Multiplicative Attention (Luong)Multiplicative attention scores "where to look" with the dot product between the decoder state and the vector at each input position (or a dot product after transforming one of them with a linear layer). Shown by Luong et al. for machine translation in 2015, it is simpler than additive attention, which uses a small network, and can be computed quickly in batches with matrix multiplications.
NyströmformerNyströmformer builds $m$ landmarks from a sequence and approximates the $n \times n$ attention matrix by a product of three small matrices that go through the landmarks. It adapts the Nyström method for matrix approximation to softmax matrices, keeping computation and memory proportional to the sequence length.
PagedAttentionPagedAttention is a way of computing attention that splits the KV cache used in text generation into fixed-size blocks, manages them with a block table like an OS's virtual memory, and computes attention while the blocks sit in non-contiguous memory. It nearly eliminates wasted KV cache memory so that many more requests can be processed at once, raising inference server throughput 2–4×.
Performer (FAVOR+)Performer is a Transformer that approximates softmax attention with dot products of always-positive orthogonal random features (FAVOR+) and reorders the computation so that no $N \times N$ matrix is built. It keeps an unbiased approximation of standard attention while making compute and memory linear in sequence length.
Reformer (LSH attention)Reformer is a Transformer that gathers similar queries and keys into the same bucket with locality-sensitive hashing (LSH) and computes attention only within each bucket. It reduces the cost from quadratic in sequence length to $O(L \log L)$ and, combined with reversible residual layers, makes it possible to train on sequences of length 64K with little memory.
Ring AttentionRing Attention splits a long sequence into blocks held by multiple GPUs or TPUs and passes the key and value blocks around a ring of hosts, overlapping the transfers with blockwise attention computation. Without approximation, it extends the manageable sequence length in proportion to the number of hosts, making it possible to train with contexts of millions of tokens.
Scaled Dot-Product AttentionScaled dot-product attention is the core attention computation of the Transformer: it divides the dot products of queries and keys by the square root of the vector length, turns them into weights with the softmax function, and sums the values using those weights.
Sparse AttentionSparse attention restricts the keys each position attends to, such as "the few preceding positions" and "positions at a fixed interval", and computes only part of the attention matrix. It reduces the cost from quadratic in sequence length to $N\sqrt{N}$, making sequences tens of thousands of tokens long tractable.
