Estimate and optimize inference cost
Estimate KV-cache memory, time-to-first-token and decode throughput for a model on given hardware. Explain how batching, GQA, quantization, FlashAttention and speculative decoding each change those numbers.
Builds on: Batching, continuous batching and paged KV, Multi-query and grouped-query attention, Weight quantization for inference, Speculative decoding, FlashAttention: tiling and online softmax
What the animation says
Bytes per token
Put a seven-billion-parameter model on one eighty-gigabyte GPU. How many long chats fit at once? Every token you keep stores a key and a value, in every layer, for every key-value head. That's the KV cache. Two, times thirty-two layers, times thirty-two KV heads, times one hundred twenty-eight numbers per head, times two bytes. About half a megabyte per token. A four-thousand-token chat needs about two point one five gigabytes. The weights take fourteen gigabytes. How many full chats fit in the rest?
First token: compute
So thirty chats fit. Memory capacity sets that number. Next, time to first token. Prefill runs the whole prompt in parallel, so math speed is the limit. About two FLOPs per parameter per token: two times seven billion times two thousand tokens is twenty-eight trillion. At half of the GPU's peak, that takes fifty-six milliseconds. Then each new token must read all fourteen gigabytes of weights, at two terabytes per second. Tokens per second for one user?
Decode: bandwidth
Fourteen gigabytes at two terabytes per second: seven milliseconds per step, about one hundred forty tokens per second. The math is far quicker than that. Decode waits on memory bandwidth, not FLOPs. Batching helps: one read of the weights serves every sequence in the batch. But each sequence's KV cache must be read too. Thirty full chats add sixty-four gigabytes. Fourteen plus sixty-four, over two terabytes per second: thirty-nine milliseconds. About seven hundred seventy tokens per second in total, but only twenty-five per user.
GQA: fewer KV heads
Look at that batched step: the KV cache is most of the bytes read. Grouped-query attention, or GQA, keeps thirty-two query heads, but every four of them share one key-value head. That leaves eight KV heads. Tap the number in the cache formula that changes.
GQA payoff, then int4
Only KV heads count, so each token now takes a quarter of the space: about half a gigabyte per chat, and about one hundred twenty-two chats fit. At thirty chats, each step reads fourteen plus sixteen gigabytes: fifteen milliseconds, about two thousand tokens per second. Quantization attacks the other term. Four-bit weights shrink fourteen gigabytes to three and a half. For one user, a step now takes under two milliseconds: about five hundred seventy tokens per second. So what happens to the fifty-six-millisecond wait for the first token?
Which term does it shrink?
The first-token wait barely moves. Prefill is limited by math, and four-bit weights cut bytes, not FLOPs. FlashAttention does the same math, but never writes the big token-by-token score grid to slow memory. Long prompts prefill faster. Speculative decoding drafts several tokens and checks them all with one weight read. A big win for one user, little once the batch is large. For any trick, ask which term it shrinks: FLOPs, bytes stored, or bytes read per token.
Three numbers, three limits
The three estimates
Worked numbers for a 7B model
Each trick shrinks one term
Remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses