When You Actually Use It — The Price of Inference
Chapter 8 was the training budget, Chapter 9 the stages that fit it to people, Chapter 10 the machinery for pulling things in from outside. All of them are about how it gets built.
This chapter counts what gets paid when it is used. Training happens once; inference happens on every response. Knowing where the price is attached also shows that there are only two ways to make it faster.
1. The price of one token, and when inference overtakes training
When Chapter 8 counted , the breakdown was forward and backward. Inference is forward only, so per token
At that is FLOPs per token, and for a 1,000-token response.
Training is , once. So inference catches up with training after, for responses of tokens,
responses. The interesting part is that cancels: it does not depend on model size. With ,
| response length | responses to match training |
|---|---|
| 100 | |
| 1,000 | |
| 10,000 |
2. Keeping it so it is not rebuilt — the KV cache
Autoregression emits one token at a time. Done naively, producing token means recomputing everything from the start.
The waste is countable. To emit tokens, running tokens' worth of forward pass at step totals ; keeping the past around makes it .
| rebuilding | keeping | ratio | |
|---|---|---|---|
| 1,024 | 1,024 | 513x | |
| 8,192 | 8,192 | 4,097x | |
| 131,072 | 131,072 | 65,537x |
So the and of past tokens (Chapter 6) are kept. That is the KV cache, and it is paid for in memory: two tensors, per layer, per token, wide, bytes each —
| layers | size | |||
|---|---|---|---|---|
| 32 | 4,096 | 8,192 | 2 | 4.29 GB |
| 32 | 4,096 | 131,072 | 2 | 68.7 GB |
| 80 | 8,192 | 8,192 | 2 | 21.5 GB |
| 32 | 4,096 | 8,192 | 1 | 2.15 GB |
Per token, at 32 layers, =4,096, 2 bytes, that is 0.524 MB. Serving eight people at once at =8,192 needs 34.4 GB for the cache alone.
3. What sets the speed is reading, not computing
Producing one token means reading every weight once. weights at bytes each is bytes read for FLOPs of work. Divide:
At 16 bits (2 bytes) that is 1 FLOP/byte — one multiply per byte read.
Running requests together means the weights are read once for all of them:
| weights | batch | FLOP/byte |
|---|---|---|
| 16-bit | 1 | 1.0 |
| 16-bit | 8 | 8.0 |
| 16-bit | 64 | 64.0 |
| 8-bit | 1 | 2.0 |
| 4-bit | 1 | 4.0 |
| 4-bit | 64 | 256.0 |
Which of reading and computing runs out first on real hardware depends on the machine. What this book can give is the ratio itself; which side is the bottleneck on which machine is not measured here.
4. Reading less — quantization
Round the weights to bits. The plainest way: take the largest magnitude in some range and split it into even steps.
If the rounding error spreads uniformly over , its variance is . Checked on standard normal weights, 4,096 per row, 64 rows.
| bits | grid step | theory | measured rmse |
|---|---|---|---|
| 8 | 0.0298 | 0.00861 | 0.00865 |
| 6 | 0.1222 | 0.03528 | 0.03536 |
| 4 | 0.5413 | 0.15625 | 0.15668 |
| 3 | 1.2630 | 0.36459 | 0.36589 |
| 2 | 3.7889 | 1.09377 | 0.90824 |
Eight through three bits agree within 0.5%; only 2 bits breaks away. The reason is measurable too. At 2 bits the grid has three rungs, , and 93.9% of the weights round to zero. The assumption that error spreads uniformly is what fails first.
Who shares a scale
The table above took one maximum across all 4,096 values in a row. Taking it again every 128 values:
| bits | whole row | every 128 |
|---|---|---|
| 8 | 0.00865 | 0.00646 |
| 4 | 0.15668 | 0.11730 |
| 3 | 0.36589 | 0.27383 |
A steady 1.34x improvement at every bit width. So far this is a modest gain.
When one outlier gets in
Put a single weight of in each row and it changes character.
Comments
Sign in to comment