Fix: Resolve KV Cache OOM & Logit Extraction Memory Spikes#678
Open
prince-shakyaa wants to merge 3 commits into
Open
Fix: Resolve KV Cache OOM & Logit Extraction Memory Spikes#678prince-shakyaa wants to merge 3 commits into
prince-shakyaa wants to merge 3 commits into
Conversation
Author
|
This PR is now ready for review! Let me know if you need any changes. Thank You. |
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.
Pull Request: Resolve KV Cache OOM & Logit Extraction Memory Spikes
Related Issue
Fixes: #675
Overview
This PR addresses critical architectural limitations in the text sampling backend that cause severe memory inflation and Out-Of-Memory (OOM) errors during long multi-turn conversations and extended context generation.
By introducing a rolling KV cache and optimizing the stateful preservation of prediction logits, we significantly reduce the VRAM overhead per sequence.
Key Changes
1. Rolling Cache via Turn Eviction
File Modified:
gemma/gm/text/_chat_sampler.pyrolling_cache_thresholdto theChatSampler.self.turns. Thelast_stateis then invalidated and gracefully re-prefilled against the truncated conversation. This ensures context lengths never surpass our safe cache boundary, eliminating sequential multi-turn OOMs.2. Ephemeral Top-K Logit Extraction
Files Modified:
gemma/gm/text/_sampler_loop.py,gemma/gm/text/_sampler.py,gemma/gm/text/_prefill.pySamplingStateto replace the massivepredicted_logits) withpredicted_top_logitsandpredicted_top_indices.top_k_logits: int = 0to the low-levelSamplerwhich pipes down to theSamplerLoopand_prefill.pystate allocations._sample_step, we usejax.lax.top_kimmediately against the model emission iftop_k_logits > 0. The large vocabulary tensor is then destroyed instead of cached along the sequence length, freeing massive amounts of GPU memory.Validation & Testing
gemma/gm/text/continue to pass without regressions.cache_lengthwithout linearly leaking memory.