Skip to main content
Back to Community & Earn Hub
Q&A Questionllms +150 pt Bounty

Llama.cpp / GGUF Server KV-Cache Quantization: Why does q4_0 KV cache degrade tool-calling JSON accuracy compared to q8_0 on 32k contexts?

@arjun_nair244 days ago 1509 views

We serve Qwen2.5-Coder-14B-Instruct-Q4_K_M.gguf locally using llama-server on an RTX 4080 16GB.

The 32k VRAM Squeeze

With default f16 KV cache, 32k context pushes VRAM past 16GB and spills layers onto slow system RAM. So we enabled 4-bit KV-cache quantization on both Keys and Values:

bash
llama-server -m qwen2.5-coder-14b-q4_k_m.gguf -c 32768 -ngl 99 -fa -ctk q4_0 -ctv q4_0

VRAM dropped to a comfortable 12.8GB! However, once agent trajectories cross ~16k tokens:

  • Exact tool parameter names from the system prompt start getting misspelled (file_path -> filepath_id).
  • Multi-line JSON tool calls drop closing braces or repeat keys.

Why does q4_0 KV quantization break exact token copying in long contexts, and can we use asymmetric KV settings (q8_0 vs q4_0) to recover accuracy within 16GB VRAM?

1 Answers

Community Answers

@tanvi_kulkarni Accepted Answer4 days ago

In Transformer attention (softmax(QK^T / sqrt(d)) * V), Keys (K) and Values (V) have completely different sensitivity to 4-bit quantization:

  • Keys (K) only compute relative dot-product attention scores—they tolerate moderate quantization noise surprisingly well.
  • Values (V) carry the exact residual information copied forward into output logits (exact JSON syntax, variable spellings, and closing braces!). Quantizing Values (-ctv) to q4_0 corrupts high-precision token copying across 16k+ tokens!

The Production Fix: Use q8_0 (or Asymmetric -ctk q8_0 -ctv f16)

For 14B–32B models handling structured JSON / tool calling, never use -ctv q4_0:

bash
# Sweet spot for 16GB VRAM at 24k-32k context with 99.8% tool-calling accuracy:
llama-server -m qwen2.5-coder-14b-q4_k_m.gguf   -c 28672   -ngl 99   -fa   -ctk q8_0   -ctv q8_0

VRAM & Accuracy Tradeoff (Qwen2.5-Coder-14B, 32k Context)

  • -ctk f16 -ctv f16: ~6.2 GB KV cache (OOMs 16GB card with weights), 100% baseline JSON accuracy.
  • -ctk q4_0 -ctv q4_0: ~1.7 GB KV cache, severe JSON key drift past 16k.
  • -ctk q8_0 -ctv q8_0 (+ -fa): ~3.2 GB KV cache (fits cleanly inside 16GB RTX 4080 with -c 28672), virtually zero perplexity or JSON schema degradation.

Have a solution or working configuration?

Sign in or create a free account to answer this question (+10 pts, +25 pts if accepted) and tag helpful AIStoreHub repositories.