GPU layer offloading decides how many transformer layers of a local model live in accelerator memory instead of system RAM. In llama.cpp, the control is explicit: -ngl, --gpu-layers, or --n-gpu-layers accepts an exact layer count, auto, or all, and defaults to auto when omitted. In Ollama, offloading is generally automatic: the runtime auto-detects the GPU and, in Ollama 1.0+, auto-splits across multiple GPUs. This guide explains how to set, verify, and troubleshoot layer offloading in both runtimes, with the practical caveat that Ollama exposes fewer per-run layer controls than llama.cpp.
What GPU layer offloading changes
A large language model is a stack of layers. When a layer is offloaded, its weights and computations are placed on the GPU; layers that are not offloaded run on the CPU. The goal is not simply to move everything to the GPU. The model weights, the context cache (also called the KV cache), and runtime overhead all compete for VRAM. If you offload too many layers, the model may fail to load or leave too little VRAM for a long context. If you offload too few, you may leave GPU capacity unused and get slower generation.
The llama.cpp documentation notes that the layer setting matters when the default fit leaves too little VRAM for the context cache, when a model fails to load, or when a smaller partial offload gives a steadier run. The same layer setting is available to llama-server and llama-cli as -ngl, --gpu-layers, or --n-gpu-layers.
Prerequisites: a visible GPU backend
Layer offloading only has an effect when the llama.cpp binary can see a GPU backend such as Metal, CUDA, HIP, Vulkan, or SYCL. Before tuning layer counts, confirm that the installed binary exposes the current GPU-layer options and can list a device.
llama-server --help
llama-server --list-devicesOn a machine with a visible accelerator, the device list might show something like Metal0: Apple M2 Pro. An empty list after Available devices: means the binary has no visible accelerator on the current host. In that case, fix the backend build, container GPU pass-through, driver, or device selection before changing --n-gpu-layers.
Configuring llama.cpp layer offload
The most direct way to configure offloading is to run llama.cpp yourself. The following steps use llama-server, but the same flag works with llama-cli.
1. Confirm the flag and its default
Run llama-server --help and look for the GPU-layer option. Current builds accept an exact number, auto, or all, with auto as the default when the option is omitted. If these options are missing, update llama.cpp or rebuild it with the backend needed for the target GPU.
2. Start with a conservative explicit count
Rather than starting with all, begin with a number that fits both the model weights and the context cache. For example:
llama-server -m models/gemma-3-1b-it-Q4_K_M.gguf --n-gpu-layers 20 --host 127.0.0.1 --port 8080This is a suggested starting point, not a universal value. The correct number depends on model size, quantization, context length, and available VRAM.
3. Find how many layers the model has
To offload everything, you need to know the layer count or use a value larger than the total. A common approach is to inspect the model:
llama-cli -m model.gguf --verbose-prompt 2>&1 | grep 'n_layer'For example, Mistral 7B has 33 layers, so -ngl 33 offloads all layers, while -ngl 99 is a convenient way to say ‘offload everything’ in many builds.
4. Tune against VRAM and context size
Context size is a major VRAM consumer. In llama.cpp, -c 8192 uses more memory than -c 4096, and the extra memory comes from the context cache. If the model fails to load after increasing context, reduce -ngl or reduce -c. If generation is slow and VRAM is still free, increase -ngl.
Other flags matter for stability. -t sets thread count and should generally match physical cores. --mlock pins memory to prevent swapping, and --no-mmap can help when memory mapping causes issues. These flags do not replace layer tuning, but they affect whether an offload configuration runs steadily.
5. Verify the result
After launch, check the server or CLI logs for the number of layers offloaded and for any out-of-memory or fallback messages. Then compare generation speed and GPU memory use against a known baseline, such as -ngl 0 for CPU-only. A successful configuration loads without errors, keeps the context cache in memory, and uses the GPU without forcing the system to swap.
Configuring Ollama offload behavior
Ollama is designed to hide most layer scheduling. The practical guide from lllm.dev describes Ollama as auto-detecting the GPU: you can simply run ollama run mistral:7b. For larger models, Ollama 1.0+ auto-splits across GPUs, so a command such as ollama run llama4:70b can use multiple GPUs without a manual layer count.
That convenience has a tradeoff: Ollama does not expose the same per-run -ngl flag as llama.cpp. If you need deterministic control over the exact number of layers on each GPU, run llama.cpp directly or use a server wrapper that exposes its flags.
Some Ollama builds accept environment variables for GPU-related behavior. For example, OLLAMA_NUM_GPU has been used in some versions to set the number of layers to offload, and OLLAMA_GPU_OVERHEAD can reserve VRAM. Because these variables are version-dependent, check ollama serve --help, the release notes, or the documentation for your installed build before relying on them. An illustrative command, if your build supports it, is:
OLLAMA_NUM_GPU=20 ollama serveWhen manual layer control is unavailable or unreliable, influence Ollama indirectly:
- Choose a quantization that fits. The Q4_K_M format is often described as the sweet spot: it halves the size of a 16-bit model with small quality loss. Q8_0 is larger but nearly lossless; Q3_K_M and Q2_K save more space with noticeable or significant quality loss.
- Reduce context length. A larger context needs more memory. If Ollama offloads too aggressively and fails, lower the context or use a smaller model.
- Check GPU detection. If Ollama runs on CPU only, confirm the driver and backend are visible. The same device-visibility checks that apply to llama.cpp apply here.
- Use multi-GPU only when it helps. Auto-splitting can make a 70B model runnable, but it also introduces inter-GPU communication. For some setups, a CPU-heavy configuration with enough RAM may be steadier.
Troubleshooting common offload failures
| Symptom | Likely cause | Check or fix |
|---|---|---|
| Model loads but runs on CPU | No visible GPU backend, driver issue, container pass-through problem, or device not selected | Run --list-devices; if the list is empty, fix the backend build, driver, container GPU pass-through, or device selection before tuning layers. |
| Out-of-memory at load | Too many layers offloaded, context cache too large, or other processes using VRAM | Reduce -ngl, reduce -c, close other GPU workloads, or use a smaller quantization. |
| Model fails only with long context | Weights fit, but the context cache does not | Lower context size or lower -ngl to free VRAM for the cache. |
| Slow generation despite GPU use | Partial offload, PCIe transfer overhead, or CPU bottleneck | Compare -ngl 0, a partial count, and all; increase offload only if VRAM allows and speed improves. |
| Ollama ignores manual layer setting | Version does not support the environment variable, or automatic scheduling overrides it | Check your Ollama version and help output; for exact control, use llama.cpp. |
A reproducible way to choose -ngl
No single layer count is correct for every machine. A useful experiment is to hold everything else constant and vary only the offload. Use the same model, quantization, context length, prompt, sampling settings, and thread count. Then run at least three configurations: -ngl 0 for CPU-only, a conservative partial count, and all or a number above the model’s layer count. Record whether the model loads, peak VRAM use, and tokens per second. This article does not report benchmark scores because no test measurements were supplied; the value of the protocol is that it produces comparable numbers on your own hardware.
Tradeoffs and limits
More GPU offload is not automatically better. Offloading every layer can starve the context cache, cause an out-of-memory failure, or create transfer overhead that outweighs the benefit. Partial offload can be the steadier choice when VRAM is tight. Quantization also changes the calculus: Q4_K_M is a common balance, but lower-bit formats such as Q3_K_M and Q2_K trade more quality for memory savings. For very large models, CPU-only is not automatically unusable. The lllm.dev guide notes that a 70B Q4 model can work on CPU with 64GB+ RAM, and that IQ3_XXS can be used to fit a 70B model on 32GB RAM. Multi-GPU auto-splitting is another option, but it depends on the runtime and the hardware topology.
Practical next steps
Start by confirming your GPU is visible to the runtime. In llama.cpp, run --list-devices, then try the default auto setting before forcing an exact layer count. If you need to tune, find the model’s layer count, begin with a conservative --n-gpu-layers value, and adjust downward if you hit VRAM limits or upward if the GPU is underused. In Ollama, accept automatic offload as the default, choose a quantization that fits your hardware, and switch to llama.cpp when you need deterministic layer control. If you share your environment, model, quantization, context length, layer setting, VRAM, and observed error or speed in the community, others can compare their results and help narrow down the right configuration.
FAQ
Does Ollama support the same -ngl flag as llama.cpp?
Ollama generally manages offloading automatically and does not expose per-run -ngl in the same way. For exact layer control, use llama.cpp or check whether your Ollama version supports a relevant environment variable.
How do I know how many layers to offload?
Start with auto or a conservative explicit count. Find the model's layer count, then reduce the number if you hit VRAM limits or long-context failures, and increase it if the GPU is underused.
Why does llama.cpp say no devices are available?
An empty device list means the binary cannot see a GPU backend. Fix the backend build, driver, container GPU pass-through, or device selection before tuning layer counts.
Can offloading all layers hurt performance?
Yes. Offloading everything can starve the context cache, cause an out-of-memory failure, or add transfer overhead. Partial offload can be steadier when VRAM is tight.
What if Ollama ignores OLLAMA_NUM_GPU?
The variable may be version-dependent or overridden by automatic scheduling. Check your Ollama version and help output, and use llama.cpp if you need deterministic layer control.