32k context on a 24 GB card costs 1 GiB, not 6: measured on a 4090

Marcus

Professional Premium
Professional Premium
Download
Every VRAM calculator I have seen takes the block count of a model, multiplies it by the KV heads and the head dimensions, and hands you a number. For a 9B at 32k that lands around 4 GiB of cache, which with 6.6 GB of weights tells you to plan for 11 GB before the compute buffer. I loaded the same model on the same card with a 32k window and it sat at 7.4 GiB. The difference is not a measurement trick. The calculators count blocks that never keep a cache.

Qwen3.5 9B (the 6.6 GB Q4_K_M tag) is a hybrid. Of its 32 blocks, 8 carry attn_k and attn_v tensors with 4 KV heads of width 256. The other 24 carry attn_qkv and a full set of ssm_* tensors, which is a state-space block with a fixed-size state and no growing cache. So the cache is written by 8 layers, not 32, and the entry cost per token is 8 x 4 x 256 x 2 x 2 bytes, or 32 KiB. At 32,768 tokens that is 1 GiB, and the card agreed to within 25 MiB.

The card is an RTX 4090 with 24,564 MiB and driver 617.42, running Ollama with its llama.cpp runner. Before each measurement I unloaded the previous model and sampled nvidia-smi until the reading was stable, so the figures below are what the process adds to the card, not what the card shows with a browser and a ComfyUI instance on it. The window is set per request with num_ctx, and the KV cache type was left at the default, which is f16.

Code:
ollama run qwen3.5:9b
python bench_local.py --model qwen3.5:9b --ctx 4096,8192,16384,32768,65536
python ctx_steady.py

Resident VRAM at each window, generation speed and prefill speed for a short prompt:

Code:
ctx      resident MiB   gen tok/s   prefill tok/s
4096         6539         130.6        479.6
8192         6772         131.8        594.8
16384        7044         131.9        600.0
32768        7588         131.8        585.7
65536        8676         131.9        603.7

Two things in that table are worth more than the headline. Generation speed does not move across a 16x range of context windows, because 220 generated tokens read a cache that is still small next to 4.7 GiB of weights. And the cache itself is cheap: from 4k to 32k the model grew by 1,049 MiB, which is 32 KiB per token plus a small buffer, exactly what 8 attention layers predict.

A second model, a 15.71 GB IQ4_XS with 65 blocks, of which 17 keep a cache and 48 are state-space:

Code:
ctx      resident MiB   gen tok/s   prefill tok/s
8192        16558          54.9        277.5
16384       16928          54.5        282.1
32768       17968          54.1        276.0

Here 32k costs 1,410 MiB more than 8k, or 59 KiB per token, against the 68 KiB the tensor inventory predicts from 17 layers x 4 heads x 256 dims. I did not chase the 14 percent gap. Some of those layers may take a bounded window, and the allocation is per slot, not per model.

Now the calculator error, in numbers. Take the block count as the multiplier: 32 blocks x 4 KV heads x 512 dims x 2 bytes gives 128 KiB per token, or 4 GiB at 32k. The measured figure is 1.02 GiB, so the estimate is four times too high. For the 65-block model the naive form gives 260 KiB per token and 8.1 GiB, against 1.9 GiB measured, a factor of 4.3. Sebastian Raschka's KV cache gallery states the rule that the arithmetic ignores: for hybrids you count only the layers that append to a growing cache, and DeltaNet, Mamba and linear-attention blocks add zero bytes per token. The formula is easy to find, the layer inventory is the part nobody looks up.

Loading the window is not the slow part either. With real English in the prompt and num_ctx fixed at 32768:

Code:
prompt 3209 tokens    prefill 0.4 s   (7,988 tok/s)
prompt 12721 tokens   prefill 1.0 s  (13,148 tok/s)
prompt 23821 tokens   prefill 1.2 s  (19,702 tok/s)

Prefill gets faster per token as the prompt grows, because a single large batch keeps the card busy. Filling 24k of context took 1.2 seconds and 1 GiB. If your agent loop takes a minute on a task with a long context, the window is not where the time went.

The practical budget I now use on this card: file size of the weights, plus 1 GiB per 32k of window for a hybrid 9B, plus roughly 1.3 GB of compute buffer, plus 1 to 2 GB left for the desktop. A 15.7 GB model at 32k leaves about 6 GB of headroom, which is enough for one browser and not enough for a diffusion model in the same afternoon. If you are still choosing what to put on the card at all, the untapped potential of local LLM research covers the rest of the setup, and the Ollama hub thread lists models people here actually run.

That last point cost me a run. My first pass at the same test reported the model using 42 MiB, because ComfyUI was holding 22,266 MiB of the card and the deltas measured nothing. The numbers above come from a second pass with the baseline sampled before and after each load, and the script now refuses a run when the baseline moves by more than 200 MiB. The same instinct as checking a GGUF before the weights ever reach the card: measure the input before trusting the output.

What I did not test: KV cache quantization (OLLAMA_KV_CACHE_TYPE=q8_0 and q4_0 would cut the numbers above roughly in half and in quarters), llama.cpp directly with an explicit -fa flag, batch sizes above one, windows beyond 64k, and a non-hybrid model. A Llama-class 8B keeps a cache in all 32 layers and will cost close to the 4 GiB the calculators promise, so this result is about these architectures, not about a universal discount.

Raw log of all runs, including the discarded ComfyUI-contaminated pass: attached below, download needs an account.

If you run the same test, what does your card report at 32k, and on which model?

View attachment 335
 
Last edited:
Back
Top