Attention Is All You Need
- Publication
- NeurIPS (NIPS) 2017Conference
- First submitted to arXiv
- arXiv:1706.03762
Cite this paper · BibTeX
The citation identifies the linked source version. It cites the original paper and its authors.
@misc{transformerstransformer2017,
title = {{Attention Is All You Need}},
author = {Ashish Vaswani and Noam Shazeer and Niki Parmar and Jakob Uszkoreit and Llion Jones and Aidan N. Gomez and Lukasz Kaiser and Illia Polosukhin},
year = {2017},
eprint = {1706.03762},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/1706.03762}
}
Unlike previous sequential models, the Transformer model introduced a new era of self-attention mechanisms.
The model replaces recurrent sequence computation with attention, allowing positions to be processed in parallel during training. Its original autoregressive decoder still generates tokens sequentially at inference time.
Problem Definition
In previous sequence modeling tasks, RNN and LSTM models were the mainstream.
However, these models have some issues during training and inference:
- Limitations of Recurrent Computation: RNN and LSTM models require step-by-step computation of each element in the sequence during training, which makes it difficult for the models to perform efficient parallel computation.
- Long-Distance Dependency Problem: Due to the recurrent computation of RNN and LSTM models, they struggle to capture dependencies between distant positions in long sequences.
Solving the Problem
Model Design
This is the Transformer model architecture diagram provided in the original paper.
Although this diagram is very concise (???), most people usually do not understand it at first glance.
Believe it or not, this is already simplified!
Let's write a simple code snippet to see how this model actually works:
Input Layer
Here, the input is a sequence of data, represented as a tensor.
- First dimension: Batch size, referred to as
B. - Second dimension: Sequence length, referred to as
T. - Third dimension: Feature dimension, referred to as
D.
Let's start with a simple example:
input_text = ['你', '好', '啊', '。']
input_text_mapping = {
'你': 0,
'好': 1,
'啊': 2,
'。': 3
}
In this example, the input text is "你好啊。", with a total of 4 characters.
Next, convert this input into a tensor:
import torch
import torch.nn as nn
import torch.nn.functional as F
input_tensor = torch.tensor([
input_text_mapping[token]
for token in input_text]
)
print(input_tensor)
# >>> tensor([0, 1, 2, 3])
Next, we embed each element:
embedding = nn.Embedding(num_embeddings=4, embedding_dim=512)
embedded_input = embedding(input_tensor)
print(embedded_input)
# Example values only; actual values depend on initialization.
# >>> tensor([[ 0.1, 0.2, 0.3, ..., 0.4],
# [ 0.5, 0.6, 0.7, ..., 0.8],
# [ 0.9, 1.0, 1.1, ..., 1.2],
# [ 1.3, 1.4, 1.5, ..., 1.6]])
print(embedded_input.shape)
# >>> torch.Size([4, 512])
Finally, don't forget we need a 3D tensor as input, so we need to add a batch size dimension. In this example, the batch size is 1.
embedded_input = embedded_input.unsqueeze(0)
print(embedded_input.shape)
# >>> torch.Size([1, 4, 512])
Positional Encoding
In the original RNN and LSTM models, the model can capture dependencies in the sequence through the position of elements in the sequence. Therefore, we do not need to explicitly design positional encodings, as the model implicitly includes positional information during each iteration of the For-Loop.
Self-attention relates tokens by content. Without positional information, it cannot distinguish their order from content alone. Positional encoding supplies that missing information; the issue is not that the network contains only linear layers or cannot relate tokens.
In the paper, the authors propose a simple method for positional encoding using sine and cosine functions. For a given sequence position and encoding dimension , the positional encoding is calculated as:
Where:
- is the position of the element in the sequence (starting from 0).
- indexes a sine/cosine pair, corresponding to dimensions and .
- is the dimension of the positional encoding (usually the same as the feature dimension of the model input).
Let's implement a positional encoding function based on the above formula:
import math
import torch
def sinusoidal_positional_encoding(length, dim):
""" Sinusoidal positional encoding for non-recurrent neural networks.
REFERENCES: Attention Is All You Need
URL: https://arxiv.org/abs/1706.03762
"""
if dim % 2 != 0:
raise ValueError(
'Cannot use sin/cos positional encoding with '
f'odd dim (got dim={dim})')
# position embedding
pe = torch.zeros(length, dim)
position = torch.arange(0, length).unsqueeze(1)
div_term = torch.exp(
(torch.arange(0, dim, 2, dtype=torch.float) * -(math.log(10000.0) / dim)))
pe[:, 0::2] = torch.sin(position.float() * div_term)
pe[:, 1::2] = torch.cos(position.float() * div_term)
return pe
This function generates a fixed encoding from each position index and feature dimension; the sequence length determines how many position vectors to generate.
Let's visualize the positional encoding, assuming a sequence length of 256 and a feature dimension of 512:
import cv2
import numpy as np
pos_mask = sinusoidal_positional_encoding(256, 512)
pos_mask = pos_mask.numpy()
pos_mask = (pos_mask - pos_mask.min()) / (pos_mask.max() - pos_mask.min())
pos_mask = np.array(pos_mask * 255).astype(np.uint8)
pos_mask = cv2.applyColorMap(pos_mask, cv2.COLORMAP_JET)
After obtaining the positional encoding, we need to add it to the input embedding tensor:
pos_emb = sinusoidal_positional_encoding(4, 512)
embedded_input = embedded_input * math.sqrt(512) + pos_emb
Self-Attention Mechanism
After obtaining the input encoding, we can move on to the core part of the Transformer model: the self-attention mechanism.
Here, we need to prepare three transformation matrices:
-
Query Matrix
W_q.First, declare a set of weights
W_q, then multiply the input embedding tensor by the Query matrix to get the Query tensor.W_q = nn.Linear(512, 512) query = W_q(embedded_input) print(query.shape) # >>> torch.Size([1, 4, 512]) -
Key Matrix
W_k.Similarly, declare a set of weights
W_k, then multiply the input embedding tensor by the Key matrix to get the Key tensor.W_k = nn.Linear(512, 512) key = W_k(embedded_input) print(key.shape) # >>> torch.Size([1, 4, 512]) -
Value Matrix
W_v.Finally, declare a set of weights
W_v, then multiply the input embedding tensor by the Value matrix to get the Value tensor.W_v = nn.Linear(512, 512) value = W_v(embedded_input) print(value.shape) # >>> torch.Size([1, 4, 512])
So, what exactly is this QKV stuff?
You can think of the transformation matrices as projections.
Projections mean "viewing from a different perspective."
The QKV process involves three different projections of the input, followed by the self-attention mechanism calculations.
The second step of the self-attention mechanism is to calculate the attention scores.
In this step, we perform a dot product between the Query tensor and the Key tensor.
attn_maps = torch.matmul(query, key.transpose(-2, -1))
print(attn_maps.shape)
# >>> torch.Size([1, 4, 4])
This gives us an attention score matrix of size 4x4.
In this example, it explores the relationships between [你, 好, 啊, 。] (you, good, ah, .).
The factor 1/sqrt(d_k) counteracts the growth in dot-product scale with dimension and helps avoid softmax saturation. Here the single head uses ; the later eight-head example uses 64 dimensions per head.
attn_maps = attn_maps / math.sqrt(512)
Next is the Softmax operation:
attn_maps = F.softmax(attn_maps, dim=-1)
After calculating the attention map, we can perform a weighted sum of the Value tensor:
attn_output = torch.matmul(attn_maps, value)
print(attn_output.shape)
# >>> torch.Size([1, 4, 512])
Finally, apply residual connections:
residual_output = embedded_input + attn_output
Multi-Head Attention Mechanism
A single head already gives each query a distribution over keys. But what if we want to examine several learned views of the sequence? That is the idea of multi-head attention: each head uses a different feature subspace.
A common implementation packs the heads' projections into larger linear layers, then splits the projected tensors. This is equivalent to separate projections. The details are mostly engineering, so we will not dig further into them here; this runnable sketch just follows eight heads through concatenation and the output projection:
num_heads, head_dim = 8, 64
batch_size, seq_len, _ = query.shape
Q = query.reshape(batch_size, seq_len, num_heads, head_dim).transpose(1, 2)
K = key.reshape(batch_size, seq_len, num_heads, head_dim).transpose(1, 2)
V = value.reshape(batch_size, seq_len, num_heads, head_dim).transpose(1, 2)
weights = torch.softmax(Q @ K.transpose(-2, -1) / math.sqrt(head_dim), dim=-1)
heads = weights @ V
merged = heads.transpose(1, 2).contiguous().reshape(batch_size, seq_len, 512)
output_projection = nn.Linear(512, 512)
multihead_output = output_projection(merged)
print(multihead_output.shape)
# >>> torch.Size([1, 4, 512])
The weights have shape [B, 8, T, T]. Each head operates in 64 dimensions; concatenation returns to 512 dimensions for the residual path.
Cross-Attention Mechanism
In the Transformer architecture, the attention mechanisms in the Encoder and Decoder are similar but have some differences.
In the Encoder, we perform self-attention calculations for each position in the sequence; in the Decoder, besides self-attention calculations for each position, we also need to perform attention calculations on the Encoder's output, which is known as cross-attention.
Each decoder layer has masked self-attention, cross-attention and a feed-forward sublayer. Cross-attention takes Q from the preceding decoder sublayer and K/V from the final encoder output. The following random states illustrate shapes, not a complete decoder.
Again, we need to prepare three transformation matrices:
-
Query Matrix
W_q.First, declare a set of weights
W_q, project the Decoder state after masked self-attention with the Query matrix to get the Query tensor. The length ofdecoder_inputcan be different fromencoder_output. If we encounter a translation problem, this length might be 10.W_q = nn.Linear(512, 512) decoder_input = torch.randn(1, 10, 512) # decoder state after masked self-attention encoder_output = torch.randn(1, 4, 512) # final encoder state decoder_query = W_q(decoder_input) print(decoder_query.shape) # >>> torch.Size([1, 10, 512])
-
Key Matrix
W_k.Similarly, declare a set of weights
W_k, multiply the Encoder's output embedding tensor by the Key matrix to get the Key tensor.W_k = nn.Linear(512, 512) encoder_key = W_k(encoder_output) print(encoder_key.shape) # >>> torch.Size([1, 4, 512])
-
Value Matrix
W_v.Finally, declare a set of weights
W_v, multiply the Encoder's output embedding tensor by the Value matrix to get the Value tensor.W_v = nn.Linear(512, 512) encoder_value = W_v(encoder_output) print(encoder_value.shape) # >>> torch.Size([1, 4, 512])
The subsequent steps are the same as the self-attention mechanism: first, calculate the attention map:
attn_maps = torch.matmul(decoder_query, encoder_key.transpose(-2, -1))
print(attn_maps.shape)
# >>> torch.Size([1, 10, 4])
Then, perform scaling and Softmax:
attn_maps = attn_maps / math.sqrt(512)
attn_maps = F.softmax(attn_maps, dim=-1)
Finally, perform a weighted sum of the Value tensor:
attn_output = torch.matmul(attn_maps, encoder_value)
print(attn_maps.shape)
# >>> torch.Size([1, 10, 4])
print(encoder_value.shape)
# >>> torch.Size([1, 4, 512])
print(attn_output.shape)
# >>> torch.Size([1, 10, 512])
Feed-Forward Network
After the self-attention mechanism, we need to pass through a simple feed-forward network to extract features.
ffn = nn.Sequential(
nn.Linear(512, 2048),
nn.ReLU(),
nn.Linear(2048, 512)
)
ffn_input = multihead_output
ffn_output = ffn(ffn_input)
output = ffn_input + ffn_output
print(output.shape)
# >>> torch.Size([1, 4, 512])
This feed-forward network is a typical fully connected network. Here, we use two fully connected layers with a ReLU activation function in between.
Additionally, there is an expand-dim operation in the module, usually with an expansion factor of 4. This operation is similar to the concept of the Inverted Residual Bottleneck Block proposed in MobileNet-V2. The main purpose is to improve the model's nonlinear representation ability by expanding the dimensions and then compressing them.
Layer Normalization
We haven't mentioned LayerNorm yet.
This operation is straightforward. After understanding all the steps above, it just takes a few lines of code.
The original Transformer uses Post-LN: residual addition followed by LayerNorm for each sublayer. Combining the earlier illustrations gives the following encoder layer. Add each residual once, and feed the first normalized state into the FFN. Dropout is omitted to focus on data flow.
norm1 = nn.LayerNorm(512)
encoder_state = norm1(embedded_input + multihead_output)
# ...
norm2 = nn.LayerNorm(512)
ffn_output = ffn(encoder_state)
output = norm2(encoder_state + ffn_output)
Discussion
Why Use Self-Attention?
The key advantage is parallel computation across positions during training. Full attention still has quadratic cost in sequence length, and autoregressive decoding remains sequential.
The authors summarized the computational complexity of RNN, CNN, and Self-Attention, as shown in the figure above.
-
Self-Attention Layer (Unrestricted):
- Per-layer complexity: : In the self-attention mechanism, each input token (with sequence length ) must compute attention with all other tokens, forming a complete attention matrix. Each matrix element requires computation based on a -dimensional embedding, so the total complexity of the attention matrix is .
- Sequential computation: : The entire attention matrix can be computed in parallel, and all comparisons can happen simultaneously.
- Maximum path length: : Since each token can directly connect to any other token via the attention mechanism, the maximum path length is just one step.
-
RNN:
- Per-layer complexity: : The recurrent layer processes each token sequentially. The computation for each token involves combining the current token embedding (with dimensions) and the hidden state (also -dimensional), resulting in an operational cost of . Since tokens are processed, the overall complexity is .
- Sequential computation: : Due to the sequential nature of RNNs, each token must wait for the computation of the previous token before processing the next.
- Maximum path length: : In an RNN, the path length between two tokens depends on the number of intermediate tokens between them.
-
CNN:
- Per-layer complexity: : In the convolutional layer, a convolutional kernel of width slides over the entire sequence to compute local features. Each of the tokens requires computation on a -dimensional embedding, and the cost of each convolution operation is proportional to . Therefore, the total complexity is .
- Sequential computation: : Each convolution filter can be applied to the entire sequence simultaneously.
- Maximum path length: : Through stacking dilated convolution layers, the network can connect tokens that are farther apart in a logarithmic manner, with respect to .
-
Restricted Self-Attention Layer:
- Per-layer complexity: : In this case, each token can only attend to a neighborhood of size . The attention matrix becomes , but each matrix element still requires computation based on a -dimensional embedding, so the overall complexity is .
- Sequential computation: : Similar to the unrestricted self-attention layer, all comparisons can be performed simultaneously.
- Maximum path length: : Since each token can only attend to a small neighborhood, the path length between two distant tokens increases to .
Experimental Results: Machine Translation
In the WMT 2014 English-German translation task, the Transformer (big) improved the BLEU score by over 2.0 points compared to the previous best models (including ensemble models), setting a new record of 28.4 BLEU. This model trained for 3.5 days using 8 P100 GPUs. Even the base model surpassed all previously published models and ensemble models at a significantly lower training cost.
In the WMT 2014 English-French translation task, the Transformer (big) achieved 41.8 BLEU in Table 2 and the abstract, outperforming all previously published single models at less than a quarter of the previous best model's training cost. Section 6.1 instead says 41.0; the figure here follows the table and abstract.
Conclusion
Transformer changed sequence modeling through training-time parallelism and shorter paths between distant tokens. Full attention has quadratic cost in sequence length, and inference efficiency still depends on decoding and implementation.
In the circles I encountered, its initial release attracted little attention outside technical audiences.
Although Transformer was continuously and passionately discussed within the academic community for several years, it was widely known from natural language processing to computer vision fields. On the other hand, in the industrial sector, it was mostly engineers and researchers who took an interest in the topic.
However, when OpenAI's ChatGPT was released, everything changed.
Yes.
Everything changed.