Textbook › Part III Deep Learning: what assumption you add
CHAPTER 11

When You Actually Use It — The Price of Inference

★ MEMBER16 min

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 C≈6NDC \approx 6ND, the breakdown was 2N2N forward and 4N4N backward. Inference is forward only, so per token

Ctoken  ≈  2NC_{\text{token}} \;\approx\; 2N
(1)

At N=7×109N = 7\times10^{9} that is 1.4×10101.4\times10^{10} FLOPs per token, and 1.4×10131.4\times10^{13} for a 1,000-token response.

Training is 6ND6ND, once. So inference catches up with training after, for responses of LL tokens,

6ND2NL  =  3DL\frac{6ND}{2NL} \;=\; \frac{3D}{L}
(2)

responses. The interesting part is that NN cancels: it does not depend on model size. With D=1012D = 10^{12},

response length LL responses to match training
100 3.0×10103.0\times10^{10}
1,000 3.0×1093.0\times10^{9}
10,000 3.0×1083.0\times10^{8}
Training once, inference every time. The crossover is at $3D/L$ responses. Watching only the build budget of Chapter 8 means missing the running total.

2. Keeping it so it is not rebuilt — the KV cache

Autoregression emits one token at a time. Done naively, producing token tt means recomputing everything from the start.

The waste is countable. To emit TT tokens, running tt tokens' worth of forward pass at step tt totals T(T+1)/2T(T+1)/2; keeping the past around makes it TT.

TT rebuilding keeping ratio
1,024 5.25×1055.25\times10^{5} 1,024 513x
8,192 3.36×1073.36\times10^{7} 8,192 4,097x
131,072 8.59×1098.59\times10^{9} 131,072 65,537x

So the KK and VV 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, dd wide, bb bytes each —

bytes  =  2×Llayers×T×d×b\text{bytes} \;=\; 2 \times L_{\text{layers}} \times T \times d \times b
(3)

layers dd TT bb 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, dd=4,096, 2 bytes, that is 0.524 MB. Serving eight people at once at TT=8,192 needs 34.4 GB for the cache alone.

The price of a long context is not only the $T^2$ of Chapter 6 in compute. It is also memory proportional to $T$ — and unlike compute, that one is multiplied by how many people are being served at once.

3. What sets the speed is reading, not computing

Producing one token means reading every weight once. NN weights at bb bytes each is NbNb bytes read for 2N2N FLOPs of work. Divide:

2NNb  =  2bFLOP/byte\frac{2N}{Nb} \;=\; \frac{2}{b} \quad \text{FLOP/byte}
(4)

At 16 bits (2 bytes) that is 1 FLOP/byte — one multiply per byte read.

Running BB requests together means the weights are read once for all of them:

weights batch BB 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
There are only two roads. ① Read less (fewer bits) or ② compute more per read (batch). Neither one is "make the arithmetic faster."

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 bb bits. The plainest way: take the largest magnitude mm in some range and split it into 2b−1−12^{b-1}-1 even steps.

step  =  m2b−1−1\text{step} \;=\; \frac{m}{2^{b-1}-1}
(5)

If the rounding error spreads uniformly over ±step/2\pm\text{step}/2, its variance is step2/12\text{step}^2/12. Checked on standard normal weights, 4,096 per row, 64 rows.

bits grid step theory step/12\text{step}/\sqrt{12} 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, {−m,0,+m}\{-m, 0, +m\}, 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 ±20\pm20 in each row and it changes character.

§

Members-only from here

All 42 chapters and every lab, $4.99/mo. Cancel anytime.

Comments

Sign in to comment