AlgoViz

Attention Is All You Need

medium 40 min

The 2017 paper that introduced the Transformer.

When it comes up

The moments this idea belongs in a design interview, and the moments it does not.

Signals

When to bring this paper up

You hearSayBecause
"How does an LLM work?"Tokens, Q/K/V, scaled dot-product, stacked layers, next-token predictionIt is the paper's decoder, used alone
"Our long-context feature is slow"Prefill is n², decode reads the KV cacheTwo phases, two different limits
"How much GPU memory per user?"Weights once, plus KV cache per requestThe cache grows with every token
"Why not an RNN?"Parallel training and constant path lengthTable 1
"Which model for search?"An encoder: tokens see both sidesBERT line, not GPT line
"Can attention be cheaper?"Restricted attention, FlashAttention, GQAThe paper itself listed restricted attention as future work

An interviewer pushes back

  1. Interviewer

    Your 128k-context feature takes 20 seconds before the first word. Where is the time going?

  2. You

    Before the first word it is prefill: the whole prompt goes through every layer at once, and attention there is n². At 128k that is about 17 billion scores per head per layer.

  3. Interviewer

    And after the first word it is still slow.

  4. You

    That is decode. Each token reads the weights and the whole KV cache, so it is limited by memory bandwidth, not arithmetic. A model with grouped-query attention has a smaller cache, and quantising the cache halves it again.

  5. Interviewer

    Users share the same long system prompt.

  6. You

    Then cache that prefix: its keys and values are the same for everyone, so we compute them once. And I would use a FlashAttention kernel for prefill and chunk it so one long prompt does not block other requests.

How do you explain why Transformers replaced RNNs?

Breaks"Attention was new"
How
Say the Transformer invented attention.
Limit
Wrong: Bahdanau attention existed since 2014 and is reference 2 of the paper.
Choose when
Never.
Works"It is more accurate"
How
Quote 28.4 BLEU.
Limit
True but misses why it won: speed of training and how it scales.
Choose when
As a supporting number.
Strong"It removed the sequential bottleneck"
How
Recurrence makes h_t wait for h_(t-1); self-attention connects all pairs in one step, so training runs in parallel and paths are O(1). Then give the price: O(n²·d) per layer.
Limit
Needs the cost side too, or it sounds like a free lunch.
Choose when
Every time.