Skip to content
Draft
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
8 changes: 7 additions & 1 deletion mlx_lm/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ def remove(self, model, tokens):
self._lru_checkpoints.remove((model, tokens))

def pop(self):
if not self._lru and not self._lru_checkpoints:
raise IndexError("pop from empty CacheOrder")
if len(self._lru) >= len(self._lru_checkpoints):
return self._lru.popleft()
else:
Expand Down Expand Up @@ -344,9 +346,13 @@ def trim_to(
while len(self._lru) > n_sequences:
model, tokens = self._lru.pop()
self._delete(model, tokens)
while self._n_bytes > n_bytes:
while self._n_bytes > n_bytes and len(self._lru) > 0:
model, tokens = self._lru.pop()
self._delete(model, tokens)
if self._n_bytes > n_bytes:
raise RuntimeError(
"LRUPromptCache byte accounting drifted out of sync with cache order"
)

def log_cache_stats(self):
ncaches, nbytes = len(self), self.nbytes
Expand Down
24 changes: 24 additions & 0 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,30 @@ def test_lru_bytes(self):
self.assertEqual(c, None)
self.assertEqual(t, [3, 4])

def test_trim_to_zero_bytes_on_empty_cache(self):
cache = LRUPromptCache(max_size=10)
# Should not raise IndexError on empty cache
cache.trim_to(n_bytes=0)
self.assertEqual(len(cache), 0)

def test_trim_to_raises_on_inconsistent_byte_accounting(self):
cache = LRUPromptCache(max_size=10)
cache._n_bytes = 1

with self.assertRaisesRegex(RuntimeError, "byte accounting"):
cache.trim_to(n_bytes=0)

def test_trim_to_zero_bytes_evicts_all(self):
cache = LRUPromptCache(max_size=10)
model = ("test", None, None)
cache.insert_cache(model, [1, 2], [MockCache("aaa")])
cache.insert_cache(model, [3, 4], [MockCache("bbb")])
self.assertEqual(len(cache), 2)

cache.trim_to(n_bytes=0)
self.assertEqual(len(cache), 0)
self.assertEqual(cache.nbytes, 0)


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