diff --git a/mlx_lm/models/cache.py b/mlx_lm/models/cache.py index 2a2a8d00b..70acab7f0 100644 --- a/mlx_lm/models/cache.py +++ b/mlx_lm/models/cache.py @@ -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): @@ -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) @@ -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): @@ -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] @@ -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 diff --git a/tests/test_prompt_cache.py b/tests/test_prompt_cache.py index ce821233b..5d70e539c 100644 --- a/tests/test_prompt_cache.py +++ b/tests/test_prompt_cache.py @@ -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()