New Tool Radar

Local AI

Set How Many Model Layers Run on the GPU in llama-server

Control GPU offload in llama-server with --n-gpu-layers, from full offload down to CPU-only, and read the load log.

Schematic with three steps: Start the server with full offload; Read the load log; Drop to CPU-only when needed.

The –n-gpu-layers flag decides how many transformer layers run on the GPU and how many fall back to the CPU. Setting it high pushes the whole model to the GPU when VRAM allows; setting it to zero forces CPU-only inference.

What you need

  • llama.cpp built or installed, with llama-server available
  • A GGUF model file on disk
  • A GPU with a supported backend, for GPU offload

Step 1: Start the server with full offload

Run llama-server -m model.gguf –n-gpu-layers 999 –port 8080. The value 999 means offload as many layers as fit; the server prints how many layers actually landed on the GPU in its startup log.

Step 2: Read the load log

Look for the layer offload lines in the server output. Every layer offloaded is one less layer computed on the CPU, and the log also reports the memory the model and KV cache use.

Step 3: Drop to CPU-only when needed

Run the same command with –n-gpu-layers 0 to disable GPU inference entirely. Use this to compare speeds, or on a machine where the GPU backend fails to load.

Worked example: offload all layers of a 7B model

The table below is a synthetic example. The flag and the zero value come from the llama.cpp build docs and guides; the layer counts are illustrative.

Setting Value Effect
Full offload –n-gpu-layers 999 All layers on GPU if they fit
Partial –n-gpu-layers 20 20 layers on GPU, rest on CPU
CPU only –n-gpu-layers 0 No GPU inference

Expected result (UNTESTED). with 999 the load log reports all layers offloaded and generation is fast; with 0 the same prompt runs on CPU only. This check was not run live; the flag behaviour comes from the cited docs.

Limits

  • Layers that do not fit in VRAM fall back to CPU automatically and slow generation.
  • The flag is spelled –n-gpu-layers in current llama.cpp; older guides use -ngl.
  • Metal builds need a native arm64 binary; an x86 build under Rosetta ignores the GPU.
  • KV cache memory grows with context length on top of the weights.

Sources

See all