Skip to content

How-ToCPU

How to Fix Slow Token Generation in llama.cpp

How to fix slow llama.cpp text generation by confirming GPU offload with -ngl and setting -t to your physical core count or lower, with the llama.cpp developers' own example showing the effect of each setting.

By , Systems & Technical Editor

2 min read

How to Fix Slow Token Generation in llama.cpp

If llama.cpp is producing text far more slowly than you expected, the cause is often one of two settings: the model isn't being offloaded to your graphics card, or llama.cpp is using too many CPU threads. The llama.cpp project's own performance troubleshooting notes cover both. cpp developers' own example.

A note on commands: the llama.cpp troubleshooting page uses ./llama-cli, while the current llama.cpp README uses llama cli and llama serve (see our llama.cpp 1.0 news story for what changed in 1.0). The -ngl and -t flags described below are the ones in the troubleshooting page; check --help on your version for the exact names.

Step 1: Make sure the model is on the GPU

The llama.cpp notes say it must be compiled with GPU support, such as CUDA for NVIDIA cards, for the GPU offload flag to work. Our guide to running llama.cpp on Windows covers installation.

Then pass -ngl N (or --n-gpu-layers N). The llama.cpp notes say you can set N very high and llama.cpp will offload as many layers as it can, even if that's fewer than you asked for. Before it starts generating, llama.cpp prints diagnostic lines; the example in the docs shows the number of layers offloaded to the GPU and the total VRAM used. If you see those lines, the GPU is being used.

If only some layers fit, the rest run from system memory and speed drops. A smaller quantisation or a shorter context helps; our guide to running a local LLM on 8GB of VRAM has more.

Step 2: Don't oversaturate the CPU

The -t N (or --threads N) option sets how many CPU threads llama.cpp uses. The llama.cpp developers say it's extremely important that this isn't set too high. Their advice:

  1. If generation is extremely slow, try -t 1.

  2. If that significantly improves speed, your CPU was oversaturated. Set -t to the number of physical CPU cores, even if you're using a GPU.

  3. If in doubt, start at 1 and double the number until speed stops improving, then scale back.

What the developers' example shows

llama.cpp's notes include a run of a 30B 4-bit model on a machine with an RTX A6000 (48GB) and 7 physical CPU cores:

Flags

Tokens per second

-t 7 (CPU only)

1.7

-t 1 -ngl 2000000

5.5

-t 7 -ngl 2000000

8.7

-t 4 -ngl 2000000

9.1

Offloading to the GPU made the biggest difference, and with full offload four threads were slightly faster than seven. Your results will differ with other hardware and models.

Still slow?

Compare with llama.cpp vs Ollama if you'd rather have a tool that sets offload automatically, and see our guide to speeding up local LLMs for more ideas.

Our verdict

Check that llama.cpp reports layers offloaded to the GPU with -ngl, then set -t to your physical core count or lower. The llama.cpp developers' own example shows those two settings can make generation several times faster.

The bottom line

Check that llama.cpp reports layers offloaded to the GPU with `-ngl`, then set `-t` to your physical core count or lower. The llama.cpp developers' own example shows those two settings can make generation several times faster.

Sources

  1. 01llama.cpp, Token generation performance troubleshooting
  2. 02llama.cpp, README (current commands)

About the author

Marcus Thorne

Systems & Technical Editor

Marcus writes Forge PC's step-by-step build, troubleshooting and system optimisation guides. His background spans Windows kernel and power-state diagnostics, UEFI/BIOS configuration, NVMe storage arrays, ATX 3.1 power delivery and running local AI and LLM workloads on consumer graphics cards.

More from Marcus Thorne

Read next

How to Run Multiple Models at Once in Ollama

How-To

How to Run Multiple Models at Once in Ollama

How Ollama keeps several models loaded and handles parallel requests, with the defaults for OLLAMA_MAX_LOADED_MODELS, OLLAMA_NUM_PARALLEL and OLLAMA_MAX_QUEUE, how to change them, and how Ollama places models across multiple GPUs.

Marcus Thorne3 min

How to Check a Driver Version in Windows 11

How-To

How to Check a Driver Version in Windows 11

How to check any device's driver version in Windows 11 through Device Manager's Driver tab, and how to see an NVIDIA graphics driver's version by typing nvidia-smi in Terminal.

Marcus Thorne2 min

How to Choose Which GPU Ollama Uses (or Force CPU)

How-To

How to Choose Which GPU Ollama Uses (or Force CPU)

How to make Ollama use a specific graphics card with CUDA_VISIBLE_DEVICES (NVIDIA), ROCR_VISIBLE_DEVICES (AMD ROCm) or GGML_VK_VISIBLE_DEVICES (Vulkan), how to force CPU-only with an invalid ID, and how to check the result with ollama ps.

Marcus Thorne2 min