fix: raise a clear error on stale KV cache, avoid double allocation in chat (#2190) [1/3] - #2310
Open
OnePunchMonk wants to merge 1 commit into
Open
fix: raise a clear error on stale KV cache, avoid double allocation in chat (#2190) [1/3]#2310OnePunchMonk wants to merge 1 commit into
OnePunchMonk wants to merge 1 commit into
Conversation
OnePunchMonk
requested review from
andyland,
k223kim,
lianakoleva and
t-vi
as code owners
August 28, 2026 08:25
This was referenced Aug 28, 2026
Open
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.
Part of #2190.
Part 1 of 3, split out of #2300 so each piece is reviewable on its own. Addresses the KV cache half of #2190.
Two things here:
Stale KV cache now raises a clear error. If you call
forwardwithinput_posafter changingmax_seq_lengthwithout re-runningset_kv_cache, the old code either printed a warning and carried on with a stale cache, or died with a crypticIndexErrorfrom the cache index. Either way you could silently get wrong attention results. It now raises aRuntimeErrorthat says what happened and what to do about it.chatno longer double-allocates the cache. When a chat turn needs a longer context,process_promptgrew the cache by callingset_kv_cacheagain. The old cache was still alive at that point, so both sat in memory at once and peak usage roughly doubled for the rest of the session. Added an explicitclear_kv_cache()before the reallocate.Also documented the init/destroy lifecycle on
set_kv_cache/clear_kv_cache, since the ordering rules were not written down anywhere.Tests
test_stale_kv_cache_raises_clear_errorcovers the new error.test_process_prompt_clears_kv_cache_before_growingcovers the chat path.test_kv_cache_full_context_lengthexercisesset_kv_cache/forwardat a real model's fullblock_sizewith a realistic batch size, which is the "test at full context lengths" ask from Memory Management Issues #2190. The existing tests only used the tinyblock_size=25configs.Related: #2311 (profiling) and #2312 (tunable chunking).
AI Usage Disclaimer