Skip to content

handle empty slots in batch cache extract and arrayscache merge - #2110

Open
DevMortimer wants to merge 1 commit into
ml-explore:mainfrom
DevMortimer:fix-batch-cache-empty-extract
Open

DevMortimer wants to merge 1 commit into
ml-explore:mainfrom
DevMortimer:fix-batch-cache-empty-extract

Conversation

@DevMortimer

Copy link
Copy Markdown

how i discovered this:

  • i've been running a fine-tuned qwen3.5 hybrid model (arrayscache + kvcache) locally on my m5 mac to score concurrent coding-agent sessions in batches
  • when cancelling or extracting a session from batchgenerator while it was still sitting in a newly merged batch before prefill ran, .extract() crashed with typeerror: 'nonetype' object is not subscriptable
  • while checking arrayscache right next to it in cache.py, i noticed empty() only checked self.cache[0] is none and merge() either silently wiped later slots to none or raised stopiteration when a slot was still none

what changed:

  • guard none and zero-length slots in arrayscache.extract, batchkvcache.extract, and batchrotatingkvcache.extract so extracting an unpopulated slot returns an empty single cache
  • check all(c is none for c in self.cache) in arrayscache.empty() and skip all-none slots in arrayscache.merge()
  • add test_extract_and_merge_empty_or_partial_caches in tests/test_prompt_cache.py

before (main, m5 mac, mlx 0.32.4):

  • ArraysCache.merge([ArraysCache(2), ArraysCache(2)]).extract(0) -> TypeError: 'NoneType' object is not subscriptable (same for BatchKVCache and BatchRotatingKVCache)
  • ArraysCache with only slot 0 populated -> ArraysCache.merge(...) raises StopIteration on slot 1
  • ArraysCache with only slot 1 populated -> empty() returns True and ArraysCache.merge(...) silently drops slot 1 ([None, None])
$ python -m unittest tests.test_prompt_cache.TestPromptCache.test_extract_and_merge_empty_or_partial_caches -v
test_extract_and_merge_empty_or_partial_caches (tests.test_prompt_cache.TestPromptCache.test_extract_and_merge_empty_or_partial_caches) ... ERROR
...
  File ".../mlx_lm/models/cache.py", line 671, in extract
    cache.cache = [c[idx : idx + 1] for c in self.cache]
                   ~^^^^^^^^^^^^^^^
TypeError: 'NoneType' object is not subscriptable

FAILED (errors=1)

after (this branch):

$ python -m unittest tests.test_prompt_cache -v
...
Ran 26 tests in 17.396s

OK
  • ☑️ I understand it is strictly prohibited to use AI to write PR description
  • AI usage disclosure:
    • ai helped check the other batch cache extract paths in cache.py and write the unit test; reviewed and tested locally on m5.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant