Sub topic 15
Measuring real speed with llama-bench
Earlier chapters gave you two estimates: whether a model fits in memory, and a ceiling on tokens per second from memory bandwidth. This chapter covers how to check those estimates against your own machine with llama-bench, the benchmarking tool that ships with llama.cpp.
This chapter assumes you can run commands in a terminal and have a GGUF file on disk. It also assumes you know the difference between prompt processing (prefill) and token generation (decode), and the bandwidth ceiling formula from the previous chapter.
Why estimates are not enough
The ceiling formula (bandwidth divided by model file size) is an upper bound. Real speed lands below it for several reasons:
- The GPU or CPU never sustains its advertised peak bandwidth.
- Each token also reads the KV cache, which adds to the bytes moved.
- Kernels have overhead, and some quantisation formats decode more slowly than others.
- With partial offload, the slower memory tier sets the pace.
A measurement tells you how far below the ceiling you are. It also lets you compare settings, such as quantisation levels or offload counts, on equal terms.
What llama-bench measures
llama-bench loads a model and runs two kinds of test, repeated several times:
- Prompt processing test. It feeds a batch of prompt tokens and reports how many it handles per second. The label is
ppfollowed by the token count, such aspp512. - Token generation test. It generates a fixed number of tokens one at a time and reports tokens per second. The label is
tgfollowed by the count, such astg128.
It does not run a conversation or sample interesting text. It times the raw work, so results are repeatable and comparable between machines.
Running your first benchmark
This is the command from the topic plan:
llama-bench -m model-Q4_K_M.gguf -p 512 -n 128 -ngl 99Each flag does one job:
| Flag | Meaning |
|---|---|
| `-m` | Path to the GGUF file |
| `-p 512` | Prompt processing test with 512 tokens |
| `-n 128` | Token generation test with 128 tokens |
| `-ngl 99` | Offload up to 99 layers to the GPU, which means all layers for most models |
| `-r` | Number of repetitions per test (the default is 5) |
| `-t` | CPU thread count |
| `-o` | Output format: `md`, `csv`, `json`, `jsonl` or `sql` |
The output is a table. A typical result looks like this (the numbers are illustrative, not from a real machine):
| model | size | params | backend | ngl | test | t/s |
| ---------------------- | ----: | -----: | ------- | --: | ----: | ------------: |
| llama 8B Q4_K - Medium | 4.58 GiB | 8.03 B | CUDA | 99 | pp512 | 2400.00 ± 8.00 |
| llama 8B Q4_K - Medium | 4.58 GiB | 8.03 B | CUDA | 99 | tg128 | 85.00 ± 0.40 |The ± figure is the standard deviation across repetitions. A small spread means the result is stable. A large spread usually means something else is competing for the hardware, such as another process, thermal throttling, or a laptop on battery power.
Reading the two numbers differently
The prompt processing number is typically far larger than the generation number. Prefill handles many tokens in one pass, so each weight read from memory is reused across all of them. That makes the work compute-bound. Decode handles one token at a time, so the whole model is read for a single token. That makes it bandwidth-bound.
Practical consequences:
- A fast
ppnumber means long prompts and large documents are ingested quickly. - A fast
tgnumber means the reply streams quickly once it starts. - Hardware with strong compute but modest bandwidth can look good on
ppand disappointing ontg. - Total response time is roughly prompt tokens divided by
pp, plus output tokens divided bytg.
Comparing the measurement to your ceiling
Take the tg128 result and divide it by the ceiling. This ratio shows how much of the memory system you are using. The script below parses llama-bench JSON output and prints that ratio. It contains a small sample result so it runs as shown. To use your own data, save the real output with -o json and load that file instead.
import json
# Sample output shaped like `llama-bench -o json`. Values are illustrative.
SAMPLE = """
[
{"n_prompt": 512, "n_gen": 0, "model_size": 4920000000, "avg_ts": 2400.0, "stddev_ts": 8.0},
{"n_prompt": 0, "n_gen": 128, "model_size": 4920000000, "avg_ts": 85.0, "stddev_ts": 0.4}
]
"""
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
def report(results, bandwidth_gb_s):
for row in results:
if row["n_gen"] > 0:
size_gb = row["model_size"] / 1e9
ceiling = decode_ceiling_tokens_per_s(bandwidth_gb_s, size_gb)
share = row["avg_ts"] / ceiling
print(f"tg{row['n_gen']}: measured {row['avg_ts']:.1f} t/s, "
f"ceiling {ceiling:.1f} t/s, {share:.0%} of ceiling")
elif row["n_prompt"] > 0:
print(f"pp{row['n_prompt']}: {row['avg_ts']:.0f} t/s")
report(json.loads(SAMPLE), bandwidth_gb_s=500)With 500 GB/s of bandwidth and a 4.92 GB file, the ceiling is about 102 tokens per second. A measured 85 is 84% of it, which is a healthy result. Typical ratios depend on the backend and hardware. If you land far below, for example under 40%, look for a cause: a layer running on the CPU, an unsuitable thread count, or a power-limited laptop.
Check the JSON field names against your installed version of llama-bench. The output format can change between llama.cpp releases.
Sweeping a setting
The most useful benchmarks vary one setting at a time. llama-bench accepts comma-separated lists for many flags and runs every combination. To find the best offload count for a model that does not fully fit in VRAM, sweep -ngl:
llama-bench -m model-Q4_K_M.gguf -p 512 -n 128 -ngl 0,10,20,30,99Expect tg to rise as more layers move to the GPU, and to jump when the last layers fit. Past the point where everything fits, higher values change nothing. If a value exceeds your VRAM, the run may fail with an out-of-memory error. Treat that as information about your budget, not a tool problem.
You can sweep other settings the same way:
- Run several files, such as Q4KM and Q8_0 of one model, to see the speed cost of higher precision.
- Vary
-p(for example128,512,2048) to see how prefill throughput changes with prompt length. - Vary
-ton a CPU-only run to find the thread count where speed stops improving. It often sits near the number of physical cores, not logical ones.
A repeatable workflow
Common errors and gotchas
- Benchmarking a cold system. The first run may include loading the file from disk. Repeated runs smooth this out, but if you see one slow outlier, rerun.
- Changing two things at once. If you switch quantisation and offload together, you cannot tell which one caused the change.
- Trusting a short test on a hot laptop. Sustained load can trigger throttling. A larger
-rvalue shows drift as large standard deviations. - Forgetting
-ngl. Without offload flags a GPU build may run on the CPU, giving numbers far below what you expected. Thenglandbackendcolumns in the output expose this. - Ignoring context. The default tests use short contexts. Real chats with long histories read a bigger KV cache for every token, so speed in practice is lower than
tg128suggests. Test with a long prompt to see the effect. - Mixing up benchmark and application speed. A chat program adds sampling, formatting, and network time. Use
llama-benchfor hardware comparisons, not for promises about an app's latency.
Exercises
Exercise
A machine has 200 GB/s of memory bandwidth and a 5 GB model file. llama-bench reports tg128 of 28 t/s. What is the ceiling, what fraction of it is reached, and what would you check next?
Show answer
The ceiling is 200 / 5 = 40 t/s. The measured 28 t/s is 70% of it, which is reasonable. Next, check that all layers are offloaded (the ngl column), that no other program is using the memory, and whether the test used a short context. A longer context would read more KV cache per token and lower the speed further.
Exercise
You benchmark a model and get pp512 of 1800 t/s and tg128 of 20 t/s. Roughly how long will a request with a 3000-token prompt and a 400-token reply take?
Show answer
Prompt processing takes about 3000 / 1800 ≈ 1.7 seconds. Generation takes 400 / 20 = 20 seconds. The total is about 22 seconds, dominated by generation. This is an estimate, because long prompts may run slower than the 512-token test.
Exercise
Why should you change only one setting between benchmark runs?
Show answer
If two settings change together, the speed difference could come from either one, so you cannot decide which setting helped or hurt. Changing one at a time isolates the cause.