Step 6 of 6

GPTs, diffusion & beyond

Decoder-only transformers (GPT), vision transformers (ViT), diffusion models, mixture of experts, and the frontier architectures shaping AI today.

18 min read

The transformer is the foundation, but the models shipping today are built on a stack of architectural refinements: better normalization, efficient attention, mixture of experts, and multimodal fusion. This is the landscape of modern architectures - what's changed since "Attention Is All You Need," and where the field is heading.

3Blue1Brown's visual walkthrough of the GPT architecture - from token embeddings through attention layers to next-token prediction.

The modern transformer recipe#

The original 2017 transformer has been refined in nearly every component. The modern LLM recipe looks like this:

ComponentOriginal (2017)Modern (2024+)
NormalizationPost-LayerNormPre-RMSNorm
ActivationReLUSiLU/SwiGLU
Position encodingSinusoidalRoPE
AttentionMulti-head (MHA)Grouped-query (GQA)
ArchitectureEncoder-decoderDecoder-only
VocabularyBPE (30K tokens)BPE/SentencePiece (32K-256K)

Pre-normalization (normalizing before attention/MLP instead of after) makes training more stable. RMSNorm replaces LayerNorm by dropping the mean-centering step - simpler, faster, and equally effective.

SwiGLU (SiLU-gated linear unit) replaces the simple two-layer MLP with a gated version that's empirically better:

RMSNorm
Root Mean Square Normalization - a simpler alternative to LayerNorm that skips mean-centering, normalizing only by the root mean square of activations.
SwiGLU
A gated MLP activation where one linear projection controls how much of another passes through - empirically better than ReLU or GELU in transformer MLPs.
BPE
Byte Pair Encoding - a tokenization method that builds a vocabulary by iteratively merging the most frequent character pairs, balancing vocabulary size with coverage.
GQA
Grouped Query Attention - multiple query heads share the same key-value heads, cutting KV cache memory 4-8× with minimal quality loss.
SwiGLU(x)=(xW1⊙SiLU(xWgate))W2\text{SwiGLU}(\mathbf{x}) = \bigl(\mathbf{x}\mathbf{W}_1 \odot \text{SiLU}(\mathbf{x}\mathbf{W}_{\text{gate}})\bigr) \mathbf{W}_2

SwiGLU: the MLP uses a gating mechanism where one projection controls how much of the other passes through.

Grouped query attention (GQA)#

Standard multi-head attention gives each head its own Q, K, and V projections. This means the KV cache scales linearly with the number of heads - expensive for serving.

GQA groups multiple query heads to share the same K and V heads:

VariantQ headsKV headsKV cache size
MHA32321×
GQA3280.25×
MQA3210.03×

GQA gives most of MHA's quality with a fraction of the memory. LLaMA 2 70B, Mistral, and most modern LLMs use GQA. Multi-query attention (MQA) goes further with a single KV head, but quality drops more noticeably.

Mixture of experts (MoE)#

A standard transformer processes every token through every parameter. A Mixture of Experts model has multiple expert MLPs per layer, but each token is routed to only a few of them:

  1. 01Token enters the MoE layer
  2. 02Router network scores all experts for this token
  3. 03Top-k experts are selected (typically k=2)
  4. 04Token is processed by selected experts only
  5. 05Outputs are combined using router weights

The result: a model with many parameters (high capacity) but low per-token compute (only the active experts run). Mixtral 8×7B has 47B total parameters but only uses ~13B per token - matching dense 13B models in compute while outperforming them in quality.

Multimodal architectures#

Modern models process multiple modalities - text, images, audio, video - through a unified transformer:

ApproachHow it worksExamples
Late fusionSeparate encoders, combined at the endCLIP (image + text)
Early fusionAll modalities tokenized and processed togetherGemini, GPT-4o
Cross-attentionImage/audio features attend to text featuresFlamingo, LLaVA

The key insight: everything becomes tokens. An image is patched into 16×16 pixel blocks, each projected to a vector (as in ViT). Audio is chunked into frames. Video is a sequence of image patches. Once tokenized, the same transformer processes all modalities.

Vision Transformers (ViT) divide an image into fixed-size patches (typically 16×16 pixels), flatten each patch into a vector, add position embeddings, and feed them through a standard transformer. No convolutions needed - pure attention over patches.

Efficient attention variants#

Standard attention is O(n²) in sequence length, which limits context windows. Several approaches reduce this:

MethodComplexityApproach
Flash AttentionO(n²) compute, O(n) memoryFused kernel; exact, just memory-efficient
Sliding windowO(n·w)Each token attends to w nearest neighbors
Linear attentionO(n)Kernel approximation of softmax
Ring attentionO(n²) across devicesDistributes long sequences across GPUs

Flash Attention doesn't change the math - it's an exact implementation that avoids materializing the full n×n attention matrix by computing attention in tiles. This reduces memory from O(n²) to O(n) and is 2-4× faster due to better GPU memory access patterns. It's now the default in all major frameworks.

State space models - beyond transformers?#

State space models (SSMs) like Mamba offer an alternative to attention for sequence modeling:

ht=Aˉ ht−1+Bˉ xtyt=C ht\mathbf{h}_t = \bar{\mathbf{A}}\,\mathbf{h}_{t-1} + \bar{\mathbf{B}}\,x_t \qquad y_t = \mathbf{C}\,\mathbf{h}_t

SSM: a continuous-time state equation discretized for sequences. Linear recurrence enables O(n) inference and O(n log n) training.

SSMs process sequences in O(n) time (linear in sequence length) and can be trained with a parallel scan in O(n log n). The Mamba architecture adds selective state spaces - the matrices A, B, C depend on the input, making the model content-aware like attention.

Hybrid architectures (Jamba, Zamba) combine transformer layers with Mamba layers, getting attention's quality for tasks that need global reasoning and Mamba's efficiency for long sequences.

State space model (SSM)
A sequence model based on continuous-time state equations - processes sequences in linear time O(n) instead of attention's quadratic O(n²).
Mamba
A selective state space model where the state transition matrices depend on the input, making the model content-aware like attention while keeping linear-time efficiency.
MoE
Mixture of Experts - routes each token to only a few expert MLPs out of many, giving large model capacity with low per-token compute cost.
Flash Attention
An exact attention implementation that avoids storing the full N×N attention matrix, reducing memory from O(n²) to O(n) with no quality loss.

Where it shows up in ML#

The LLaMA recipe is the standard

Pre-RMSNorm, SwiGLU, RoPE, GQA - this combination (from LLaMA/Mistral) is now the default for open-source LLMs.

MoE enables scale without cost

Mixtral, DBRX, and likely GPT-4 use MoE to scale total parameters while keeping inference cost manageable.

Flash Attention is universal

Every modern training and inference framework uses Flash Attention. It's exact (no approximation), just dramatically more memory-efficient.

Multimodal is the new default

GPT-4o, Gemini, Claude - frontier models natively process text, images, and audio. The trend is toward single-model, multi-modal systems.

Further reading

Search Data Science After Dark

Search across prompts, models, papers, guides, and library topics.