The math behind how LLMs are trained and served – Reiner Pope

Reiner Pope’s lecture explores the mathematical and hardware considerations in training and serving large language models, emphasizing the impact of batch size, memory bandwidth, and GPU architecture on performance and cost, as well as the balance between training, inference, and reinforcement learning compute. He also highlights challenges in extending context length due to memory constraints and draws parallels between neural network designs and cryptographic principles, showcasing innovative approaches like reversible networks to optimize resource usage.

In this in-depth blackboard lecture, Reiner Pope, CEO of MatX and former Google TPU architect, explains the mathematical and hardware principles behind training and serving large language models (LLMs). The discussion begins with the concept of batch size and its critical role in balancing latency and cost during inference. Pope introduces a roofline analysis to model the compute and memory bandwidth constraints when running transformer models on GPU clusters, highlighting how batch size affects the amortization of memory fetches and compute time. He explains that larger batch sizes significantly reduce per-token cost by spreading the fixed cost of loading model weights across many tokens, but also introduces trade-offs related to memory bandwidth and KV cache size.

The conversation then delves into the architecture of mixture of experts (MoE) models and how expert parallelism is mapped onto GPU racks. Pope describes the all-to-all communication pattern required between GPUs hosting different experts and explains the physical and network constraints of scaling beyond a single rack. He emphasizes that while pipelining across racks can reduce memory capacity requirements, it introduces latency and complexity, especially during training where micro-batching is necessary to keep all pipeline stages busy. The practical implication is that most frontier models perform inference within a single scale-up domain (rack) to optimize performance and cost.

Pope further explores the interplay between training, reinforcement learning (RL), and inference compute costs. Using heuristic cost equalization, he suggests that the total tokens processed during inference, RL, and pre-training should be roughly balanced to optimize overall compute efficiency. This leads to the insight that current models may be significantly over-trained relative to Chinchilla scaling laws, with inference token volumes comparable to or exceeding pre-training data volumes. This analysis provides a framework to estimate training data scale and compute allocation based on observable inference usage.

The lecture also covers the challenges of extending context length in LLMs, focusing on memory bandwidth and capacity as primary bottlenecks rather than compute. Pope explains why context lengths have plateaued around 100-200k tokens in recent models, attributing this to the physical limits of high-bandwidth memory (HBM) and the cost trade-offs involved. Sparse attention mechanisms offer some relief but come with quality trade-offs. He also discusses the economics of KV cache storage across different memory tiers, highlighting the balance between rematerialization compute costs and memory storage costs, and how these influence caching strategies in deployed models.

Finally, Pope draws an intriguing parallel between neural network architectures and cryptographic protocols, noting their shared need to mix and scramble information but with opposite goals—neural nets extract structure while cryptography obscures it. He discusses reversible neural networks inspired by Feistel cipher constructions, which enable invertible layers that can reduce memory usage during training by rematerializing activations on the backward pass. This concept exemplifies the cross-pollination of ideas between cryptography and machine learning, illustrating how hardware constraints and algorithmic innovations co-evolve to advance AI capabilities.