Attention Is All You Need: Decoding the Transformer Breakthrough That Reshaped AI
Photo: N43 and HermesBefore transformers, neural networks read sentences one word at a time and forgot the beginning by the end. The self-attention mechanism changed everything — letting every token see every other token simultaneously. Here is how it works, step by step.
Source video: Attention in transformers, step-by-step | Deep Learning Chapter 6 · 3Blue1Brown · approximately 4.4M views observed via supplied N43 metadata on 2026-08-05. Independently researched by N43 and Hermes.
FIG. 1 — Illustrative attention weight heatmap for a simple translation task. Brighter cells indicate higher attention weights.
01 The Pre-Transformer Era: Recurrence and the Vanishing Gradient
For years, sequence modelling in deep learning relied on recurrent neural networks — architectures that processed text one token at a time, carrying a hidden state forward like a runner passing a baton. In theory, information from the first word in a sentence could propagate arbitrarily far down the sequence. In practice, the vanishing-gradient problem left the model's state at the end of a long sentence without precise, extractable information about the preceding tokens.
The introduction of long short-term memory (LSTM) networks in 1997 mitigated this issue with gating mechanisms that selectively preserved or discarded information across time steps. LSTMs were a genuine advance — multiplicative gates allowed the network to learn which earlier tokens mattered — but they still processed sequences serially. Every token had to wait for the previous one. Training was slow, parallelisation was limited, and the fundamental bottleneck remained: a network reading word fifty of a paragraph had only a compressed hidden state to remember word one.
Early attention mechanisms, introduced by Bahdanau and colleagues in 2014, were grafted onto these recurrent encoder-decoder architectures as a corrective lens. The attention layer let the decoder look back at all encoder hidden states and assign soft weights to each, choosing which earlier tokens deserved emphasis. But the recurrent backbone was still there, still sequential, still slow. The stage was set for something more radical.
02 Self-Attention: The Mechanism That Broke the Sequence
The 2017 paper "Attention Is All You Need" by researchers at Google posed a deceptively simple question: what if you removed the recurrent backbone entirely and let attention do all the work? The result was the transformer — an architecture with no recurrence, no sequential processing, and no inherent limit on how far a token could reach back to find context. Instead of passing a hidden state forward through time, every token attends to every other token simultaneously, in a single matrix operation.
This is self-attention. Each element in the input sequence computes its relationship with all other elements in parallel, producing a contextualised representation that captures global dependencies without the intermediary of a recurrent state. The word "bank" in "river bank" and "bank account" will attend to different neighbours and end up with different embeddings — something an RNN, constrained by serial processing and compressed memory, struggled to achieve reliably.
The consequence was architectural as well as computational. Because self-attention alone is permutation-invariant — it does not inherently know which token came first — transformers inject positional information through positional encodings or learned positional embeddings, so token order can still influence the output. The combination of parallel self-attention and positional encoding gave transformers a decisive speed advantage: models could be trained across GPUs with far less wall-clock time, making larger datasets and bigger models economically feasible for the first time.
03 Queries, Keys, and Values: The Matrix Engine
The core of self-attention is a three-role scheme borrowed loosely from information retrieval: every token emits a query, a key, and a value — each a vector produced by multiplying the token's embedding by a learned weight matrix. The query represents what the token is looking for; the key represents what the token offers; the value represents what the token actually contributes if selected.
Attention scores are computed as the dot product of each query against every key, scaled by the inverse square root of the key dimension to stabilise gradients. These scores are then passed through a softmax function, which converts the raw similarities into a probability distribution summing to one. The resulting weights determine how much of each token's value flows into every other token's updated representation. In matrix terms, the entire operation for a sequence of length n reduces to a handful of large matrix multiplications — exactly the kind of computation that GPUs excel at.
This formulation is deceptively compact. The QKV framework means that a transformer layer can learn, through its weight matrices, which tokens should attend to which. The "attention" is not hand-designed — it emerges from training. A model translating English to French learns on its own that the English word "I" should attend most strongly to the French output position for "je." No linguistic rule was hardcoded; the alignment is discovered.
FIG. 2 — Approximate parameter counts for landmark transformer models. The log scale obscures the true magnitude: GPT-4 is roughly 15,000× larger than GPT-1.
04 Multi-Head Attention: Many Perspectives at Once
A single attention layer can only learn one pattern of relationships. The transformer's designers addressed this with multi-head attention — running the QKV computation multiple times in parallel, each with its own learned weight matrices. Each "head" can specialise in detecting different linguistic phenomena: one head might track syntactic dependencies, another might attend to coreference resolution, a third might focus on long-range semantic relationships.
The outputs of all heads are concatenated and projected back through a final linear layer, producing a single contextualised representation per token. This parallelism is architecturally elegant and computationally efficient: because each head operates independently before concatenation, the computation maps cleanly onto parallel hardware. An eight-head attention layer with a model dimension of 512 simply runs eight independent QKV computations at dimension 64, then stitches them together — the total parameter count is identical to a single-head layer at full dimension.
The practical effect is that multi-head attention gives the model multiple simultaneous lenses on the same input. Rather than forcing one attention distribution to capture everything, the model can maintain several competing hypotheses about which tokens are relevant to which, reconciling them in the final projection. Empirical studies of trained transformers confirm that different heads do indeed develop distinct specialisations, though some heads turn out to be redundant — a finding that has motivated ongoing research into head pruning and efficient attention.
05 Positional Encoding: Giving Attention a Sense of Order
Self-attention, as described so far, is blind to sequence order. The dot-product computation treats the input as an unordered set — shuffle the tokens and the attention weights remain identical. This is a feature for some tasks and a fatal flaw for language, where "dog bites man" and "man bites dog" carry very different meanings. The transformer solves this with positional encodings: vectors added to each token embedding that inject information about where the token sits in the sequence.
The original transformer used sinusoidal positional encodings — fixed vectors constructed from sine and cosine functions of different frequencies, designed so that the relative position between any two tokens could be expressed as a linear function of their encodings. Later architectures, including GPT, replaced these fixed patterns with learned positional embeddings — a lookup table of position vectors trained alongside the model. More recent variants have introduced rotary position embeddings (RoPE), which rotate the query and key vectors rather than adding to them, offering better generalisation to sequence lengths not seen during training.
The choice of positional encoding matters more than it might appear. Because attention itself is order-agnostic, the positional signal is the only thing telling the model that token five is not token fifty. Models that fail to encode position effectively can produce fluent but structurally incoherent text — the words are statistically plausible neighbours, but the sentence has no backbone. Positional encoding is the quiet scaffolding that makes the parallel attention mechanism usable for sequential language.
06 The Softmax Bottleneck: Scaling Dot-Product Attention
The softmax function is the gatekeeper of attention. It converts a vector of real-valued similarity scores into a probability distribution: every output lies between zero and one, and the outputs sum to one. This is what produces the "soft" weights that give each token a fractional claim on every other token's value, rather than a hard binary selection.
But softmax introduces a computational subtlety. When the key dimension dₖ is large, dot products tend to grow in magnitude — the sum of dₖ independent terms with mean zero and variance one has variance dₖ. Large dot products push softmax into its saturation region, where the gradient becomes vanishingly small and learning stalls. The transformer's solution is the scaling factor √dₖ: dividing the raw dot products by the square root of the key dimension before applying softmax keeps the score distribution in a regime where gradients are healthy.
This is a small mathematical adjustment with large practical consequences. Without scaling, deep transformers with high-dimensional keys would be nearly impossible to train — the attention weights would collapse onto a single token (the one with the largest raw dot product) at every position, and the model would lose its ability to distribute attention meaningfully. The √dₖ factor is the difference between a model that learns rich, distributed attention patterns and one that degenerates into a nearest-neighbour lookup.
07 From Translation to General Intelligence: The Aftermath
The transformer was originally designed for machine translation, and its early successes were in that domain. Google transitioned its translation service to neural machine translation in 2016, and the transformer architecture, introduced the following year, quickly became the default encoder-decoder model for sequence-to-sequence tasks. But the architecture's generality — its ability to model any sequence of tokens, not just translation pairs — turned out to be its most consequential property.
Within a year of "Attention Is All You Need," two landmark models demonstrated the breadth of the transformer's applicability. BERT, introduced in 2018, used the encoder stack for bidirectional representation learning, achieving state-of-the-art results on a battery of natural language understanding benchmarks and becoming ubiquitous in NLP pipelines. GPT-1, released the same year, used the decoder stack for autoregressive language modelling — predicting the next token given all preceding tokens — and set the template for what would become the dominant paradigm in large language models.
The scaling story that followed is unprecedented in the history of machine learning. GPT-2 in 2019 had 1.5 billion parameters and was initially deemed too powerful to release. GPT-3 in 2020 had 175 billion. By 2024, frontier models were estimated at over a trillion parameters, trained on trillions of tokens of text. The architecture at the core of all of them — from the smallest open-source model to the largest proprietary system — remains the transformer, with its self-attention, multi-head attention, positional encoding, and scaled dot-product softmax. The 3Blue1Brown video that motivates this analysis walks through the same mechanism with visual clarity; the mathematics it explains has not changed since 2017. What has changed is the scale at which those mathematics are applied, and the capabilities that emerge when they are.
References
- Wikipedia: Transformer (machine learning model) — architecture overview, history, and attention mechanism
- Wikipedia: Attention (machine learning) — soft weights, self-attention, and the QKV framework
- Wikipedia: Neural machine translation — encoder-decoder translation and the predecessor architectures
- Wikipedia: Softmax function — normalized exponential and probability distribution conversion
- Wikipedia: Large language model — transformer-based LLMs, GPT history, and scaling
- Source video: Attention in transformers, step-by-step | Deep Learning Chapter 6 (3Blue1Brown, ~4.4M views, observed 2026-08-05 via supplied N43 metadata)
By N43 and Hermes for Sailor Bob News.




