Ollama GPU and Performance Optimization
When a model runs slowly, 90% of the causes can be found in a single line of ollama ps. This chapter goes from troubleshooting methods to hardware support, multi-GPU allocation, and then to two advanced switches: Flash Attention and KV cache quantization.
There is only one goal: make the most of your graphics card.
The first step is always: confirm where the model is running
The first action in performance troubleshooting is to look at the PROCESSOR column, which directly tells you where the model weights are loaded.
$ ollama ps
NAME ID SIZE PROCESSOR CONTEXT UNTIL
qwen3.5:27b a8b2c9d3e4f5 20.4GB 52%/48% CPU/GPU 4 minutes from now
Three states correspond to completely different situations:
| PROCESSOR shows | Meaning | Expected speed |
|---|---|---|
| 100% GPU | All weights loaded into VRAM | Full GPU speed |
| 100% CPU | VRAM cannot hold it at all, pure CPU-memory inference | One to two orders of magnitude slower |
| 52%/48% CPU/GPU | Insufficient VRAM, weights are split | Noticeable drop, bottlenecked by cross-device transfer |
It is more intuitive when viewed together with generation speed: eval_count / eval_duration * 10^9 at the end of the API response is token/s (see later in the API chapter for usage); use this number as the quantitative yardstick for each optimization.
When encountering a slow model, follow the decision path below:
GPU acceleration support by platform
Ollama supports four major acceleration backends; installing the right driver is a prerequisite.
| Platform | Backend | Requirement |
|---|---|---|
| NVIDIA | CUDA | Compute capability 5.0+, driver 550+ (older 5.0~6.2 cards need 570+) |
| AMD(Linux) | ROCm v7 | Requires ROCm v7 driver; older drivers cause discovery timeout and fallback to CPU |
| AMD(Windows) | ROCm v7 / Vulkan | ROCm v7 or HIP7 driver stack; some RX 6000 series use Vulkan as fallback |
| Apple Silicon | Metal | Works out of the box, no configuration needed |
| Intel / others | Vulkan | Supplementary backend enabled by default, covering older AMD cards and Intel GPUs |
To check whether the GPU is recognized, the discovery records in the log are most convincing; NVIDIA users can also use docker run --gpus all ubuntu nvidia-smi to verify passthrough in container scenarios.
To restrict Ollama to only certain cards, or to force pure CPU, each backend has corresponding switches:
Example
NVIDIA: let Ollama use only the first card (UUID recommended, check with nvidia-smi -L)
CUDA_VISIBLE_DEVICES
=GPU-xxxx ollama serve
AMD: similarly use the ROCr device list
ROCR_VISIBLE_DEVICES
=
0
ollama serve
Vulkan: specify the device index
GGML_VK_VISIBLE_DEVICES
=
1
ollama serve
"-1" in all three notations means disable GPU, forcing pure CPU
CUDA_VISIBLE_DEVICES
=-
1
ollama serve
Multi-GPU load distribution
Allocation on multi-card machines is automatic, with a single rule: prefer a single card, split only when it does not fit.
When the model fits entirely on any one card, Ollama picks one that fits - a single card avoids cross-PCIe data movement and is usually the optimal throughput solution.
When it does not fit on a single card, the model is split across all available cards; at this point you must also leave headroom for the KV cache and context, which is the common reason large-parameter models "still split even though total VRAM is sufficient".
When mixing multiple AMD cards on Linux, some driver versions may produce garbled output; such issues are documented in AMD's official multi-card known-issues documentation; prioritize upgrading the ROCm driver.
Optimization headroom for CPU inference
It runs without a discrete GPU, but there are a few points worth squeezing.
Ollama ships several CPU inference libraries, ranked by performance as cpu_avx2 > cpu_avx > cpu, automatically selected by default; when auto-detection fails you can force one:
Example
Force the AVX2 inference library
OLLAMA_LLM_LIBRARY
=cpu_avx2 ollama serve
Check which instruction sets your CPU supports
cat
/
proc
/
cpuinfo
|
grep
flags
|
head
-1
Two additional facts: macOS's Rosetta translation environment can only use the basic cpu library; the thread count is controlled by num_thread in options, defaulting to the number of physical cores, and blindly increasing it actually slows things down due to hyperthreading contention.
The hidden major factor in CPU inference is context length: for the same model, the CPU inference speed difference between 4K and 32K contexts is very significant. When configuring models for CPU scenarios, do not casually set num_ctx very large.
Flash Attention and KV cache quantization
These two are the most important VRAM optimizations for long-context scenarios; they target not the model weights but the KV cache that grows with context.
New versions of Ollama automatically enable Flash Attention when the backend supports it, and you can also force it on/off with a variable:
Example
Force-enable Flash Attention
OLLAMA_FLASH_ATTENTION
=
1
ollama serve
KV cache quantized to 8-bit: VRAM halved, almost no loss in precision (recommended)
OLLAMA_KV_CACHE_TYPE
=q8_0 ollama serve
| KV cache type | Memory usage | Quality impact | Recommendation |
|---|---|---|---|
| f16 (default) | Baseline | Lossless | Keep the default when VRAM is ample |
| q8_0 | About half | Almost imperceptible | Recommended choice for long-context scenarios |
| q4_0 | About one quarter | Noticeable under long contexts | Consider only when pushing to the limit |
Two caveats: KV cache type is a global configuration that applies to all loaded models; quantization's impact on quality varies by model, with some high-GQA-architecture models being more sensitive, so after switching it is recommended to compare and verify with a fixed-seed prompt.
The quality trade-off among quantization precision and speed
The model selection chapter covered the size benefits of quantization; here are a few conclusions from the speed perspective.
| Precision | Size | Speed | Quality | Applicable |
|---|---|---|---|---|
| q8_0 | About half of fp16 | Close to fp16 | Almost lossless | Preferred choice when VRAM is ample |
| q4_K_M | About one quarter of fp16 | Usually faster | Slight decrease | Default tier for local deployment |
| q4_K_S and lower | Smaller | Not necessarily faster | More noticeable decrease | Extreme VRAM saving |
Counterintuitive point: lower quantization is not always faster. Inference speed is constrained by memory bandwidth; some low-bit formats require extra dequantization computation, and in practice may be on par or even slower, so don't choose a quantization tier based on the numbers alone.
Recommended comparison method: fix the seed and use the same set of prompts, compare the output and token/s of two quantization tiers at a low temperature of 0.3, looking at quality and speed together.
Concurrency and throughput tuning
After single-request optimization is maxed out, the next step is to let the server handle concurrency.
The core variable is OLLAMA_NUM_PARALLEL (number of parallel requests per model), and its cost must be kept in mind: parallel processing multiplies the context cache, and the required VRAM is roughly this value times the context length in KV space; setting it too high may push the model out of GPU.
Server-side queuing is controlled by OLLAMA_MAX_QUEUE, default 512; exceeding it returns 503 directly, and the client should retry.
Operation order for throughput tuning:
Step 1, single-request tuning (spec, quantization, FA and KV cache) to confirm single-path speed.
Step 2, gradually increase NUM_PARALLEL, using ollama ps at each tier to confirm it is still 100% GPU, and observe whether total throughput rises.
Step 3, if queuing 503 or mixed splitting appears, step back one tier; that is the VRAM red line.