Sub topic 12
Splitting a model between GPU and CPU
This chapter assumes you can estimate weight and KV cache memory and know that token generation is limited by memory bandwidth. Here you will learn what happens when a model does not fit in VRAM, and how to pick a partial offload setting.
What partial offload means
A transformer model is a stack of layers. Runtimes such as llama.cpp can place some layers in GPU memory and leave the rest in system RAM. The -ngl option (number of GPU layers) sets how many layers go to the GPU. Setting it higher than the layer count, such as -ngl 99, offloads everything.
For each token, the GPU runs its layers and the CPU runs the remaining layers. The two parts run one after the other, not at the same time.
Choosing the layer count
Each layer holds about the same share of the weights. The KV cache for an offloaded layer also sits in VRAM, so count both. Subtract some VRAM for runtime buffers, then divide what is left by the cost of one layer.
def layers_that_fit(vram_gib, weights_gib, kv_gib, layers, overhead_gib=0.5):
per_layer = (weights_gib + kv_gib) / layers
return min(layers, int((vram_gib - overhead_gib) // per_layer))
# 8B model at ~4.85 bits per weight (4.52 GiB), 1.0 GiB KV cache at 8192 tokens, 32 layers, 4 GiB GPU.
print(layers_that_fit(vram_gib=4, weights_gib=4.52, kv_gib=1.0, layers=32))This prints 20, so you would start with -ngl 20. The estimate ignores the embedding and output tensors, which are not spread evenly across layers. Treat it as a starting point and lower the value if you get an out-of-memory error.
What speed to expect
Time per token is the sum of the time spent in each memory pool. For a fraction f of the weights on the GPU:
time per token = f × size / GPU bandwidth + (1 − f) × size / CPU bandwidth
def split_ceiling(model_gb, gpu_bw, cpu_bw, gpu_fraction):
seconds = (gpu_fraction * model_gb / gpu_bw
+ (1 - gpu_fraction) * model_gb / cpu_bw)
return 1 / seconds
for f in (0.0, 0.5, 1.0):
print(f, round(split_ceiling(4.9, gpu_bw=300, cpu_bw=50, gpu_fraction=f), 1))The output is 10.2, 17.5 and 61.2 tokens per second. Offloading half the model gives only a 1.7× gain over CPU alone, while a full fit gives 6×. The slow part dominates, so each layer left on the CPU costs more than each layer on the GPU saves.
Measuring the effect
Run the benchmark at several offload settings and compare:
llama-bench -m model-Q4_K_M.gguf -p 512 -n 128 -ngl 0,10,20,32Prompt processing usually improves quickly with every offloaded layer, because it is compute-bound and GPUs have far more compute. Generation improves more slowly until nearly all layers are on the GPU.
Common errors
- Setting
-ngl 99on a model that does not fit, which causes an out-of-memory failure or a slowdown from spilling into shared memory. - Forgetting that a longer context grows the KV cache and can push a working setup over the limit.
- Expecting a linear speedup from partial offload.
Exercise
A model file is 10 GB. Your GPU has 600 GB/s bandwidth and your CPU memory has 60 GB/s. If 80% of the weights are on the GPU, what is the approximate ceiling in tokens per second?
Show answer
Time per token = 0.8 × 10 / 600 + 0.2 × 10 / 60 = 0.0133 + 0.0333 = 0.0467 seconds, so the ceiling is about 21 tokens per second. Full GPU would reach 60, so the 20% on the CPU costs most of the speed.