Skip to content
mentoor

Topic 2 of 5 · 4 min read

Tuning GPU offload and context length

Goal: Choose offload, context and KV cache settings that fit a model in a stated amount of GPU memory.

A model runs fast only if the weights and the KV cache fit in GPU memory; GPU offload and context length are how you make them fit. Everything else in this topic is arithmetic on that one rule.

Check yourself · 1 of 3

What does the -ngl flag in llama.cpp set?

Choose one answer

Why it matters

Generation speed depends on where the weights sit. Layers on the GPU are read at GPU memory speed. Layers left on the CPU are read from system RAM, which is much slower. And because tokens pass through the layers in order, the slow layers set the pace for every token.

The speed cliff of partial offload

Offloading is set by the number of layers on the GPU (-ngl in llama.cpp, --n-gpu-layers in the server). It is tempting to expect speed to rise smoothly with each layer you add. In practice it improves a little at a time while any layers remain on the CPU, then jumps when the last layer moves over. A model that fits entirely is often several times faster than the same model with a few layers left behind. If it nearly fits, a smaller quant or a shorter context is usually better than a partial offload.

The KV cache grows with context

The KV cache stores keys and values for every token in the context, so memory use grows linearly with context length. A rough size is: 2 (keys and values) x layers x KV heads x head size x context tokens x bytes per value. For a model with 32 layers, 8 KV heads, head size 128 and a 16-bit cache, that is 128 KiB per token, so an 8,192-token context uses 1 GiB and a 32,768-token context uses 4 GiB. This sits on top of the weights.

Two levers cut it:

  • Shorter context. Set -c to what the task needs, not to the model's maximum.
  • KV cache quantization. --cache-type-k q8_0 --cache-type-v q8_0 stores the cache in 8 bits, roughly halving it. In llama.cpp a quantized V cache needs flash attention on (--flash-attn on). Lower bit widths save more and cost more quality, so test your task.

Before and after

Weak: llama-server -m big-Q6_K.gguf -c 65536 -ngl 20. Ask for the model's full context, push 20 layers to the GPU, and the rest runs on the CPU at crawling speed.

Better: pick a quant that fits, set -c 16384 for the job, quantize the cache, and use -ngl 99 so every layer goes to the GPU.

What goes wrong

  • Max context by default. The cache for a huge window can take more memory than the weights.
  • Forgetting the cache when sizing. "The file is 14 GB and I have 16 GB" ignores the cache and the runtime's own buffers.
  • Stopping at a half offload. Moving 20 of 33 layers helps less than people expect.
  • Quantizing the cache without a quality check. Long-context retrieval is where a heavily quantized cache shows damage first.

Examples

Sizing a cache by hand

A model has 32 layers, 8 KV heads and head size 128, with a 16-bit cache.

2 x 32 x 8 x 128 x 2 bytes = 131,072 bytes per token (128 KiB)
8,192 tokens  -> 1 GiB
32,768 tokens -> 4 GiB

On a 12 GB card, a 7 GB quant plus a 4 GiB cache leaves almost nothing for buffers. Cutting the context to 16,384 or quantizing the cache fits it.

A server command that fits

llama-server -m model-Q4_K_M.gguf -ngl 99 -c 16384 \
  --flash-attn on --cache-type-k q8_0 --cache-type-v q8_0

All layers on the GPU, a context sized for the job, and an 8-bit cache that needs flash attention for the V half.

Key points

  • Layers on the CPU set the pace for every token, so speed jumps only when the last layer reaches the GPU.
  • KV cache memory grows linearly with context length and sits on top of the weights.
  • Estimate cache size as 2 x layers x KV heads x head size x tokens x bytes per value.
  • Set context to what the task needs and quantize the cache before accepting a partial offload.
  • A quantized V cache in llama.cpp needs flash attention enabled, and quality should be rechecked.

Common mistakes

  • Leaving context at the model's maximum when the task needs a fraction of it.
  • Sizing by the model file alone and forgetting the KV cache and runtime buffers.
  • Accepting a partial offload when a smaller quant or shorter context would fit everything.
  • Quantizing the KV cache without rechecking quality on long inputs.

Check yourself · 2 of 3

Why does doubling the context length increase memory use even though the model weights stay the same?

Choose one answer

Check yourself · 3 of 3

A 16 GB GPU cannot hold a model and its 32k-token cache. Which change usually keeps generation fastest?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic