Skip to content
atlas

Attention mechanism

Also known as: attention, self-attention

The step inside a model that decides, for each token, which other tokens in the input matter most right now.

Draft - this entry has not been reviewed yet.

Formal

A method in a neural network where each token's embedding is compared with every other token's, the match scores are turned into shares that add up to one, and the token takes on a mix of the others weighted by those shares.

In plain English

Like following one friend at a noisy party - out of all the voices in the room you pick up the few that matter to what you are hearing and let the rest fade.

In practice

A case officer in a municipality asks a chat assistant whether a 120-page local plan allows a garage; attention lets the question draw directly on the one paragraph, far back in the text, that settles it.

Why it matters

Every token is compared with every other, so the work grows much faster than the length of the input - the main reason a longer context window costs more time and money.

Technical deep dive

Attention was introduced by Bahdanau, Cho and Bengio (2014) as an alignment layer for recurrent encoder-decoder translation: instead of squeezing a source sentence into one fixed vector, the decoder computed a weighted average of all encoder states at each output step, with weights from a small feed-forward scoring network (additive attention). Luong et al. (2015) simplified the score to a dot product. Vaswani et al. (2017) then removed recurrence entirely and made attention the main operation of the transformer.

The transformer form is scaled dot-product attention: Attention(Q, K, V) = softmax(QKᵀ / √d_k) V. Each token's hidden state is projected by learned matrices into a query, a key and a value vector; the query of token i is compared with the key of every token j, the scores are divided by √d_k so that the softmax does not saturate as the dimension grows, and the resulting weights mix the value vectors. Multi-head attention runs h such operations in parallel on lower-dimensional projections and concatenates them; the original base model used d_model = 512 with 8 heads of d_k = 64. Self-attention takes Q, K and V from the same sequence; cross-attention takes queries from one sequence (the decoder) and keys and values from another (the encoder output). A causal mask sets scores for future positions to −∞ so a decoder cannot look ahead. Attention itself is permutation-invariant, so position must be injected separately, originally with sinusoidal encodings and today usually with rotary position embeddings (RoPE) or relative biases such as ALiBi.

The cost is the defining constraint. The score matrix is n × n, so compute and naive memory grow as O(n²·d) in sequence length n. FlashAttention (Dao et al., 2022) keeps the exact result but tiles the computation so the full matrix is never written to GPU main memory, which removes the quadratic memory traffic but not the quadratic arithmetic. During generation the keys and values of earlier tokens are cached (the KV cache), and multi-query attention (Shazeer, 2019) and grouped-query attention (Ainslie et al., 2023) share key/value heads across query heads to shrink that cache. Sparse, sliding-window and linear-attention variants trade exactness or expressiveness for sub-quadratic cost.

Two misconceptions are common. Attention weights are not a reliable explanation of why a model produced an output: heads interact across many layers and residual paths, and several studies have shown that quite different weight patterns can give the same prediction. And a model that accepts a long context does not use all of it equally well; retrieval accuracy often drops for material in the middle of long inputs, so a large context window is not the same as reliable long-range attention.

What to learn first

Everything this builds on, foundations first.

  1. Neural network
  2. →Token
  3. →Embedding
  4. →Attention mechanism

Relationships

Requires
Embedding

Sources & further reading

Reference works

  • Bahdanau, Cho & Bengio (2014), Neural Machine Translation by Jointly Learning to Align and Translate
  • Vaswani et al. (2017), Attention Is All You Need

Textbooks

  • Goodfellow, Bengio & Courville, Deep Learning (ch. 12.4) · MIT Press

Where this data comes from

This entry was drafted by an AI from the sources above and has not yet been checked by a person. Treat it as a starting point, and check anything important against the sources.

See the review queueSuggest a correction on GitHubThis term as JSON

Check yourself

Loading…

Atlas is in beta.