Two files with the same architecture name,
The formula is not wrong. It gets applied to the wrong set of blocks. K and V are appended per layer, so the cost is (KV heads) x (key width + value width) x (bytes per element) for every block that ships an
That is the same habit as scanning a GGUF before you load it: read the manifest, do not trust the label. The GGUF specification defines the header as a list of key and value pairs followed by one entry per tensor, with a type code and a byte offset. A script that walks it needs sixty lines and no dependencies, and it answers in a third of a second on a 15.7 GB file because it stops at the first tensor data offset.
So I wrote the one I wanted when planning the 32k measurement. It prints four things: weight bytes summed from the tensor table, the count of blocks that actually write a cache, the KV bytes per token for the cache type you choose, and resident VRAM per window against a budget.
The quant census is worth its own line. A file sold as Q4_K_M is 231 Q4_K tensors, 27 Q6_K tensors and 166 F16 tensors. That 0.85 GiB of F16 in a 6.13 GiB file is the embedding and the norms, and it does not shrink when you pick a smaller quant of the same model. If your budget is failing by 800 MiB, this is where it went.
Then the measurements. Resident means the median of five
Three windows, 48 MiB of error at the widest, on a model I had never measured before. The dense case is the easy one: 40 blocks, all of them attention, 160 KiB per token, and the card reproduced 162.
The two hybrid files miss in opposite directions, so I quote them as a band of plus or minus 15 percent rather than a number. I did not chase which blocks account for the gap on the 27B, and until I do, the honest reading is that the planner bounds the cache and does not nail it on state-space architectures. On dense models it nails it.
The other thing the same header answers: what a quantised cache buys. With
Two point three gigabytes recovered by changing one setting. On llama.cpp's server that setting is
What the script does not do, in the order those gaps will bite you. It counts every expert tensor in the weight total, so a MoE file with CPU expert offload will read high.
The script is too long for one post body, so it follows whole in the next post. The attached file, gguf-vram-planner-2026-12-05.md, carries the same source plus the raw output of every run quoted here. One file, standard library, no model download required. The link below asks for an account first.
View attachment gguf-vram-planner-2026-12-05.md
The one number I would want from you: if your window were 16k instead of 32k, what would you spend the recovered 2 GiB on, a wider context, a bigger quant, or a second model kept resident?
qwen35, disagree about the number a memory plan depends on. In the 9B file, attention.head_count_kv is an array of 32 entries, most of them zero and eight of them four. In the 27B file it is the single number 4, written next to a block count of 65. Read that scalar as per-block and the usual formula gives 260 KiB of cache per token, which at 32k is 8.1 GiB. The tensor table in the same file says 68 KiB, and 2.1 GiB. The second number is the one the card agreed with.The formula is not wrong. It gets applied to the wrong set of blocks. K and V are appended per layer, so the cost is (KV heads) x (key width + value width) x (bytes per element) for every block that ships an
attn_k and an attn_v tensor. Hybrid models keep attention in a minority of layers and give the rest a fixed-size recurrent state that does not grow with the window. Qwen3.5 9B keeps 8 of 32. The 27B keeps 17 of 65 and still writes 4 as its KV head count. The metadata is a summary written by whoever converted the file. The tensor names are the inventory, and they answer before a byte of weights is loaded.That is the same habit as scanning a GGUF before you load it: read the manifest, do not trust the label. The GGUF specification defines the header as a list of key and value pairs followed by one entry per tensor, with a type code and a byte offset. A script that walks it needs sixty lines and no dependencies, and it answers in a third of a second on a 15.7 GB file because it stops at the first tensor data offset.
So I wrote the one I wanted when planning the 32k measurement. It prints four things: weight bytes summed from the tensor table, the count of blocks that actually write a cache, the KV bytes per token for the cache type you choose, and resident VRAM per window against a budget.
Code:
$ python gguf_vram.py qwen3.5-9b.gguf --ctx 4096,32768,65536 --reserve 280
arch qwen35 32 blocks 16 heads head_dim 256 kv heads [0, 4]
weights 6.13 GiB in 883 tensors
cache 8 of 32 blocks write KV (24 state-space)
kv/token 32.0 KiB (f16)
quants Q4_K 231t 3.82G, Q6_K 27t 1.46G, F16 166t 0.85G, F32 459t 0.01G
ctx 4096 | kv 128.0 | total 6685.5 MiB | fits
ctx 32768 | kv 1024.0 | total 7581.5 MiB | fits
ctx 65536 | kv 2048.0 | total 8605.5 MiB | fits
The quant census is worth its own line. A file sold as Q4_K_M is 231 Q4_K tensors, 27 Q6_K tensors and 166 F16 tensors. That 0.85 GiB of F16 in a 6.13 GiB file is the embedding and the norms, and it does not shrink when you pick a smaller quant of the same model. If your budget is failing by 800 MiB, this is where it went.
Then the measurements. Resident means the median of five
nvidia-smi samples taken after the model settled, minus the reading before it loaded, on a 4090 with 24,564 MiB and Ollama 0.40.19. The script's --check mode reads those runs back and prints the residual:
Code:
$ python ctx_steady_today.py qwen3:14b 8192,16384,32768
qwen3:14b ctx 8192 | resident 10396 MiB
qwen3:14b ctx 16384 | resident 11692 MiB
qwen3:14b ctx 32768 | resident 14284 MiB
$ python gguf_vram.py qwen3-14b.gguf --ctx 8192,16384,32768 --reserve 280 \
--check state/bench_ctx_steady_2026-12-05.json --tag qwen3:14b
check qwen3:14b ctx 8192: measured 10396, predicted 10401, error -5 MiB
check qwen3:14b ctx 16384: measured 11692, predicted 11681, error +11 MiB
check qwen3:14b ctx 32768: measured 14284, predicted 14241, error +43 MiB
Three windows, 48 MiB of error at the widest, on a model I had never measured before. The dense case is the easy one: 40 blocks, all of them attention, 160 KiB per token, and the card reproduced 162.
Code:
model file blocks kv blocks KiB/tok ctx measured predicted
qwen3:14b Q4_K_M 8.63 GiB 40 40 160 32,768 14,284 14,241
qwen3.5:9b Q4_K_M 6.13 GiB 32 8 32 32,768 7,588 7,582
qwen3.5:9b Q4_K_M 6.13 GiB 32 8 32 65,536 8,676 8,606
qwen38liber IQ4_XS 14.62 GiB 65 17 68 32,768 17,968 17,424
growth between the two widest windows, which is what the cache term predicts
model window measured predicted error
qwen3:14b 8k -> 32k 3,888 MiB 3,840 MiB +1.2%
qwen3.5:9b 4k -> 64k 2,137 MiB 1,920 MiB -10.1%
qwen38liber 8k -> 32k 1,410 MiB 1,632 MiB +15.7%
The two hybrid files miss in opposite directions, so I quote them as a band of plus or minus 15 percent rather than a number. I did not chase which blocks account for the gap on the 27B, and until I do, the honest reading is that the planner bounds the cache and does not nail it on state-space architectures. On dense models it nails it.
The other thing the same header answers: what a quantised cache buys. With
--kv q8_0 the 14B at 32k drops from 160 to 85 KiB per token. I restarted Ollama with OLLAMA_KV_CACHE_TYPE=q8_0 and measured the same window again, which is the only reason I am willing to quote it:
Code:
qwen3:14b, 32,768 tokens, f16 weights
kv f16 14,284 MiB resident
kv q8_0 11,890 MiB resident (-2,394 MiB measured, -2,400 MiB predicted)
Two point three gigabytes recovered by changing one setting. On llama.cpp's server that setting is
--cache-type-k and --cache-type-v, and it needs flash attention enabled; the server documentation lists the accepted types. I did not measure the quality difference between an f16 and a q8_0 cache, and I would not ship it on a model doing long-context retrieval without that test. The KV cache arithmetic in Raschka's gallery is the reference I used to check the per-layer form.What the script does not do, in the order those gaps will bite you. It counts every expert tensor in the weight total, so a MoE file with CPU expert offload will read high.
--ngl takes a fraction of the layers, not a layer list, so a two-card split is outside it. It predicts allocation, not speed: prompt processing depends on the compute buffer and on how much of the model runs on the CPU, so a file that fits can still prefill slowly. And the reserve term is per machine. 280 MiB matched this 4090 under Ollama; on Windows with a browser open it will not. Fit it once with --check against a run you measured yourself.The script is too long for one post body, so it follows whole in the next post. The attached file, gguf-vram-planner-2026-12-05.md, carries the same source plus the raw output of every run quoted here. One file, standard library, no model download required. The link below asks for an account first.
View attachment gguf-vram-planner-2026-12-05.md
The one number I would want from you: if your window were 16k instead of 32k, what would you spend the recovered 2 GiB on, a wider context, a bigger quant, or a second model kept resident?
Last edited: