Attention mechanism

An attention mechanism lets a neural network weigh how relevant each part of its input is to the part it is currently working on, and is the core building block of transformer models.

Attention gives a model a way to decide, for each word or token it is processing, which other parts of the input deserve its focus. Each position produces a query, a description of what it is looking for, and every position offers a key, a description of what it contains, along with a value, the information it will pass on. The model compares the query with every key, turns the resulting scores into weights that add up to one using a softmax function, and takes a weighted average of the values. The weights are computed fresh for every input, so the focus shifts with the content.

Take a pronoun in a sentence being translated. To render "it" correctly in a language with grammatical gender, the model needs to know which noun "it" refers to. Attention lets the representation of "it" draw heavily on that noun while largely ignoring the other words, wherever in the sentence the noun happens to be.

The idea came from machine translation. In September 2014 Dzmitry Bahdanau, Kyunghyun Cho and Yoshua Bengio argued that the encoder-decoder networks of the time were held back by squeezing a whole source sentence into a single fixed-length vector. Their fix let the model search the source sentence for the parts most relevant to each word it was producing. In June 2017 the paper Attention Is All You Need went further, introducing the transformer, which dropped the older recurrent and convolutional machinery altogether and relied entirely on attention. It set new translation records and trained much faster, and it became the basis of today's large language models.

Several variants are in common use. In self-attention, queries, keys and values all come from the same sequence, so every token can look at every other. In cross-attention, the queries come from one sequence and the keys and values from another, as when a translation model consults the source sentence. Multi-head attention runs several attention operations side by side, each with its own learned projections, so different heads can track different kinds of relationship.

Attention has a cost that shapes the industry. In its standard form every token is compared with every other, so the work grows with the square of the sequence length: doubling the input roughly quadruples the attention calculation, and the memory it needs grows the same way. That is a large part of why long context windows are expensive, and why researchers keep producing cheaper variants such as linear attention and sparse attention and faster implementations such as FlashAttention.

Common questions

What is attention in AI, in simple terms?

Attention is a way for a model to decide which parts of its input matter most for the step it is taking. It scores how relevant every other token is to the current one, turns those scores into weights and blends the information accordingly. It is the main ingredient of the transformer architecture behind modern language models.

What are queries, keys and values?

They are three learned transformations of each token's representation. The query describes what a token is looking for, the key describes what a token offers, and the value is the information it passes on. Matching queries against keys produces the attention weights, which are then used to combine the values.

What is the difference between attention and self-attention?

Self-attention is one kind of attention, in which a sequence attends to itself: every token's query is compared with the keys of all tokens in the same sequence. Attention in general can also link two different sequences, which is called cross-attention and is used, for example, when a decoder consults an encoder's reading of the input.

Why is attention expensive for long documents?

Standard attention compares every token with every other token, so the amount of work and memory grows with the square of the input length. A prompt ten times longer needs roughly a hundred times more attention computation. This drives the cost of long context windows and motivates research into sparse and linear attention.

Finding more

Search for "attention mechanism explained", "scaled dot-product attention" or "multi-head attention" for walkthroughs, and for "Attention Is All You Need" and "Bahdanau attention" for the original papers. The papers on arXiv are the authoritative sources, and university course notes and well-cited encyclopedia articles explain the maths. Illustrated blog explanations are often good for intuition but sometimes oversimplify how real models implement attention.

Working out what this means for your business?

Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.