Navigation
πŸ“ FoundationsΒ· 10 min read

Transformer Architecture Overview

A high-level map of the Transformer β€” components, data flow, and how each part connects to AI infrastructure challenges.

transformeroverviewarchitectureai-infra

Every optimization in AI infrastructure acts on some component of the Transformer. Before diving into the details, this chapter provides the map β€” what are the building blocks, how do they fit together, and why does each one matter for infrastructure.


1. Why AI Infra Engineers Must Understand the Transformer

The Transformer is not just a model architecture β€” it’s the compute workload that defines all AI infrastructure requirements. Every infrastructure decision maps to a Transformer component:

Infra layerTransformer componentChallenge
CUDA kernelsAttention, FFN, Softmax, LayerNormOptimizing individual operations (FlashAttention, fused kernels)
Distributed trainingAttention heads, FFN weights, layersSplitting the model across GPUs (tensor/pipeline/expert parallelism)
Inference servingKV cache, autoregressive decodeManaging memory, batching, latency optimization
Hardware designMatrix multiplications, memory accessTensor Cores, HBM bandwidth, NVLink for parallelism

Understanding which component dominates which cost tells you where to optimize.


2. The Building Blocks

LLM Architecture

A modern decoder-only LLM has surprisingly few distinct components:

Input text
  β†’ Tokenizer (text β†’ token IDs)
  β†’ Embedding table (token IDs β†’ vectors)
  β†’ N Γ— Decoder Block:
      β†’ RMSNorm β†’ Masked Self-Attention β†’ Residual
      β†’ RMSNorm β†’ SwiGLU FFN β†’ Residual
  β†’ Final RMSNorm
  β†’ LM Head (vectors β†’ vocabulary logits)
  β†’ Sampling (logits β†’ next token)

Only 7 distinct operation types: tokenization, embedding lookup, normalization, attention, FFN, linear projection, and softmax/sampling. Everything else is just these operations repeated and composed.

Component Map

ComponentWhat it doesParametersCompute costMemory concern
EmbeddingToken ID β†’ vectorVΓ—dV \times dNegligible (lookup)Large if vocab is big
Self-AttentionToken-to-token interaction4d24d^2 per layerO(S2d)O(S^2 d)KV cache grows with sequence length
FFN (SwiGLU)Per-token processing3dΓ—dff3d \times d_{ff} per layerO(Sβ‹…dβ‹…dff)O(S \cdot d \cdot d_{ff})Activation memory
RMSNormStabilize activationsdd per instanceNegligibleβ€”
LM HeadVector β†’ vocab probabilitiesdΓ—Vd \times VOne GEMMOften tied with embedding

Where the parameters live: In a typical 7B model, ~66% of parameters are in FFN weights, ~33% in attention weights, and ~1% in everything else. In a 70B model with GQA, FFN dominance increases further.

Where the compute goes: For short sequences, FFN dominates compute. For long sequences, attention’s O(S2)O(S^2) term takes over. The crossover point depends on model dimensions and sequence length.


3. How It Evolved

The Three Architecture Families

The original Transformer (2017) had an encoder-decoder design for machine translation. Three families emerged:

ArchitectureKey modelsHow it worksStatus
Encoder-onlyBERT, RoBERTaBidirectional attention, masked token predictionSuperseded for generation
Encoder-DecoderT5, BART, WhisperEncoder processes input, decoder generatesUsed for translation, ASR
Decoder-onlyGPT, LLaMA, Qwen, MistralCausal attention, next-token predictionDominant for LLMs

Why Decoder-Only Won

  1. Simplicity: One stack of identical blocks β€” no encoder, no cross-attention
  2. Scalability: Just add more blocks. Same architecture from 1B to 400B+
  3. Unified: Next-token prediction handles every task when prompted correctly
  4. Inference efficiency: KV cache works naturally; encoder-decoder needs two separate caches

Key Architecture Innovations Since 2017

InnovationWhat changedWhy it matters for infra
GQA (2023)Fewer KV heads than Q heads4-8Γ— KV cache reduction
RoPE (2021)Rotary position embeddingEnables long-context extension
SwiGLU (2020)Gated FFN activation3 weight matrices instead of 2
RMSNormSimpler normalization10-15% faster than LayerNorm
MoE (2022+)Sparse expert routing10Γ— parameters at ~1Γ— compute
MLA (2024)Latent KV compressionFurther KV cache reduction
MTP (2024)Multi-token prediction headsEnables speculative verification

4. Reading Order for This Section

The remaining articles in this section build on each other:

  1. Tokenization β€” How text becomes numbers (the input)
  2. Transformer as Compute Graph β€” Full forward pass with shapes and FLOPs
  3. Self-Attention β€” The core O(S2)O(S^2) operation
  4. FFN & Normalization β€” The other half of each block
  5. Positional Encoding β€” How the model knows token order (RoPE)
  6. Autoregressive Generation β€” How the model actually generates text (prefill, decode, KV cache)
  7. Attention Variants β€” GQA, MLA, Mamba β€” reducing KV cache cost
  8. MoE Architecture β€” Sparse models for more capacity at less compute

Each article includes shape tracking, compute analysis, and connections to infrastructure challenges.


Self-Check

  • Name the 7 distinct operation types in a decoder-only Transformer
  • Explain where parameters and compute concentrate (FFN vs attention)
  • Explain why decoder-only architecture became dominant
  • Name 3 architecture innovations since the original Transformer and their infrastructure impact
  • Explain the crossover between FFN-dominated and attention-dominated compute as sequence length grows

References