Sub topic 11
Estimating a tokens-per-second ceiling
This chapter assumes you can already compute a model's file size in memory (parameters × bits per weight ÷ 8) and that you know what the KV cache is. It also assumes you know the bandwidth figure for your memory tier, such as VRAM, system RAM or unified memory. Here you will turn those numbers into an upper bound on generation speed, learn why real speed lands below it, and learn when the bound does not apply.
The core rule
To produce one token, the model runs a forward pass. That pass multiplies the current token's activations by essentially every weight matrix in the network. The processor does not keep the weights, which run to gigabytes. It must stream them from memory on every pass. For a single stream of text, one token means one full read of the weights.
That gives a simple limit:
tokens per second <= memory bandwidth / bytes read per tokenFor a dense model, bytes read per token is close to the size of the model file. The spine's example function expresses this:
def decode_ceiling_tokens_per_s(bandwidth_gb_s, model_file_gb):
# Each generated token reads roughly all weights once,
# so bandwidth divided by model size is an upper bound.
return bandwidth_gb_s / model_file_gb
print(decode_ceiling_tokens_per_s(bandwidth_gb_s=100, model_file_gb=4.9))A 4.9 GB Q4KM file on a 100 GB/s memory system prints about 20.4. No software can exceed that on this hardware with this file, because the memory system cannot deliver the bytes faster.
This also explains why quantisation speeds things up. A smaller file means fewer bytes per token. Halving the bits per weight roughly doubles the ceiling, even though the arithmetic itself is no cheaper.
Which bandwidth number to use
Use the bandwidth of the memory that holds the weights. That is not always the memory the chip is best known for.
| Memory holding the weights | Approximate peak bandwidth | Ceiling for a 4.9 GB file |
|---|---|---|
| Dual-channel DDR5-5600 system RAM | 90 GB/s | about 18 tokens/s |
| Apple unified memory, 150 GB/s class | 150 GB/s | about 31 tokens/s |
| Apple unified memory, 400 GB/s class | 400 GB/s | about 82 tokens/s |
| High-end desktop GPU VRAM | about 1000 GB/s | about 205 tokens/s |
The figures are approximate peaks from vendor specifications, so check your own hardware's datasheet. You can work out DDR bandwidth yourself: transfers per second × 8 bytes per transfer × number of channels. DDR5-5600 is 5600 million transfers per second, so two channels give 5600 × 8 × 2 = 89.6 GB/s.
A GPU with fast VRAM does not help if the model sits in system RAM. The PCIe link between them is far slower than either memory, so the slower tier sets the speed.
Why real speed is lower
The ceiling is an upper bound. You should expect measured speed to be well under it, for four reasons:
- Peak is not sustained. Real workloads usually reach somewhere between half and most of the peak bandwidth. A rough planning range is 50% to 85%, depending on the chip and the software.
- The KV cache is also read. Each token's attention step reads the stored keys and values for the whole context so far. At a long context this adds real bytes.
- Other work takes time. Sampling, kernel launch overhead and non-matrix operations all add time per token.
- Compute can bind first. A weak processor paired with fast memory may not keep up with the arithmetic. This is uncommon for single-stream decoding on modern GPUs, but it happens on small CPUs.
You can fold the KV cache and an efficiency factor into the estimate:
def decode_estimate_tokens_per_s(bandwidth_gb_s, model_file_gb,
kv_cache_gb=0.0, efficiency=0.7):
# Bytes touched per token: all weights plus the KV cache at this context.
bytes_per_token_gb = model_file_gb + kv_cache_gb
return efficiency * bandwidth_gb_s / bytes_per_token_gb
# 4.9 GB model, 1.0 GB KV cache at a full 8192-token context, 100 GB/s memory.
print(round(decode_estimate_tokens_per_s(100, 4.9, kv_cache_gb=1.0), 1))This prints 11.9. That is far below the naive 20.4 ceiling. Most of the drop comes from the efficiency factor. The KV cache accounts for only a small part of it. The cache matters more with long contexts and with models that have many KV heads.
Prompt processing needs a different estimate
Do not apply the bandwidth rule to prompt processing, also called prefill. In prefill the model handles hundreds or thousands of prompt tokens in one batch. It reads each weight matrix once and reuses it for all of those tokens. The bytes per token are tiny, so memory bandwidth stops being the limit and arithmetic throughput takes over.
A rough compute-bound estimate uses the fact that a forward pass costs about 2 floating-point operations per parameter per token:
def prefill_ceiling_tokens_per_s(effective_tflops, params_billion):
flops_per_token = 2 * params_billion * 1e9
return effective_tflops * 1e12 / flops_per_token
# 8B model on hardware sustaining 20 TFLOPS of useful matrix math.
print(round(prefill_ceiling_tokens_per_s(20, 8.0)))This prints 1250. The effective TFLOPS figure is far lower than the marketing peak, so treat the result as a loose upper bound. The two phases scale with different hardware properties. A machine with big, fast memory but modest compute reads prompts slowly and writes quickly. A machine with strong compute and slow memory does the opposite.
When the model is split across tiers
If part of the model sits in VRAM and the rest in system RAM, each token needs both reads, one after the other. Add the times, not the speeds:
def offload_ceiling_tokens_per_s(gpu_gb, gpu_bw_gb_s, cpu_gb, cpu_bw_gb_s):
seconds_per_token = gpu_gb / gpu_bw_gb_s + cpu_gb / cpu_bw_gb_s
return 1 / seconds_per_token
# 4.9 GB model: 3.5 GB in VRAM at 500 GB/s, 1.4 GB in RAM at 80 GB/s.
print(round(offload_ceiling_tokens_per_s(3.5, 500, 1.4, 80), 1))This prints 40.8, against about 102 tokens/s if all 4.9 GB fit in VRAM at 500 GB/s. Only 29% of the file sits in the slow tier, yet it accounts for 71% of the time per token (0.0175 s of 0.0245 s). A small spill costs far more than its share of the model. The next chapters build on this when they cover choosing an offload setting.
Mixture-of-experts models
A mixture-of-experts (MoE) model stores many expert sub-networks but runs only a few of them for each token. All of it must fit in memory, so the weights-memory calculation uses the full file size. The per-token read is only the active portion: the shared layers plus the selected experts. A model with 30B total and 3B active parameters needs memory for 30B but can decode closer to the speed of a 3B model. Use the active bytes in the formula, and the total bytes for the fit check. Real routing adds some overhead, so treat the result as optimistic.
Common errors
- Using the parameter count instead of the file size. Bandwidth is in bytes, so use the quantised file's size. A 8B model at Q4 reads about 4.9 GB, not 8 GB.
- Mixing GB and GiB. Bandwidth specs use decimal GB/s. Your size calculation may be in GiB. The difference is about 7%, which matters when you compare against a benchmark.
- Treating the ceiling as a prediction. It is an upper bound. Plan with a factor around 0.5 to 0.85 and confirm by measurement.
- Applying the rule to prefill or to batched serving. With many concurrent requests, weight reads are shared across them and the formula undercounts total throughput.
- Ignoring context length. The speed you measure at 512 tokens of context is higher than at 32k tokens, because the KV cache reads grow.
Checking your estimate
Measure generation speed with llama-bench and compare it with your ceiling:
llama-bench -m model-Q4_K_M.gguf -p 512 -n 128 -ngl 99The pp512 row is prompt processing and the tg128 row is token generation. Divide the tg128 figure by your ceiling to get your machine's efficiency. A value of 0.6 to 0.8 means the setup is behaving normally. A value under 0.3 suggests something is wrong, such as layers silently running on the CPU, a thermal limit, or a model that spilled out of VRAM. Keep that efficiency number. You can reuse it to predict how other files will perform on the same machine.
Exercise
A machine has 273 GB/s of unified memory. You want to run a 70B-parameter model whose Q4 file is 42 GB, with a small KV cache you can ignore. What is the decode ceiling, and what speed should you plan for if the machine typically reaches 70% of peak bandwidth?
Show answer
The ceiling is 273 / 42 = 6.5 tokens per second. Planning at 70% efficiency gives about 0.7 × 6.5 = 4.5 tokens per second. Prompt processing is a separate, compute-bound figure and is not covered by this estimate.
Exercise
You move a model from a Q80 file of 8.5 GB to a Q4K_M file of 4.9 GB on the same hardware. By what factor does the decode ceiling rise, and why does measured speed often rise by a little less?
Show answer
The ceiling rises by 8.5 / 4.9, about 1.7 times, because each token reads fewer bytes. Measured speed rises by slightly less because the KV cache reads, sampling and other overheads do not shrink when the weights do, so they take a larger share of each token's time.