Skip to content

[None][feat] Add per-conversation KV cache block reuse - #16115

Merged
jiaganc merged 7 commits into
NVIDIA:mainfrom
jiaganc:dsv4-conv-aware-block-reuse
Jul 20, 2026
Merged

[None][feat] Add per-conversation KV cache block reuse#16115
jiaganc merged 7 commits into
NVIDIA:mainfrom
jiaganc:dsv4-conv-aware-block-reuse

Conversation

@jiaganc

@jiaganc jiaganc commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • New Features

    • Added per_conversation as a KV-cache block reuse policy.
    • Improved cache reuse across conversation turns while safely dropping outdated divergent blocks.
    • Added planned cache-block drop support for more predictable memory reuse.
  • Tests

    • Added coverage for multi-chunk conversations, overlapping requests, divergent prompts, fallback behavior, and planned drops.

Description

Add a per_conversation KV cache block reuse policy for KV cache manager V2.

The core runtime can now create a PlannedDropHandle for committed SWA-window pages needed only by the next conversation turn. Dropping the handle removes already-droppable pages when no other plan covers them. Full-attention and attention-sink pages remain managed by the existing global reuse policy, while SSM state support remains a TODO.

The PyTorch executor tracks the current request and latest planned drop handle for each conversation. Completing a context saves its drop plan and applies the preceding plan only when all required pages are available. Finishing or cancelling a request clears its active conversation state. Concurrent requests for the same conversation are ignored by conversation tracking, and requests without conversation parameters retain per_request behavior.

Test Coverage

  • Added planned-drop lifecycle coverage in tests/unittest/kv_cache_manager_v2_tests/test_kv_cache_manager_v2.py.
  • Added per-conversation chunking, fallback, divergent-turn, overlapping-request, and cancellation coverage in tests/unittest/_torch/executor/test_kv_cache_manager_v2.py.
  • Extended KvCacheConfig validation coverage in tests/unittest/llmapi/test_llm_args.py.
  • Python compilation, git diff --check, and relevant pre-commit hooks passed locally.
  • Pytest was not run locally because it is unavailable in this checkout environment.

PR Checklist

Please review the following before submitting this 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.

Comment thread tensorrt_llm/runtime/kv_cache_manager_v2/_core/_kv_cache.py Outdated
Comment thread tensorrt_llm/runtime/kv_cache_manager_v2/_core/_kv_cache.py Outdated
@jiaganc
jiaganc force-pushed the dsv4-conv-aware-block-reuse branch 2 times, most recently from b5a8913 to 8260269 Compare July 14, 2026 14:02
@jiaganc
jiaganc marked this pull request as ready for review July 15, 2026 02:32
@jiaganc
jiaganc requested review from a team as code owners July 15, 2026 02:32
@jiaganc
jiaganc force-pushed the dsv4-conv-aware-block-reuse branch from 8260269 to 2ef8d7b Compare July 15, 2026 02:41
@coderabbitai

coderabbitai Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds per_conversation KV block reuse, planned committed-block drop handles, conversation-scoped lifecycle tracking, public exports and stubs, manifest updates, and CUDA-gated tests covering commits, drops, divergence, and overlapping requests.

Changes

Conversation-scoped KV reuse

Layer / File(s) Summary
Policy and public contracts
tensorrt_llm/llmapi/llm_args.py, tensorrt_llm/usage/llm_args_golden_manifest.json, tensorrt_llm/runtime/kv_cache_manager_v2/*
Adds per_conversation configuration support and exposes PlannedDropHandle plus plan_committed_block_drop() in runtime exports and stubs.
Planned committed-block drops
tensorrt_llm/runtime/kv_cache_manager_v2/_core/_kv_cache.py, tensorrt_llm/runtime/kv_cache_manager_v2/_page.py
Tracks planned drops on committed pages and creates handles that release eligible SWA pages after drop().
Conversation lifecycle integration
tensorrt_llm/_torch/pyexecutor/kv_cache_manager_v2.py
Enables conversation tracking for the new policy, updates active requests, saves completed-context drop plans, and clears state during reset or shutdown.
Configuration and runtime validation
tests/unittest/_torch/executor/test_kv_cache_manager_v2.py, tests/unittest/kv_cache_manager_v2_tests/test_kv_cache_manager_v2.py, tests/unittest/llmapi/test_llm_args.py
Tests delayed commits, per-request fallback, divergent prompts, overlapping requests, planned handle behavior, and configuration acceptance.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Request
  participant KVCacheManagerV2
  participant ConversationManager
  participant _KVCache
  Request->>KVCacheManagerV2: prepare first context chunk
  KVCacheManagerV2->>ConversationManager: update_conversation(request)
  Request->>KVCacheManagerV2: finish context
  KVCacheManagerV2->>ConversationManager: save_drop_plan(request, kv_cache)
  ConversationManager->>_KVCache: plan_committed_block_drop()
Loading

Suggested reviewers: lfr-0531, lowsfer

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 6.90% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title follows the required template and accurately summarizes the main change: adding per-conversation KV cache block reuse.
Description check ✅ Passed The description includes the required Description, Test Coverage, and PR Checklist sections and gives relevant implementation and test details.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tensorrt_llm/_torch/pyexecutor/kv_cache_manager_v2.py`:
- Around line 157-208: Update ConversationManager and the free_resources cleanup
path to release per-conversation state when a tracked request terminates before
save_drop_plan, clearing current_request_id only when it matches the freed
request and preserving any planned drop handle as appropriate. Ensure all
early-termination paths use this cleanup, and evict conversation entries that no
longer contain active request or drop-plan state so _conversation_states cannot
grow without bound.

In `@tests/unittest/_torch/executor/test_kv_cache_manager_v2.py`:
- Around line 36-63: Add a cache_salt field to the _ContextRequest test helper,
separate from cache_salt_id, so KVCacheManagerV2._prepare_context_impl() and
prepare_resources() can access req.cache_salt for first-chunk requests without
AttributeError.

In `@tests/unittest/kv_cache_manager_v2_tests/test_kv_cache_manager_v2.py`:
- Around line 613-647: Update the nested plan_drop helper in
test_planned_drop_handle to consume the stream holder’s pending finish event via
take_finish_event() after the cache operations complete and before closing the
cache. Preserve the existing drop-handle creation and return behavior while
matching neighboring test cleanup.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: d942f280-a57f-448c-91c3-55b6c7f0c639

📥 Commits

Reviewing files that changed from the base of the PR and between 02cedf6 and 8260269.

📒 Files selected for processing (11)
  • tensorrt_llm/_torch/pyexecutor/kv_cache_manager_v2.py
  • tensorrt_llm/llmapi/llm_args.py
  • tensorrt_llm/runtime/kv_cache_manager_v2/__init__.py
  • tensorrt_llm/runtime/kv_cache_manager_v2/__init__.pyi
  • tensorrt_llm/runtime/kv_cache_manager_v2/_core/__init__.py
  • tensorrt_llm/runtime/kv_cache_manager_v2/_core/_kv_cache.py
  • tensorrt_llm/runtime/kv_cache_manager_v2/_page.py
  • tensorrt_llm/usage/llm_args_golden_manifest.json
  • tests/unittest/_torch/executor/test_kv_cache_manager_v2.py
  • tests/unittest/kv_cache_manager_v2_tests/test_kv_cache_manager_v2.py
  • tests/unittest/llmapi/test_llm_args.py

Comment thread tensorrt_llm/_torch/pyexecutor/kv_cache_manager_v2.py
Comment thread tests/unittest/_torch/executor/test_kv_cache_manager_v2.py
@jiaganc jiaganc added the api-compatible Accepted LLM API contract change that is backwards-compatible label Jul 15, 2026
@jiaganc

jiaganc commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59372 [ run ] triggered by Bot. Commit: 790fca8 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59372 [ run ] completed with state SUCCESS. Commit: 790fca8
/LLM/main/L0_MergeRequest_PR pipeline #47846 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@jiaganc

jiaganc commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59399 [ run ] triggered by Bot. Commit: 790fca8 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59399 [ run ] completed with state SUCCESS. Commit: 790fca8
/LLM/main/L0_MergeRequest_PR pipeline #47872 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@nvpohanh
nvpohanh requested a review from VALLIS-NERIA July 16, 2026 07:25
@nvpohanh

Copy link
Copy Markdown
Collaborator

[by Codex] @VALLIS-NERIA Friendly reminder to review this PR as the primary KV-cache-manager reviewer when you have a chance. Thanks!

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59855 [ run ] completed with state ABORTED. Commit: 8b9e34a

Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59860 [ run ] completed with state SUCCESS. Commit: 5ef3b66
/LLM/main/L0_MergeRequest_PR pipeline #48265 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@JunyiXu-nv JunyiXu-nv left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM for LLM api changes.

@JunyiXu-nv

Copy link
Copy Markdown
Collaborator

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59933 [ run ] triggered by Bot. Commit: 5ef3b66 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59933 [ run ] completed with state FAILURE. Commit: 5ef3b66
/LLM/main/L0_MergeRequest_PR pipeline #48335 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

Signed-off-by: Jiagan Cheng <jiaganc@nvidia.com>
@jiaganc

jiaganc commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59984 [ run ] triggered by Bot. Commit: 519c8d2 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59984 [ run ] completed with state FAILURE. Commit: 519c8d2
/LLM/main/L0_MergeRequest_PR pipeline #48378 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@jiaganc

jiaganc commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59992 [ run ] triggered by Bot. Commit: 519c8d2 Link to invocation

@jiaganc
jiaganc enabled auto-merge (squash) July 17, 2026 15:58
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59992 [ run ] completed with state FAILURE. Commit: 519c8d2
/LLM/main/L0_MergeRequest_PR pipeline #48385 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@jiaganc

jiaganc commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60249 [ run ] triggered by Bot. Commit: 3eb95f4 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60249 [ run ] completed with state SUCCESS. Commit: 3eb95f4
/LLM/main/L0_MergeRequest_PR pipeline #48611 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@jiaganc

jiaganc commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60316 [ run ] triggered by Bot. Commit: 3eb95f4 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60316 [ run ] completed with state SUCCESS. Commit: 3eb95f4
/LLM/main/L0_MergeRequest_PR pipeline #48663 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@jiaganc

jiaganc commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60376 [ run ] triggered by Bot. Commit: 3eb95f4 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60376 [ run ] completed with state FAILURE. Commit: 3eb95f4
/LLM/main/L0_MergeRequest_PR pipeline #48715 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@jiaganc

jiaganc commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60390 [ run ] triggered by Bot. Commit: 3eb95f4 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60390 [ run ] completed with state SUCCESS. Commit: 3eb95f4
/LLM/main/L0_MergeRequest_PR pipeline #48732 completed with status: 'SUCCESS'

CI Report

Link to invocation

@jiaganc
jiaganc merged commit fd9c32f into NVIDIA:main Jul 20, 2026
7 checks passed
@jiaganc
jiaganc deleted the dsv4-conv-aware-block-reuse branch July 21, 2026 02:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api-compatible Accepted LLM API contract change that is backwards-compatible

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants