# Mac exercise for mission 4.2: Count a model from its config.json

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

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

In the mission you calculated the feed-forward share of Qwen3-8B by hand. Here a small script recalculates all components from the model’s configuration file. For that you download only a text file of about 700 bytes, not a 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 only need Ollama for the comparison in step 4. Check:

```
python3 --version
```

### 1. Download the configuration

The `config.json` describes how a model is built: width, number of layers, heads, vocabulary. First the small Qwen3-1.7B from Hugging Face:

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

```
cat q17.json
```

**Excerpt of the output (Measured on an M4 Mac (24 GB) on November 25, 2026)**

```
  "head_dim": 128,
  "hidden_size": 2048,
  "intermediate_size": 6144,
  "num_attention_heads": 16,
  "num_hidden_layers": 28,
  "num_key_value_heads": 8,
  "tie_word_embeddings": true,
  "vocab_size": 151936
```

`tie_word_embeddings: true` means the embedding table also serves as the output matrix and is stored only once.

### 2. Save the script

Save these lines as `zaehlen.py` („zählen“ is German for “count”). It’s the script from [chapter 4](https://llm.yoga/en/04-transformer-3.md), with the shares added at the end. The German comments at the end say: shares as name, logarithm of the share, share in percent; the rest is embedding, output and normalization.

```
import json, math, sys
c = json.load(open(sys.argv[1]))
V, H, L = c["vocab_size"], c["hidden_size"], c["num_hidden_layers"]
nq, nkv = c["num_attention_heads"], c["num_key_value_heads"]
hd = c.get("head_dim", H // nq)
I = c["intermediate_size"]
emb = V * H
att = H * nq * hd + 2 * H * nkv * hd + nq * hd * H + 2 * hd  # Q, K, V, O, QK-Norm
mlp = 3 * H * I                                              # SwiGLU
pro_schicht = att + mlp + 2 * H
gesamt = emb + L * pro_schicht + H
if not c.get("tie_word_embeddings", False):
    gesamt += emb
print(f"Embedding {emb:,}  Attention {L*att:,}  Feed-Forward {L*mlp:,}")
print(f"Gesamt {gesamt:,}")
# Anteile: Name, Logarithmus des Anteils, Anteil in Prozent
rest = gesamt - L * mlp - L * att  # Embedding, Ausgabe, Normierung
for name, n in [("Feed-Forward", L * mlp), ("Attention", L * att), ("Rest", rest)]:
    print(f"'{name}' {math.log(n / gesamt):.3f} {n / gesamt * 100:.1f} %")
```

Every matrix that maps a numbers to b numbers has a × b weights. You don’t need the middle number in the last three lines; it’s only there so the check in step 6 can recalculate.

### 3. Count Qwen3-1.7B

```
python3 zaehlen.py q17.json
```

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

```
Embedding 311,164,928  Attention 352,328,704  Feed-Forward 1,056,964,608
Gesamt 1,720,574,976
'Feed-Forward' -0.487 61.4 %
'Attention' -1.586 20.5 %
'Rest' -1.710 18.1 %
```

1.72 billion in total („Gesamt“), matching the model card (1.7 billion). The feed-forward layers hold 61.4 percent.

### 4. Compare with Ollama

Only if you already have Ollama and qwen3:1.7b:

```
ollama show qwen3:1.7b
```

**Excerpt of the output (Measured on an M4 Mac (24 GB) on November 25, 2026)**

```
    architecture        qwen3
    parameters          2.0B
```

Ollama shows 2.0 billion. If you count the embedding table (311,164,928) a second time, you get 2,031,739,904. The obvious explanation is that Ollama counts the shared table twice; that isn’t documented.

### 5. Count Qwen3-8B

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

```
python3 zaehlen.py q8.json
```

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

```
Embedding 622,329,856  Attention 1,509,958,656  Feed-Forward 5,435,817,984
Gesamt 8,190,735,360
'Feed-Forward' -0.410 66.4 %
'Attention' -1.691 18.4 %
'Rest' -1.884 15.2 %
```

8,190,735,360, as in the chapter. Here the embedding table is counted twice, because Qwen3-8B stores input and output separately. The attention Ralf wanted to shrink: 18.4 percent.

### 6. Let the page check the shares

Paste the three lines with the shares from step 5. The page reads the same format as the token probabilities in chapter 3 (name, logarithm, percent). It checks whether the numbers fit together and which part comes first; the “candidates” here are the components.

The check needs JavaScript. Without it: feed-forward 66.4, attention 18.4, rest 15.2 percent, together 100.

The sentence about “all other tokens” in the feedback doesn’t apply here: the three shares already add up to 100 percent.

### 7. Clean up

```
rm q17.json q8.json zaehlen.py
```

With 16 GB and 8 GB the exercise works the same way; it doesn’t load a model. The script is only right for models with the same architecture as Qwen3; models with bias terms or Mixture of Experts need an adapted calculation (chapter 8).

#### On the DGX Spark

The calculation runs the same everywhere. The Spark gets interesting because of what comes after: use the script to count a model that only makes sense to run there, such as Qwen3-32B, and work out its memory requirement in BF16, 2 bytes per parameter. The playbook “Serve LLMs with vLLM on One Device” loads, among others, models in the 4-bit format NVFP4, and the DGX Dashboard from the playbook “Monitor Your AI Compute” shows the load. Compare the memory in use with your calculation: parameters times bytes per parameter, a little over half a byte for NVFP4, plus the KV cache. (Checked against the documentation, not run here)
