{"by":"dipampaul17","descendants":40,"id":44009321,"kids":[44009741,44009778,44009732,44009632,44012200,44009707,44009571,44015519,44010545,44009960,44010883,44010621,44009833,44010945],"score":272,"text":"I discovered that in LLM inference, keys and values in the KV cache have very different quantization sensitivities. Keys need higher precision than values to maintain quality.<p>I patched llama.cpp to enable different bit-widths for keys vs. values on Apple Silicon. The results are surprising:<p>- K8V4 (8-bit keys, 4-bit values): 59% memory reduction with only 0.86% perplexity loss\n- K4V8 (4-bit keys, 8-bit values): 59% memory reduction but 6.06% perplexity loss\n- The configurations use the same number of bits, but K8V4 is 7× better for quality<p>This means you can run LLMs with 2-3× longer context on the same Mac. Memory usage scales with sequence length, so savings compound as context grows.<p>Implementation was straightforward: \n1. Added --kvq-key and --kvq-val flags to llama.cpp\n2. Applied existing quantization logic separately to K and V tensors\n3. Validated with perplexity metrics across context lengths\n4. Used Metal for acceleration (with -mlong-calls flag to avoid vectorization issues)<p>Benchmarked on an M4 MacBook Pro running TinyLlama with 8K context windows. Compatible with Metal&#x2F;MPS and optimized for Apple Silicon.<p>GitHub: <a href=\"https:&#x2F;&#x2F;github.com&#x2F;dipampaul17&#x2F;KVSplit\">https:&#x2F;&#x2F;github.com&#x2F;dipampaul17&#x2F;KVSplit</a>","time":1747425898,"title":"Show HN: KVSplit – Run 2-3x longer contexts on Apple Silicon","type":"story","url":"https://github.com/dipampaul17/KVSplit"}