Sehej Jain

Quantized cache storage in MLX LM

A proposed upstream fix for how the inference library calculates the storage width of its packed key and value caches.

Read the pull request

Packing five bits at a time

During text generation, a model saves keys and values from earlier tokens. MLX can compress them by packing several low-precision numbers into 32-bit words. For example, 64 five-bit values occupy 320 bits, or ten words. Counting how many whole values fit in one word gives the wrong answer when a value spans two words.

Checking the cache against MLX

My patch sizes the allocation from the total number of bits. The regression tests compare the cache with MLX’s quantizer at six bit widths and check that the stored values survive cache growth and a save-and-load round trip.