Attention Mechanisms
The evolution of attention in neural networks: from Bahdanau’s additive attention through self-attention, multi-head attention, and Flash Attention, the idea that made modern AI possible.
Attention Mechanisms
If backpropagation is the engine of deep learning, attention is the steering wheel. It is the mechanism that lets a neural network focus, deciding dynamically which parts of the input matter for each part of the output. The slow generalization of that one idea, from a clever patch for machine translation into the fundamental operation of the Transformer, is one of the strangest and most consequential arcs in the recent history of AI. A trick someone reached for to fix a translation bug turned out to be most of what you need to build a language model.
This article traces that arc. I want to start where attention actually started, with Bahdanau’s alignment model, then follow it through self-attention, multi-head attention, and the hardware-aware reimplementations like Flash Attention that make it practical at the scale we now take for granted. If you have already read the companion pieces on the Transformer architecture and Attention Is All You Need, some of the architectural framing will be familiar. This piece is narrower and more obsessive: it is about the attention operation itself, what it does, why it works, and the persistent suspicion that we still do not fully understand why it works as well as it does.
I will say up front that I find attention genuinely beautiful in a way that not much in deep learning is. Most of the field is empirical mud. You try something, it trains faster, you keep it, and the theory gets backfilled later if at all. Attention is one of the few cases where the operation is simple enough to write on a napkin, the intuition is clean, and the thing it unlocked was enormous. That combination is rare, and it is worth slowing down for.
The Problem That Birthed Attention
In 201412ya, neural machine translation worked like this. An encoder RNN read the input sentence one word at a time and compressed everything it had seen into a single fixed-length hidden state vector. A decoder RNN then generated the output sentence from that one vector.1
The problem with that design is almost embarrassing once you see it. The single vector is a brutal bottleneck. You are asking the model to cram “The cat sat on the mat because it was tired and the mat was comfortable and the sun was warm” into, say, 512 floating-point numbers, and then reconstruct the whole meaning from that summary. Information has to be lost. And indeed Cho et al. (201412ya) measured exactly this: translation quality fell off a cliff as sentences got longer. The model was fine on short inputs and increasingly hopeless on long ones, which is precisely the failure mode you would predict from a fixed-size summary.
Bahdanau, Cho & Bengio (201412ya) proposed a fix that looks obvious in hindsight and was not obvious at all at the time. Instead of forcing the decoder to work from one summary vector, let it look back at all of the encoder’s hidden states at every generation step, and learn which of them to focus on right now.

Sequence-to-sequence RNN encoder-decoder with an attention mechanism. The decoder, at each output step, computes a weighted combination over all encoder hidden states rather than relying on a single summary vector. Figure by Daniel Voigt Godoy, CC BY 4.0, via Wikimedia Commons.
Concretely, at each decoding step \(t\), the model computes an alignment score \(e_{ti}\) between the decoder’s current state \(s_{t-1}\) and each encoder hidden state \(h_i\):
\[e_{ti} = a(s_{t-1}, h_i)\]
where \(a\) is itself a small neural network, the “alignment model.” Those scores get normalized with a softmax into attention weights \(\alpha_{ti}\) that sum to one, and the context vector handed to the decoder is just a weighted average of the encoder states:
\[c_t = \sum_i \alpha_{ti} h_i\]
The decoder consumes this context vector along with its previous state and previous output token, and produces the next word.2
The result was not a marginal gain. Attention-augmented models did not just translate a little better; they handled long sentences that had been completely intractable for vanilla seq2seq. The fixed-size bottleneck was gone, because the decoder could now reach back to any source position directly. And there was a bonus that turned out to matter enormously for the field’s psychology: the attention weights were interpretable. You could plot which source words the model leaned on while generating each target word, and the alignments made linguistic sense. A French adjective lined up with the English noun it modified, even across the word-order swap. For a field used to opaque vectors, seeing a model “point” at the right word felt like a window into its reasoning.
But it is worth being precise about what Bahdanau attention is and is not, because the distinction sets up everything that follows. It is a mechanism for the decoder to attend to the encoder. In modern terms it is cross-attention, one sequence looking at another. The encoder itself still chugged through the input left to right as a plain RNN with no attention of its own, and the decoder still emitted tokens one at a time in sequence. Attention was a powerful accessory bolted onto a fundamentally recurrent architecture. The recurrence, with all its sequential bottlenecks, was still there. The next three years of the story are really about someone asking: what if the accessory is the whole engine?
Self-Attention: Attending to Yourself
The conceptual jump from Bahdanau attention to self-attention is small to state and large to absorb. Instead of one sequence attending to a different sequence, what if a sequence attends to itself? Every token gets to look at every other token in the same input, and build its own representation out of what it finds.
The idea was clearly in the water around 2016. It showed up independently in several places before anyone packaged it cleanly.3 What the Transformer (Vaswani et al., 2017) did was crystallize it into the form everyone now uses: scaled dot-product attention over learned queries, keys, and values.
For each token, you compute three different linear projections of its embedding:
Query \(q_i = x_i W^Q\), which encodes “what am I looking for?”
Key \(k_i = x_i W^K\), “what do I have to offer to others looking?”
Value \(v_i = x_i W^V\), “what information do I actually pass along if attended to?”
The compatibility between token \(i\) and token \(j\) is the dot product of \(i\)’s query with \(j\)’s key:
\[\text{score}(i, j) = \frac{q_i \cdot k_j}{\sqrt{d_k}}\]
Softmax those scores over all \(j\), and the output for position \(i\) is the resulting weighted sum of every token’s value. In the compact matrix form everyone memorizes:
\[\text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right) V\]

Scaled dot-product self-attention: queries and keys produce a compatibility matrix, scaled by the square root of the key dimension, passed through softmax, and used to weight the values. Diagram CC BY 4.0, via Wikimedia Commons.
That \(\sqrt{d_k}\) in the denominator looks like a fussy detail and is actually load-bearing.4
The differences from Bahdanau attention are worth listing slowly, because each one is a load-bearing wall of the modern architecture:
No recurrence. Self-attention looks at all positions at once. It is a parallel operation, which means it maps beautifully onto GPU hardware. This is the single biggest practical reason the Transformer won: you can throw a whole sequence at it simultaneously instead of marching through it one timestep at a time.
Every token reaches every other token in one hop. The “path length” between any two positions is \(O(1)\), not \(O(n)\) as in an RNN where information has to be relayed through every intermediate step. Long-range dependencies, the thing RNNs were notoriously bad at, become structurally easy.
Queries, keys, and values all come from the same sequence. The hard wall between “encoder” and “decoder” dissolves. Self-attention is just a general operation for mixing a set of vectors based on their learned compatibilities, and you can stack it as deep as you like.
This is the primitive the Transformer is built from. Recurrence is gone entirely, replaced by stacked self-attention and feedforward layers. It is genuinely hard to overstate how much of modern AI is downstream of this one substitution.
A Skeptic’s Footnote on the Word “Attention”
I want to flag something, because it bugs me and it bugs a lot of people who think carefully about this. The word “attention” is doing some unearned anthropomorphic work. On Hacker News, the commenter fc417fc802 put it sharply: the attention mechanism in transformers “appears to have about as much to do with human attention as the ‘neurons’ in a multi-layer perceptron have to do with biological neurons.” That is correct and worth internalizing. What the operation does is a soft, differentiable, content-based lookup over a set of vectors. It is a weighted average where the weights are learned from compatibility scores. Calling it “attention” is a useful metaphor that occasionally smuggles in claims the math does not support. When you read breathless takes about models “deciding what to focus on,” remember that under the hood it is matrix multiplies and a softmax, and the focusing is an emergent statistical property, not a little homunculus choosing what to look at.
Multi-Head Attention: Parallel Perspectives
A single attention function gives you one set of weights, one “view” of the relationships in a sequence. That turns out to be limiting. A word relates to other words in many different ways at once: syntactically, semantically, positionally. Forcing all of those into a single softmax distribution is asking too much of one operation.
Multi-head attention is the fix, and it is almost insultingly simple. Run \(h\) separate attention operations in parallel, each with its own learned projections, then concatenate the results and mix them with a final linear layer:
\[\text{MultiHead}(Q, K, V) = \text{Concat}(\text{head}_1, \dots, \text{head}_h) W^O\]
where each \(\text{head}_i = \text{Attention}(QW_i^Q, KW_i^K, VW_i^V)\).
The original Transformer uses \(h = 8\) heads, with each head working in a \(d_k = d_v = d_{model}/h = 64\) dimensional subspace. The total compute is roughly the same as one full-dimensional attention, because you split the dimensions across heads rather than adding to them. You get eight cheap perspectives for the price of one expensive one.
Why does this help so much in practice? Because different heads specialize, and you can actually go look at what they specialize in.5 One head learns that “it” refers back to “cat.” Another links “sat” to “mat.” Another just attends to the previous token, a surprisingly useful default. The concatenation and the \(W^O\) projection blend these into a representation richer than any single head could build alone.
This is not just a pretty story. Ablations consistently show multi-head beating single-head even when you control for parameter count. The ability to hold several “soft addressing” patterns simultaneously is doing real work, and it is one of the few places in deep learning where the interpretability story and the performance story actually agree with each other.
Variants: Multi-Query and Grouped-Query Attention
As models grew to billions of parameters and the question shifted from “can we train it” to “can we afford to serve it,” a new bottleneck appeared: the KV cache. During autoregressive generation, you generate one token at a time, and to avoid recomputing everything you cache the keys and values of all previous tokens. With 32 or 64 heads and long contexts, that cache balloons, and it lives in precious GPU memory. The memory math, not the FLOP math, becomes the thing that decides how many users you can serve per GPU.
Multi-query attention (Shazeer, 2019) proposed sharing a single key head and a single value head across all the query heads. The KV cache shrinks by a factor of \(h\), and the quality cost is surprisingly small. It was an unusually practical paper, the kind of thing that comes from someone who has actually had to pay an inference bill.6
Grouped-query attention (GQA, Ainslie et al., 2023) splits the difference. Group the query heads into \(g\) groups, and let each group share one set of key-value heads. Set \(g = 1\) and you have multi-query; set \(g = h\) and you are back to standard multi-head. Everything in between is a tunable knob trading memory against quality.7 Most large language models you interact with today are running GQA under the hood.
Flash Attention: Making Attention Fast
Self-attention has a complexity problem that everyone learns on day one: it is \(O(n^2)\) in the sequence length. For each of \(n\) tokens you score against all \(n\) tokens, so the attention matrix has \(n^2\) entries. At a 128K context window that is around sixteen billion entries per layer per head, which sounds catastrophic.
The complexity is real, and the community has chewed on it endlessly. As simonw summarized one analysis on HN, self-attention “works by having every token attend to every other token in a sequence, which is quadratic, n^2 against input, which should limit the total context length available to models,” and he tied that directly to why top models stalled around the one-million-token mark for a long stretch. The quadratic wall is not a theoretical curiosity, it shows up in the product.
But here is the subtle thing that took the field years to fully internalize: for the sequence lengths people actually used, the bottleneck was usually not the arithmetic. It was memory bandwidth.8 Standard attention writes the full \(n \times n\) score matrix out to GPU high-bandwidth memory, reads it back to do the softmax, writes the result, then reads it yet again for the multiply with the values. All that round-tripping of a giant matrix is where the time actually goes.
Flash Attention (Dao et al., 2022) attacked exactly this. The idea is to compute attention in tiles that fit in the GPU’s small, fast on-chip SRAM, and never materialize the full attention matrix in slow memory at all. It processes blocks of queries against blocks of keys and values, accumulating the output with an online-softmax trick that lets it normalize correctly without ever seeing the whole row at once.9
The payoff:
2 to 4 times faster than the standard implementation on typical workloads.
Memory drops from \(O(n^2)\) to \(O(n)\), because the full matrix never gets stored.
Exact, not approximate. The output is bit-for-bit the same as standard attention up to floating-point reordering. You give up nothing in quality.
That last point is why Flash Attention swept the field while a decade of approximate-attention papers did not. People do not want to trade accuracy for speed on their flagship models, and here was a method that just made the exact computation faster by being smart about hardware.
Flash Attention 2 (Dao, 2023) reworked the tiling and parallelism to push GPU utilization close to the theoretical roofline, and Flash Attention 3 specialized further for Hopper-class GPUs. The practical upshot is that long-context models, the 128K and million-token windows that now feel ordinary, would simply not be economical without this line of work. Flash Attention is the default in essentially every production training and serving stack, and most practitioners never think about it, which is the highest compliment you can pay a piece of infrastructure.
The Efficient-Attention Landscape, and Why Most of It Lost
Flash Attention solved the implementation problem without touching the underlying \(O(n^2)\) math. A separate and much larger research program tried to attack the complexity itself, replacing exact softmax attention with something cheaper. It is a graveyard of clever ideas, and the autopsy is instructive.
Sparse attention (Child et al., 2019): only attend to a structured subset of positions, such as local windows plus a few strided long-range links. Powered some early long-context models.
Linformer (Wang et al., 2020): project keys and values down to a fixed low rank, making attention linear in sequence length.
Performer (Choromanski et al., 2020): approximate the softmax kernel with random features, again reaching \(O(n)\).
Linear attention (Katharopoulos et al., 2020): drop softmax for a kernel feature map, which lets you rewrite attention as a recurrence and run it like an RNN at inference.
State-space models (Gu et al., 2021): not attention at all, but structured linear recurrences that do \(O(n)\) sequence modeling. Mamba is the headline example and the most credible challenger.
The uncomfortable truth is that as of 2026 none of these has displaced full softmax attention for the models that matter most. The approximate methods tend to bleed quality on exactly the long-range, needle-in-a-haystack tasks where you most wanted the extra context. And Flash Attention moved the goalposts: by making exact attention fast enough, it removed much of the pressure that motivated the approximations in the first place. The linear-complexity approaches, especially the state-space line, may still win at the extreme end of very long sequences, and hybrid architectures that interleave attention with SSM layers are an active and promising area. But “attention is quadratic and therefore doomed” has been a losing bet for several years running, and I would not bet against exact attention again lightly.
Cross-Attention: Bridging Two Sequences
I have spent most of this on self-attention, but the original Transformer also kept cross-attention alive, which is really just Bahdanau attention rebuilt in the query-key-value framework. In cross-attention the queries come from one sequence and the keys and values from another. The decoder asks questions; the encoder’s representations answer them.
Cross-attention is everywhere once you know to look for it:
Encoder-decoder Transformers like T5 and BART, where the decoder attends to the encoder’s output at every layer.
Multimodal models, where text tokens attend to image-patch embeddings or the reverse, as in Flamingo.
Retrieval-augmented generation, where the model attends to embeddings of retrieved documents to ground its output in external facts.
Stable Diffusion, where the image-generation network cross-attends to CLIP text embeddings so that the prompt actually steers the picture.
Cross-attention is the connective tissue. Any time two different sequences, modalities, or knowledge sources need to talk to each other inside one model, the odds are good that cross-attention is the channel they talk through.
Attention As a Computational Primitive
The deepest way I know to think about attention, the framing that survives once you strip away the specific variants, is that it is a differentiable dictionary lookup. You have a query, a set of keys, and a set of values. A hard dictionary returns the single value whose key matches the query. Attention returns a soft, weighted blend of all the values, with the weights set by how well each key matches. Because the whole thing is differentiable, you can learn the keys, the values, and the thing producing the queries, all by gradient descent.
That framing is why attention is so unreasonably general.10 Mechanistic interpretability research keeps finding attention heads that implement recognizable little algorithms:
Copying: find a previous occurrence of the current pattern and copy whatever came after it.
Induction: match a sequence “A B … A” and predict “B”, a primitive form of in-context learning.
Syntax tracking: attend from a word to the words it grammatically depends on.
Positional routing: attend to specific relative offsets, like “the token three back.”
And that is in small, legible models. In large language models with hundreds of layers and thousands of heads, these little algorithms compose. The output of one head becomes the query material for a head three layers up, building a computational depth that the interpretability community is, honestly, still mostly in the dark about. When remexre described it on HN, they stripped the anthropomorphism out nicely: “for each token position, the model considers whether relevant information has been produced at the position of every other token.” That is the whole game, repeated across layers until something that looks like reasoning falls out the other end. Nobody fully understands why stacking that simple operation produces what it produces, and anyone who tells you otherwise is selling something.
The Trajectory
Step back and the shape of the last decade is clean:
2014: additive attention as a patch for the seq2seq bottleneck.
2015: simplified to multiplicative and dot-product alignment.
2017: self-attention replaces recurrence outright in the Transformer.
2018 to 2019: scaled up into BERT and GPT-2, and the field realizes pretraining plus attention is a general recipe.
2020 to 2021: a wave of efficient and linear attention variants, most of which fail to displace softmax.
2022: Flash Attention makes exact quadratic attention practical at long context.
2023 to 2024: GQA, Flash Attention 2 and 3, and the first serious attention-SSM hybrids.
2025 to 2026: attention is still the dominant paradigm, context windows keep stretching, and the open question is how far the same primitive scales.
The thing I keep coming back to is how little the core operation has changed. Dot-product softmax attention in 2026 is, mathematically, the same operation as in 2017. Every major advance since has been in how it is implemented (Flash Attention), how it is parameterized (GQA, rotary position embeddings), or simply how big the surrounding model is. The primitive itself has proven close to optimal for sequence modeling, which is not something you can say about most ideas in this field. Whether it stays optimal as we push toward multi-million-token contexts and deeply multimodal inputs is the real open question, and I genuinely do not know the answer. I have learned not to bet against attention, but I have also learned that the field’s confident predictions age badly. Both of those can be true at once.
Further Reading
The Transformer Architecture, the architecture built around attention.
Attention Is All You Need, a closer look at the paper that started it all.
Mechanistic Interpretability, on what attention heads actually learn.
In-Context Learning, the emergent capability that attention seems to enable.
Lilian Weng’s illustrated guide to attention, a clear visual walkthrough.
The Annotated Transformer, a line-by-line implementation you can read alongside the math.
The Eight Google Employees Who Invented Modern AI, Wired’s oral history of the Transformer paper.