Transformers & attention, section 1 of 4, 8h

Attention, conceptually

By the end: You can explain how a transformer works in five minutes with no equations.

  • The problem attention solves
  • Queries, keys and values as an idea
  • Why multiple heads
  • Why cost grows with the square of length

New to transformers & attention? Earlier models read a sentence one word at a time, in order, carrying a running summary forward — which made them slow to train and forgetful across long passages. The transformer threw that out and used attention instead: when handling any one word, the model looks at every other word in the input at once and decides which of them matter here. The word "it" can look straight back at the noun it refers to, however far away that noun is. Why it matters

Every current language model is a transformer, and the part of a transformer that does the work is attention. This section explains it in plain language with no equations, because being able to say what attention does in five minutes is a genuinely separate skill from being able to implement it — the first is asked in interviews constantly and the second is asked rarely. The implementation, with the shapes and the arithmetic, is the from-scratch section later in this topic.

The problem#

To understand a word you often need another word far away. In the sentence about the trophy not fitting in the suitcase because it was too big, working out what "it" refers to means looking back at two candidate nouns and deciding which one the word "big" makes sensible. Change "big" to "small" and the answer flips to the other noun. Nothing local to the word "it" contains that information.

Older sequence models processed one step at a time and carried a summary forward, so distant information degraded. They also could not be parallelised across the sequence, because step ten could not begin until step nine finished. The sequence-models topic measures exactly how badly the first problem bites: in a plain recurrent network the gradient reaching fifty steps back is around 5e-19, which is not small but absent.

Every position looks at every position#

Attention lets each position look directly at all the others in the same sequence — self-attention — and decide how much each one matters for it. Distance costs nothing: the fiftieth word and the first are one step apart, exactly as the fiftieth and the forty-ninth are. And because every position can do this at the same time, the whole sequence is processed in parallel, which is what made training at scale possible.

Attention, one step at a time
1 / 6
Thetrophyfitinthesuitcasebecauseitwas

A sentence, split into tokens. We want to work out what “it” refers to.The answer is six positions away, and nothing nearby helps.

The output for each position is a blend: a weighted average of information drawn from every position, where the weights are how relevant each one was judged to be. Those weights are not fixed. They are computed fresh for every position on every input, which is why the same word contributes differently in different sentences, and why a model can resolve "it" one way in one sentence and the other way when a single adjective changes.

Queries, keys and values#

A useful analogy is a lookup. Each position produces a query describing what it is looking for, a key describing what it offers, and a value which is the information it contributes if selected. Every query is compared against every key, the comparisons become the weights, and the weights blend the values.

query   "I am a pronoun, what noun do I refer to?"
key     "I am a singular concrete noun"
value   the information contributed if selected

match query against every key   ->  a weight per position
blend the values by those weights  ->  this position's output
The three roles are three different learned views of the same position, which is the part that sounds arbitrary and is not: a word needs to advertise something different from what it is asking for.

That last point is the one worth holding on to. If a position had only one representation, what it offers to others and what it seeks from them would be forced to be the same thing, and they are not. The pronoun is seeking a noun and offering the fact that it is a pronoun. Separating query from key lets those be different, and separating value from both lets what gets passed on differ again from what made it match.

Multiple heads#

One set of weights captures one kind of relationship. Several heads run in parallel with different learned parameters, so one can track grammatical agreement while another tracks which noun a pronoun refers to and another tracks position in a list. Their outputs are combined, so the layer can express several relationships at once rather than being forced to choose.

It is tempting to think each head learns one clean, nameable job, and the honest position is that some do and most do not. Interpretability research has found heads with crisp roles — copying a token from earlier, tracking the previous token — and also found that heads share work, change role with context, and rarely divide up as neatly as the explanation suggests. The interpretability topic measures this directly: ablating heads one at a time leaves accuracy at 1.000 for six of the eight, and above 0.99 for the other two, while the logit margin moves by between 1.02 and 5.18, so the ones that look unused are doing something.

The cost#

Every position compares against every position, so doubling the length quadruples the comparisons. This is the single most important consequence of the design, and it is easier to believe as a table than as a sentence.

  tokens     comparisons    vs 1k    KV cache, 8B model
   1,000       1,000,000       1x               0.12 GB
   2,000       4,000,000       4x               0.24 GB
   8,000      64,000,000      64x               0.98 GB
  32,000   1,024,000,000    1024x               3.91 GB
 128,000  16,384,000,000   16384x              15.62 GB
1,000,000 1,000,000,000,000 1000000x           122.07 GB
Arithmetic, not a benchmark: comparisons are length squared, and the cache is what an 8B-shaped model must keep per token to avoid recomputing. At a million tokens the cache alone exceeds a single 80 GB card, which is why million-token context is an engineering achievement rather than a setting.

Two things follow that come up constantly in practice. Long context is expensive in a way that is not linear, so a prompt twice as long is not twice the cost; and the memory that grows is the cache of keys and values for everything already read, which is why generation slows as a conversation lengthens even though each new token is one token. Most of the work in modern architectures — the variants in the next section — is about making one or both of those grow more slowly.

How deep to go#

For applied work this level is sufficient: what attention does, why it replaced recurrence, what the three roles are for, and what it costs. If you can say those in five minutes without writing anything down, you can hold your end of any conversation an AI engineering interview will have about transformers.

Go further in two cases. If you are interviewing for research or for a role that trains models, implement causal multi-head attention from scratch — the from-scratch section in this topic does it in NumPy, and the exercise is an afternoon rather than a week. And if you are debugging inference cost or latency, read the modern-variants section instead, because what you need is not the mechanism but what recent architectures changed about it and why.

Where people go wrong

Learning the mathematics because it feels like the serious version. For applied roles the conceptual model and its cost implications are what gets asked; implementation is an ML engineer question.

If this comes up in an interview

Explain how attention works.

Each position produces a query, a key and a value. The query is compared against every key to produce weights, and the output is a weighted mixture of the values. It lets any position draw directly on any other regardless of distance, and unlike recurrence it parallelises across the sequence.

Then they push further

Why divide by the square root of the key dimension?

Dot products grow with dimension, so without scaling the scores get large, softmax becomes extremely peaked and gradients vanish. Dividing by the square root of the dimension keeps the scores in a workable range.

What changes in a decoder-only model?

A causal mask stops each position attending to later ones. The model can then learn to predict the next token at every position at once during training, and generate one token at a time afterwards.

What does the KV cache store, and why?

The key and value vectors of tokens already processed. For each new token only its own query, key and value are computed and the past ones are reused. Generation gets much faster, at the cost of memory that grows with sequence length and batch size.

What gets marked up. Strong answers explain queries, keys and values in plain terms, the scaling, and the mask, and connect them to the cost of long context. Reciting the formula without explaining it is marked down.

Why did transformers replace recurrent networks?

Recurrence processes sequentially, so distant information degrades through a carried summary and nothing parallelises across the sequence. Attention connects any two positions directly and computes all positions at once, which is what made training at scale practical.

Why is long context expensive?

Attention compares every position with every other, so cost grows with the square of sequence length. Doubling the input quadruples the work, which is why context limits exist and why efficient attention is an active research area.

What do multiple attention heads do?

Each head learns a different kind of relationship, such as grammatical agreement in one and topical relevance in another, and their outputs are combined. One set of weights can only represent one notion of relevance.

Build this: Explain attention out loud in five minutes with no equations, to someone who knows programming but not machine learning. If you cannot, you do not understand it yet.

This is one section of Transformers & attention, 26 hours across 4 sections. Read it in full to get the whole topic in order.

Stop 26 of 50 on the AI Engineer route

0 of 50 topics done · 0h behind you, 603h to go

Next on this routeFine-tuningWhen prompting is not enough, and it usually is.18h

What you have marked done is stored in this browser only. It does not reach a server, and clearing site data will clear it.