The Breakthrough — Transformers & Attention
What "Attention Is All You Need" (2017) actually changed — and why the world hasn't gone back.
RNNs process language one word at a time. For a 500-word paragraph, word 1 must survive 499 steps of compression before influencing the output — often, it does not survive. In 2017, Vaswani et al. proposed a radical alternative: what if every word could directly attend to every other word, all at once?
Attention Mechanism: The Key to Modern LLMs
September 2014: Dzmitry Bahdanau, Kyunghyun Cho, and Yoshua Bengio published a paper that would lastingly change the NLP world. 'Neural Machine Translation by Jointly Learning to Align and Translate' solved a fundamental problem in sequence-to-sequence models. Previous encoder-decoder architectures compressed every input sentence into a single fixed-length vector — an information bottleneck for long sentences. Bahdanau attention was a significant step forward: instead of a fixed vector, the model used dynamic attention over different parts of the input sentence. Like the human eye jumping while reading, AI attention moves between relevant words. This 'additive attention' became the conceptual precursor to modern NLP systems. The later Transformer (2017) built on the attention idea, but replaced the additive variant with the more efficient Scaled Dot-Product Attention. Without Bahdanau's attention concept, no Transformers; without Transformers, no GPT family or BERT. This breakthrough happened three years before 'Attention Is All You Need.'
That idea — Self-Attention alone, with no RNN at all — eliminated the sequential bottleneck and unleashed a wave of scaling that produced GPT, BERT, and virtually every major AI system in use today.
Self-Attention — Every Token Sees Every Other Token
The central innovation of Transformers: parallel processing, direct connections, dynamic weights. Why this solves the RNN problems:
Definition
Self-Attention
Analogy
Example
Definition
Tokens are processed one by one. Information from token 1 must pass through 499 hidden states to reach token 500. The vanishing gradient problem causes long-range dependencies to disappear.
Every token has direct access to every other — the distance is always exactly one step. No information lost by passing it along through many steps. Massive GPU parallelization possible.
Note
The word "bank" in two sentences: "The bank is by the river" vs. "The bank is bankrupt." Without attention, "bank" has the same representation in both. With Self-Attention: in sentence 1, "bank" attends strongly to "river" — its representation shifts toward "bench/seat." In sentence 2, "bank" attends strongly to "bankrupt" — representation shifts toward "financial institution." Same word, different meaning — computed automatically. That is how it works in models like BERT, which see the whole sentence. In GPT-style models, attention is causally masked: each token sees only itself and the tokens before it. There, "bank" cannot yet look at the later "river" or "bankrupt"; the meaning is resolved only at later positions, which see both words.
The attention weights are dynamic: they are computed fresh for each input — unlike static CNN filters or fixed dense layer weights.
Caution
Watch Out: Transformers Don't "Understand" Language
Transformers compute statistical patterns, not meaning. The attention weights show correlations, not real comprehension. The impressive results come from pattern recognition on massive datasets — not from cognition.
Try It Out: Self-Attention Step by Step
See how Self-Attention computes attention weights for the token "Fuchs" in the example sentence "Der schlaue Fuchs":
Three tokens are in the sequence. Self-Attention will now compute how much attention each token pays to each other — all at once, in parallel.
Query, Key, Value — The Mechanics of Attention
How attention is actually computed — step by step:
Definition
Query, Key, Value
Analogy
Example
Definition
Mini-attention for "Der schlaue Fuchs" (3 tokens, d_k = 4).
- 1. Dot ProductsQuery for "Fuchs": Q = [2, 0, 2, 0]. Keys: K_Der = [1, 0, 0, 0], K_schlaue = [2, 0, 2, 0], K_Fuchs = [1, 1, 0, 0]. Dot products: Q_Fuchs · K_Der = 2, Q_Fuchs · K_schlaue = 8, Q_Fuchs · K_Fuchs = 2
- 2. ScalingDivided by sqrt(d_k) = sqrt(4) = 2: Result [1, 4, 1]
- 3. SoftmaxSoftmax([1, 4, 1]) ≈ [0.05, 0.90, 0.05]
- 4. Weighted ValuesAssume the value vectors are: V_Der=[1,0], V_schlaue=[0,1], V_Fuchs=[1,1]. Weighted sum: 0.05·[1,0] + 0.90·[0,1] + 0.05·[1,1] ≈ [0.10, 0.95]
- 5. Result"Fuchs" (fox) attends mainly to "schlaue" (clever) — its new representation now carries the information that this fox is clever.
Without dividing by sqrt(d_k), dot products become very large in high dimensions. Large values push softmax to extreme values (near 0 or 1), causing vanishing gradients — so-called "softmax saturation." Dividing by sqrt(d_k) keeps values in a moderate range so softmax produces usable gradients and training remains stable.
Interactive: The Attention Formula
Click on Q, K, or V to understand what each vector does in the attention computation:
Attention Formula
The Query vector encodes what information this token is looking for. Each token generates its own Query by multiplying its embedding with the learned weight matrix W_Q. The Query is then compared against all Keys to determine relevance.
Concrete Example
Formula: Attention(Q, K, V) = softmax(Q·Kᵀ/√dₖ) · V
Components: Q·Kᵀ (relevance scores) , / √dₖ (numerical stability)
The Transformer Block — Assembling the Machine
The three components of a Transformer Block in detail:
Definition
Transformer Block
Analogy
Example
Definition
Transformer Block: Data Flow
Multiple parallel attention computations — each head learns different patterns (grammar, semantics, references)
Residual connection + Layer Normalization — stabilizes gradient flow in deep networks
Two dense layers with ReLU — processes the information gathered by attention, per token
Second residual connection + normalization — output ready for the next block
Modern models stack dozens to over a hundred such blocks:
Transformer Blocks
96 blocks
GPT-3: 96 blocks, each with 96 attention heads
Parameters
175 billion
GPT-3: 175 billion trainable parameters
The Paper
2017
"Attention Is All You Need" — Vaswani et al.
BERT Significantly Improves Language Understanding
A key advance in bidirectional language models and the birth of modern NLP. In October 2018, Jacob Devlin and his team at Google Research published the paper on BERT — Bidirectional Encoder Representations from Transformers. This innovation substantially changed language processing by training deep bidirectional representations from unlabeled text for the first time. Unlike earlier models, BERT considers both left and right context simultaneously across all layers. The results were noteworthy: BERT achieved new state-of-the-art results on eleven NLP tasks and improved the GLUE score by a notable 7.7 percentage points to 80.5%. The pre-training itself took several days on many TPUs — but the open-source release democratized cutting-edge technology: the ready-trained model could be fine-tuned for a custom task on a single cloud TPU in about an hour at most. ULMFiT and OpenAI's first GPT had demonstrated the pre-training/fine-tuning paradigm shortly before; BERT made it the standard, and it remains the foundation of all large language models today.
Caution
Watch Out: More Attention Heads ≠ Automatically Better
More heads mean more parallel perspectives, but not necessarily better results. The heads share the total dimension — with the same model size, individual heads become smaller. The optimal number of heads depends on the task and model size.
Self-Attention compares every token with every other — that's n × n comparisons. Double the sequence length, quadruple the cost. At 1,000 tokens: 1 million comparisons. At 10,000 tokens: 100 million. This is why context length is a hard problem: GPT-3 was limited to 2,048 tokens. Modern models use various optimizations (Flash Attention, Sparse Attention) to enable longer contexts — but with full attention, the fundamental quadratic complexity remains. Some newer models therefore use full attention in only some of their layers and fill the rest with variants whose cost grows only linearly with length: Qwen3.5, for example, in every fourth layer, NVIDIA's Nemotron 3 in a few layers between Mamba layers. This lets such models reach contexts of up to about one million tokens.
Note
Key Takeaways
- Self-Attention gives every token direct access to every other token — in parallel, without sequential bottleneck and without vanishing gradient over distance. The weights are dynamic (computed fresh for each input), not static like CNN filters.
- The QKV mechanism structures attention as an information retrieval system: Query asks "what do I need?", Key offers "what do I have?", Value delivers the actual content. The dot product Q·K measures relevance, softmax normalizes to probabilities.
- A Transformer Block combines Multi-Head Attention, residual connections with Layer Normalization, and a Feed-Forward Network. GPT-3 stacks 96 such blocks with 96 heads each — 175 billion parameters total.
Check
Checkpoint
Can you explain this in your own words? Tick what you've got.
0 of 3 done
Check
Quiz: Transformers & Attention
What fundamental problem of RNNs does Self-Attention solve by giving every token direct access to every other token?
Select one answer
1. What fundamental problem of RNNs does Self-Attention solve by giving every token direct access to every other token?
2. In the QKV attention computation, the dot product Q·K for token A and token B equals 12, and d_k = 64. What is the scaled score before softmax?
3. A Transformer model has 12 blocks, each with 12 attention heads. How many different attention patterns are computed in total across all blocks?
4. Self-Attention has O(n²) computational complexity where n is the sequence length. If processing a 1,000-token document requires X computation, approximately how much more does a 10,000-token document require?