Running models in production
About this lesson
A trained model is only useful once it answers real requests, quickly and cheaply. Follow one prompt through an inference server: the weights waiting in GPU memory, prefill and decode, the KV cache that grows with every token, and why memory, not maths, sets the speed. Then see the three tricks every serving system uses: batching, paging the cache, and quantization.
A request arrives
Running a model in production means serving it. An inference server waits for requests. Your app sends a prompt over HTTP, "Write a haiku about the sea", and the server queues it until the GPU, the chip that runs the model, is free.
POST /v1/completions (HTTP request) prompt "Write a haiku about the sea" max_tokens 64 the server queues it until the GPU can take it
Read the lesson step by step
A request arrives
Running a model in production means serving it. An inference server waits for requests. Your app sends a prompt over HTTP, "Write a haiku about the sea", and the server queues it until the GPU, the chip that runs the model, is free.
POST /v1/completions (HTTP request) prompt "Write a haiku about the sea" max_tokens 64 the server queues it until the GPU can take itWeights live on the GPU
Before any request, the server loads the weights, the model's learned numbers, into GPU memory. Our example: 13 billion weights at 2 bytes each (16-bit numbers) is 26 GB, about two thirds of a 40 GB GPU.
model OPT-13B: 13 billion weights GPU A100 40GB w16 = 13e9 * 2 / GB -> 26.0 GB (65% of 40 GB) loaded once at start-up, shared by every requestPrefill: read the prompt
First comes prefill. All the prompt's tokens go through the model in one pass, side by side. Every layer saves each token's key and value vectors, what attention looks up later, in the KV cache. Out comes the first new token.
prompt 6 tokens, processed together each layer, each token -> one key + one value stored in the KV cache (blocks of 4 tokens here) block 0 A:4 block 1 A:2 -> first new token "Grey"Decode: one token per step
Then decode: one new token per step. The newest token goes in, its key and value join the cache, and attention reads all the cached ones instead of recomputing them. The cache grows by one token every step.
step 1 feed "Grey", add its key+value A:3 attend over 7 cached tokens -> "waves" step 2 feed "waves", add its key+value A:4 attend over 8 cached tokens -> "fold" without the cache, every step would redo the promptThe cache is big
Each token stores a key and a value in each of 40 layers, 5,120 numbers each, 2 bytes per number: 819,200 bytes, about 0.8 MB. A full 2,048-token request needs about 1.7 GB of cache.
kv = 2 * 40 * 5120 * 2 key+value x layers x numbers x bytes -> 819,200 bytes per token req = kv * 2048 / GB -> 1.68 GB for one full-length requestDecode waits on memory
Each decode step reads all 26 GB of weights from memory. This GPU reads 1,555 GB per second, so a step takes at least 17 ms: at most about 60 tokens a second for one request. Memory, not arithmetic, is the limit.
bandwidth 1,555 GB/s (A100 40GB datasheet) ms = w16 / 1555 * 1000 -> 16.7 ms per step, at least -> at most ~60 tokens/s for a single request the GPU could do far more maths in that timeBatching shares the read
So servers batch: many requests run through the same step, and one read of the weights serves them all. With continuous batching, a new request joins at the very next step instead of waiting for the whole batch to finish.
request B arrives: 3-token prompt block 2 B:3 next step serves A and B with one weight read B -> block 2 B:4 A -> new block 3 A:1 continuous batching: join or leave at any stepPaging the cache
Older servers reserved cache for the longest possible answer up front, and only 20 to 38% of it held real tokens. vLLM's PagedAttention hands out small blocks only when needed, anywhere in memory, and raises throughput 2 to 4 times.
request A blocks 0, 1, 3 (not side by side) request B blocks 2, 4 a block is taken only when the last one is full waste: at most part of one block per request more requests fit -> bigger batchesSmaller weights
Quantization stores each weight in fewer bits. At 4 bits our 26 GB of weights shrinks to 6.5 GB, leaving far more room for cache, and so for bigger batches. Methods such as GPTQ keep the accuracy loss small; always measure it.
bits per weight 16 8 4 weights (GB) 26.0 13.0 6.5 w4 = 13e9 * 0.5 / GB -> 6.5 GB (plus a little for scale factors) fewer bytes to read also speeds up each stepStream the answer
Each new token is sent to your app the moment it is made, so text appears while the model is still writing. Time to first token is the queue plus prefill; after that, each token takes one decode step.
data: {"token": "Grey"} (server-sent events) data: {"token": "waves"} data: {"token": "fold"} time to first token = queue + prefill time per token = one decode step
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01A 13-billion-weight model stores each weight as a 16-bit number. How much GPU memory do the weights alone need?
16 bits is 2 bytes, and 13 billion × 2 bytes = 26 billion bytes, 26 GB. At 8 bits it would be 13 GB; at 4 bits, 6.5 GB.
02What does the KV cache store?
Attention at each new step needs the keys and values of all earlier tokens. The cache keeps them, so each decode step only computes the new token's own pair.
03Why is decoding one request at a time slow, even on a very fast GPU?
26 GB read at 1,555 GB/s takes at least about 17 ms, so one request gets at most about 60 tokens a second. The arithmetic per step is small; reading the weights is the limit.
04With continuous batching, when does a newly arrived request start being served?
Iteration-level scheduling (Orca) re-forms the batch at every step, so new requests join and finished ones leave without waiting for each other.
05What problem does PagedAttention (vLLM) solve?
Older systems reserved a contiguous chunk per request and used only 20 to 38% of it for real tokens. Small on-demand blocks cut that waste, which allows bigger batches: 2 to 4 times the throughput.
Glossary
- Inference server
- A program that keeps a trained model loaded and answers requests with it, for example over HTTP.
- Weights
- The model's learned numbers. They are loaded into GPU memory once and used by every request.
- Prefill
- The first phase of a request: all the prompt's tokens go through the model together, filling the KV cache and producing the first new token.
- Decode
- The phase after prefill: the model makes one new token per step, each step reading the KV cache.
- KV cache
- Memory that keeps every earlier token's key and value vectors from every layer, so attention does not recompute them.
- Memory bandwidth
- How many bytes per second a chip can read from its memory. It limits how fast decode steps can run.
- Continuous batching
- Running many requests through the same model step, letting requests join or leave at every step.
- Quantization
- Storing weights in fewer bits (8 or 4 instead of 16) to save memory and time, at a small cost in accuracy.
Notes and sources
- The numbers are the worked example from the vLLM paper: a 13-billion-weight model (OPT-13B) on one NVIDIA A100 with 40 GB. The paper writes the KV cache as 800 KB per token and 1.6 GB per request, counting in binary units (1 KB = 1,024 bytes); here 1 GB = 10^9 bytes.
- The cache blocks in the figure hold 4 tokens so they stay readable; vLLM uses 16 tokens per block by default. Real KV caches also live in every layer at once, not in one row.
- About 60 tokens a second is an upper limit for one request at 16-bit precision: real servers also read the KV cache and lose time to overheads, and batching changes the trade-off.
- Quantized weights also store small scale factors, so a 4-bit model is a little bigger than half a byte per weight. Accuracy usually drops a little; measure it on your own evaluations before you ship.
- The prompt and the generated words are illustrations, not the output of a real model.
- Kwon et al., Efficient Memory Management for Large Language Model Serving with PagedAttention (SOSP 2023)
- Yu et al., Orca: A Distributed Serving System for Transformer-Based Generative Models (OSDI 2022)
- Pope et al., Efficiently Scaling Transformer Inference (MLSys 2023)
- Vaswani et al., Attention Is All You Need (2017)
- Zhang et al., OPT: Open Pre-trained Transformer Language Models (2022)
- NVIDIA A100 Tensor Core GPU datasheet
- Dettmers et al., LLM.int8(): 8-bit Matrix Multiplication for Transformers at Scale (2022)
- Frantar et al., GPTQ: Accurate Post-Training Quantization for Generative Pre-trained Transformers (2022)
- MDN, Using server-sent events
- Stanford CS224N: Natural Language Processing with Deep Learning
Quiz Unlocks after the last step.