Count token pairs in full self-attention
Type the indices of each value in their original order. · 172 words
Full self-attention lets every input token look at every other input token. For a sequence of n tokens, the transformer forms n query vectors, n key vectors, and n value vectors. A query is a request for information. The matching key says which topic that request addresses. The value carries the content to combine. The attention layer computes all n times n query-key scores, then uses those scores to mix the n values. Thus a 4-token input such as [Museums, are, open, today] creates 4 x 4 = 16 token-pair scores.
The count includes pairs involving the same position, and the score matrix is usually directed: token A can attend to token B even when B cannot attend back to A. Doubling the length has a larger effect than merely adding tokens. For an 8-token review, [This, book, is, clear, useful, and, practical, indeed], there are 8 x 8 = 64 query-key comparisons, compared with 16 for 4 tokens. That is four times as many comparisons for only twice as many tokens.
Check your recall
A 10-token sequence goes through full self-attention. How many query-key token-pair scores are computed?
Check your answer
100 scores, because full self-attention computes one score for every ordered pair of tokens: 10 x 10 = 100.
Sources and credits
Original KeyTouch lesson. Background reading · Vaswani et al. · Attention Is All You Need.