AI
Machine Learning
Transformers
NLP
Deep Learning

Decoding 'Attention Is All You Need': The Paper That Changed AI Forever

June 1, 2025 18 min read
Decoding 'Attention Is All You Need': The Paper That Changed AI Forever

Decoding ‘Attention Is All You Need’: The Paper That Changed AI Forever

If you have interacted with ChatGPT, Claude, LLaMA, or any modern Large Language Model, you have witnessed the power of the Transformer architecture. But where did it all begin?

In 2017, a team of eight researchers at Google Brain and the University of Toronto (Vaswani et al.) published a paper titled “Attention Is All You Need”. It proposed a radically new network architecture—the Transformer—based solely on attention mechanisms, completely dispensing with the recurrence (RNNs, LSTMs) and convolutions (CNNs) that dominated the field of Natural Language Processing at the time.

This guide is an exhaustive, mathematically rigorous, and intuitive breakdown of that seminal paper. We will unpack every equation, component, and architectural choice that made Transformers the bedrock of modern Artificial Intelligence.


1. The Pre-Transformer Era: The Bottleneck of Recurrence

Before 2017, sequence transduction tasks—such as machine translation, text summarization, and speech recognition—were dominated by Recurrent Neural Networks (RNNs), Long Short-Term Memory networks (LSTMs), and Gated Recurrent Units (GRUs).

The Sequential Processing Bottleneck

An RNN processes sequence tokens sequentially, step-by-step. To compute the hidden state h_t at time step t, the network requires the hidden state h_{t-1} from the previous step alongside the current input x_t:

h_t = f(h_{t-1}, x_t)

This inherent sequential nature presents two insurmountable flaws when scaling to modern massive datasets:

  1. No Parallelization Across Time: Because h_t depends on h_{t-1}, GPU acceleration is severely constrained during training. Training a model on millions of long sentences meant waiting weeks or months because computations could not be parallelized across the time dimension.
  2. Vanishing and Exploding Gradients over Long Dependencies: Although LSTMs introduced memory cells and gating mechanisms to preserve long-range dependencies, the physical distance between tokens in a sequence still degraded information flow. If a crucial subject word appears at position 5 and its corresponding verb appears at position 500, gradient signals must travel back through 495 recurrent steps during backpropagation.

Convolutional Networks as an Alternative

To enable parallelization, researchers tried Convolutional Neural Networks (CNNs) like ByteNet and ConvS2S. While CNNs could compute operations in parallel across tokens, the number of operations required to relate signals from two distant positions grew logarithmically or linearly with distance. Relating two tokens k steps apart required stacking multiple convolutional layers (e.g. O(k) with standard convolutions or O(log_k) with dilated convolutions).

The Vision of Vaswani et al.

The researchers posed a radical hypothesis: Can we eliminate recurrence and convolution entirely, and build a sequence-to-sequence model using only attention mechanisms?

By doing so, the operation distance between any two tokens in a sequence drops to O(1) constant operations, and the entire sequence can be processed concurrently during training.


2. Overall Architecture: Encoder & Decoder Stacks

The Transformer follows a classic Encoder-Decoder paradigm, but its internal structural blocks are built entirely from Multi-Head Attention and Position-Wise Feed-Forward Networks.

Transformer High-Level Architecture Overview

The Encoder Stack (N = 6 layers)

Processes the input sequence (x_1, ..., x_n) into a continuous vector representation z = (z_1, ..., z_n). Each layer contains:

  • Multi-Head Self-Attention
  • Position-Wise Feed-Forward Network
  • Residual Connections & Layer Normalization
The Decoder Stack (N = 6 layers)

Takes encoder outputs z and auto-regressively generates output tokens (y_1, ..., y_m) one token at a time. Each layer contains:

  • Masked Multi-Head Self-Attention (prevents looking ahead)
  • Encoder-Decoder Cross-Attention
  • Position-Wise Feed-Forward Network

The Encoder

The encoder is composed of a stack of N = 6 identical layers. Each layer has two sub-layers:

  1. A Multi-Head Self-Attention mechanism.
  2. A simple, position-wise Fully Connected Feed-Forward Network.

Around each of the two sub-layers, a residual connection is employed, followed by Layer Normalization:

LayerNorm(x + SubLayer(x))

To facilitate these residual connections, all sub-layers in the model, including the embedding layers, produce outputs of fixed dimension d_model = 512.

The Decoder

The decoder is also composed of a stack of N = 6 identical layers. In addition to the two sub-layers in each encoder layer, the decoder inserts a third sub-layer: 3. Encoder-Decoder Cross-Attention, which performs multi-head attention over the output of the encoder stack.

Crucially, the self-attention sub-layer in the decoder is masked to prevent positions from attending to subsequent tokens (future information). This masking, combined with the fact that output embeddings are offset by one position, ensures that predictions for position i can depend only on the known outputs at positions less than i.


3. The Core Mechanics of Self-Attention

Self-attention—sometimes called intra-attention—is an attention mechanism relating different positions of a single sequence in order to compute a representation of the sequence.

Queries, Keys, and Values (Q, K, V)

The paper conceptualizes attention through a database retrieval lens. Given an input matrix of word embeddings X:

  1. Query (Q): Q = X · W^Q - Represents what a token is looking for.
  2. Key (K): K = X · W^K - Represents what a token contains or offers as context.
  3. Value (V): V = X · W^V - Represents the actual content payload to be extracted.

Where W^Q in R^(d_model x d_k), W^K in R^(d_model x d_k), and W^V in R^(d_model x d_v) are learned projection weight matrices.

Scaled Dot-Product Attention Equation

The attention output is computed as a weighted sum of the values, where the weight assigned to each value is computed by a compatibility function of the query with the corresponding key:

Attention(Q, K, V) = softmax( (Q · Kᵀ) / √d_k ) · V
  Input Vectors (Q, K, V)

         ├───> Q · Kᵀ  (Matrix Multiplication -> Similarity Scores)
         │        │
         │        ▼
         │   Scale by 1 / √dₖ  (Prevents Vanishing Gradients in Softmax)
         │        │
         │        ▼
         │   Apply Mask (Optional: Mask future tokens in Decoder)
         │        │
         │        ▼
         │   Softmax  (Converts scores to probability distribution)
         │        │
         └──────> · V  (Multiply by Values matrix)


            Attention Output

Why Scale by 1 / \sqrt{d_k}?

The scaling factor \sqrt{d_k} is critical. For large values of d_k (e.g. d_k = 64), the dot products grow large in magnitude, pushing the softmax function into regions that have extremely small gradients (vanishing gradient problem during backpropagation). Dividing by \sqrt{d_k} keeps the variance of the dot products equal to 1, maintaining healthy gradient flow.


4. Multi-Head Attention: Attending to Multiple Subspaces

Instead of performing a single attention function with d_{model}-dimensional queries, keys, and values, the authors found it beneficial to linearly project the queries, keys, and values h times with different, learned linear projections to d_k, d_k, and d_v dimensions, respectively.

MultiHead(Q, K, V) = Concat(head_1, ..., head_h) W^O

where:

head_i = Attention(Q W_i^Q, K W_i^K, V W_i^V)

In the original paper:

  • h = 8 parallel attention heads
  • d_k = d_v = d_{model} / h = 512 / 8 = 64

Multi-Head Attention Pipeline

V (Value)
K (Key)
Q (Query)
Head 1
Syntactic Relationships
Head 2
Coreference Resolution
… Head 8

Concat(Head₁, …, Head₈) · Wᴼ → Output Dimension (d_model = 512)

Why Multi-Head Attention Works

Multi-head attention allows the model to jointly attend to information from different representation subspaces at different positions. With a single attention head, averaging inhibits this capacity.

For example, in the sentence “The animal didn’t cross the street because it was too tired”:

  • One head might attend heavily to the relationship between “it” and “animal” (coreference).
  • Another head might attend to the relationship between “tired” and “animal” (state attribution).
  • A third head might track positional/grammatical distance.

5. Positional Encoding: Giving Order to Tokens

Since the Transformer contains no recurrence and no convolution, it processes all tokens simultaneously. Without additional information, the model would view a sentence as an unordered “bag of words”—it could not distinguish “The dog bit the man” from “The man bit the dog”.

To give the model awareness of sequence order, the authors added Positional Encodings to the input embeddings at the bottoms of both the encoder and decoder stacks.

Sine and Cosine Positional Functions

The positional encodings have the same dimension d_{model} as the embeddings, so that the two can be summed directly:

PE(pos, 2i)   = sin( pos / 10000^(2i / d_model) )
PE(pos, 2i+1) = cos( pos / 10000^(2i / d_model) )

where pos is the position in the sequence and i is the dimension index.

FeatureSinusoidal Positional Encoding
DimensionSame as embedding (d_{model} = 512)
Range[-1.0, 1.0]
WavelengthGeometric progression from to 10000 · 2π
PropertyAllows model to attend by relative positions because PE_{pos+k} can be expressed as a linear function of PE_{pos}

6. Position-Wise Feed-Forward Networks (FFN)

In addition to attention sub-layers, each of the layers in our encoder and decoder contains a fully connected feed-forward network, which is applied to each position separately and identically.

This consists of two linear transformations with a ReLU activation in between:

FFN(x) = max(0, x · W₁ + b₁) · W₂ + b₂

While the linear transformations are identical across different positions, they use different parameters from layer to layer.

  • Input/Output dimension: d_{model} = 512
  • Inner layer dimension: d_{ff} = 2048 (4x expansion)

7. Computational Complexity Comparison

Why are Transformers superior to RNNs and CNNs for sequence modeling? Let’s analyze computational complexity per layer:

Layer TypeComplexity per LayerSequential OperationsMaximum Path Length
Self-AttentionO(n^2 \cdot d)O(1)O(1)
Recurrent (RNN)O(n \cdot d^2)O(n)O(n)
ConvolutionalO(k \cdot n \cdot d^2)O(1)O(\log_k(n))
Restricted Self-AttentionO(r \cdot n \cdot d)O(1)O(n/r)

Where:

  • n is the sequence length
  • d is the representation dimension (d_{model})
  • k is the kernel size of convolution
  • r is the neighborhood size in restricted self-attention

When sequence length n is smaller than representation dimension d (which was typical in 2017 with n = 50 and d = 512), Self-Attention is faster per layer than recurrent layers and exhibits a constant O(1) maximum path length for information flow!


8. Training Details & Hyperparameters

The paper evaluated the model on two standard machine translation benchmarks: WMT 2014 English-to-German and WMT 2014 English-to-French.

Model Configurations

  • Base Model: N=6, d_{model}=512, d_{ff}=2048, h=8, d_k=d_v=64, Dropout=0.1, Parameters = 65 Million.
  • Big Model: N=6, d_{model}=1024, d_{ff}=4096, h=16, d_k=d_v=64, Dropout=0.3, Parameters = 213 Million.

Optimizer & Learning Rate Warmup

The authors used the Adam optimizer (β₁ = 0.9, β₂ = 0.98, ε = 10⁻⁹) with a custom learning rate schedule that increases linearly for warmup_steps = 4000, and then decreases proportional to the inverse square root of the step number:

lrate = (d_model)^(-0.5) * min( (step_num)^(-0.5), step_num * (warmup_steps)^(-1.5) )

Results

  • WMT 2014 English-to-German: Achieved 28.4 BLEU, outperforming all previous single and ensemble models by over 2.0 BLEU points.
  • WMT 2014 English-to-French: Achieved 41.8 BLEU, setting a new state-of-the-art while training in just 3.5 days on 8 P100 GPUs (a fraction of previous training costs).

Conclusion: The Bedrock of Modern AI

The paper “Attention Is All You Need” did far more than improve machine translation benchmarks. By demonstrating that recurrence was completely unnecessary for sequence processing, Vaswani et al. unlocked massive parallel computing on modern hardware.

Every landmark AI breakthrough since 2017—GPT-4, Claude, LLaMA, BERT, ViT (Vision Transformers), Midjourney, and Whisper—traces its core architecture directly back to this paper. It stands as one of the most impactful scientific papers in human history.


Up next in our tech series: Designing Data-Intensive Applications Part 1: Foundations of Data Systems.

Samuel Olubukun

Samuel Olubukun

Full Stack AI Engineer

I'm a Full Stack AI Engineer focused on applied AI, autonomous agents, and production-grade web applications.

Tags:
AI
Machine Learning
Transformers
NLP
Deep Learning