Large Language Models
# Large Language Models: The Memory Wall, the Roofline, and Hardware Choice
A large language model (LLM) is, from a hardware perspective, an enormous sequence of matrix multiplications with an attention mechanism threaded through it. What makes LLMs a distinct *hardware* problem — not just a bigger version of prior deep learning workloads — is the ratio between how much data has to move and how much arithmetic gets done with it. That ratio, called arithmetic intensity, determines whether a given LLM workload runs at a chip's peak compute speed or is stuck waiting on memory, and it differs sharply between training and inference.
---
## 1. The Roofline Model and the Memory Wall
Every chip has two hard ceilings: a peak compute rate (FLOPs/s) and a peak memory bandwidth (bytes/s). For any given operation, arithmetic intensity = FLOPs performed ÷ bytes moved. The roofline model plots achievable throughput against arithmetic intensity and shows exactly where a workload sits relative to those two ceilings.
Autoregressive decoding — generating one token at a time, re-reading model weights and the KV cache on every step — has very low arithmetic intensity: a small amount of compute per byte moved. It sits firmly on the memory-bound slope, meaning faster compute units bring almost no speedup; only more memory bandwidth, smaller model footprint (quantization), or bigger batches (amortizing the same weight read over more tokens) move the needle.
---
## 2. Attention Matrix Multiplication Cost
The two matrix multiplications inside attention — $Q \cdot K^T$ and $\text{softmax}(\cdot) \cdot V$ — both scale as $\mathcal{O}(\text{sequence\_length}^2 \times \text{head\_dim})$, while the feed-forward layers scale as $\mathcal{O}(\text{sequence\_length} \times \text{hidden\_dim}^2)$. At short sequence lengths the feed-forward cost dominates; past a crossover length (roughly where $\text{seq\_len} \approx \text{hidden\_dim}$), the quadratic attention term takes over and long-context inference becomes dominated by attention rather than the dense layers. This is the direct hardware motivation for fused attention kernels and for architectural variants (grouped-query attention, sliding-window attention) that cut the quadratic term.
---
## 3. Inference vs. Training: The Autoregressive Memory Wall Dynamics
Training optimizes for sustained throughput across a large, synchronized multi-chip cluster, where arithmetic intensity is pushed high by large batches and the dominant hardware constraint is interconnect bandwidth for gradient synchronization. Inference optimizes for latency and cost per token on far smaller hardware footprints, where decode's low arithmetic intensity makes memory bandwidth and KV-cache size — not peak FLOPs — the binding constraint. The same model, run in these two modes, wants genuinely different hardware.
---
## 4. Key Takeaway
Large language model performance is governed by arithmetic intensity, not FLOPs on a spec sheet: the roofline model shows that decode sits on the memory-bound slope where more compute buys nothing, attention's quadratic cost in sequence length determines when long-context workloads stop being feed-forward-dominated, and training versus inference demand genuinely different hardware shapes — high-bandwidth multi-chip fabrics for training's gradient synchronization versus low-latency, quantization-friendly single-chip paths for inference's memory-bound decode loop. Understanding which side of the memory wall a given LLM workload sits on is the single most useful fact for choosing or designing hardware for it.
---
## References
- Williams, Waterman & Patterson, "Roofline: An Insightful Visual Performance Model for Multicore Architectures," CACM 2009.
- Kaplan et al., "Scaling Laws for Neural Language Models" — compute and data scaling behavior.
- Pope et al., "Efficiently Scaling Transformer Inference," MLSys 2023.
- Rajbhandari et al., "ZeRO: Memory Optimizations Toward Training Trillion Parameter Models," SC 2020 — training memory footprint breakdown.