GenAIHub
← Back to Technical Section

Transformers

The Neural Architecture Powering Modern AI

What is a Transformer?

The Transformer is a neural network architecture introduced in the landmark paper "Attention Is All You Need" (Vaswani et al., 2017). It revolutionized deep learning by replacing recurrent (RNN/LSTM) and convolutional architectures with a purely attention-based mechanism, enabling efficient parallelization and superior modeling of long-range dependencies.

πŸ’‘ Key Innovation: Unlike RNNs that process sequences step-by-step, Transformers process all tokens in parallel, dramatically reducing training time and enabling models to scale to billions of parameters.

Transformers are the foundation of virtually all modern AI systems including:

GPT

OpenAI

BERT

Google

LLaMA

Meta

Claude

Anthropic

High-Level Architecture

The original Transformer uses an encoder-decoder structure designed for sequence-to-sequence tasks like translation:

Input Embeddings Encoder (NΓ—) Self-Attention Feed-Forward + Add & Norm Decoder (NΓ—) Masked Self-Attn Cross-Attention Feed-Forward Output Probabilities
Detailed Transformer Architecture (Vaswani et al., 2017)

The original Transformer architecture from "Attention Is All You Need" (Vaswani et al., 2017)

Encoder-Only

Understanding & Classification

Examples: BERT, RoBERTa, ALBERT

Decoder-Only

Text Generation

Examples: GPT, LLaMA, Claude

Encoder-Decoder

Seq-to-Seq Tasks

Examples: T5, BART, Flan-T5

Self-Attention Mechanism

Self-attention is the core innovation of Transformers. It allows each token in a sequence to "attend to" (or consider) every other token, capturing contextual relationships regardless of distance.

How Self-Attention Works

For each token embedding, three vectors are computed via linear projections:

Query (Q)

"What am I looking for?"

Key (K)

"What do I offer?"

Value (V)

"What's my content?"

The attention formula:

Attention(Q, K, V) = softmax(QKα΅€ / √dβ‚–) Β· V

Scaling by √dβ‚– prevents extremely large dot products, stabilizing gradients during training.

Multi-Head Attention

Instead of a single attention operation, Transformers use multiple attention heads in parallel. Each head learns different types of relationships:

Head 1

Syntax

Head 2

Semantics

Head 3

Position

Head N

Coreference

headα΅’ = Attention(QWα΅’α΄½, KWα΅’α΄·, VWα΅’β±½)
MultiHead(Q,K,V) = Concat(head₁,...,headβ‚•)Wα΄Ό

Positional Encoding

Since Transformers process all tokens simultaneously (no inherent order), positional information must be explicitly injected. Common approaches:

Method Description Used In
Sinusoidal Fixed sin/cos functions at different frequencies Original Transformer
Learned Absolute Trainable position embeddings BERT, GPT-2
Relative Position Encodes distance between tokens Transformer-XL, T5
RoPE Rotary Position Embeddings LLaMA, Mistral, GPT-NeoX

BERT vs GPT: Key Differences

Aspect BERT GPT
Architecture Encoder-only Decoder-only
Attention Bidirectional (sees all tokens) Causal/Masked (left-to-right only)
Pre-training Masked Language Modeling (MLM) Next Token Prediction (autoregressive)
Best For Understanding, Classification, NER Text Generation, Conversation
Developer Google (2018) OpenAI (2018+)

Computational Complexity

⚠️ Challenge: Standard self-attention has O(n²) complexity in both time and memory, limiting context length to thousands of tokens.

This has driven research into efficient variants:

  • FlashAttention: IO-aware algorithm that reduces memory reads/writes
  • Sparse Attention: Attend only to specific positions (Longformer, BigBird)
  • Linear Attention: Approximate attention with O(n) complexity (Performer)
  • State Space Models: Alternative to attention (Mamba, S4)
  • Ring Attention: Distributed attention across multiple devices

Modern Improvements (2024)

πŸ”„ Pre-Layer Normalization

Apply LayerNorm before attention/FFN instead of after, improving training stability.

🎯 Grouped-Query Attention

Share Key/Value heads across Query heads to reduce KV-cache size.

πŸ“ RoPE Embeddings

Rotary embeddings enable better length generalization and efficient inference.

⚑ SwiGLU Activation

Gated linear units replacing ReLU in FFN for better performance.

Applications

πŸ’¬

ChatBots & LLMs

πŸ”

Search & RAG

πŸ’»

Code Generation

πŸ–ΌοΈ

Vision (ViT)

🎡

Audio & Speech

🧬

Protein Folding

Learn More

Related Topics

Test Your Knowledge

Score 8/10 or higher to pass