How to Build Production AI Agents in 2026: LangChain, CrewAI, and AutoGen
Transformers have become the dominant architecture for natural language processing, computer vision, and multimodal AI. This guide takes you from the core attention mechanism to advanced Mixture of Experts (MoE) designs used in frontier models like GPT-5.
Introduction
Transformers have become the dominant architecture for natural language processing, computer vision, and multimodal AI. This guide takes you from the core attention mechanism to advanced Mixture of Experts (MoE) designs used in frontier models like GPT-5.
Prerequisites
- ā Basic linear algebra (matrix multiplication)
- ā Familiarity with neural networks
- ā Python and PyTorch basics
Key Concepts
Step-by-Step Guide
- 1
Understand the Attention Mechanism
At its core, attention computes a weighted sum of values (V) based on the similarity between queries (Q) and keys (K). The formula is: Attention(Q,K,V) = softmax(QK^T / sqrt(d_k))V. The scaling factor sqrt(d_k) prevents the dot products from growing too large, which would push softmax into regions with small gradients.
Tip: The sqrt(d_k) scaling is critical ā without it, gradients vanish for large dimension sizes. - 2
Implement Multi-Head Attention
Instead of a single attention function, the model projects Q, K, and V into h different subspaces, runs attention in parallel, and concatenates the results. This allows the model to attend to different types of relationships simultaneously ā syntactic, semantic, and positional.
pythonclass MultiHeadAttention(nn.Module): def __init__(self, d_model, n_heads): self.heads = nn.ModuleList([ AttentionHead(d_model, d_model // n_heads) for _ in range(n_heads) ]) self.linear = nn.Linear(d_model, d_model) def forward(self, q, k, v, mask=None): heads = [h(q, k, v, mask) for h in self.heads] return self.linear(torch.cat(heads, dim=-1)) - 3
Add Positional Encoding
Since attention is permutation-invariant, the model needs explicit position information. The original transformer uses sinusoidal encodings: PE(pos, 2i) = sin(pos / 10000^(2i/d_model)). Modern models often use learned positional embeddings or RoPE (Rotary Position Embedding), which encodes relative positions more naturally.
Tip: RoPE is used in most modern LLMs (LLaMA, GPT-5) because it generalizes to longer sequences better than learned absolute positions. - 4
Build the Encoder-Decoder Stack
The original transformer has an encoder (for the input sequence) and a decoder (for the output sequence). Each layer has: multi-head self-attention, a feed-forward network with residual connections, and layer normalization. The decoder adds a cross-attention layer that attends to the encoder output.
- 5
Understand the Feed-Forward Network
Each transformer layer contains a position-wise feed-forward network: FFN(x) = W2 * activation(W1 * x + b1) + b2. The hidden dimension is typically 4x the model dimension. Modern models use SwiGLU or GeGLU activations instead of ReLU for better performance.
pythonclass FFN(nn.Module): def __init__(self, d_model, d_ff): self.w1 = nn.Linear(d_model, d_ff) self.w2 = nn.Linear(d_ff, d_model) def forward(self, x): return self.w2(F.gelu(self.w1(x))) - 6
Implement Layer Normalization
LayerNorm normalizes across the feature dimension, stabilizing training. Pre-norm (normalizing before the sublayer) is preferred in modern architectures because it allows better gradient flow through deep stacks.
Tip: Use RMSNorm instead of LayerNorm for large models ā it is simpler, faster, and performs equally well. - 7
Scale to Decoder-Only Architecture
Most modern LLMs (GPT, LLaMA, Claude) use decoder-only architectures. They use causal (masked) self-attention to prevent looking at future tokens, and the model is trained with next-token prediction loss. This simplification removes the encoder and cross-attention entirely.
- 8
Add Mixture of Experts
MoE replaces the FFN with multiple parallel FFN "experts" and a gating network that routes each token to the top-k experts. This increases parameter count without proportional compute increase. GPT-5 uses 128 experts with top-8 routing.
Warning: MoE training is tricky ā load balancing loss is essential to prevent all tokens routing to the same expert. - 9
Optimize with Flash Attention
Flash Attention is an IO-aware exact attention algorithm that avoids materializing the full NĆN attention matrix in HBM. It tiles the computation to stay in SRAM, reducing memory from O(N²) to O(N) and speeding up training by 2-4x.
Tip: Flash Attention 2 is available in PyTorch 2.0+ via torch.nn.functional.scaled_dot_product_attention. - 10
Train with Mixed Precision
Use bf16 (bfloat16) for training stability and fp16 for inference. Gradient scaling is not needed with bf16. For MoE models, keep the router in fp32 to prevent routing instability.
pythonmodel = model.to(torch.bfloat16) optimizer = torch.optim.AdamW(model.parameters(), lr=3e-4) scaler = torch.cuda.amp.GradScaler() # only for fp16 - 11
Implement KV Cache for Inference
During autoregressive generation, previously computed K and V tensors can be cached and reused. This reduces inference from O(N²) to O(N) per token. KV cache memory grows linearly with sequence length and is the primary bottleneck for long-context inference.
Warning: KV cache for 128K context with a 70B model requires ~40GB of VRAM. Use PagedAttention or quantized KV cache to manage this. - 12
Evaluate and Benchmark
Evaluate your transformer on standard benchmarks: MMLU for knowledge, HumanEval for coding, GSM8K for math. Track perplexity during training, but remember that perplexity does not always correlate with downstream task performance. Use few-shot evaluation to measure in-context learning.
Summary
Transformers combine multi-head self-attention, positional encoding, and feed-forward networks in a residual stack. Modern frontier models use decoder-only architectures with MoE for sparse scaling, RoPE for positional encoding, RMSNorm for stability, and Flash Attention for efficient training. The key engineering challenges are KV cache management, MoE load balancing, and long-context optimization.
Frequently Asked Questions
Self-attention computes relationships within a single sequence (Q, K, V all come from the same input). Cross-attention has Q from one sequence and K, V from another ā used in encoder-decoder models for translation.
Decoder-only is simpler, trains more efficiently with next-token prediction, and performs equally well or better on most tasks. The encoder-decoder architecture is mainly used for translation where bidirectional encoding of the source helps.
MoE increases total parameters but only activates a subset per token. A 1T parameter MoE model with top-8 routing of 128 experts activates only ~62B parameters per token ā comparable to a dense 62B model but with more capacity.
The attention mechanism is O(N²) in sequence length. Techniques like Flash Attention, sparse attention patterns, sliding window attention, and ring attention extend context from 4K to 2M tokens.
Test Your Knowledge
1. Why is the attention formula divided by sqrt(d_k)?
Without scaling, large dot products push the softmax function into saturation where gradients are near zero, making training unstable.
2. What advantage does RoPE have over learned positional embeddings?
RoPE encodes relative positions using rotation, allowing the model to extrapolate to longer sequences at inference time.
3. In MoE, what is the purpose of the load balancing loss?
Without load balancing, the router may send most tokens to a few experts, wasting the other experts and creating training instability.