Build llama.cpp for your NVIDIA card and make long prompts read faster
At the end of this you will have a llama.cpp server built for your own NVIDIA card, spread over every card you have, and set up to read long prompts quickly. It is for anyone with an NVIDIA GPU, and it pays special attention to older cards that the newest CUDA toolkit no longer supports.
Requirements: One or more NVIDIA GPUs with a working driver · Linux · CUDA 12 toolkit for Volta and older (CUDA 12 or 13 for Turing and newer) · cmake, a C++ compiler, git · a current llama.cpp release
Requirements
- One or more NVIDIA cards and a working NVIDIA driver.
- The compute capability of each card, from NVIDIA's list.
- A CUDA toolkit that still supports that compute capability.
- cmake, a C++ compiler and git.
- A GGUF model file and enough VRAM, or VRAM plus RAM, to hold it.
Steps
1. Pick the CUDA toolkit for your card generation
CUDA 13.0 removed offline compilation and library support for Maxwell, Pascal and Volta cards (compute capability 5.x, 6.x and 7.0), according to NVIDIA's CUDA features archive and CUDA 13.0 release notes. If your card is one of those, install a CUDA 12 toolkit. Turing (7.5) and newer cards can use either. Install the toolkit in its own directory rather than replacing a system one, so other software is not affected.
2. Get the source
Clone the project and check out a tagged release, so you know which version you are running.
git clone https://github.com/ggml-org/llama.cpp
cd llama.cpp
git checkout <release-tag>
3. Build with CUDA for your card
Set CMAKE_CUDA_ARCHITECTURES to your card's compute capability without the dot, for example 70 for a Tesla V100 or 86 for an RTX 3090. That keeps build time down. For mixed cards, list each one, for example "75;86". If you have several toolkits, point cmake at the right nvcc with CMAKE_CUDA_COMPILER, as the build docs describe.
cmake -B build -DGGML_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES=70 \
-DCMAKE_CUDA_COMPILER=<cuda-12-dir>/bin/nvcc
cmake --build build --config Release -j 8
Set -j to your core count. When the toolkit sits in its own directory, make its libraries findable before you start the server:
export LD_LIBRARY_PATH=<llama-dir>/build/bin:<cuda-12-dir>/lib
4. Split the model over your cards
With -ngl all every layer goes to the GPUs, and the default split mode, layer, gives each card a run of whole layers. Add -ts (for example -ts 3,1) only when the cards differ in size. -c sets the context, -np the number of request slots, and --kv-unified lets the slots share one KV buffer.
llama-server -m <models-dir>/model-00001-of-00004.gguf \
-ngl all -c 65536 -np 4 --kv-unified --metrics
Add the sampling settings your model's card recommends, such as --temp, --top-p, --top-k and --min-p.
5. Raise the batch size for long prompts
-b is the logical batch (default 2048) and -ub the physical batch the GPU works on at once (default 512). Raising -ub lets each pass read more of the prompt, at the cost of a larger compute buffer in VRAM. --load-mode none reads the file into memory with no special loading mode instead of memory-mapping it, which can shorten start-up for a large model.
llama-server -m <models-dir>/model-00001-of-00004.gguf \
-ngl all -c 65536 -np 4 --kv-unified --metrics \
--load-mode none -b 4096 -ub 4096
On three Tesla V100s, as one example, batch 4096 cut a cold read of 24,340 tokens from 88.2 s to 59.8 s, with batch 2048 giving most of that gain (our tuning test). Your cards will differ, so measure (step 7).
6. Leave the prompt cache on
The server keeps earlier conversations and reuses the shared start of a new request, so only the new part is read. It is on by default (--cache-prompt), and --cache-ram caps its size in MiB (default 8192). Change it only if you want a different cap.
7. Measure with llama-bench
llama-bench runs a prompt-reading test (pp) and a generation test (tg) and prints tokens per second, so you can compare batch sizes without a client. Comma-separated values run one test per value.
llama-bench -m <models-dir>/model-00001-of-00004.gguf -ngl 99 \
-p 4096 -n 128 -ub 512,2048,4096 -r 3
Check it worked
The llama-bench README shows its output as a table with one row per test, for example:
| model | size | params | backend | ngl | test | t/s |
| llama 7B mostly Q4_0 | 3.56 GiB | 6.74 B | CUDA | -1 | tg 128 | 132.19 ± 0.55 |
Compare the pp rows across -ub values: a larger batch should give a higher t/s for prompt reading. The backend column should say CUDA.
On the server, look at the timing lines printed after each request: prompt eval time is prompt reading and eval time is generation, both with tokens per second. With the prompt cache working, a follow-up message in a long conversation should read only a few tokens. Watch VRAM with nvidia-smi while a long prompt is read; a larger batch uses more.
Common problems
- The build fails with CUDA 13 on a compute capability 7.0 or older card: the toolkit can no longer target it. Use CUDA 12.
- The server will not start with a missing-library error:
LD_LIBRARY_PATHdoes not include the toolkit'slibdirectory (step 3). - A large batch fails to allocate its compute buffer at start: the first card is too full. Step down to
-b 2048 -ub 2048, or shorten-c. - You are tempted by
GGML_CUDA_FORCE_MMQ: the build docs describe it only as forcing custom matrix kernels for quantised models instead of FP16 cuBLAS. Measure with llama-bench before keeping it.
Our recommendation
On any card with room to spare, we'd set -b and -ub to 2048 or 4096 for agent and coding work, where long prompts are common. We'd pick 2048 if the cards are near full, because in our tuning test it gave most of the gain for less VRAM. We'd set --load-mode none for a faster start with a large file. We would not switch the KV cache to 8 bits just to save memory without a quality check, because in that test it changed the output. If long prompts dominate your use, compare engines too: see our comparison.