# Inference Engineering, from Scratch

> Source: <https://ai-inference-engineering-from-scrat.vercel.app/>
> Published: 2026-12-05 06:23:09+00:00

**Recommendation:** Optimize single-user decode for memory bandwidth rather than TFLOPS, and budget KV cache memory separately from weights: one 128K-token Mistral 7B user needs 17 GB versus 14.5 GB of weights.
More in the playbook "Inference and self-hosting": <https://wpnews.pro/playbooks/inference-serving>

A field guide in 119 chapters

# Inference engineering,

from scratch.

Learn why GPUs look the way they do, write the kernels an LLM runs, and understand the KV cache down to the byte. Then build a serving engine and run it at scale. Each idea gets an explanation, an animation and runnable code.

- levels
- 4
- levels
- modules
- 18
- modules
- chapters
- 119
- chapters
- sources
- 9
- sources

fig. 0: what a GPU does to write one word

- Roofline✦
- Arithmetic intensity✦
- Warps & SIMT✦
- Memory coalescing✦
- Tiled GEMM✦
- FlashAttention✦
- KV cache✦
- PagedAttention✦
- Continuous batching✦
- Chunked prefill✦
- RadixAttention✦
- Speculative decoding✦
- FP8 · INT4✦
- Tensor parallelism✦
- Prefill/decode disaggregation✦
- KV-aware routing✦
- SLO goodput✦
- Cost per million tokens✦

Watch · 22 seconds, sound on

## A chapter, in 22 seconds

Chapter 6.1, recorded on this site: the naive matmul kernel, the interactive tiling figure clicked live, and the**measured** output from an RTX 3050 Laptop GPU. A naive kernel is 60× slower than cuBLAS; the chapter shows why, and how to close the gap.

Where to start

## Four levels, one path

New to all of this? Start at level 1. Already know some of it? Pick the level whose question you can't answer yet.

[Full roadmap →](https://ai-inference-engineering-from-scrat.vercel.app/roadmap)

[121 chaptersBeginnerWhat is inference, and why is it hard?](https://ai-inference-engineering-from-scrat.vercel.app/learn/m00/what-is-inference-engineering)

- M0Orientation
- M1The LLM forward pass, for inference engineers
- M2Why GPUs: hardware first principles
- M3The performance mental model

[238 chaptersIntermediateHow do I write and optimize GPU code?](https://ai-inference-engineering-from-scrat.vercel.app/learn/m04/kernels-are-not-functions)

- M4CUDA programming fundamentals
- M5Parallel patterns
- M6GEMM and memory optimization
- M7Profiling: measure, don't guess
- M8Transformer kernels and compilers

[337 chaptersAdvancedHow do I build and tune a serving engine?](https://ai-inference-engineering-from-scrat.vercel.app/learn/m09/kv-cache-size)

- M9KV cache engineering
- M10Serving engine internals
- M11Decoding and sampling
- M12Quantization for inference
- M13Measurement and benchmarking

[423 chaptersExpertHow do I run inference at scale?](https://ai-inference-engineering-from-scrat.vercel.app/learn/m14/collectives-nccl)

- M14Multi-GPU inference and parallelism
- M15Distributed serving and routing
- M16Economics and the inference control plane
- CCapstone: ship one service

Try it

## How fast can a GPU possibly talk?

To write each token, the GPU has to read **every weight** of the model from memory. Pick a model and a GPU to see the theoretical limit, then add users and watch the bottleneck move.

Model size

Weight precision

GPU

Per user

209tok/s

Whole GPU

209tok/s

Time for one decode step

**Memory-bound.** The cores wait 99.7% of every step for weights. Add users: they ride along almost for free.

Upper bound: ignores the KV cache, attention and overheads. Real systems reach 50–80% of it. Derived in [0.1](https://ai-inference-engineering-from-scrat.vercel.app/learn/m00/what-is-inference-engineering) and [3.3](https://ai-inference-engineering-from-scrat.vercel.app/learn/m03/roofline).

Four numbers to know

## The facts the rest of the course explains

Myth vs fact

## Six things most people get wrong about LLM speed

Tap a card to flip it. Each fact links to the chapter that proves it.

“A GPU with 2× the TFLOPS writes text 2× faster.”

↻Tap to flip
Usually not. Single-user decode is memory-bound, so speed follows memory bandwidth (TB/s), not TFLOPS.

[Learn why · 3.3 →](https://ai-inference-engineering-from-scrat.vercel.app/learn/m03/roofline)

“Longer prompts make every output token slower.”

↻Tap to flip
Mostly they delay the first token (prefill). Per-token speed barely moves; only the larger KV cache adds a little.

[Learn why · 0.2 →](https://ai-inference-engineering-from-scrat.vercel.app/learn/m00/life-of-a-request)

“Batching users together makes everyone slower.”

↻Tap to flip
Until you reach the ridge point, extra users ride along on the same weight read. Throughput scales almost linearly.

[Learn why · 3.3 →](https://ai-inference-engineering-from-scrat.vercel.app/learn/m03/roofline)

“The model weights are what fill GPU memory.”

↻Tap to flip
At long context the KV cache wins: one 128K-token Mistral 7B user needs 17 GB, more than the 14.5 GB of weights.

[Learn why · 9.1 →](https://ai-inference-engineering-from-scrat.vercel.app/learn/m09/kv-cache-size)

“Quantization only saves memory.”

↻Tap to flip
In decode, fewer bytes per weight means fewer bytes moved per token, so it is also a direct speedup.

[Learn why · 12.1 →](https://ai-inference-engineering-from-scrat.vercel.app/learn/m12/number-formats)

“With enough replicas, routing doesn’t matter.”

↻Tap to flip
Sending a conversation back to the replica that already holds its KV cache skips re-running the whole prefix.

[Learn why · 15.3 →](https://ai-inference-engineering-from-scrat.vercel.app/learn/m15/load-balancing-policies)

How you learn

## The anatomy of a chapter

1. ✎01Concept First principles in plain language, with the maths written out.
2. ◐02Animation Simulators you can break: sliders, drags, step-throughs.
3. ⌘03Code lab NumPy on a CPU first, then CUDA, Triton or vLLM where marked.
4. §04Sources The repo, paper or book section each idea comes from.
5. ?05Quiz Sharp questions, each with an explanation.

Built from open repos, papers and books

## Start with one question: why is the GPU idle?

The answer leads through memory bandwidth, KV caches, batching and routing. That's the whole course.

- repoBEFORE-GPU-ENGINEERING
- coursetime-to-first-token
- course100 Days of CUDA
- blogvLLM Router release
- blogLLM Inference at Scale
- postAkshay Pachaar on X
- paperFrom Inference Engine to Inference Control Plane
- bookAI Systems Performance Engineering
- repoAI Inference Resources
