How do I make it faster?
Put the whole model on the GPU. That one change is worth more than everything else on this page combined. Then work down the list in order, and stop when the speed is good enough for your task.
Ordered by payoff
- 1Whole model on GPU
- 2Right backend
- 3Right-size context
- 4Keep it loaded and cached
- 5Speculative decoding
- 6Flash attention and KV cache
- 7Parallel slots
Measure before and after each change with the same prompt. How do I benchmark my own setup? shows how. A change you did not measure is a change you are guessing about.
1. Get the whole model onto the GPU
When part of the model sits in system RAM, every tokenTokenA chunk of text, usually part of a word. Models read and write in tokens.Read more waits for the CPU to process those layers. One layer on the CPU slows every token, and a quarter of the model on the CPU can halve your speed or worse. What makes a model fast? explains why memory bandwidthMemory bandwidthHow fast data moves out of memory, in GB per second. It sets decode speed.Read more decides this.
Check what you have. In Ollama, ollama ps shows the GPU share, and in llama.cpp the startup log says how many layers were offloaded. In LM Studio, the load settings show the GPU offloadOffloadRunning part of a model on the CPU because it does not fit in VRAM.Read more slider.
If the model does not fit, pick a smaller What is quantization? or a smaller model before you accept a split. A Q4 model fully on the GPU usually beats a Q6 split across GPU and CPU. On a Mac this step is about memory pressure instead, covered in Is a Mac good for local AI?.
Fully on the GPU at Q4 beats partly on the GPU at Q8. Drop bits before you drop layers.
2. Pick the right backend
The same model on the wrong backendBackendThe hardware path an engine uses: Metal, CUDA, Vulkan, ROCm or CPU.Read more can run several times slower. Check that your runtimeRuntimeThe software that loads a model file and runs it.Read more uses the vendor's own path:
| Hardware | Use | Avoid |
|---|---|---|
| Apple silicon | MLX, or llama.cpp with Metal | Anything running on CPU |
| NVIDIA | CUDA | Vulkan, unless CUDA will not install |
| AMD | ROCm on supported cards, Vulkan otherwise | CPU fallback |
| Intel GPU | Vulkan or SYCL | CPU fallback |
A prebuilt download for the wrong backend quietly falls back to the CPU. Why is my GPU not being used? covers how to spot it. What is a runtime, and what sits under my app? explains what a backend is.
3. Right-size the context
The KV cacheKV cacheSaved work from earlier tokens, so the model does not reread the whole conversation for every new token.Read more grows with every token of context you reserve. A huge context steals GPU memory from the weightsWeightsThe full set of a model's parameters, stored in a file.Read more and can push layers onto the CPU. A long prompt also takes longer to prefillPrefillThe phase where the model reads your prompt. Limited by compute.Read more, and each new token attends to more history.
Set the context to what the task needs, not the model's maximum. Chat needs a few thousand tokens. Agents need 32K tokens, comfortably 64K tokens. How much can the model keep in mind? has the memory sums.
4. Keep the model loaded and the prompt cached
Loading a model from disk takes seconds to minutes. Most runtimes unload idle models after a few minutes. If you use a model all day, keep it loaded, for example with OLLAMA_KEEP_ALIVE in Ollama.
Prompt caching saves the prefill. When a new request starts with the same tokens as the last one, the runtime reuses the KV cache for that part and skips straight to the new text. llama-server, Ollama and LM Studio do this for a single conversation. It matters most for agents, which resend a long system promptSystem promptStanding instructions placed before the conversation.Read more on every turn.
The cache only helps if the start of the prompt is byte-for-byte identical. A tool that puts the date or a random ID at the top of the system prompt breaks caching on every turn.
5. Speculative decoding and draft models
A small draft model guesses the next few tokens, and the big model checks them all in one pass. When the guesses are right, you get several tokens for the price of one. When they are wrong, you lose a little.
It works best on predictable text such as code and structured output, and on dense modelsDense modelA model where every parameter runs for every token.Read more. The draft must share the main model's tokenizer, so pick one from the same family.
In llama-server the flag is -md or --model-draft. LM Studio has a draft model setting for the same thing.
The draft takes memory and compute. On a GPU that is already full, or with a mixture of expertsMixture of expertsA model split into experts, where only a few run for each token.Read more model that is already fast, it can make things slower. Measure it.
6. Flash attention and KV cache quantization
Flash attention computes attention in tiles instead of building the full attention matrix. It saves memory and speeds up long contexts. Many runtimes now turn it on by default for supported hardware, so turn it on if yours does not.
KV cache quantizationQuantizationStoring each weight with fewer bits so the model takes less memory.Read more stores the cache at 8 or 4 bits instead of 16. At q8_0 the cache takes half the memory, which buys a longer context or keeps layers on the GPU. The quality cost at 8-bit is small, and at 4-bit it starts to show on long contexts.
In llama.cpp the flags are --cache-type-k and --cache-type-v. In Ollama it is OLLAMA_KV_CACHE_TYPE.
7. Parallel slots for multiple agents
If several agents or users hit one model at once, parallel slots let the runtime serve them together instead of queuing. In llama.cpp the flag is -np. In Ollama it is OLLAMA_NUM_PARALLEL.
Each slot gets a share of the context. Raise the total context when you raise the slot count. Past a handful of parallel users, a serving engine does this far better. When do I need vLLM or SGLang? covers when to switch.
What not to bother with
Some settings get passed around forums and rarely move the number.
- Thread count, when the model is fully on the GPU. The CPU barely works during GPU decoding.
- Batch size, unless long prompts are slow. The defaults are tuned for most hardware.
- Exotic sampling settings. Should I touch temperature and the other sliders? changes what the model writes, not how fast.
- Compiling with custom CPU flags for a GPU workload. The gain is in noise.
- Overclocking. DecodeDecodeThe phase where the model writes its answer one token at a time. Limited by memory bandwidth.Read more speed follows memory bandwidth, and most GPU overclocks target the core.
ollama psCheck the processor column for the model you use most. If it does not say 100% GPU, start at step 1.
Go deeperThat's enough
Why the order is the order
Decode speed is roughly memory bandwidth divided by the bytes read per token. GPU memory on a discrete card is several times faster than system RAM, so any layer in RAM sets the pace for the whole model. That is why step 1 dwarfs the rest.
The backend decides how close you get to the hardware's bandwidth. Context size decides how many bytes of KV cache you read on top of the weights. Everything after that trims overhead.
Prefill and decode tune differently
Prefill is compute-bound: it processes the whole prompt at once, and a faster GPU or a bigger batch helps. Decode is bandwidth-bound: each token reads the whole model. A setting that helps one rarely helps the other. What happens when I press enter? explains both stages.
So if your complaint is "it takes ages to start answering", look at prompt length, caching and batch size. If it is "it writes slowly", look at offloading, backend and model size.
MoE models
A mixture of experts model only reads its active parametersActive parametersThe parameters an MoE model actually uses per token. They decide its speed.Read more per token, so it decodes fast for its size. That changes the advice. Offloading the inactive experts to system RAM costs less than offloading a dense model's layers. llama.cpp has options to keep expert weights on the CPU on purpose. What is a mixture of experts model? explains the split.