Skip to content

[None][feat] Add TensorRT-LLM runtime integration for KV cache compression#15697

Open
Hudayday wants to merge 6 commits into
NVIDIA:mainfrom
Hudayday:kvcache-v2-nongrowing-reclaim
Open

[None][feat] Add TensorRT-LLM runtime integration for KV cache compression#15697
Hudayday wants to merge 6 commits into
NVIDIA:mainfrom
Hudayday:kvcache-v2-nongrowing-reclaim

Conversation

@Hudayday

@Hudayday Hudayday commented Jun 28, 2026

Copy link
Copy Markdown
Collaborator

Description

Eviction-based KV cache compression makes the target cache's physical KV
length shorter than the request's logical generation length. Before this
change the runtime could not represent that: KVCacheManagerV2 pushed both
capacity and history back to the logical token count on every generation
step (undoing any compaction), and attention inputs always assumed logical
lengths.

This PR adds the minimal runtime support for generation-length-changing KV
cache compression, on top of the compression-manager framework merged in
#15106. The whole mechanism is one integer carried on the request:

Request-carried compressed length

  • LlmRequest.py_num_compressed_tokens (default 0), written only by a
    compression manager: how many of the request's tokens have been physically
    evicted.
  • The model engine subtracts it when building num_cached_tokens_per_seq,
    so attention kernels read the compacted KV length and append new tokens at
    the right slot. Position ids and the cached_tokens stat keep the logical
    count.
  • Without a compression manager the field stays 0: dense runs are unchanged.

KVCacheManagerV2 length policy

  • New kv_compression_manages_history flag: when set by a compression
    manager, generation-step resize updates capacity only and preserves the
    committed history, instead of forcing it back to the logical length.

Compression manager base and speculative gate

  • BaseKVCacheCompressionManager accepts an optional draft
    KVCacheManagerV2 and applies the same length policy to it, so an
    algorithm can evict the draft cache together with the target. Block reuse
    is rejected at attach (compression rewrites stored K/V).
  • on_context_step_end receives the requests whose prefill finished this
    iteration, so prefill-end methods process the cohort in one launch.
  • KvCacheCompressionMode carries algorithm traits the same way
    SpeculativeDecodingMode does; configs map their algorithm string to it
    and callers read is_* predicates.
  • Speculative compatibility is decided at the executor call site before any
    manager is created: evicting methods require one-model MTP/EAGLE3 draft
    KV; otherwise the run stays uncompressed with a warning.
  • ModelConfig.kv_cache_compression_config plumb so attention modules can
    read method traits.

Explicitly not in this PR: no scheduler, attention, or
attention-metadata changes; no concrete eviction algorithm and no draft
compaction (follow-up algorithm PR).

Activation and scope

Inert unless kv_cache_compression_config selects an algorithm; the factory
then registers the manager as a regular resource manager.
KVCacheManagerV2 only.

Test Coverage

  • test_kv_cache_compression_manager.py: base-class contract, resource-
    manager-to-hook translation, target/draft length-policy attach, call-site
    speculative gate, factory registration.
  • test_kv_cache_v2_capacity_only.py: default V2 history behavior
    unchanged; capacity-only generation updates preserve history. Both
    registered in l0_a10.

@Hudayday
Hudayday requested a review from a team as a code owner June 28, 2026 16:33
@Hudayday
Hudayday requested a review from lowsfer June 28, 2026 16:33
@Hudayday

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@coderabbitai

coderabbitai Bot commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

KVCacheManagerV2.update_resources gains a compression reclaim branch: when py_kv_evicted_tokens > 0 and the request is not completing, it calls kv_cache.fork(max_beam - evicted), falling back to resize(None, max_beam - evicted) on exception. A new unit test file covers all five dispatch scenarios and is added to the A10 pre-merge test list.

Changes

KV-cache compression reclaim

Layer / File(s) Summary
Compression reclaim branch in update_resources
tensorrt_llm/_torch/pyexecutor/kv_cache_manager_v2.py
Adds completing flag and py_kv_evicted_tokens check; routes evicted non-completing requests through kv_cache.fork(max_beam - evicted) with exception-triggered fallback to resize(None, max_beam - evicted). Non-evicted path retains prior new_capacity assignment logic.
Unit tests and CI registration
tests/unittest/_torch/pyexecutor/test_kv_cache_v2_compression_reclaim.py, tests/integration/test_lists/test-db/l0_a10.yml
New test module with _fake_manager and mock helpers covers five branches: fork-on-eviction, original resize on unevicted, None-capacity on completing, fork-failure fallback, and inactive-cache skip. File added to A10 pre-merge list.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

api-compatible

Suggested reviewers

  • byshiue
  • heyuhhh
  • lfr-0531
  • Superjomn
  • PerkzZheng
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 54.55% 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 [None][feat] format and accurately summarizes the KV cache compression runtime support.
Description check ✅ Passed The description covers the issue, solution, and test coverage; only the PR checklist section is missing.
✨ 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: 2

🤖 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 2497-2502: The no-free fallback in
kv_cache_manager_v2._KVCacheManagerV2 should not ignore the boolean result from
kv_cache.resize(None, req.max_beam_num_tokens - evicted). After the warning in
the fallback branch, check the return value just like the normal resize path and
treat a failed resize as fatal or otherwise handle it consistently so the
request state stays aligned with the live _KVCache state.

In `@tests/unittest/_torch/pyexecutor/test_kv_cache_v2_compression_reclaim.py`:
- Around line 77-83: The current test coverage only exercises completion with
evicted=0, so the combined evicted-and-completing path in the reclaim logic is
still untested. Add a test in test_kv_cache_v2_compression_reclaim.py using
_fake_manager, _run, and _req with evicted > 0 and a completing state such as
LlmRequestState.GENERATION_COMPLETE or CONTEXT_INIT, and assert that the request
still calls resize(None, max_beam - 1) while fork() is not called. This should
verify the guard in the kv cache reclaim behavior when both conditions are
present.
🪄 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: f47017d9-645a-4086-ab61-1067b72e308b

📥 Commits

Reviewing files that changed from the base of the PR and between 5ec0c84 and c1c180c.

📒 Files selected for processing (3)
  • tensorrt_llm/_torch/pyexecutor/kv_cache_manager_v2.py
  • tests/integration/test_lists/test-db/l0_a10.yml
  • tests/unittest/_torch/pyexecutor/test_kv_cache_v2_compression_reclaim.py

Comment thread tensorrt_llm/_torch/pyexecutor/kv_cache_manager_v2.py Outdated
Comment thread tests/unittest/_torch/pyexecutor/test_kv_cache_v2_compression_reclaim.py Outdated
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #56237 [ run ] triggered by Bot. Commit: c1c180c Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #56237 [ run ] completed with state SUCCESS. Commit: c1c180c
/LLM/main/L0_MergeRequest_PR pipeline #45098 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

@Hudayday

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #56247 [ run ] triggered by Bot. Commit: c1c180c Link to invocation

@Hudayday
Hudayday marked this pull request as draft June 29, 2026 02:32
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #56247 [ run ] completed with state SUCCESS. Commit: c1c180c
/LLM/main/L0_MergeRequest_PR pipeline #45107 completed with status: 'SUCCESS'

CI Report

Link to invocation

@Hudayday
Hudayday force-pushed the kvcache-v2-nongrowing-reclaim branch 2 times, most recently from 5cdbbf3 to 095f9ff Compare June 29, 2026 03:32
@Hudayday

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #56292 [ run ] triggered by Bot. Commit: 095f9ff Link to invocation

@Hudayday
Hudayday force-pushed the kvcache-v2-nongrowing-reclaim branch 2 times, most recently from 2f10293 to c11ca19 Compare June 29, 2026 05:19
@Hudayday
Hudayday marked this pull request as ready for review June 29, 2026 05:21
@Hudayday

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #56307 [ run ] triggered by Bot. Commit: c11ca19 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #56292 [ run ] completed with state ABORTED. Commit: 095f9ff

Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #56307 [ run ] completed with state SUCCESS. Commit: c11ca19
/LLM/main/L0_MergeRequest_PR pipeline #45158 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

@Hudayday

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #56337 [ run ] triggered by Bot. Commit: c11ca19 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #56337 [ run ] completed with state SUCCESS. Commit: c11ca19
/LLM/main/L0_MergeRequest_PR pipeline #45186 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

@Hudayday

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #56371 [ run ] triggered by Bot. Commit: c11ca19 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #56371 [ run ] completed with state SUCCESS. Commit: c11ca19
/LLM/main/L0_MergeRequest_PR pipeline #45216 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

@Hudayday

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@Hudayday

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59608 [ run ] triggered by Bot. Commit: 6cedd04 Link to invocation

Signed-off-by: tianruih <tianruih@nvidia.com>
@Hudayday

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59620 [ run ] triggered by Bot. Commit: c4d80d3 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59608 [ run ] completed with state ABORTED. Commit: 6cedd04

Link to invocation

@BowenFu BowenFu left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM. Inert runtime plumbing — ships no compression algorithm and dense runs are byte-identical (py_num_compressed_tokens defaults to 0). Independently double-checked the one API-surface item: the new kv_cache_compression_mode is a @property (not a Pydantic field), so it isn't snapshotted by the api_stability harness and needs no golden-manifest regen; the only gate is the api-compatible label, which its CI check already passed. The dispatch change (isinstanceresource_type==) is analyzed safe (draft/cross managers don't consume the withheld args) and even fixes a latent V2 arg-passing case.

@nvpohanh

Copy link
Copy Markdown
Collaborator

[by Codex] @lowsfer 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 #59620 [ run ] completed with state SUCCESS. Commit: c4d80d3
/LLM/main/L0_MergeRequest_PR pipeline #48057 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

@Hudayday

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59700 [ run ] triggered by Bot. Commit: c4d80d3 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59700 [ run ] completed with state SUCCESS. Commit: c4d80d3
/LLM/main/L0_MergeRequest_PR pipeline #48130 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

@Hudayday

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59742 [ run ] triggered by Bot. Commit: 23d654b Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59742 [ run ] completed with state FAILURE. Commit: 23d654b
/LLM/main/L0_MergeRequest_PR pipeline #48170 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

@Hudayday

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59822 [ run ] triggered by Bot. Commit: 23d654b Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59822 [ run ] completed with state SUCCESS. Commit: 23d654b
/LLM/main/L0_MergeRequest_PR pipeline #48232 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

@Hudayday

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59928 [ run ] triggered by Bot. Commit: 23d654b Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59928 [ run ] completed with state SUCCESS. Commit: 23d654b
/LLM/main/L0_MergeRequest_PR pipeline #48330 completed with status: 'SUCCESS'

CI Report

Link to invocation

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.

8 participants