Digital Garden
ArchiveWriteAbout
Digital Garden
ArchiveWriteAbout
AI
Deep Learning
NLP

Understanding Transformers: The Architecture That Changed AI

A deep dive into the transformer architecture and how it revolutionized natural language processing.

December 18, 2024

The Transformer architecture, introduced in the seminal paper "Attention Is All You Need" (2017), fundamentally changed how we approach sequence modeling tasks. Before transformers, recurrent neural networks (RNNs) and LSTMs dominated NLP. Today, transformers power everything from GPT to BERT.

The Core Innovation: Self-Attention

At the heart of the transformer is the self-attention mechanism, which allows the model to weigh the importance of different words in a sequence when processing each word.

How Self-Attention Works

The attention mechanism can be expressed mathematically as:

import torch
import torch.nn as nn
 
class SelfAttention(nn.Module):
    def __init__(self, embed_size, heads):
        super(SelfAttention, self).__init__()
        self.embed_size = embed_size
        self.heads = heads
        self.head_dim = embed_size // heads
 
        assert (
            self.head_dim * heads == embed_size
        ), "Embedding size must be divisible by heads"
 
        self.values = nn.Linear(embed_size, embed_size)
        self.keys = nn.Linear(embed_size, embed_size)
        self.queries = nn.Linear(embed_size, embed_size)
        self.fc_out = nn.Linear(embed_size, embed_size)
 
    def forward(self, values, keys, query, mask):
        N = query.shape[0]
        value_len, key_len, query_len = values.shape[1], keys.shape[1], query.shape[1]
 
        # Split embedding into self.heads pieces
        values = self.values(values).reshape(N, value_len, self.heads, self.head_dim)
        keys = self.keys(keys).reshape(N, key_len, self.heads, self.head_dim)
        queries = self.queries(query).reshape(N, query_len, self.heads, self.head_dim)
 
        # Scaled dot-product attention
        energy = torch.einsum("nqhd,nkhd->nhqk", [queries, keys])
 
        if mask is not None:
            energy = energy.masked_fill(mask == 0, float("-1e20"))
 
        attention = torch.softmax(energy / (self.embed_size ** (1 / 2)), dim=3)
        out = torch.einsum("nhql,nlhd->nqhd", [attention, values])
 
        return out.reshape(N, query_len, self.embed_size)

Why Transformers Beat RNNs

Transformers have several key advantages over recurrent architectures:

  1. Parallelization: Unlike RNNs that process sequences sequentially, transformers can process all tokens simultaneously
  2. Long-Range Dependencies: Self-attention directly connects all positions, solving the vanishing gradient problem
  3. Scalability: The architecture scales beautifully with more data and compute

The Architecture Components

A standard transformer consists of:

  • Encoder: Processes the input sequence
  • Decoder: Generates the output sequence
  • Multi-Head Attention: Multiple attention mechanisms running in parallel
  • Feed-Forward Networks: Applied to each position independently
  • Positional Encoding: Injects sequence order information
// Simplified transformer block structure
interface TransformerConfig {
  vocabSize: number;
  maxSeqLength: number;
  embedSize: number;
  numHeads: number;
  numLayers: number;
  ffnHiddenSize: number;
  dropoutRate: number;
}
 
class TransformerBlock {
  multiHeadAttention: MultiHeadAttention;
  feedForward: FeedForwardNetwork;
  layerNorm1: LayerNormalization;
  layerNorm2: LayerNormalization;
  dropout: Dropout;
 
  forward(x: Tensor, mask?: Tensor): Tensor {
    // Multi-head self-attention with residual connection
    const attended = this.multiHeadAttention(x, x, x, mask);
    const norm1 = this.layerNorm1(x + this.dropout(attended));
 
    // Feed-forward with residual connection
    const ffOutput = this.feedForward(norm1);
    return this.layerNorm2(norm1 + this.dropout(ffOutput));
  }
}

Modern Transformer Variants

Since 2017, numerous transformer variants have emerged:

ModelYearKey Innovation
BERT2018Bidirectional pre-training
GPT-22019Scaled decoder-only architecture
T52019Text-to-text framework
GPT-32020175B parameters, few-shot learning
Vision Transformer2020Applied transformers to images
GPT-42023Multimodal capabilities

Practical Considerations

When implementing transformers, keep these factors in mind:

# Key hyperparameters for transformer training
config = {
    "vocab_size": 50000,
    "max_position_embeddings": 512,
    "hidden_size": 768,
    "num_attention_heads": 12,
    "num_hidden_layers": 12,
    "intermediate_size": 3072,
    "hidden_dropout_prob": 0.1,
    "attention_probs_dropout_prob": 0.1,
}
 
# Training typically requires:
# - Large datasets (100M+ tokens)
# - Significant compute (multiple GPUs)
# - Careful hyperparameter tuning
# - Warmup learning rate schedule

Computational Complexity

The self-attention mechanism has O(n²) complexity with respect to sequence length, which becomes prohibitive for very long sequences. This has led to efficient variants like:

  • Sparse Attention: Only attend to subset of positions
  • Linear Attention: Approximate attention with linear complexity
  • Flash Attention: Optimize memory access patterns

Conclusion

Transformers represent a paradigm shift in how we build AI systems. Their ability to capture long-range dependencies, parallelize training, and scale effectively has made them the foundation of modern NLP and increasingly, computer vision.

Understanding transformers deeply is essential for anyone working in AI today. The architecture's elegance lies in its simplicity: attention, normalization, and feed-forward networks, stacked and repeated.

As we continue to scale these models, we're discovering emergent capabilities that weren't explicitly programmed — a fascinating direction for future research.


Further reading: "Attention Is All You Need" by Vaswani et al., 2017

← 목록으로 돌아가기