Phase 10: LLMs from Scratch

Inference Optimization

Two phases define LLM inference. Prefill processes your prompt in parallel -- compute-bound. Decode generates tokens one at a time -- memory-bound. Every optimization targets one or both. Implement KV-cache to eliminate redundant computation during autoregressive token generation. Explain the prefill vs decode phases of LLM inference and why each has different bottlenecks (compute-bound vs memory-bound). Implement continuous batching and PagedAttention concepts to maximize GPU utilization under concurrent requests. Compare inference optimization techniques (KV-cache, speculative decoding, flash attention) and their throughput/latency tradeoffs. You deploy Llama 3 70B on 4xA100 GPUs. A single user gets 50 tokens per second. Feels fast. Then 100 users hit the endpoint simultaneously. Throughput drops to 3 tokens/second/user. Your $25,000/month GPU bill is serving responses slower than a human types. The model itself does not change between 1 user and 100 users. Same weights, same architecture, same math. What changes is how you schedule the work. Naive inference wastes 90%+ of available GPU compute. A user waiting for token 47 holds an entire batch slot open while the GPU memory bus sits idle between matmuls. Meanwhile, a new user's 2,000-token prompt could fill that dead time with useful compute. This is not a scaling problem. It is a scheduling problem. The techniques in this lesson -- KV caching, continuous batching, PagedAttention, speculative decoding, prefix caching --…

Inference Optimization: Two phases define LLM inference. Prefill processes your prompt in parallel -- compute-bound. Decode generates tokens one at a time --…

This free lesson is part of the AI Engineering from Scratch curriculum. Read the full explanation, run the lesson code, and verify the result in the interactive reader or from the repository source.

Browse the complete course catalog or open this lesson on GitHub.