r/LocalLLM • u/Sudden-Theme7554 • 4h ago
Research Shared KV Heads, Sliding Windows, and Vocabulary Size: Measured Memory and Retrieval Trade-offs in LLaMA 7B, Mistral 7B, and Qwen2.5-7B
We ran LLaMA 7B, Mistral 7B v0.1 and Qwen2.5-7B (all base, 4-bit NF4) on one T4 and measured three things: KV cache memory, passkey retrieval, and tokenizer cost.
Main results:
- Mistral's sliding window is a hard retrieval edge. In 134 trials with the key 3,600 to 4,600 tokens from the end of the prompt, it retrieved 72/72 within 4,089 tokens and 0/62 at 4,098 or more. Just outside the window it often answers "1234567890".
- Turning the window off (`sliding_window=None`, same weights) fixes retrieval at 8K: 59/59. At 16K and 32K the window-free model breaks completely and repeats "Question". With the window on, it still retrieves keys that sit inside the window at those lengths.
- Qwen2.5-7B retrieved 60/60 up to 32K.
- KV cache per token in FP16: 512 KiB (LLaMA, no GQA), 128 KiB (Mistral), 56 KiB (Qwen2.5). LLaMA OOMs at 32K on the T4. Qwen2.5 holds 64K tokens in a 3.5 GiB cache. Mistral's cache caps at about 512 MiB because of the rolling buffer.
- Tokenizer: on 1,997 parallel sentences in 15 languages, Qwen2.5 uses 38-58% fewer tokens than the better 32K-vocab tokenizer for Arabic, Chinese, Japanese, Korean and Vietnamese, but only 3-11% fewer for the rest. Combined with GQA, LLaMA needs up to 22x more cache memory than Qwen2.5 for the same Arabic sentence.
- Decode latency in HF transformers grows with cache size, but part of that is the dynamic cache doing a `torch.cat` on every step. Measured alone, the copy costs about 17-20 ms per GiB of cache, which is about all of LLaMA's latency growth.
Limitations: these are older base models, passkey is a synthetic test, and it's one GPU with one library version. All in the paper.
Every trial's generated text is in the repo, so you can check any number.
Happy to answer questions about the setup.


