fix: reset DeltaNet recurrent state on prompt cache trim#14
Merged
raullenchai merged 3 commits intomainfrom Mar 15, 2026
Merged
fix: reset DeltaNet recurrent state on prompt cache trim#14raullenchai merged 3 commits intomainfrom
raullenchai merged 3 commits intomainfrom
Conversation
Qwen3.5 uses a hybrid architecture: 75% Gated DeltaNet layers (ArraysCache, non-trimmable) + 25% full attention layers (KVCache, trimmable). The prompt cache logic previously skipped non-trimmable caches during trim, leaving stale recurrent state from prior requests. This could corrupt multi-turn conversations where the DeltaNet hidden state contained information from tokens no longer in the prompt. - Add _reset_all_caches() to handle both KVCache and ArraysCache - Detect non-trimmable caches and force full recomputation when needed - Add 8 unit tests covering all cache reset scenarios Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ache reset P1-a: Pure non-trimmable models (e.g. Mamba/DeltaNet with only ArraysCache layers) never triggered cache reset — exact repeat reused dirty recurrent state. Now detected via can_trim_prompt_cache() and cache is recreated from scratch. P1-b: CacheList wrapping non-trimmable sub-caches (e.g. CacheList(ArraysCache, KVCache)) was skipped entirely because CacheList.is_trimmable() returns all(...). Same fix — recreate when not trimmable. Also fixed: CacheList has no .offset property — use .size() fallback for trim amount calculation on wrapper types. P2: Added 7 regression tests using real upstream cache types (ArraysCache, KVCache, CacheList) instead of only MockCacheEntry. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Moves top-level `from mlx_lm.models.cache import ...` behind pytest.importorskip so mock-only tests still collect without mlx-lm. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Summary
ArraysCache) + 25% full attention (trimmableKVCache)Changes
_reset_all_caches()method to handle bothKVCache(trim to zero) andArraysCache(set entries to None)_prepare_cache_for_prompt()and force full prompt recomputation when trimming is neededtests/test_deltanet_cache.pywith 8 unit tests covering: no overlap, partial overlap, exact repeat, pure KV regression, reset verification, growing conversation scenariosTest plan
🤖 Generated with Claude Code