Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions mlx_lm/models/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ def cat(a, b):

def extract(self, idx):
cache = ArraysCache(len(self.cache))
cache.cache = [c[idx : idx + 1] for c in self.cache]
cache.cache = [c[idx : idx + 1] if c is not None else None for c in self.cache]
return cache

def prepare(self, lengths=None, **kwargs):
Expand Down Expand Up @@ -706,7 +706,9 @@ def merge(cls, caches):
return cache

for e in range(n_state):
c_init = next(iter(c[e] for c in caches if c[e] is not None))
c_init = next((c[e] for c in caches if c[e] is not None), None)
if c_init is None:
continue
shape = list(c_init.shape)
shape[0] = B
cache[e] = mx.zeros(shape, c_init.dtype)
Expand All @@ -717,7 +719,7 @@ def merge(cls, caches):
return cache

def empty(self):
return self.cache[0] is None
return all(c is None for c in self.cache)

@property
def nbytes(self):
Expand Down Expand Up @@ -1046,8 +1048,12 @@ def pad(c):

def extract(self, idx):
cache = KVCache()
if self.keys is None:
return cache
mx.eval(self.left_padding)
padding = self.left_padding.tolist()[idx]
if padding >= self._idx:
return cache
cache.keys = mx.contiguous(self.keys[idx : idx + 1, :, padding : self._idx])
cache.values = mx.contiguous(self.values[idx : idx + 1, :, padding : self._idx])
cache.offset = cache.keys.shape[2]
Expand Down Expand Up @@ -1390,10 +1396,14 @@ def pad(c):
self._offset = max(self._offset, other._offset)

def extract(self, idx):
mx.eval(self.left_padding, self.offset)
cache = RotatingKVCache(self.max_size)
if self.keys is None:
return cache
mx.eval(self.left_padding, self.offset)
padding = max(0, self.left_padding.tolist()[idx])
offset = self.offset.tolist()[idx]
if offset <= 0:
return cache
cache.keys = self.keys[idx : idx + 1]
cache.values = self.values[idx : idx + 1]
cache._idx = self._idx
Expand Down
41 changes: 41 additions & 0 deletions tests/test_prompt_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,47 @@ def test_window_mask_with_full_kv_cache(self):
expected = create_causal_mask(1, offset=32, window_size=4)
self.assertTrue(mx.array_equal(mask, expected))

def test_extract_and_merge_empty_or_partial_caches(self):
# Extracting from merged empty batch caches returns empty single caches
arr_batch = ArraysCache.merge([ArraysCache(2), ArraysCache(2)])
arr_ex = arr_batch.extract(0)
self.assertTrue(arr_ex.empty())
self.assertEqual(arr_ex.cache, [None, None])

kv_batch = BatchKVCache.merge([KVCache(), KVCache()])
kv_ex = kv_batch.extract(0)
self.assertTrue(kv_ex.empty())
self.assertEqual(kv_ex.offset, 0)

rot_batch = BatchRotatingKVCache.merge([RotatingKVCache(8), RotatingKVCache(8)])
rot_ex = rot_batch.extract(0)
self.assertTrue(rot_ex.empty())
self.assertEqual(rot_ex.offset, 0)

# Extracting an empty slot from a mixed batch preserves empty()
c_full = KVCache()
c_full.update_and_fetch(mx.ones((1, 2, 4, 8)), mx.ones((1, 2, 4, 8)))
mixed_kv = BatchKVCache.merge([KVCache(), c_full])
self.assertTrue(mixed_kv.extract(0).empty())
self.assertEqual(mixed_kv.extract(1).offset, 4)

r_full = RotatingKVCache(8)
r_full.update_and_fetch(mx.ones((1, 2, 4, 8)), mx.ones((1, 2, 4, 8)))
mixed_rot = BatchRotatingKVCache.merge([RotatingKVCache(8), r_full])
self.assertTrue(mixed_rot.extract(0).empty())
self.assertEqual(mixed_rot.extract(1).offset, 4)

# ArraysCache with partial slots preserves populated slots and None slots
a1 = ArraysCache(2)
a2 = ArraysCache(2)
a2[1] = mx.ones((1, 4))
self.assertFalse(a2.empty())
merged_slot1 = ArraysCache.merge([a1, a2])
self.assertIsNone(merged_slot1[0])
self.assertEqual(merged_slot1[1].shape, (2, 4))
self.assertIsNone(merged_slot1.extract(0)[0])
self.assertEqual(merged_slot1.extract(1)[1].shape, (1, 4))


if __name__ == "__main__":
unittest.main()