KIVI, End to End: How 2-bit KV Cache Quantization Works Inside VeloxQuant-MLX
Imagine running a language model locally on your Mac. You load the model, start a conversation, and everything works smoothly. But as the conversation gets longer, memory consumption keeps increasing. Eventually, you may find that your Mac is running out of available memory, even though the model itself has already been loaded.
Why does this happen?
One important reason is something called the KV cache.
Every time a language model processes another token, it needs information about the tokens it has already seen. Rather than calculating that information again and again, the model stores it in memory. This makes text generation much faster, but it also creates a problem: the longer the conversation, the more memory the model needs.
This is particularly important when running large language models on consumer hardware. A Mac with 24 GB of unified memory, for example, has to share that memory between model weights, the KV cache, the operating system, and other applications.
One way to address this problem is to store the KV cache using fewer bits.
This is where KIVI comes in.
KIVI is a research method that reduces the precision of cached keys and values. Instead of storing every number as a 16-bit floating-point value, it represents most of them using only 2 bits. The method does not require additional model training or a calibration dataset.
That sounds simple, but there is an important detail: KIVI does not compress every number in the same way. It uses different grouping strategies for keys and values because they behave differently inside the attention mechanism.
In this article, we will follow that idea from the mathematics to its implementation inside VeloxQuant-MLX.
We will explore:
- Why the KV cache consumes so much memory.
- What quantization actually does to a number.
- How KIVI decides which numbers should share a quantization scale.
- Why keys and values use different grouping directions.
- How the newest tokens are protected using a residual window.
- How the implementation works inside VeloxQuant-MLX.
- How custom Metal kernels accelerate the quantization operation.
- What our experiments reveal about memory consumption, generation speed, and model quality.
- What it takes to deliver real memory savings, and what is still missing.
We will also distinguish between what the original KIVI paper demonstrates and what our current implementation actually does.
That distinction matters because a cache that contains quantized values is not necessarily a cache that occupies less memory.
