What is Attention mechanism?

The Transformer mechanism in which each token gathers information by computing its relationship to the tokens in the sequence. In models that generate text, a token does not refer to the tokens after it. Its cost grows with the square of sequence length.

Field: AI Hardware & Software Stack

Japanese name: アテンション

Gathering information with queries, keys and values

Attention is the mechanism by which each token gathers the information it needs from the tokens in the sequence, itself included. First, three kinds of vectors, a query, a key and a value, are made from each token. The dot product of one token's query with every token's key is computed. A larger dot product is taken to mean a stronger relationship. A function called softmax turns these scores into weights that sum to 1. The values added together with those weights become the new representation of that token.

Why the cost grows with the square of the length

For each of n tokens, the query is compared with n keys. That makes n × n pairs. With 1000 tokens there are 1 million pairs, and with 2000 tokens there are 4 million. Doubling the length makes this part of the computation four times larger. A straightforward implementation also places an n × n table of weights in memory. The longer the text, the more the efficiency of attention decides overall performance.

How it is used inside a Transformer

A Transformer computes attention in several heads. This lets each head capture relationships from a different point of view. In a model that generates text, the tokens after each token are hidden from it, so that tokens not yet generated are not used. The attention computation itself does not tell token order apart, so position information is supplied separately. Unlike a recurrent network, which processes a sequence in order, attention can compute every position of the sequence in parallel.

Try it: questions about Attention mechanism

How does the computation of Transformer self-attention grow with input sequence length n?

  1. AIn proportion to n
  2. BIn proportion to n squared
  3. CIt is constant regardless of n
Show answer and explanation

Answer: B. In proportion to n squared

In self-attention, each token's query is dotted with the keys of all tokens, producing an n × n table of weights. Computation, and the memory for that intermediate table if built naively, therefore scale with the square of sequence length. Doubling the length quadruples the attention part, so attention efficiency increasingly dominates performance for long contexts.

FAQ

Is self-attention different from attention?

Self-attention is one kind of attention. When the queries, keys and values are all made from the same sequence, it is called self-attention. There is also a form that refers to the keys and values of another sequence, as in a translation model.

Does a KV cache remove the quadratic cost?

No. A KV cache saves the work of rebuilding the keys and values of past tokens. The query of each new token is still compared with all the past keys, so the number of comparisons over the whole sequence stays proportional to the square of the length.

Books on AI Hardware & Software Stack Ad

As an Amazon Associate I earn from qualifying purchases.

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary