TAU-HOME.COM
LOADING

llama.cpp CUDA Kernel Optimization: A 28-Line Change Boosts H100 Token Generation Speed by Up to 18.5%

Tensward has demonstrated that modifying just 28 lines of CUDA kernel code in llama.cpp improves H100 GPU memory bandwidth utilization, accelerating token gener

tau · November 27, 2026

#llama.cpp #CUDA #GPU #H100 #PerformanceOptimization

llama.cpp CUDA Kernel Optimization: A 28-Line Change Boosts H100 Token Generation Speed by Up to 18.5%

The open-source Large Language Model (LLM) inference framework llama.cpp is widely used to run artificial intelligence models efficiently across a diverse range of hardware, from consumer graphics cards to high-end enterprise servers. According to recent technical findings, a targeted modification of just 28 lines of CUDA kernel code for NVIDIA's high-performance H100 GPUs has successfully accelerated token generation speeds by up to 18.5%, drawing significant attention from the developer community.

llama.cpp CUDA kernel optimization code changes and H100 benchmark comparison screen

Image source: https://tensward.com/blog/llama-cpp-fast-memory

This optimization is particularly valuable because it precisely targets a hardware utilization bottleneck where software previously failed to fully saturate high-end memory bandwidth, all while maintaining exact mathematical parity. This article examines the background of the issue, the specific code modifications, benchmark results across 18 models, and the verified operational boundaries.

The Memory Bandwidth Bottleneck on H100 GPUs

When llama.cpp generates text on an NVIDIA GPU, the vast majority of execution time is spent reading model weights (the parameter values functioning as the model's neural network connections) from GPU memory. On most mainstream graphics cards, this reading phase runs as fast as the physical memory allows.

However, the flagship H100 enterprise accelerator behaved differently. While the card is capable of reading data at approximately 3.1 TB/s, this generation step only pulled about 1.5 TB/s. The GPU was effectively waiting on itself, utilizing only about half of the memory bandwidth potential.

The root cause lay in the granularity of data requests. The existing code assigned each group of GPU threads just one row of the weight matrix at a time. With extremely fast memory, this did not request enough data concurrently to keep the memory controllers fully occupied.

A 28-Line Fix: Prefetching and Dual-Row Reading in mmvq.cu

To resolve this limitation, the optimization patch modifies exactly 28 lines within the llama.cpp CUDA kernel file ggml/src/ggml-cuda/mmvq.cu. The core mechanics include:

  • Two Rows per Thread Group: Instead of taking a single row of weights at a time for every weight format, each group of threads now fetches two rows simultaneously—extending a pattern already utilized elsewhere in llama.cpp for multi-token generation or Jetson Orin boards.
  • Proactive Prefetching: Each thread group requests its weight row slightly before starting the actual computation, keeping the memory request queue constantly fed.
  • Bit-for-Bit Identical Output: The underlying arithmetic remains unchanged. Every row is accumulated by the same threads in the exact same sequence, ensuring that output results match the unpatched version bit for bit.

This change supplies fast memory with more parallel work requests to process at any given moment. On slower memory cards where bandwidth is already saturated, the change is entirely neutral, introducing neither performance gains nor regressions.

Benchmark Results Across 18 Models

Testing was conducted on a single H100 across 18 diverse models, with two rounds of evaluation per model using standardized benchmarks (llama-bench -p 0 -n 128 -r 5 -fa 1). Every single model experienced acceleration without regressions.

Key performance improvements include:

  • gemma-3-1b Q8_0: Reduced from 2.120 ms to 1.728 ms per token, achieving an 18.5% speedup.
  • Phi-3.5-mini Q8_0: Reduced from 3.609 ms to 3.019 ms, a 16.3% speedup.
  • Qwen3-32B Q4_K_M: Reduced from 13.338 ms to 11.365 ms, a 14.8% speedup.
  • Qwen3-1.7B Q8_0: 13.7% speedup.
  • Qwen3-8B Q4_K_M: 13.2% speedup.
  • Qwen3.8-27B UD-Q4_K_M: Reduced from 11.539 ms to 10.468 ms, a 9.3% speedup.
  • Qwen3-14B Q6_K: 3.2% speedup.

Across all evaluated positions, token probabilities and top-8 alternatives matched the unpatched version precisely.

Limitations and Future Outlook

While highly effective, this optimization has distinct boundaries:

  • No Effect on Slower Memory Cards: On cards with slower memory bandwidth like the NVIDIA L4, the change produced no measurable difference, confirming it is harmless where inapplicable.
  • Large 8-bit Matrix Exceptions: For large 8-bit matrices where processing two rows simultaneously proved slightly slower, the original single-row execution path was retained.
  • Further Room for Improvement: For instance, on a 27B model, theoretical weight reading limits suggest a floor of roughly 4.9 ms per token, whereas actual performance improved from 11.5 ms to 10.5 ms. This represents a meaningful step forward rather than the final hardware limit.

Consumer cards featuring fast memory (such as the RTX 4090 and RTX 5090) utilize the same code path, making them prime candidates for similar speedups in real-world deployment.

Sources

Impact-Site-Verification: 9b3ebd10-6c45-44cf-a014-5b8c19b7b285