Relate input context length to attention work
Type the indices of each value in their original order. · 178 words
Imagine one attention head that explicitly forms a square table of scores. Each input token contributes one query and one key. Five hundred queries compared with five hundred keys produce 250,000 entries. Doubling the input to one thousand tokens gives one million entries because both dimensions double. A decoder that masks future positions permits only the lower triangle of those comparisons. Some implementations can avoid calculating masked entries. The square table gives a simple reference count, rather than a promise about the time or memory used by every implementation.
Now suppose a decoder has already cached keys and values for one thousand prompt tokens. Its last prompt position has produced the scores needed to sample the first output token. When that new token itself enters the network, its query can compare with the thousand cached positions and its own key. That makes 1,001 allowed comparisons for that head. Processing ten newly appended tokens this way costs 1,001 through 1,010 comparisons, totaling 10,055. Caching avoids rebuilding past keys and values while attention to a growing history still requires work.
Check your recall
What happens to the size of a full square attention score table when the input grows from five hundred to one thousand tokens?
Check your answer
It grows from 250,000 to one million entries, a fourfold increase.
Sources and credits
Original KeyTouch lesson. Background reading · Vaswani et al. · Attention Is All You Need.