# Mac exercise for mission 7.2: KV needs from the config.json

Part of the course “AI from Zero to Autonomous Agents” (https://llm.yoga/en). Page for humans: https://llm.yoga/en/mac-7-2

About 10 minutes at your computer · Type this address: **llm.yoga/en/mac-7-2**

In the mission you worked out the cache by hand. Here a small script reads the values from a model’s configuration file itself and does the math for a context length of your choice. All you download are text files of less than one kilobyte each, no model.

**Before you start**

You need **Python 3** and `curl` in the terminal. The transfer tool is preinstalled; `python3` may require developer tools or an installation from python.org. You don’t need Ollama here. Check:

```
python3 --version
```

### 1. Download the configuration

The `config.json` describes how a model is built. Download the one for Qwen3-8B from Hugging Face and show the four values you need:

```
curl -sL -o q8.json https://huggingface.co/Qwen/Qwen3-8B/resolve/main/config.json
```

```
grep -E "num_hidden_layers|heads|head_dim" q8.json
```

**Output (Measured on an M4 Mac (24 GB) on November 25, 2026)**

```
  "head_dim": 128,
  "num_attention_heads": 32,
  "num_hidden_layers": 36,
  "num_key_value_heads": 8,
```

`num_attention_heads` are the query heads, `num_key_value_heads` the KV heads, `head_dim` the numbers per head, `num_hidden_layers` the layers.

### 2. Save the script

Save these lines as `kv.py`, for example with `nano kv.py`. The last line calculates as if every query head had its own keys and values. The script is the one used for the measurement, so its comments and output are in German: the comments say “context length in tokens” and “key and value, 2 bytes per number (FP16)”; the output lines mean layers, query heads, KV heads, head dimension, „KiB je Token“ (KiB per token), „GiB bei … Tokens“ (GiB at … tokens) and „GiB mit … KV-Köpfen“ (GiB with … KV heads).

```
import json, sys
c = json.load(open(sys.argv[1]))
n = int(sys.argv[2])                       # Kontextlänge in Tokens
L, q, kv = c["num_hidden_layers"], c["num_attention_heads"], c["num_key_value_heads"]
hd = c.get("head_dim") or c["hidden_size"] // q
je = 2 * L * kv * hd * 2                   # Key und Value, 2 Byte je Zahl (FP16)
print(f"{L} Schichten, {q} Query-Köpfe, {kv} KV-Köpfe, Kopfdimension {hd}")
print(f"KiB je Token: {je / 1024:g}")
print(f"GiB bei {n} Tokens: {je * n / 1024**3:.2f}")
print(f"GiB mit {q} KV-Köpfen: {je * q / kv * n / 1024**3:.2f}")
```

### 3. Ralf’s context length

```
python3 kv.py q8.json 65536
```

**Output (Measured on an M4 Mac (24 GB) on November 25, 2026)**

```
36 Schichten, 32 Query-Köpfe, 8 KV-Köpfe, Kopfdimension 128
KiB je Token: 144
GiB bei 65536 Tokens: 9.00
GiB mit 32 KV-Köpfen: 36.00
```

9 GiB of cache next to 5.2 GB of weights. With separate keys and values for every head it would be 36 GiB, more than the entire memory of the 24 GB test Mac.

### 4. Let the page check it

Paste the four lines. The page reads the three results and compares them with the math from the mission.

The check needs JavaScript. Without it: 144 KiB per token, 9 GiB at 65,536 tokens, 36 GiB with 32 KV heads.

### 5. A shorter length

Maybe Ralf’s contracts fit into 8,192 tokens. The script rounds to two decimal places; the exact value is 1.125 GiB.

```
python3 kv.py q8.json 8192
```

**Output (Measured on an M4 Mac (24 GB) on November 25, 2026)**

```
36 Schichten, 32 Query-Köpfe, 8 KV-Köpfe, Kopfdimension 128
KiB je Token: 144
GiB bei 8192 Tokens: 1.12
GiB mit 32 KV-Köpfen: 4.50
```

### 6. A different model

The same script for the smaller Qwen3-1.7B from mission 7.1:

```
curl -sL -o q17.json https://huggingface.co/Qwen/Qwen3-1.7B/resolve/main/config.json
```

```
python3 kv.py q17.json 65536
```

**Output (Measured on an M4 Mac (24 GB) on November 25, 2026)**

```
28 Schichten, 16 Query-Köpfe, 8 KV-Köpfe, Kopfdimension 128
KiB je Token: 112
GiB bei 65536 Tokens: 7.00
GiB mit 16 KV-Köpfen: 14.00
```

A fifth of the parameters, but just over three quarters of the cache of Qwen3-8B: both have 8 KV heads; the small one just has fewer layers. In Qwen3-1.7B, every two query heads share one KV head.

### 7. Clean up

Just the three small files:

```
rm q8.json q17.json kv.py
```

Models with a sliding window in some of their layers, such as Gemma 3 or gpt-oss, need less than the script prints. If the config.json contains `sliding_window` or `layer_types`, calculate the layers separately: use the full context length for full-attention layers and at most the window length for the others.

#### On the DGX Spark

The DGX Spark comes with 64 GB and with 128 GB of memory. Ralf’s setting would be no problem there: 5.2 GB of weights plus 9 GiB of cache. The math stays the same; there’s just more room. With Ollama from the playbook “Open WebUI with Ollama”, you can set `num_ctx` 65536 and check with `ollama ps` whether it says “100% GPU”. (Checked against the documentation, not run here)
