Blueprints · 1,550 words · 7 min read

The Transformer: Attention Is All You Need

A 2017 paper from Google replaced recurrence with self-attention — and created the architecture behind GPT, BERT, and the modern AI revolution.

#TL;DR

Before 2017, neural networks processed sequences — text, audio, time series — one step at a time. Recurrent networks (RNNs, LSTMs) read words left to right, carrying a hidden state that compressed everything seen so far into a fixed-size vector. This worked, but it was slow (steps couldn’t be parallelized) and forgetful (long-range dependencies faded). In June 2017, eight researchers at Google published “Attention Is All You Need” — a paper that replaced recurrence entirely with a mechanism called self-attention, where every element in a sequence can directly attend to every other element in a single step. The resulting architecture, the Transformer, was faster to train (fully parallelizable on GPUs), better at capturing long-range relationships, and scalable to unprecedented sizes. Within two years, Transformers dominated natural language processing. Within five, they powered GPT-4, BERT, Stable Diffusion, AlphaFold, and virtually every breakthrough in modern AI. It is the most consequential neural network architecture since the perceptron.

#The Sequence Problem

Language is sequential. Words come in order. Meaning depends on context — “bank” means something different after “river” than after “investment.” Any model that processes language must handle sequences and capture relationships between distant elements.

Recurrent Neural Networks (RNNs) processed sequences one token at a time, maintaining a hidden state that was updated at each step:

Input:   "The  cat  sat  on  the  mat"
          ↓    ↓    ↓    ↓   ↓    ↓
State:   h₀ → h₁ → h₂ → h₃ → h₄ → h₅ → output

Each hidden state hₜ was a function of the current input and the previous state. In theory, h₅ encoded information about every word in the sentence. In practice, information from early tokens faded as the sequence grew — the vanishing gradient problem. By the time the model reached the end of a long paragraph, it had largely forgotten the beginning.

LSTMs (Long Short-Term Memory, 1997) and GRUs (Gated Recurrent Units, 2014) added gating mechanisms that helped information persist longer. They were the dominant architecture for machine translation, speech recognition, and text generation through the mid-2010s. But they had a fundamental limitation: sequential processing. Each step depended on the previous step’s output. You couldn’t compute h₅ until you had h₄, which required h₃, and so on. The computation was inherently serial, which meant training couldn’t fully exploit the massive parallelism of modern GPUs.

#Attention: Looking at Everything at Once

The attention mechanism was introduced in 2014 by Bahdanau et al. as an addition to RNN-based machine translation. The idea: instead of compressing the entire input sentence into one hidden vector, let the decoder look back at all positions of the input and decide which ones are relevant for generating each output word.

When translating “The cat sat on the mat” to French, generating “le” should focus on “The”, generating “chat” should focus on “cat”, and so on. Attention gave the model a way to explicitly attend to relevant parts of the input at each step.

This worked brilliantly — but it was still layered on top of an RNN. The sequential bottleneck remained.

The Transformer paper’s radical proposal: throw away the RNN entirely. Use attention as the only mechanism for processing sequences.

#Self-Attention

The Transformer’s core operation is scaled dot-product self-attention. For each token in a sequence, self-attention computes how much that token should “attend to” every other token — including itself.

Each input token is projected into three vectors:

  • Query (Q) — “what am I looking for?”
  • Key (K) — “what do I contain?”
  • Value (V) — “what information do I carry?”

Attention scores are computed by comparing each query against all keys, then using those scores to weight the values:

Attention(Q, K, V) = softmax(Q · Kᵀ / √dₖ) · V
Input: "The cat sat on the mat"

For the word "sat":
  Q_sat · K_The  = 0.1   (low attention)
  Q_sat · K_cat  = 0.7   (high — "cat" is the subject)
  Q_sat · K_sat  = 0.3   (moderate — self-reference)
  Q_sat · K_on   = 0.1   (low)
  Q_sat · K_the  = 0.0   (negligible)
  Q_sat · K_mat  = 0.2   (some — "mat" is related)

  → softmax → weighted sum of values
  → "sat" now carries contextual information from all tokens,
     weighted by relevance

The critical property: every token attends to every other token in a single step. There’s no sequential chain. The relationship between the first word and the last word is captured directly, not through a lossy chain of hidden states. Long-range dependencies are first-class citizens.

And because the computation is a matrix multiplication across all positions simultaneously, it’s fully parallelizable on GPUs. Training a Transformer on a sequence of 1,000 tokens doesn’t take 1,000 sequential steps. It takes one matrix operation.

#Multi-Head Attention

A single attention computation captures one type of relationship. But language has many simultaneous relationships — syntactic (subject-verb), semantic (synonyms), positional (adjacent words), coreference (pronouns to nouns).

Multi-head attention runs multiple attention computations in parallel, each with its own learned Q, K, V projections:

┌─────────────────────────────────────────┐
│              Multi-Head Attention         │
│                                          │
│  Head 1: syntactic relationships         │
│  Head 2: semantic similarity             │
│  Head 3: positional patterns             │
│  Head 4: coreference resolution          │
│  ...                                     │
│  Head 8: (learned, not predefined)       │
│                                          │
│  → Concatenate all heads → Linear layer  │
└─────────────────────────────────────────┘

Each head learns to attend to different aspects of the input. The model doesn’t know in advance what each head will specialize in — the training process discovers useful attention patterns. Researchers visualizing trained heads have found ones that track subject-verb agreement, others that link pronouns to antecedents, and others that capture positional proximity.

#The Architecture

The original Transformer has an encoder-decoder structure, designed for machine translation:

Input: "The cat sat"        Output: "Le chat assis"

     Encoder                      Decoder
┌──────────────┐           ┌──────────────────┐
│ Self-Attention│           │ Masked            │
│              │           │ Self-Attention     │
├──────────────┤           ├──────────────────┤
│ Feed-Forward │           │ Cross-Attention    │
│ Network      │           │ (attends to       │
├──────────────┤           │  encoder output)   │
│ Self-Attention│           ├──────────────────┤
│              │           │ Feed-Forward       │
├──────────────┤           │ Network            │
│ Feed-Forward │           ├──────────────────┤
│ Network      │           │ ...               │
└──────┬───────┘           └────────┬──────────┘
       │                            │
       └────── context ─────────────┘

The encoder processes the input sequence. Each layer applies self-attention (every token attends to every other token) followed by a feed-forward network. The output is a sequence of contextual representations — one per input token, each enriched with information from the entire input.

The decoder generates the output sequence one token at a time. It uses masked self-attention (each position can only attend to previous positions — you can’t look at future tokens when generating left to right) and cross-attention (attending to the encoder’s output to pull in information from the input).

Two additional components are essential:

Positional encoding — since self-attention has no inherent notion of order (it’s a set operation, not a sequence operation), the model adds positional information to the input embeddings. The original paper used sinusoidal functions; later models learn position embeddings directly.

Residual connections and layer normalization — each sub-layer (attention, feed-forward) has a residual connection (output = sublayer(x) + x) and layer normalization. These stabilize training and allow gradients to flow through deep networks — the Transformer architecture scales to dozens or hundreds of layers because of these connections.

#The Cambrian Explosion

The Transformer architecture proved astonishingly versatile. Within two years of the 2017 paper, it had been adapted for every major AI task:

BERT (2018) — Google trained a Transformer encoder on masked language modeling: hide random words and predict them from context. The pre-trained model could be fine-tuned for classification, question answering, and named entity recognition. BERT showed that pre-training on large unlabeled text produced representations useful for almost any NLP task.

GPT (2018) and GPT-2 (2019) — OpenAI trained Transformer decoders on next-token prediction: given all previous words, predict the next one. GPT-2 generated text coherent enough that OpenAI initially withheld the full model over misuse concerns. GPT-3 (2020) scaled to 175 billion parameters and demonstrated in-context learning — the ability to perform tasks from just a few examples in the prompt, without fine-tuning. GPT-4 (2023) extended this to multimodal inputs and reasoning capabilities.

Vision Transformers (ViT) (2020) — applied the Transformer to images by splitting them into patches and treating each patch as a token. Transformers matched or exceeded convolutional neural networks on image classification.

AlphaFold 2 (2020) — DeepMind used a modified Transformer to predict protein structures from amino acid sequences, solving a 50-year-old biology problem.

Stable Diffusion (2022) — text-to-image generation using Transformers in the diffusion process, making AI-generated images accessible to anyone.

The pattern is consistent: take the Transformer, pre-train it on massive data, and it learns representations that generalize to downstream tasks. The architecture itself is domain-agnostic — it processes sequences of tokens, and anything can be tokenized: words, image patches, amino acids, code, chess moves, audio frames.

#What the Transformer Got Right

The Transformer is seven years old and shows no signs of being superseded:

  • Parallelism — by eliminating recurrence, the Transformer made sequence processing parallelizable. Training that would have taken months on RNNs took weeks. This didn’t just make existing models faster — it made previously impossible models possible. GPT-3’s 175 billion parameters could not have been trained with RNNs. The Transformer’s parallelism is what made the scaling era of AI feasible.
  • Attention as a universal mechanism — self-attention computes pairwise relationships between all elements of a sequence. This is a general operation — it works for words, pixels, proteins, and anything else that can be represented as a sequence of vectors. The Transformer isn’t a language model architecture; it’s a sequence model architecture that happens to work extraordinarily well on language.
  • Scaling laws — Transformers exhibit remarkably predictable behavior as they scale. More parameters, more data, and more compute produce better performance in a smooth, predictable curve. This predictability is what gives organizations the confidence to invest billions in training larger models — the returns are foreseeable, if not guaranteed. No previous architecture had this property at this scale.
  • The pre-train/fine-tune paradigm — the Transformer enabled a new workflow: train a large, general-purpose model on enormous unlabeled data, then adapt it to specific tasks with small amounts of labeled data. This separated “learning to understand language” (expensive, done once) from “learning to do a specific task” (cheap, done many times). This paradigm shift — from task-specific models to foundation models — is the defining change in modern AI.

Eight researchers at Google wrote a paper about machine translation. The architecture they described became the foundation for systems that write code, generate images, predict protein structures, and hold conversations. The Transformer didn’t solve AI. But it provided the computational substrate on which the current generation of AI is being built — and the next generation, for now, shows no signs of needing a different one.