The Transformer Architecture: From Attention to Modern AI

The Transformer Architecture: From Attention to Modern AI

14 min read
IQLAS

In June 2017, eight researchers at Google published a paper titled “Attention Is All You Need.” It proposed a novel neural network architecture — the Transformer — intended initially to improve machine translation. Seven years later, almost every significant advance in natural language processing, much of computer vision, protein structure prediction, and the generation of code, audio, and images traces its lineage directly to that architecture.

Understanding the Transformer is not optional for anyone who wants to reason clearly about what modern AI systems can and cannot do. This is an attempt to provide that understanding — precise but not condescending, technical but not opaque.

The Problem Being Solved

To understand why the Transformer’s design choices matter, you need to understand what came before.

Before Transformers, the dominant architecture for sequential data (text, audio, time series) was the Recurrent Neural Network (RNN) and its more capable variants, the Long Short-Term Memory (LSTM) and Gated Recurrent Unit (GRU). These architectures processed sequences one token at a time, left to right, maintaining a hidden state that carried forward information from previous tokens.

This sequential processing created two fundamental problems:

The vanishing gradient problem. When training on long sequences, gradients propagating backward through many timesteps would shrink exponentially, making it difficult for the network to learn dependencies separated by many tokens. LSTMs mitigated this but did not eliminate it.

Parallelization impossibility. Because each step depended on the previous step’s hidden state, you could not process a sequence in parallel. Training on long documents was slow. This limited the scale at which these models could be trained.

The Transformer solves both problems by abandoning sequential processing entirely. Instead of reading text left to right, a Transformer looks at all tokens simultaneously and lets each token attend to every other token directly, regardless of distance.

Self-Attention: The Core Mechanism

Self-attention is the conceptual heart of the Transformer. Here is the intuition before the math.

Consider the sentence: “The pilot landed the aircraft because she was excellent.” To understand what “she” refers to, a model needs to connect “she” to “pilot,” not “aircraft.” In an RNN, this connection must propagate through several recurrence steps. In a Transformer, “she” can directly attend to “pilot” in a single operation.

Self-attention computes, for every token in a sequence, a weighted sum of the representations of all other tokens — where the weights represent how relevant each other token is.

Queries, Keys, and Values

The mechanism works through three learned linear transformations of the input representation: Query (Q), Key (K), and Value (V).

Think of it like a soft database lookup. The Query is what you’re searching for. The Keys are what database entries expose as their searchable index. The Values are the actual content returned when a match is found.

For a given token, the attention score against every other token is computed as the dot product of the query against each key, scaled by the square root of the key dimension (to prevent the dot products from growing too large):

$$\text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V$$

The softmax converts the raw scores into a probability distribution — so for any given query, the attention weights over all keys sum to 1. The final representation for a token is the weighted sum of the Values, where higher-attended tokens contribute more.

This operation is fully parallelizable over the sequence length. All attention scores for all token pairs can be computed simultaneously as matrix multiplications.

Multi-Head Attention

A single attention function computes one way of relating tokens. But tokens relate to each other in multiple ways simultaneously — grammatical relations, semantic relations, coreference, and so on.

Multi-head attention runs several attention functions in parallel (the “heads”), each with separate Query, Key, and Value weight matrices. The outputs of all heads are concatenated and linearly projected, combining the multiple relationship perspectives into a single representation.

The original Transformer used 8 attention heads. GPT-3 used 96. Each head can specialize — some heads tend to learn syntactic patterns, others semantic ones, others positional relationships. This specialization emerges from training rather than being explicitly designed.

Positional Encoding

The attention mechanism, as described, is entirely position-agnostic. It sees a set of tokens, not a sequence. But word order matters — “the pilot landed the aircraft” and “the aircraft landed the pilot” mean different things.

Positional encodings inject position information into the token representations before they enter the attention layers. The original Transformer used sinusoidal functions:

$$PE_{(pos, 2i)} = \sin\left(\frac{pos}{10000^{2i/d_{model}}}\right)$$ $$PE_{(pos, 2i+1)} = \cos\left(\frac{pos}{10000^{2i/d_{model}}}\right)$$

These encodings have the useful property that relative positions can be expressed as linear combinations of absolute positions — allowing the model to generalize to positions not seen during training.

Modern architectures use learned positional embeddings or more sophisticated schemes like RoPE (Rotary Position Embedding) used in LLaMA and GPT-NeoX, which encode relative position directly into the attention computation.

The Full Architecture

The original Transformer was an encoder-decoder architecture for machine translation. Understanding both components clarifies how different modern architectures specialize them.

The Encoder

The encoder processes the source sequence (the input text) and produces contextualized representations. Each encoder layer contains:

  1. Multi-head self-attention — each token attends to every other token in the input
  2. Add & Norm — residual connection adding the input to the attention output, followed by layer normalization
  3. Feed-forward network — two linear transformations with a non-linearity (originally ReLU, now usually GELU), applied independently to each position
  4. Add & Norm — another residual connection and normalization

The residual connections (adding input to output) are critical for training deep networks — they provide gradient highways that maintain signal integrity through many layers.

Multiple encoder layers stack, with each layer’s output feeding the next, allowing progressively more abstract representations to emerge.

The Decoder

The decoder generates the target sequence (the translation) one token at a time. Each decoder layer contains:

  1. Masked self-attention — attention over tokens generated so far, with masking to prevent attending to future tokens (which don’t exist yet at generation time)
  2. Cross-attention — the decoder’s queries attend to the encoder’s keys and values, connecting output generation to the input context
  3. Feed-forward network

The masking in the decoder is what makes autoregressive generation work — each output token can only see previous output tokens, not future ones.

Modern Specializations

Contemporary large language models typically abandon the full encoder-decoder structure for specialized variants:

Encoder-only (BERT, RoBERTa): Only the encoder stack, used for tasks requiring understanding of the full input (classification, named entity recognition, question answering). Bidirectional — every token attends to every other token. Cannot generate text autoregressively.

Decoder-only (GPT series, LLaMA, Claude): Only the decoder stack with masked self-attention, no cross-attention (no encoder to attend to). Trained autoregressively — predict the next token given all previous tokens. Extremely capable at text generation. The dominant architecture for large language models.

Encoder-decoder (T5, BART, original Transformer): Full architecture, used when there is a clear distinction between input and output (translation, summarization, instruction following with the encoder processing the instruction and decoder generating the response).

Scaling and Emergence

One of the most surprising findings of the past five years is how consistently Transformer capabilities improve with scale — more parameters, more training data, more compute — and how qualitatively new capabilities appear at scale thresholds in ways that were not present at smaller scales.

These “emergent capabilities” — multi-step reasoning, in-context learning (adapting behavior from examples in the context window without weight updates), few-shot generalization — appear at model scales where they were essentially absent. This was not predicted by training loss curves. The models became qualitatively different, not just quantitatively better.

The understanding of why this happens is incomplete. Some researchers argue these are genuinely emergent properties arising from the richness of representations that become possible at scale. Others argue they are artifacts of the metrics used to measure capability — capabilities that were always there, just below measurement thresholds.

The Attention Bottleneck

No survey of the Transformer is complete without acknowledging its principal scaling challenge: attention complexity.

Standard self-attention is $O(n^2)$ in sequence length — the number of attention pairs grows quadratically with context length. A sequence of 1,000 tokens requires computing $1,000^2 = 1,000,000$ attention pairs. A sequence of 1,000,000 tokens (a book) requires computing $10^{12}$ attention pairs — computationally infeasible with standard attention.

This is why context windows matter and why extending them is an active research area. Current approaches include:

Sparse attention (only attending to a subset of positions), local attention (attending only within a sliding window), linear attention (approximating softmax attention with linear complexity), and architectural innovations like Mamba (state space models that achieve linear scaling) and RetNet (retention mechanism with recurrence for inference, parallel for training).

The standard Transformer’s $O(n^2)$ is a fundamental architectural constraint. The race to replace it — while preserving the quality of what attention learns — is one of the defining research directions of the coming decade.

Why It Works

The final question: what makes the Transformer architecture so capable?

Representational expressiveness. Multi-head attention allows every token to build rich, contextually-informed representations by attending to any part of the input. The representations are not fixed or extracted — they are dynamically computed, dependent on context.

Trainability at scale. Residual connections, layer normalization, and the fully-parallelizable attention operation make Transformers stable to train at very large scales. The architecture tolerates thousands of layers in principle (though practical models cap at ~100).

Inductive bias alignment. The Transformer’s inductive biases — permutation equivariance (the attention mechanism doesn’t inherently care about position, then positional encoding is added), the ability to model arbitrary pairwise relationships — are well-aligned with the structure of language and many other relational data types.

Data efficiency at scale. Transformers are data-hungry but reward more data more than alternatives. At the scales where pre-training can use trillions of tokens, Transformers get better more consistently than other architectures do.

The 2017 paper that introduced this architecture could not have anticipated that it would become the substrate of a technology that billions of people would interact with daily. That it did is a testament to how well its fundamental design decisions hold under pressure — and a reminder that papers with unassuming titles sometimes contain ideas that change what’s possible.