Benchmark Llama.cpp Tokens Per Second: GPU Comparison Guide
In this article
Benchmarking llama.cpp allows you to measure inference speed in tokens per second (TPS) to objectively compare how different GPUs handle large language models. This process helps identify which hardware offers the best performance for your specific model size and context length, ensuring optimal deployment efficiency.
What You'll Need
- A computer with a compatible GPU (NVIDIA, AMD, or Apple Silicon).
- The llama.cpp repository cloned from GitHub.
- A quantized GGUF model file (e.g., Llama-3-8B-Q4_K_M.gguf).
- Command-line terminal access.
- Stable power supply and adequate cooling.
Step-by-Step
- Install Dependencies: Ensure your system has the necessary compilers (like GCC or Clang) and CUDA drivers for NVIDIA cards. Clone the llama.cpp repository using
git clone https://github.com/ggerganov/llama.cpp.git. - Build the Binary: Navigate into the cloned directory. For NVIDIA GPUs, build with CUDA support by running
make LLAMA_CUDA=1. For AMD ROCm, usemake LLAMA_HIPBLAS=1. On Apple Silicon, simply runmake. Verify the build completes without errors. - Obtain a Model: Download a GGUF model from Hugging Face. Ensure the model matches your hardware capabilities; smaller quantizations (Q4_K_M) run faster than higher precision ones (Q8_0). Place the .gguf file in the llama.cpp directory for easy access.
- Run the Benchmark Command: Use the
mainbinary with the-tflag for threads and-nglfor GPU layers. The standard benchmark command is:./main -m model.gguf -b 1024 -t 8 -ngl 99 -s 0 --prompt "Your test prompt here" --repeat-penalty 1.1 -n 512. Adjust-nglto match your VRAM capacity. Setting it too high will cause CPU fallback, skewing results. - Analyze Output: After execution, look for lines starting with "evals" or "tok/s". This number represents tokens generated per second. Record this value. Higher numbers indicate better performance.
- Test Multiple Configurations: Repeat the test with different batch sizes (
-b) and context lengths (-c). A model might perform well at short contexts but slow down significantly at longer ones due to attention mechanism overhead. - Compare Results: Create a simple table comparing TPS across different GPUs. Note that raw TPS does not always equal perceived responsiveness; latency matters for chat interfaces.
Common Mistakes
One frequent error is ignoring VRAM limits. If -ngl exceeds available video memory, llama.cpp offloads layers to system RAM, drastically reducing speed. Always monitor GPU utilization during tests. Another mistake is using random prompts instead of realistic text. Benchmarks should reflect actual usage patterns. Additionally, failing to close other applications can lead to inconsistent results due to background resource contention.
Tips
Use consistent temperatures and seed values for reproducible results. Enable flash attention if supported by your backend, as it significantly boosts speed on modern architectures. For NVIDIA users, ensure you are using the latest driver versions, as llama.cpp often optimizes for new CUDA releases. When comparing AMD and NVIDIA, remember that ROCm support varies by Linux distribution; Ubuntu generally offers the most stable experience for benchmarking.
Troubleshooting
If the benchmark fails to start, check your build flags. Ensure the correct backend was enabled during compilation. If speeds are unexpectedly low, verify that the model is actually loading onto the GPU by checking system monitors like Task Manager or nvidia-smi. If you encounter out-of-memory errors, reduce the context window (-c) or use a more aggressive quantization (e.g., Q4 instead of Q8). On AMD systems, ensure ROCm environment variables are correctly set. For Apple Silicon, ensure Metal backend is active.
Frequently Asked Questions
Does higher VRAM always mean faster speeds?
No. While sufficient VRAM prevents CPU offloading, bandwidth is often the bottleneck. A card with more VRAM but lower bandwidth may be slower than one with less VRAM but higher bandwidth.
How many tokens should I generate for an accurate benchmark?
Generate between 128 and 512 tokens. Shorter runs may include startup overhead, while longer runs risk hitting context limits or thermal throttling.
Can I benchmark multiple models simultaneously?
Not accurately on a single GPU instance. Concurrent processes compete for resources, invalidating individual TPS measurements. Test one model at a time.
Why do my results vary between runs?
Background processes, thermal throttling, and dynamic clock speeds cause variance. Run each test three times and take the average for reliability.
Is llama.cpp faster than Python-based frameworks?
Generally, yes, for local inference. llama.cpp is optimized for C++ efficiency and minimal overhead, whereas Python frameworks add interpretation layers.
Should I use Q4 or Q8 quantization for benchmarks?
Use Q4_K_M for speed comparisons, as it is the most common production setting. Q8 provides higher quality but lower speed, serving as a baseline for accuracy trade-offs.