Self-attention

Self-attention is the operation at the heart of transformers, in which every element of a sequence looks at every other element and builds a new representation of itself from a weighted mix of them.

Google's machine learning glossary describes self-attention as a layer that turns a sequence of embeddings into another sequence of embeddings. Each token goes in as a vector and comes out as a new vector that has taken the rest of the sequence into account. It is called self-attention because the sequence attends to itself, unlike earlier attention that linked, say, the words of a translation to the words of its source sentence.

The mechanics use three learned projections. Each token's vector is turned into a query, a key and a value. A token's query is compared with every key using a dot product, the scores are scaled and passed through a softmax so they add up to one, and the output is the weighted sum of all the value vectors. In the sentence the animal did not cross the street because it was too tired, a well-trained model building its representation of it should give a large weight to animal, so the new vector for it carries that meaning.

Transformers run several of these in parallel, an arrangement called multi-head attention, where each head has its own projections and can pick up a different kind of relationship; the heads' outputs are joined and projected back together. Two further details matter. On its own, self-attention ignores word order, so transformers add positional information to the tokens. And in models that generate text, a causal mask stops each token from attending to tokens that come after it, which is what lets them write one token at a time.

Attention itself came first in recurrent networks: Dzmitry Bahdanau and colleagues used it for machine translation in 2014, and a form of self-attention called intra-attention appeared in 2016. In June 2017 Ashish Vaswani and colleagues published Attention Is All You Need, which built the transformer on attention alone and dropped recurrence and convolutions. It scored 28.4 BLEU on the WMT 2014 English-to-German translation task, beating previous results, and trained far faster because the work could run in parallel.

The cost is the catch. Because every token is compared with every other, the work grows with the square of the sequence length: double the input and the attention work roughly quadruples. That is why long context windows are expensive to serve, and why so much research goes into cheaper alternatives such as sparse attention, memory-efficient implementations and state space models.

Common questions

What is the difference between attention and self-attention?

Attention is the general idea of letting a model weight some inputs more than others when computing an output. Self-attention is the case where the queries, keys and values all come from the same sequence, so each element weighs every other element of that sequence. Cross-attention, by contrast, lets one sequence attend to another, such as a translation attending to its source text.

What are queries, keys and values?

They are three vectors computed from each token by learned weight matrices. A token's query is matched against every token's key to produce attention scores, and those scores decide how much of each token's value is mixed into the output. The names borrow loosely from looking up entries in a database.

Why is self-attention quadratic?

Every token is compared with every other token, so a sequence of n tokens needs about n times n comparisons. Doubling the length of the input roughly quadruples the attention work and memory, which is the main reason long contexts are costly.

What is multi-head attention?

Multi-head attention runs several self-attention operations side by side, each with its own learned projections. Different heads can track different relationships, such as grammar or which noun a pronoun refers to, and their outputs are combined into one result.

Finding more

Search for "scaled dot-product attention", "multi-head attention", "causal masking" or "attention quadratic complexity" to go deeper. The original transformer paper on arXiv is short and readable, and university lecture notes and well-sourced encyclopedia articles explain the maths step by step. Treat claims that a model has solved the cost of long-context attention with care until they are backed by a paper or independent tests.

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.