Skip to content
mentoor

Topic 1 of 5 · 5 min read

Estimating memory to fit a model

Goal: Estimate whether a given model, quant and context length fits in a stated amount of GPU memory.

A model fits when weights, KV cache and runtime overhead add up to less than the memory you have; you can work all three out on paper before downloading a single file. Each is simple arithmetic.

Check yourself · 1 of 3

Roughly how much memory do the weights of a 7B-parameter model need at 16-bit precision?

Choose one answer

Why it matters

Download sizes are 5 to 80 GB. A wrong guess costs an hour and ends in an out-of-memory error, or in a model that silently spills into slow system RAM. Ten minutes of arithmetic tells you which model, quant and context length to try.

The three parts

  1. Weights = parameters x bytes per parameter. A 16-bit model uses 2 bytes per parameter, 8-bit uses 1, and 4-bit quants use about 0.5 to 0.6 once the scale values are counted. Example: an 8B model is 16 GB at 16-bit and about 4.5 GB at 4-bit. A 70B model at 4-bit is about 39 GB.
  2. KV cache = 2 x layers x KV heads x head dimension x bytes x tokens. The cache stores keys and values for every token in the context. Read the layer count, KV head count and head dimension from the model's config.json. For an 8B model with 32 layers, 8 KV heads and head dimension 128 at 16-bit, that is 2 x 32 x 8 x 128 x 2 = 131,072 bytes per token. At 32,000 tokens this is about 4.3 GB. It grows linearly with context and with the number of parallel requests.
  3. Overhead = about 1 to 2 GB. The runtime needs space for CUDA or Metal buffers and activations. Leave more room on a card that also drives your display.

Worked example

You have a 12 GB GPU and want an 8B model at 4-bit with a 32k context:

weights   8e9 x 0.5625 bytes   = 4.5 GB
KV cache  131,072 B x 32,768   = 4.3 GB
overhead                       = 1.5 GB
total                          = 10.3 GB   -> fits in 12 GB

The same model at 16-bit needs 16 GB for weights alone and does not fit. Dropping the context to 8k saves about 3.2 GB if you need room for a larger quant.

Before and after

Weak estimate: "8B at 4-bit is 4.5 GB, so my 8 GB card is plenty for 32k context." That counts only the weights. Fixed: 4.5 + 4.3 + 1.5 = 10.3 GB, which does not fit in 8 GB. Options are a 16k context (about 8.2 GB, still tight), a smaller quant, or a quantised KV cache, which most runtimes offer and which roughly halves the cache at 8-bit at some quality risk.

What goes wrong

  • Forgetting the KV cache. Models that look small on disk run out of memory at long context.
  • Using the file size as the whole answer. File size is the weights only.
  • Ignoring parallel requests. A server handling four chats at 8k each holds four caches.
  • Assuming it will spill gracefully. Layers offloaded to system RAM run many times slower, since each token must read them across a slow link.

The Hugging Face memory estimator and the formulas in the references give a quick check, but your own table with the three rows is the habit to build.

Examples

A 70B model on two 24 GB cards

Weights at 4-bit: 70e9 x 0.5625 = 39.4 GB. For a 70B model with 80 layers, 8 KV heads and head dimension 128, the cache is 2 x 80 x 8 x 128 x 2 = 327,680 bytes per token, so 8,192 tokens take 2.7 GB. With 2 GB overhead the total is about 44 GB, which fits in 48 GB across two cards but not in one. At a 32k context the cache alone is 10.7 GB and the total is about 52 GB, which does not fit.

Reading a config.json

A config lists num_hidden_layers: 32, num_key_value_heads: 8 and a hidden size of 4096 with 32 attention heads. Head dimension is 4096 / 32 = 128. That gives the 131,072 bytes per token used above. A model that uses 32 KV heads instead of 8 would need four times as much cache.

Key points

  • Total memory is weights plus KV cache plus about 1 to 2 GB of overhead.
  • Weights are parameters times bytes per parameter: 2 at 16-bit, 1 at 8-bit, about 0.5 to 0.6 at 4-bit.
  • KV cache is 2 x layers x KV heads x head dimension x bytes per value x tokens, and grows with context and parallel requests.
  • The file size on disk is the weights only; it understates the memory you need at long context.
  • Halving context or quantising the KV cache are the cheapest ways to fit a model that is slightly too big.

Common mistakes

  • Sizing a model by its download size and ignoring the KV cache.
  • Using the same bytes-per-parameter for every quant; 4-bit formats carry scale data and land nearer 0.55 than 0.5.
  • Planning for one chat while serving several, so the cache is multiplied by users.
  • Leaving no overhead headroom, so the model loads and then fails on the first long prompt.

Check yourself · 2 of 3

What makes the KV cache grow while a chat gets longer?

Choose one answer

Check yourself · 3 of 3

A 4-bit 8B model (4.5 GB) has a 4.3 GB KV cache at 32k context, plus 1.5 GB overhead. Will it fit on an 8 GB GPU?

Choose one answer

Practice items, not counted towards your mastery.

Start here