All courses

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

▶ Watch this lesson Start the whole course

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.

How many users, how soon, how fast?

You put a 7B-parameter model on one 80 GB GPU. How many long conversations fit at once? How long until the first token appears (**time-to-first-token, TTFT**)? How many tokens per second come out? Each answer is set by one hardware limit: memory capacity, FLOP rate, or memory bandwidth. Each serving trick relaxes mostly one of these limits. If you know which one, you can predict the trick's payoff with a few lines of arithmetic.

Three numbers, three limits

- **KV-cache memory is a capacity limit.** Every live token stores a key and a value per layer per KV head. - **TTFT is set by prefill.** Prefill runs the whole prompt in parallel, so it is compute-bound: FLOPs ÷ FLOP rate. - **Decode is bandwidth-bound.** Each step does little math but must stream all weights plus every sequence's KV cache from memory, so step time ≈ bytes ÷ bandwidth. Every trick cuts FLOPs, bytes stored, or bytes read per token.

The three estimates

$$\text{KV}=2\,L\,h_{kv}\,d_{head}\,b\ \text{ bytes/token}$$ The 2 counts keys and values; $b$ = bytes per number. Sum this over all tokens of all live sequences to get $\text{KV}_{all}$. $$\text{TTFT}\approx\frac{2N\,T_p}{u\,F}$$ $T_p$ = prompt tokens, $F$ = peak FLOP/s, $u\approx0.5$ = achieved fraction of peak. This ignores attention's extra FLOPs, which grow with $T_p^2$ and matter only for long prompts. $$t_{step}\approx\frac{b_w N+\text{KV}_{all}}{BW},\quad \text{throughput}=\frac{B}{t_{step}}$$ $b_w$ = bytes per weight, $BW$ = memory bandwidth in bytes/s, $B$ = batch size. For weight bytes, count all weights, embeddings included. For a 7B model just use $N\approx7\text{e}9$.

Worked numbers for a 7B model

Model: $N=7\text{e}9$, $L=32$, $h_{kv}=h=32$, $d_{head}=128$, bf16. GPU: 80 GB, $F=10^{15}$ FLOP/s, $BW=2\cdot10^{12}$ bytes/s. - **KV:** $2\cdot32\cdot32\cdot128\cdot2=524{,}288$ bytes ≈ 0.5 MB/token, so ≈2.15 GB per 4,096-token chat. - **Capacity:** weights take 14 GB, leaving 66 GB. 66 ÷ 2.15 ≈ 30.7, so 30 chats fit (ignoring activations and other overhead). - **TTFT** for a 2,000-token prompt: $2.8\text{e}13/(0.5\cdot10^{15})$ = 56 ms. - **Decode at B=1:** 14 GB ÷ 2 TB/s = 7 ms, so ~140 tok/s. - **Decode at B=30, full context:** (14+64.4) GB ÷ 2 TB/s ≈ 39 ms, so ~770 tok/s total (~25 per user).

Each trick shrinks one term

Same model and GPU as before: - **Batching:** one weight read serves all B sequences (140 → 770 tok/s), but per-user speed drops. - **GQA with $h_{kv}=8$:** KV shrinks 4× to ~0.54 GB per chat, so ~122 chats fit. At B=30 the step becomes (14+16.1) GB ÷ 2 TB/s ≈ 15 ms, about 2,000 tok/s. - **int4 weights:** 14 GB → ~3.5 GB, so a B=1 step takes ~1.75 ms (~570 tok/s). TTFT barely moves because prefill is compute-bound. - **FlashAttention:** same FLOPs, but no T×T matrix in slow memory, so long-prompt prefill gets faster. - **Speculative decoding:** one weight read verifies several drafted tokens. Big latency gain at small B, little once B is large.

Remember

- KV = $2Lh_{kv}d_{head}b$ bytes per token. - TTFT ≈ $2NT_p$ ÷ achieved FLOP/s. - Decode step ≈ (weight bytes + all KV bytes) ÷ bandwidth, and throughput = B ÷ step. For any trick, ask which term it shrinks: - GQA shrinks KV. - Quantization shrinks weight bytes. - Batching and speculative decoding spread one weight read over more tokens. - FlashAttention cuts attention's memory traffic.

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.