[None][fix] Derive KV capacity once and select windowed blocks by ordinal - #18668
Draft
Shixiaowei02 wants to merge 1 commit into
Draft
[None][fix] Derive KV capacity once and select windowed blocks by ordinal#18668Shixiaowei02 wants to merge 1 commit into
Shixiaowei02 wants to merge 1 commit into
Conversation
…inal A generation-side request's KV size was computed in two places. The allocator used prompt_len + draft_len + num_extra_kv_tokens; the disaggregated transfer re-derived it as prompt_len + num_extra_kv_tokens. They differ by exactly one block whenever the draft tokens cross a tokens_per_block boundary. For a sliding-window layer group that surplus block reached a trim which kept the last expected_valid entries -- a claim about position made from a length. It held only while the surplus was the stale head; when it was speculative scratch instead, the trim dropped a live prompt block off the front and every survivor landed one position late. The trim equalises the two counts, so no size, capacity or rewind ever disagreed: 24,815 traced decode steps showed no violation while the transferred content was wrong. Measured on a sliding-window MoE model with one-engine speculative decoding at max_draft_len=3, 448 of 1152 windowed slices carried the surplus, GSM8K lost 2.5 points (McNemar p=5.42e-07), and eight of 1319 responses degenerated into repetition loops. get_kv_capacity_tokens() becomes the single definition, read by both the allocator and the transceiver. prompt_len covers context-parallel Helix requests; include_drafts covers the context side, whose drafts are not its own to hold -- without it the caller has to spell the sum out again. The V2 adapter exposes get_block_ordinals(), where entry i is the slot holding tokens [i * tokens_per_block, ...) and -1 marks a block with no page bound. The transceiver selects ordinals[stale_end:total_blocks] and drops the placeholders, so position comes from the index and there is nothing left to infer. The scratch strip and the front trim remain only on the legacy path, which V1 keeps via exposes_block_ordinals; scratch removal there is now bounded by the surplus actually present, so it can no longer empty a short list and transfer nothing. Tests assert positions, not counts, since correct counts are what the defect produced. The sweep builds its input through the shared helper and its expectation from prompt/window/tokens_per_block alone, so changing one side turns it red, and a companion case puts an unbound page inside the window to show its neighbours do not shift. Contract suite in a release container: 80 failed before this change, 330 passed after. Signed-off-by: Xiaowei Shi <39303645+Shixiaowei02@users.noreply.github.com>
1 task
1 task
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.
@coderabbitai summary
Description
This pull request introduces improvements to how KV cache token allocation is calculated and handled, especially in the context of sliding window attention (SWA) and speculative draft tokens. The changes centralize the token accounting logic, add support for block ordinal metadata, and update both the implementation and tests to ensure consistent and correct behavior when draft tokens are present.
Core logic improvements:
get_kv_capacity_tokens, to centralize the calculation of KV cache token requirements, ensuring all consumers use a consistent definition.transceiver.py,kv_cache_manager_v2.py, and test stubs) to useget_kv_capacity_tokensinstead of duplicating token accounting logic. [1] [2] [3]Block ordinal support and interface changes:
get_block_ordinalsmethod andexposes_block_ordinalsattribute to cache reuse adapters, allowing consumers to distinguish between legacy (V1) and ordinal-aware (V2) managers. This enables more accurate handling of block trimming and windowed attention. [1] [2] [3]Sliding window and speculative allocation fixes:
_create_kv_sliceto properly handle speculative blocks and window trimming, ensuring that prompt blocks are not dropped or shifted due to undercounting draft tokens. Improved error handling and logging for capacity mismatches.Test improvements:
Imports and minor cleanup:
These changes collectively ensure that KV cache allocation is robust, consistent, and future-proof against changes in how draft tokens and sliding windows are handled.
Test Coverage
PR Checklist
Please review the following before submitting your PR:
PR description clearly explains what and why. If using CodeRabbit's summary, please make sure it makes sense.
PR Follows TRT-LLM CODING GUIDELINES to the best of your knowledge.
Test cases are provided for new code paths (see test instructions)
If PR introduces API changes, an appropriate PR label is added - either
api-compatibleorapi-breaking. Forapi-breaking, includeBREAKINGin the PR title.Any new dependencies have been scanned for license and vulnerabilities
CODEOWNERS updated if ownership changes
Documentation updated as needed
Update tava architecture diagram if there is a significant design change in PR.
The reviewers assigned automatically/manually are appropriate for the PR.
Please check this after reviewing the above items as appropriate for this PR.
GitHub Bot Help
To see a list of available CI bot commands, please comment
/bot help.