# How much memory a model needs

> [!NOTE]
>
> **In one minute**
>
> - Serving needs memory for **the weights**, the **KV cache** of every request in flight, and some **overhead**.
> - Weights: parameters × bytes per parameter. KV cache: a fixed amount per token, times tokens, times users.
> - Fine-tuning and training need several times more than serving the same model.

**Before you start.** [What a model is](<https://ai.ozarland.com/understand/what-is-a-model/index.md>) and [How a large language model answers](<https://ai.ozarland.com/understand/how-an-llm-answers/index.md>). The arithmetic is background ([Conventions](<https://ai.ozarland.com/start/conventions/index.md#background>)); the figures of a real model come from its published configuration.

## The four parts

```text
GPU memory needed = weights + KV cache + activations + overhead
```

| Part | What it is | How it grows |
| --- | --- | --- |
| Weights | The parameters | Fixed for a model and a precision |
| KV cache | What the model keeps for each token already processed | With tokens per request × requests in flight |
| Activations | Intermediate results of the pass in progress | With the batch; small for serving |
| Overhead | The GPU runtime, the serving software, fragmentation | A few GB; serving software reserves a share of memory up front |

## Weights and quantization

Weights = parameters × bytes per parameter ([What a model is](<https://ai.ozarland.com/understand/what-is-a-model/index.md#worked-example-the-size-of-a-model>)). **Quantization** stores each weight with fewer bits:

| Model | BF16 (2 bytes) | 8-bit (1 byte) | 4-bit (0.5 byte) |
| --- | --- | --- | --- |
| 8 billion parameters | 16 GB | 8 GB | 4 GB |
| 70 billion parameters | 140 GB | 70 GB | 35 GB |

Quantization costs some quality. How much depends on the model and the method; test it on your own tasks.

## The KV cache

For every token in the context, each layer of the model keeps two vectors: a key and a value. The size per token is:

```text
KV cache per token = 2 × layers × KV heads × head size × bytes per value
```

These four numbers are in the published configuration of a model. Take a model **shaped like** a common 8-billion parameter model: 32 layers, 8 KV heads, head size 128, values in 16 bits (2 bytes):

```text
2 × 32 × 8 × 128 × 2 bytes = 131,072 bytes = 128 KiB per token
```

| Context per request | KV cache for one request | For 20 requests at once |
| --- | --- | --- |
| 4,000 tokens | 0.5 GiB | 10 GiB |
| 32,000 tokens | 4 GiB | 80 GiB |
| 128,000 tokens | 16 GiB | 320 GiB |

With long contexts and many users, the KV cache becomes larger than the weights.

## Worked example: does it fit on one 80 GB GPU?

Serve the 8B model above in BF16, for 20 users at once, with up to 8,000 tokens each:

1. Weights: 8 × 10⁹ × 2 bytes = 16 GB.
2. KV cache: 128 KiB × 8,000 tokens × 20 users ≈ 20 GB.
3. Activations and overhead: say 5 GB.
4. Total: about 41 GB. It fits on one 80 GB GPU, with room for more users.

The same users on a 70B model in BF16 need 140 GB of weights alone: at least two 80 GB GPUs, before the KV cache.

## Fine-tuning and training

Training keeps much more than the weights. With the usual mixed-precision method and the Adam optimiser, each parameter costs about **16 bytes** (16-bit weights and gradients, and a 32-bit copy of the weights with two optimiser values), before activations:

| Job, for an 8B model | Rough memory |
| --- | --- |
| Serving in BF16 | 16 GB + KV cache |
| LoRA fine-tuning on BF16 weights | 16 GB + small extras + activations |
| Full fine-tuning or training | 8 × 10⁹ × 16 bytes = 128 GB + activations, spread over several GPUs |

## Common mistakes

- **"The model is 16 GB, so a 16 GB GPU is enough."** Nothing is left for the KV cache and the runtime: it will refuse long contexts or more than one user.
- **"Serving software uses only what it needs."** Many servers reserve most of the GPU memory at start-up for their KV cache. A GPU that looks full may be serving very little.
- **"Quantization is free."** It saves memory at a cost in quality, which you must measure.

## Check yourself

> [!NOTE]
>
> **How much memory do the weights of a 70B model need in 4-bit?**
>
> 70 × 10⁹ × 0.5 bytes = 35 GB.

> [!NOTE]
>
> **Using 128 KiB per token, how much KV cache do 10 users with 32,000 tokens each need?**
>
> 128 KiB × 32,000 × 10 ≈ 39 GiB.

> [!NOTE]
>
> **Why does full fine-tuning of an 8B model not fit on one 80 GB GPU?**
>
> At about 16 bytes per parameter, the training state alone is about 128 GB, before activations.

## Go further

- Next page: [Linking GPUs](<https://ai.ozarland.com/hardware/linking-gpus/index.md>).
- The 16 bytes per parameter: [ZeRO: Memory Optimizations Toward Training Trillion Parameter Models](<https://arxiv.org/abs/1910.02054>), section 3.1.
