skip to content

When building llama.cpp for an NVIDIA, AMD, Intel or Vulkan-capable GPU, which CMake flags select the ggml backend, and how do you confirm it?

level: middleimportance: should knowfreq 6%

answer

  1. the backend is chosen at compile time
  2. one -DGGML_* switch per backend
  3. Metal needs no flag on macOS
  4. CMAKE_ARGS for the pip install
  5. --list-devices and llama-bench's backend column

basics

~10 s

The backend is compiled in: -DGGML_CUDA=ON for NVIDIA, -DGGML_HIP=ON for AMD ROCm, -DGGML_SYCL=ON for Intel GPUs, -DGGML_VULKAN=ON for Vulkan; Metal is on by default on macOS. Confirm with --list-devices or llama-bench's backend column.

solid answer

~40 s

llama.cpp's GPU support is a **build-time** choice of ggml backend. `cmake -B build -DGGML_CUDA=ON` builds CUDA, `-DGGML_HIP=ON` (optionally with `-DGPU_TARGETS=gfx1030`) builds ROCm for AMD, `-DGGML_SYCL=ON` with the Intel `icx`/`icpx` compilers targets Intel GPUs, and `-DGGML_VULKAN=ON` builds the portable Vulkan backend; on macOS Metal is enabled unless you pass `-DGGML_METAL=OFF`. Several backends can be built together and picked at run time with `--device`. To confirm, run `llama-server --list-devices` or `llama-bench`, whose `backend` column says `CPU` when no GPU backend made it in. The bindings take the same flags: `CMAKE_ARGS="-DGGML_CUDA=on" pip install llama-cpp-python` (reinstall with `--force-reinstall --no-cache-dir` to rebuild), while node-llama-cpp ships prebuilt CUDA, Vulkan and Metal binaries and detects them.

code

bash · 13 lines
bash
# NVIDIA: CUDA backend, pinned to two compute capabilities
cmake -B build -DGGML_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES="86;89"
cmake --build build --config Release

# Vulkan backend (any vendor with a Vulkan driver)
# cmake -B build -DGGML_VULKAN=ON

# Prove the backend is there before tuning anything
./build/bin/llama-server --list-devices
./build/bin/llama-bench -m ./models/your-model.gguf   # check the backend column

# Same switch for the Python binding, forcing a rebuild
CMAKE_ARGS="-DGGML_CUDA=on" pip install --upgrade --force-reinstall --no-cache-dir llama-cpp-python

go deeper

for a junior

Remember that the GPU backend is chosen when llama.cpp is compiled, one -DGGML_ switch per backend, and that Metal is already on for Macs.

for a middle

Explain the CUDA, HIP, SYCL and Vulkan switches, how llama-cpp-python receives them through CMAKE_ARGS, and three ways to verify a build: --list-devices, llama-bench's backend column and the load log.

for a senior

Diagnose a silently CPU-bound deployment: a stale cached wheel, a binary built on another machine's architecture, or a backend present but outranked, and prove the fix with a benchmark.

for a principal

Decide between native per-machine builds, GGML_NATIVE=OFF portable builds and GGML_BACKEND_DL for a mixed fleet, weighing binary size, startup compilation and who maintains the build matrix.

## Why the GPU is a build decision llama.cpp does its math through **ggml**, a tensor library with one **backend** per kind of hardware: CPU, CUDA, HIP (ROCm), SYCL, Vulkan, Metal and others. A backend is C/C++ or shader code that has to be **compiled in**. A llama.cpp binary built without the CUDA backend cannot use an NVIDIA card no matter which runtime flags you pass, and it will quietly run on the CPU. That is why the first question about a slow GPU box is "which backend was this built with?", not "which flag did you forget?". ## The CMake switches | Hardware | CMake switch | Notes from the build guide | |---|---|---| | NVIDIA | `-DGGML_CUDA=ON` | needs the CUDA toolkit; `CMAKE_CUDA_ARCHITECTURES="86;89"` pins compute capabilities | | AMD (ROCm) | `-DGGML_HIP=ON` | `-DGPU_TARGETS=gfx1030` is optional; omitted, it builds for the GPUs in the machine | | Intel GPUs | `-DGGML_SYCL=ON` | built with the oneAPI compilers, `-DCMAKE_C_COMPILER=icx -DCMAKE_CXX_COMPILER=icpx` | | Any Vulkan driver | `-DGGML_VULKAN=ON` | needs the Vulkan SDK; covers NVIDIA, AMD and Intel through one API | | Apple Silicon | none | Metal is on by default on macOS; `-DGGML_METAL=OFF` removes it | A few build options change **where** the result can run: - By default llama.cpp is built for the hardware connected at build time. `-DGGML_NATIVE=OFF` produces a CUDA build that runs on all CUDA GPUs, at the price of a bigger binary and some just-in-time compilation. - Backends can be combined, for example `-DGGML_CUDA=ON -DGGML_VULKAN=ON`, and chosen at run time with `--device`. - `GGML_BACKEND_DL` builds each backend as a dynamic library loaded at run time, so one llama.cpp binary can serve machines with different GPUs. ## Confirming what you built 1. **List devices.** `llama-server --list-devices` (also accepted by `llama-bench`) prints the devices the compiled backends can see and exits. A list without your GPU means the backend is missing or the driver is not visible. 2. **Benchmark once.** `llama-bench -m your-model.gguf` prints a table whose `backend` column names the backends in the binary, such as `CUDA`, or `CPU` when there are none, and a `build:` line with the commit. 3. **Read the load log.** The `load_tensors:` lines report a model buffer size per device, so you can see how much of the model landed on the GPU and how much stayed in CPU memory. Two traps sit next to this: - `-ngl 0` does not guarantee CPU-only execution: the build guide notes the GPU may still accelerate parts of the computation. `--device none` disables GPU use entirely. - A Vulkan build and a CUDA build on the same NVIDIA card are both "GPU builds" and can still perform very differently, so compare them with the same benchmark rather than assuming. ## The same choice inside the bindings - **llama-cpp-python** compiles llama.cpp during `pip install`, so the backend is fixed then: `CMAKE_ARGS="-DGGML_CUDA=on" pip install llama-cpp-python`, or `-DGGML_HIP=on`, `-DGGML_VULKAN=on`, `-DGGML_SYCL=on`, `-DGGML_METAL=on`. Any llama.cpp CMake option passes the same way, or through `-C cmake.args=...`. Pip caches built wheels, so changing `CMAKE_ARGS` later needs `--upgrade --force-reinstall --no-cache-dir`. `llama_cpp.llama_supports_gpu_offload()` returns `False` on a CPU-only build, and `verbose=True` prints the system info at load. Prebuilt CUDA, Metal, ROCm and Vulkan wheels also exist on separate package indexes. - **node-llama-cpp** ships prebuilt binaries and `getLlama()` picks Metal, CUDA or Vulkan when it detects support. `npx --no node-llama-cpp inspect gpu` shows what it found, and `npx --no node-llama-cpp source download --gpu cuda` builds from source when a prebuilt binary does not match your toolkit; extra CMake options go in environment variables prefixed `NODE_LLAMA_CPP_CMAKE_OPTION_`. ## What to say in the interview Name the switch for the hardware, say that Metal needs none, mention `GGML_NATIVE=OFF` or `GGML_BACKEND_DL` when the binary must travel to other machines, and show how you would **prove** the backend is present before tuning anything else. Choosing which GPU to buy is a separate question; this one is only about making the binary use the GPU you have.

  • How do you make one llama.cpp build run on machines with different GPUs?
    Build each backend as a dynamically loaded library with `GGML_BACKEND_DL`, so the binary loads whichever backend the machine supports at run time. For CUDA alone, `-DGGML_NATIVE=OFF` or an explicit `CMAKE_CUDA_ARCHITECTURES` list avoids a binary tuned only to the build machine's card.
  • A llama-cpp-python service ignores the GPU even after you reinstalled it with CMAKE_ARGS set. What do you check?
    Whether pip reused a cached CPU wheel: reinstall with `--upgrade --force-reinstall --no-cache-dir` and add `--verbose` to see the CMake log. Then call `llama_cpp.llama_supports_gpu_offload()`; if it is `True`, the likely remaining cause is the `Llama` constructor's own `n_gpu_layers=0` default.
  • Why can a llama.cpp run with -ngl 0 still touch the GPU?
    With a GPU backend compiled in, the build guide notes that some computation may still be accelerated on the GPU even when no layers are stored there. To keep a run entirely on the CPU, pass `--device none` or build without the higher-priority backend, for example `-DGGML_METAL=OFF` on a Mac.

  • Thinks passing -ngl to a CPU-only build makes it use the GPU.
  • Says a -DGGML_METAL flag is required to get GPU support on a Mac.
  • Changes CMAKE_ARGS and reinstalls llama-cpp-python without forcing a rebuild.
  • Assumes -ngl 0 guarantees the run never touches the GPU.
  • Treats Vulkan and CUDA builds on one NVIDIA card as interchangeable in speed.