feat: add rolling cache turn pruning for ChatSampler (Issue #675)#703
Open
Paramveersingh-S wants to merge 1 commit into
Open
feat: add rolling cache turn pruning for ChatSampler (Issue #675)#703Paramveersingh-S wants to merge 1 commit into
Paramveersingh-S wants to merge 1 commit into
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.
Description
This PR addresses the Context Exhaustion issue outlined in Issue #675, specifically focusing on the
ChatSamplercrashing when long multi-turn conversations exceed the static 4096-tokencache_length.Since JAX arrays are statically compiled and dynamic
jnp.rollsliding-window operations introduce significant compilation and latency overheads, this PR solves the issue at the orchestration layer by implementing Context Window Management (Turn Pruning) directly insidegemma/gm/text/_chat_sampler.py.Key Changes
_prune_context_to_fitmechanism to theChatSampler.chatmethod. Before triggering theSamplerLoop, it calculates ifused_cache + new_prompt_tokens + max_out_length > cache_length.self.turnswhile explicitly preserving the initialSystemprompt (if present).history_imagesandhistory_audioproperties to theChatSampler. This ensures that when the context is pruned, the sampler can safely flush the staticlast_stateKV Cache and execute a full re-prefill using the dynamically retained multimodal history without dropping user-provided media from active turns._chat_sampler_test.pyto statically verify the eviction constraints.Fixes #675.