Sub topic 7
VRAM, System RAM and Unified Memory
This chapter assumes you can estimate weight memory from parameter count and bits per weight, and that you know what a KV cache is. Here you learn where those bytes can live and what that means for fitting a model.
Three places a model can live
A local model's weights and KV cache must sit in memory the processor can read quickly. Which memory that is depends on your hardware.
- VRAM is memory attached to a discrete GPU. It is fast, often several hundred GB/s or more. Its size is fixed by the card, commonly 8 to 24 GB on consumer cards.
- System RAM is the memory on the motherboard, used by the CPU. It is usually larger and cheaper per GB, but slower. Dual-channel desktop DDR5 reaches roughly 60 to 100 GB/s.
- Unified memory is one pool shared by CPU and GPU, as on Apple Silicon and some integrated-GPU systems. The GPU can use a large share of the whole pool, and bandwidth is usually higher than desktop RAM.
On a machine with a discrete GPU, the CPU and GPU do not share memory. Data crosses the PCIe bus, which is much slower than VRAM. That is why a model that spills out of VRAM slows down so sharply.
Comparing the tiers
| Tier | Typical size | Relative bandwidth | Main limit |
|---|---|---|---|
| VRAM | 8 to 24 GB (consumer) | Highest | Small capacity |
| System RAM | 16 to 128 GB | Lowest | Slow for generation |
| Unified memory | 16 to 192 GB | Middle to high | Cost, and not upgradable |
What counts toward your budget
The usable amount is less than the number on the box.
- The operating system and other programs use memory too. On a 16 GB unified-memory machine, you cannot give the model all 16 GB.
- Some systems cap how much of the pool the GPU may use. On macOS the default limit is a fraction of total memory, and it can be raised with a system setting.
- A GPU that also drives your display loses some VRAM to the desktop.
- The runtime needs scratch buffers for computation on top of weights and KV cache.
A safe habit is to leave 1 to 2 GB of headroom on a GPU and more on a shared pool.
def fits(weights_gib, kv_gib, usable_gib, overhead_gib=1.0):
need = weights_gib + kv_gib + overhead_gib
return need, need <= usable_gib
# Example from the previous chapters: about 4.6 GiB of weights and 1.0 GiB of KV cache.
# A 6 GiB card with the desktop using roughly 0.5 GiB leaves 5.5 GiB usable.
need, ok = fits(weights_gib=4.6, kv_gib=1.0, usable_gib=5.5)
print(f"need {need:.1f} GiB, fits: {ok}")This prints need 6.6 GiB, fits: False. Dropping the context to 4096 tokens halves the KV cache to 0.5 GiB, giving 6.1 GiB, which still does not fit. The next options are a smaller quantisation or a partial offload, covered in later chapters.
Common errors
- Comparing a file size to total RAM and forgetting the KV cache and overhead.
- Assuming unified memory is as fast as VRAM. Check the memory bandwidth of the specific chip.
- Forgetting that integrated GPUs on a PC may only reserve a small fixed slice of RAM unless configured otherwise.
Exercise
A laptop has 16 GiB of unified memory, and the GPU may use at most 75% of it. The OS and apps use 3 GiB of the pool. A model needs 7.5 GiB for weights, 1.5 GiB for KV cache and 1 GiB of overhead. Does it fit, and what is the limiting number?
Show answer
The GPU limit is 0.75 × 16 = 12 GiB. The model needs 7.5 + 1.5 + 1 = 10 GiB, which is under 12 GiB. The OS and apps use 3 GiB, which leaves 13 GiB in the pool, so the 12 GiB GPU cap is the tighter limit. It fits, with 2 GiB to spare.