# Mission 7.2: Why does Qwen3-8B have only 8 KV heads?

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

Belongs to [Chapter 7: KV cache](https://llm.yoga/en/07-kv-cache.md).

Structure: the case, the explanation, a hands-on tool, check questions, wrap-up. The answers to all questions are collected at the end.

## The case

**Ralf**, CTO at a customer:

> “We’re testing Qwen3-8B for long contracts. Since I set the context length to 65,536 tokens, the model partly runs on the CPU and is much slower. Then I looked at the configuration: 32 attention heads, but only 8 KV heads. Is this a stripped-down version? Surely it would be better with all 32.”

Customers who want to process long documents locally run into exactly this limit; if you estimate the memory beforehand, you save them bad purchases. Attention is the part of the model where each token, that is each word or word piece, looks at the tokens before it. It works with several heads in parallel that pay attention to different relationships. The configuration is the config.json file provided on Hugging Face for the models considered here.

From the config.json of Qwen3-8B: 36 layers, 32 query heads, 8 KV heads, 128 numbers per head. The qwen3:8b version in the program Ollama is 5.2 GB.

Ralf’s test machine has 16 GB of RAM (example).

**Question 1 (prediction):** What’s behind the 8 KV heads?

- A) The shrunk 4-bit version; the original model has 32 KV heads.
- B) That’s how the model is built: every four query heads share one KV head.
- C) A leftover from older models; newer ones give each head its own keys and values.

## What’s behind it

Picture 32 people searching an archive for clues. Each brings her own question. But they don’t each have to set up the card boxes they search in: every four share one. The archive then needs a quarter of the boxes, and almost the same things get found.

In attention, the searchers are the query heads, and the boxes are the keys and values. Only keys and values go into the KV cache, the notebook in which the model keeps intermediate results for all previous tokens. In Qwen3-8B, every four of the 32 query heads share one KV head. This is called **grouped-query attention** (GQA). According to the 2023 paper, quality comes close to the design with separate keys and values for every head, and speed comes close to the variant with a single KV head for all.

So Qwen3-8B needs 144 KiB of cache per token (kibibytes, 1,024 bytes each); with 32 KV heads it would be 576 KiB. At Ralf’s 65,536 tokens that’s 9 GiB (gibibytes, 1,024³ bytes, about 9.7 GB) instead of 36 GiB.

For Ralf this means: the 8 KV heads are not a cut-down version; without them, such long contexts would hardly work. His problem is the length. 5.2 GB of weights plus 9.7 GB of cache is almost 15 GB, plus compute buffers. On a machine with 16 GB, where the operating system needs room too, that no longer fits into the part the graphics unit can use. Ollama then puts layers on the CPU, which computes more slowly. At 16,384 tokens the cache would need only 2.25 GiB.

### In more detail: the math and the three designs

Bytes per token = 2 (key and value) × layers × KV heads × head dimension × bytes per number. Qwen3-8B in FP16: 2 × 36 × 8 × 128 × 2 = 147,456 bytes = 144 KiB. Times 65,536 tokens: 9,663,676,416 bytes = 9 GiB. With 32 KV heads, four times that, 36 GiB. At 8,192 tokens 1.125 GiB, at 16,384 tokens 2.25 GiB. The longest setting requires a supported context extension such as YaRN. More memory alone does not extend the model’s usable context.

Multi-head attention (2017): each of the 32 heads with its own keys and values. Multi-query attention (Shazeer, 2019): one KV head for all, which saves the most but costs some quality; for Qwen3-8B it would be 18 KiB per token. Grouped-query attention (Ainslie et al., 2023): the middle way, with groups.

Llama 3.1 8B also has 8 KV heads of 128 numbers, but 32 layers instead of 36: 128 KiB per token. The parameter count doesn’t appear in the formula.

## Try it

**Cache for a bigger model**

**Task:** Ralf is thinking about switching to a bigger model and wants to do the math beforehand this time. How many GiB of KV cache does it need at 16,384 tokens?

**From the config.json (example values)**

48 layers, 8 KV heads, 128 numbers per head. The cache is stored in FP16, so 2 bytes per number. Key and value are two lists.

KV cache in GiB

## Check

**Question 2 (check question):** Qwen3-8B and Llama 3.1 8B both have about 8 billion parameters and 8 KV heads of 128 numbers each. Qwen3-8B has 36 layers, Llama 3.1 8B has 32. Which one needs more cache at the same context length?

- A) Both the same, since they’re the same size.
- B) Llama 3.1 8B, because it can handle up to 131,072 tokens.
- C) Qwen3-8B, with 144 versus 128 KiB per token.

**Question 3 (check question):** Ralf wants the model to run entirely on the graphics unit again. What’s the most effective first step?

- A) Lower the context length to what his contracts need, for example 16,384 tokens.
- B) Load the model with fewer bits; then the cache shrinks along with it automatically.
- C) Look for a version with 32 KV heads, which spreads the load better.

**Question 4 (check question):** If sharing saves so much: why don’t all 32 query heads in Qwen3-8B just share a single KV head?

- A) That’s technically impossible; every head needs its own key.
- B) That saves even more memory but costs some quality.
- C) A single KV head would be slower because it does all the work alone.

## Done

**What you can do now**

- You read layers, KV heads and head dimension from a config.json and work out the cache per token.
- You can explain why several query heads share one KV head in grouped-query attention, and what that saves.
- When a customer complains about slow answers after raising the context length, you weigh weights plus cache against their memory and suggest a suitable length.

### Two cards for review

**Card 1:** Which figure do you not need to work out the KV cache per token?

- A) the number of layers
- B) the number of KV heads
- C) the number of parameters

**Card 2:** What is grouped-query attention, and what does it save in Qwen3-8B?

- [Next mission: 50 questions about the same manual: what does prompt caching save?](https://llm.yoga/en/einsatz-7-3.md)
- [Try it on your Mac](https://llm.yoga/en/mac-7-2.md)

## Read more in the chapter

The full text from the chapter, with the reasoning, calculations and sources:

- [Why there are only 8 KV heads: grouped-query attention](https://llm.yoga/en/07-kv-cache-1.md)
- [Worked example: Qwen3-8B and Llama 3.1 8B](https://llm.yoga/en/07-kv-cache-1.md)
- [How to make the cache smaller](https://llm.yoga/en/07-kv-cache-2.md)

## Answers: only reveal them after the learner has answered or explicitly asks

**Question 1:** The correct answer is B.
- A: The config.json of the original on Hugging Face also says 8. Fewer bits make the individual numbers smaller, not the number of heads.
- B, correct: This is called grouped-query attention. Only the KV heads go into the cache, which is why Qwen3-8B needs 144 instead of 576 KiB per token.
- C: It’s the other way around: in the 2017 transformer, every head had its own. Sharing methods were introduced in 2019 and 2023 and are used in models such as Qwen3 and Llama.

**KV cache in GiB** Answer: 2 × 48 × 8 × 128 × 2 = 196,608 bytes per token, times 16,384 tokens = 3,221,225,472 bytes = 3 GiB (about 3.2 GB).

**Question 2:** The correct answer is C.
- A: The parameter count doesn’t appear in the formula. What counts are layers, KV heads and head dimension.
- B: The maximum length says how far you can go, not how much cache each token takes. At the same length setting, Qwen3-8B needs more.
- C, correct: The four extra layers have their own keys and values. That’s why it pays to look at the config.json when comparing models.

**Question 3:** The correct answer is A.
- A, correct: The cache grows evenly with the length: 2.25 instead of 9 GiB. Together with the 5.2 GB of weights, that leaves plenty of room.
- B: Weights and cache are separate: qwen3:8b is already in 4 bits, but the cache is still in FP16. A cache with fewer bits is a separate setting (mission 9.3).
- C: That would be four times as much cache: 36 GiB at 65,536 tokens. The machine would hit its limit even faster.

**Question 4:** The correct answer is B.
- A: It is possible: Noam Shazeer proposed multi-query attention, with one KV head for all, in 2019.
- B, correct: Grouped-query attention is the middle way: according to the 2023 paper, quality comes close to models with separate keys and values per head, and speed close to the variant with one KV head.
- C: One shared KV head reduces the amount of data to read and can speed up generation. Its possible drawback is lower quality.

**Card 1:** Correct: C, the number of parameters The formula is 2 × layers × KV heads × head dimension × bytes per number. The parameter count doesn’t appear: Qwen3-8B and Llama 3.1 8B are about the same size but need 144 and 128 KiB per token.

**Card 2:** Several query heads share one KV head; in Qwen3-8B, every four of the 32. Only the 8 KV heads go into the cache: 144 instead of 576 KiB per token, 9 instead of 36 GiB at 65,536 tokens. According to the paper, quality stays close to the design with separate keys and values per head.
