Skip to content

Commit 3cd9a52

Browse files
authored
Fix ArraysCache extend (#1177)
1 parent 2f1ab85 commit 3cd9a52

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

mlx_lm/models/cache.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,9 @@ def extend(self, other):
644644
In-place extend this cache with the other cache.
645645
"""
646646

647+
a_batch = self.batch_size
648+
b_batch = other.batch_size
649+
647650
def cat(a, b):
648651
shape = dtype = None
649652
if a is not None:
@@ -657,9 +660,9 @@ def cat(a, b):
657660
return None
658661

659662
if a is None:
660-
a = mx.zeros((self.batch_size,) + shape[1:], dtype=dtype)
663+
a = mx.zeros((a_batch,) + shape[1:], dtype=dtype)
661664
if b is None:
662-
b = mx.zeros((other.batch_size,) + shape[1:], dtype=dtype)
665+
b = mx.zeros((b_batch,) + shape[1:], dtype=dtype)
663666

664667
return mx.concatenate([a, b])
665668

tests/test_prompt_cache.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,14 @@ def test_arrays_cache_extend_with_empty(self):
743743
self.assertEqual(empty2[0].shape, (4, 4, 8))
744744
self.assertEqual(empty2[1].shape, (4, 4))
745745

746+
# Extend content with empty
747+
content = ArraysCache.merge((c1, c2))
748+
empty2 = ArraysCache.merge((ArraysCache(2), ArraysCache(2)))
749+
content.extend(empty2)
750+
self.assertEqual(content[0].shape, (4, 4, 8))
751+
self.assertEqual(content[1].shape, (4, 4))
752+
self.assertEqual(content.make_mask(10).shape, (4, 10))
753+
746754
# multiple empty extensions accumulate correctly
747755
stepwise = ArraysCache.merge((c1,))
748756
stepwise.extend(ArraysCache(2))

0 commit comments

Comments
 (0)