Inference Optimization & Efficient Serving

Chapter opener illustration: Inference Optimization & Efficient Serving.

"The fastest code is the code that never runs. The fastest inference is the inference that predicts the answer before it finishes thinking."

QuantQuant, Bandwidth-Conscious AI Agent
Looking Back

So far you know how LLMs are built (Chapter 7), which ones to use (Chapter 8), and how reasoning models trade compute for quality (Chapter 9). This chapter is the engineering chapter: how do you make any of them fast and cheap at inference? Quantization, KV cache management, continuous batching, speculative decoding, and the serving frameworks (vLLM, TGI, SGLang) that put it all together. By the end you will know why a 70B model can be served on consumer hardware.

Chapter Overview

Training a large language model is only half the challenge (see Chapter 06: Pretraining & Scaling Laws). The other half is making inference fast enough and affordable enough to serve real users. A 70-billion-parameter model consumes over 140 GB of GPU memory at full precision, generates tokens one at a time via autoregressive decoding, and must maintain an ever-growing cache of key/value tensors for each active request. Without optimization, serving LLMs at scale is prohibitively expensive.

This chapter covers the four pillars of inference optimization. First, quantization reduces the precision of model weights (and sometimes activations) so that models fit on fewer GPUs and run faster. Second, KV cache and memory optimization techniques such as PagedAttention, grouped-query attention, and prefix caching eliminate memory waste and boost throughput. Third, speculative decoding breaks the sequential token-generation bottleneck by drafting multiple tokens at once and verifying them in parallel. Finally, serving infrastructure frameworks like vLLM, SGLang, TGI, and TensorRT-LLM tie everything together into production-ready systems that handle thousands of concurrent requests.

By the end of this chapter, you will understand the math behind each technique, know when to apply each one, and have hands-on experience quantizing models, profiling memory, implementing speculative decoding, and deploying high-throughput inference servers.

Big Picture

Even the most capable model is useless if it is too slow or too expensive to serve. This chapter covers quantization, KV cache optimization, speculative decoding, and other techniques that determine whether your LLM application can meet real-world latency and cost requirements in production (Part VIII).

Note: Learning Objectives

Prerequisites

Sections

What's Next?

Next: Chapter 10: Interpretability & Mechanistic Understanding. You can now run a model fast; can you explain what it is doing? Chapter 10 cracks the black box with probing classifiers, attention analysis, mechanistic circuits, and sparse autoencoders, the techniques that let researchers point at specific neurons and say "this one fires on indirect-object identification". Interpretability is also the foundation Part X will need when we audit models for safety.