Skip to content
StackDecode
System Design
19. AI System Design and LLM InfrastructureAnimated

GPU Memory Management

Fitting models and caches into GPU memory.

The idea

Weights, activations, optimiser state and KV cache compete for limited memory.

Watch it work

Press play, pause, or step through. Some animations have settings you can change.

GPU memory for the KV cache (each square is a fixed-size block)freefreefreefreefreefreefreefreefreefreefreefreefreefreefreefree

Memory is split into small blocks, like pages in an operating system.

used:
0
free:
16
1/12

PagedAttention (vLLM) removes the fragmentation that wastes most KV-cache memory.

Step by step

  1. 1Estimate memory for weights
  2. 2Add activations or KV cache
  3. 3Reduce with quantisation, sharding or paging
  4. 4Monitor out-of-memory

Where it is used

Training and inference.

Watch out for

Fragmentation causes out-of-memory errors even with free space.

Related