Fix ArraysCache missing is_trimmable/trim for hybrid model prompt cache#1254
Open
EagerofLight wants to merge 1 commit intoml-explore:mainfrom
Open
Fix ArraysCache missing is_trimmable/trim for hybrid model prompt cache#1254EagerofLight wants to merge 1 commit intoml-explore:mainfrom
EagerofLight wants to merge 1 commit intoml-explore:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Hybrid models (Qwen3.5, Qwen3.6, Qwen3-Next) mix
ArraysCache(DeltaNet recurrent layers)with
KVCache(attention layers).ArraysCacheinheritsis_trimmable() → Falsefrom_BaseCacheand has notrim()method, socan_trim_prompt_cache()returnsFalseforthe entire hybrid cache.
This silently breaks:
mlx_lm.server—fetch_nearest_cache()skips the trim pathinsert_cache()can't evict redundant prefixesgenerate.pyraisesValueErroron non-trimmable cachesRoot cause
Solution
Add
is_trimmable()→Trueandtrim(n)toArraysCache.Recurrent state is a compressed summary of all past tokens — it can't be partially
rolled back like KV cache. So
trim(n)resets the recurrent state to empty; therecurrent layers recompute on next forward while KVCache layers still benefit from
their own trim. For exact-match reuse (same prompt repeated),
trim()is nevercalled and the full state is preserved.
Verification (Qwen3.5-4B-4bit, M4 16GB,
mlx_lm.server)Before this fix, prefix reuse and extended prompt got 0 cached tokens.
Fixes #1162