Skip to content

[None][fix] Derive KV capacity once and select windowed blocks by ordinal - #18668

Draft
Shixiaowei02 wants to merge 1 commit into
NVIDIA:mainfrom
Shixiaowei02:user/xiaoweis/kv-capacity-contract
Draft

[None][fix] Derive KV capacity once and select windowed blocks by ordinal#18668
Shixiaowei02 wants to merge 1 commit into
NVIDIA:mainfrom
Shixiaowei02:user/xiaoweis/kv-capacity-contract

Conversation

@Shixiaowei02

Copy link
Copy Markdown
Collaborator

@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:

  • Added a shared helper function, get_kv_capacity_tokens, to centralize the calculation of KV cache token requirements, ensuring all consumers use a consistent definition.
  • Updated all relevant code paths (transceiver.py, kv_cache_manager_v2.py, and test stubs) to use get_kv_capacity_tokens instead of duplicating token accounting logic. [1] [2] [3]

Block ordinal support and interface changes:

  • Introduced the get_block_ordinals method and exposes_block_ordinals attribute 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:

  • Refined the logic in _create_kv_slice to 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:

  • Updated and expanded tests to cover the new block ordinal logic and to verify that windowed slices are not shifted by the presence of draft tokens. Added parameterized tests to ensure edge cases (such as draft tokens crossing block boundaries) are exercised. [1] [2] [3]
  • Refactored test stubs to use the shared token accounting helper and to simulate both V1 and V2 manager behaviors accurately.

Imports and minor cleanup:

  • Updated imports to include the new shared helper where needed. [1] [2] [3]

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-compatible or api-breaking. For api-breaking, include BREAKING in 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.

…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant