Lab

Memory for weights and KV cache

Memory for weights and KV cache: What running a model locally needs in memory, which is its weights plus a KV cache that grows with the context, plus room for the runtime.

To run a model on your own machine, three things have to fit in memory: the weights, the KV cache and some room for the runtime.

Weights are the easy part. Every parameter is stored with some number of bits, so the weights take the parameter count times the bits per weight, divided by 8 to get bytes. Quantised formats use fewer bits per weight, which is why the same model can come in very different file sizes.

The KV cache is the part people forget. Attention reuses every earlier token’s keys and values, so the runtime keeps them, and the cache grows with every token in the context. The KV cache entry shows how to work out its size per token.

The runtime needs room too, for the graphics driver, scratch buffers and the logits it produces.

When the total is more than a graphics card holds, runtimes such as llama.cpp can put some layers on the card and the rest in system RAM, at a cost in speed, because every token then has to wait for the slower memory.

The explainer adds these up for real models and hardware, and shows which part fills the memory first.

See it working

Explore it in Will it run on my machine?

Work out whether a model fits on your hardware, and how fast it will talk, from memory and bandwidth alone.

Open the explainer