Skip to main content

Performance Tuning

Optimize llama.cpp inference performance across CPU, GPU, and hybrid configurations.

Quick Wins

Use GPU

Offload layers to GPU with --n-gpu-layers

Optimize Threads

Set --threads to physical CPU cores

Choose Quantization

Use Q4_K_M or Q5_K_M for best speed/quality

Adjust Context

Reduce --ctx-size to minimum needed

GPU Acceleration

CUDA (NVIDIA)

Offload layers to GPU:
Set --n-gpu-layers to a large number (e.g., 200000) to offload all possible layers automatically.
Verify GPU usage in the startup logs:

Metal (Apple Silicon)

Metal is enabled by default on macOS:
Monitor GPU utilization:

ROCm (AMD)

Check GPU usage:

Thread Configuration

Incorrect thread settings are the #1 cause of slow inference!

Finding Optimal Thread Count

Start conservative:
Recommended values:
  • CPU-only: Physical CPU cores (not logical/hyperthreaded)
  • With GPU: 4-8 threads regardless of core count
  • Server (parallel requests): 2-4 threads per request

Batch Thread Configuration

Separate threads for prompt processing:

Context Size Optimization

Context size directly impacts:
  • Memory usage (RAM/VRAM)
  • Inference speed
  • Maximum conversation length
Only use large context (>4096) when absolutely necessary. Most tasks work well with 2048.

Batch Size Tuning

Logical batch size (prompt processing parallelism):
Physical batch size (hardware limit):
Guidelines:
  • Larger batch = faster prompt processing, more memory
  • CPU: 512-2048
  • GPU: 512-2048 (depends on VRAM)
  • Server: 2048+ for parallel requests

Flash Attention

Enables more efficient attention computation:
Flash Attention is enabled by default (auto) when beneficial. Explicitly enable with --flash-attn on.

Quantization Selection

Benchmark Example

Real-world benchmark on NVIDIA A6000 (48GB VRAM), 7-core CPU, 30B Q4_0 model:
Note how too many threads (7) actually decreased performance compared to 4 threads!

Hybrid CPU+GPU Inference

For models larger than VRAM:
llama.cpp automatically splits:
  • 40 layers on GPU
  • Remaining layers on CPU

Memory Optimization

Memory Mapping

Enable mmap (default, recommended):
Disable mmap (faster startup, more RAM):

Memory Locking

Prevent swapping (requires sufficient RAM):

Server Performance

Parallel Request Handling

Configuration guide:
  • --n-parallel: Number of simultaneous requests (2-8)
  • --threads: Threads per request (2-4 recommended)
  • --batch-size: Must be ≥ ctx-size × n-parallel

Continuous Batching

Enabled by default, improves throughput:

Platform-Specific Tips

Optimal configuration:
Multi-GPU:

Profiling and Monitoring

Built-in Performance Stats

Enable timing information:
Outputs:
  • Prompt evaluation time
  • Token generation time
  • Tokens per second

Server Metrics

Query server metrics endpoint:
Returns:
  • Request counts
  • Processing times
  • KV cache usage
  • Queue statistics

Benchmark Tool

Systematic performance testing:
Learn more about benchmarking →

Common Performance Issues

Likely causes:
  • Too many threads (oversaturation)
  • No GPU acceleration
  • Context size too large
Solutions:
  • Set --threads 1 and gradually increase
  • Enable GPU layers: --n-gpu-layers 32
  • Reduce context: --ctx-size 2048
Solutions:
  • Use smaller quantization (Q4_K_M instead of Q8_0)
  • Reduce context size: --ctx-size 1024
  • Reduce batch size: --batch-size 256
  • Offload fewer layers: --n-gpu-layers 20
  • Enable mmap: --mmap
Check:
  • Are layers offloaded? (check startup logs)
  • Is batch size large enough? Try 512 or 1024
  • Are you using optimal quantization? (Q4_K_M recommended)
Optimize:
Solutions:
  • Increase --n-parallel 8
  • Ensure batch size ≥ ctx-size × n-parallel
  • Reduce per-request threads: --threads 2
  • Enable continuous batching: --cont-batching

Advanced Optimizations

CPU Affinity

Bind threads to specific cores:

Process Priority

Increase process priority:
Levels: -1 (low), 0 (normal), 1 (medium), 2 (high), 3 (realtime)

Polling Level

Reduce latency with busy-waiting:
Range: 0-100 (0=no polling, 100=full busy-wait)

Next Steps

Quantization Guide

Learn about quantization types and tradeoffs

Backend Configuration

Configure GPU backends for your hardware

Benchmarking

Measure and compare performance

Server Tuning

Optimize server for production