Skip to the page
Tecyfy

What is self-attention in a transformer, and why does it matter for long prompts?

The step in a transformer where every token looks at the other tokens in the same text and takes in what is relevant to it, so meaning depends on context.

intermediate · Also called Intra-Attention, Scaled Dot-Product Attention

Self-attention is how a TransformerTransformerThe design behind almost every modern language model. It uses attention to let each word in a passage look at every other word, all at once. lets each word take its meaning from the words around it. In "the bank raised its rates" and "the boat reached the bank", the same token needs different meanings, and self-attention is the step that mixes in the context that decides which.

How it works

Every TokenTokenThe small piece of text a language model reads and writes, often part of a word. Prices, limits and the context window are all counted in tokens. is first turned into a list of numbers. For each token the model then makes 3 versions of it:

  • a query: what this token is looking for,
  • a key: what this token offers to others,
  • a value: the information it passes on if chosen.

Each token's query is compared with every token's key. The closer the match, the larger the weight, and the token's new meaning is the weighted sum of all the values. In the 2017 paper that introduced the transformer this is called scaled dot-product attention, and "self" means the queries, keys and values all come from the same text.

A model does this many times in parallel (attention headsMulti‑Head AttentionParallel attention layers allowing a model to focus on different subspaces), each free to track something different, and stacks many such layers.

Why an engineer should care

  • It is why a transformer trains fast. Every position is computed at once instead of word after word, which the paper names as the reason it needs less training time than the recurrent networks before it.
  • It is why long prompts cost more. Every token compares itself with every other, so the work grows with the square of the length. A KV cacheKV CacheWork a model saves from the text it has already read, so it is not redone for every new token. Prompt caching keeps that work between requests and charges less for it. saves the keys and values already computed so a model writing its answer does not redo them for every new token.
  • It only looks back. In a model that writes text, each position may attend only to the positions before it. The model writes one token at a time, and each new token is fed back in as input for the next.
  • Everything in the context windowContext WindowEverything a model can see at once while it writes an answer, counted in tokens. When the window is full, something has to be left out. can influence everything after it. An instruction hidden in a pasted document sits in the same attention as your own, which is part of why prompt injectionPrompt InjectionAn attack that hides instructions in text a model reads, such as a web page, an email or a file, so the model follows the attacker instead of you. works.
Try itSee what a word pays attention to
  1. The2%
  2. trophy43%
  3. did2%
  4. not2%
  5. fit2%
  6. in2%
  7. the2%
  8. case11%
  9. because2%
  10. it9%
  11. was2%
  12. too2%
  13. big17%

“it” attends most to “trophy”. With “big” at the end, the trophy is what is too big, and the weights follow the meaning, not the nearest noun.

The weights are made up to show the idea; a real model has many layers and heads, each with its own. In a model that writes text a word can only look back, so the link is made at the last word, which looks back to both “it” and the trophy or the case.

Sources

Where each fact comes from

  1. 1Attention Is All You Need

Put this demo on your own site

Copy the code into any web page: a lesson, a blog post, a course or your team's wiki. The demo runs in a frame and works just as it does here, so your readers can try it where they read. It is free and needs no account, and nothing a reader does in it is sent anywhere.

The short script at the end lets the frame grow and shrink with the demo. Where a page cannot take scripts, choose a fixed height and the frame scrolls.

<iframe src="https://tecyfy.com/embed/demo/attention" title="See what a word pays attention to" width="100%" height="1150" style="border:0;display:block" loading="lazy" allow="clipboard-write"></iframe>
<p><a href="https://tecyfy.com/concepts/self-attention">See what a word pays attention to</a> by Tecyfy</p>
<script>addEventListener("message",function(e){if(e.origin!=="https://tecyfy.com"||!e.data||e.data.type!=="tecyfy:height")return;var h=Number(e.data.height);if(!(h>0))return;document.querySelectorAll("iframe").forEach(function(f){if(f.contentWindow===e.source)f.style.height=Math.min(h,20000)+"px"})})</script>