Conversation
(overlap, non-overlap) x (page>=1) x (spec, non-spec, spec v2) x (retract, finished)
Summary of ChangesHello @hnyls2002, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly refactors and unifies the Key-Value (KV) cache memory management logic across various operational modes and states within the system. By introducing dedicated request-level attributes for tracking allocated, committed, and freed KV cache lengths, the changes aim to standardize memory handling, improve accuracy, and enhance the overall robustness of memory management during request extension, decoding, and completion. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request unifies request-level memory management by introducing kv_committed_len, kv_allocated_len, and kv_freed_len to the Req class. These fields are consistently updated during prefill and decode stages. The logic for freeing memory for finished requests is now centralized in cache_finished_req, which uses req.kv_allocated_len as the source of truth. This is a good refactoring that improves robustness. I have one minor suggestion to remove a stale comment that became misleading after the changes.
(overlap, non-overlap) x (page>=1) x (spec, non-spec, spec v2) x (retract, finished)(overlap, non-overlap) x (page>=1) x (spec, non-spec, spec v2) x (retract, finished)
There is another thing we can do: |
|
@cctry Not sure, we can discuss offline. |
…e>=1) x (spec, non-spec, spec v2) x (retract, finished)` (sgl-project#12224)" revert some change This reverts commit 665416f.
This PR replaces the old approach to releasing KV cache, which relied on
len(self.origin_input_ids) + max(len(self.output_ids) - 1, 0)to determine the KV length.That approach is brittle with overlap scheduling and with multiple finish paths (normal completion, disaggregation-decode finish, retract, abort). With speculative decoding, we also perform over-allocation, which makes allocation/freeing logic even more error-prone.
This PR introduces two explicit notions for a request’s KV cache state
We tightly couple the allocation steps with updates to
kv_committed_lenandkv_allocated_len, so these fields faithfully reflect request-level memory usage at all times.The previous memory allocation involves
We now replace the ad-hoc “extra token” arithmetic with the recorded
kv_committed_lenandkv_allocated_len. Consumers no longer need to infer offsets: they simply consult these fields.To ensure correctness:
Future TODOs (cc @cctry)