Making llama.cpp generate faster on fast-memory GPUs
A 28-line change that makes token generation 3–18% faster on an H100, with output identical bit for bit.
- H100 · llama.cpp b12603 · 18 models
- llama.cpp
- CUDA
- H100
- memory bandwidth
The problem
When llama.cpp generates text on an NVIDIA GPU, almost all the time goes into one step: reading the model’s weights from GPU memory. On most cards that step runs as fast as the memory allows. On an H100 it doesn’t. The card can read about 3.1 TB/s, but this step only pulls about 1.5 TB/s. The GPU is waiting on itself.
The idea
The code gives each group of GPU threads one row of the weight matrix at a time. On very fast memory, that doesn’t ask for enough data at once to keep the memory busy: the memory could deliver more, but nobody has asked for it yet.
llama.cpp already uses a fix for this elsewhere: when it handles 2 to 8 tokens at a time, and on the Jetson Orin board, each group takes two rows instead of one. We tried the same thing for normal one-token generation.
What we changed
Two small changes in one file (ggml/src/ggml-cuda/mmvq.cu, 28 lines):
- Each group of threads takes two rows instead of one, for every weight format. The exception is large 8-bit matrices, where two rows were slightly slower.
- Each group asks for its row of weights a moment before it starts computing.
The math itself is unchanged: every row is added up by the same threads in the same order. So the output is exactly the same, bit for bit.
The numbers
One H100, 18 models, two rounds each, repeated on a second machine for five of them.
llama-bench -p 0 -n 128 -r 5 -fa 1, two rounds per model.The same numbers as a table
| Model | Stock, ms per token | With the change | Less time |
|---|---|---|---|
| gemma-3-1b Q8_0 | 2.120 | 1.728 | 18.5% |
| Phi-3.5-mini Q8_0 | 3.609 | 3.019 | 16.3% |
| Qwen3-32B Q4_K_M | 13.338 | 11.365 | 14.8% |
| Qwen3-1.7B Q8_0 | 1.936 | 1.671 | 13.7% |
| Qwen3-0.6B Q8_0 | 1.358 | 1.173 | 13.6% |
| Qwen3-8B Q4_K_M | 3.956 | 3.433 | 13.2% |
| Qwen3-4B Q8_0 | 3.583 | 3.164 | 11.7% |
| LFM2-8B-A1B Q4_K_M | 1.558 | 1.399 | 10.2% |
| GLM-4-9B Q4_K_M | 4.480 | 4.049 | 9.6% |
| granite-3.3-8b Q4_K_M | 4.245 | 3.840 | 9.5% |
| granite-3.3-2b Q4_K_M | 2.468 | 2.239 | 9.3% |
| Qwen3.8-27B UD-Q4_K_M | 11.539 | 10.468 | 9.3% |
| Qwen3-30B-A3B Q4_K_M | 3.373 | 3.071 | 9.0% |
| gemma-4-E4B Q4_K_M | 4.403 | 4.042 | 8.2% |
| Qwen3-4B Q4_0 | 2.687 | 2.478 | 7.8% |
| Inkling-2-Mini IQ4_NL | 13.744 | 13.027 | 5.2% |
| Qwen3.8-27B Q8_0 | 14.579 | 13.982 | 4.1% |
| Qwen3-14B Q6_K | 6.944 | 6.722 | 3.2% |
- No model got slower in any round.
- The output matched the unpatched version exactly on all 18 models (the probabilities of the chosen token and the top 8 alternatives, at every position checked).
- On a slower-memory card (an L4, one model) it made no difference, which suggests it’s harmless where it doesn’t help.
- With 8 requests at once there’s no change. That path already uses two rows.
What’s still open
This closes only part of the gap. On a 27B model the weights alone would allow about 4.9 ms per token; we went from 11.5 to 10.5.
We haven’t tested consumer cards with fast memory (RTX 4090 / 5090). They use the same code path, so they’re the interesting case. If you have one, I’d love to see your numbers: run stock and the patched branch with the llama-bench line above.
The patch and benchmarks were made with help from an AI assistant.