Skip to content

[TRTLLM-15218][chore] KVCacheManagerV2: report the prefix attention alone supports - #17448

Open
brnguyen2 wants to merge 2 commits into
NVIDIA:mainfrom
brnguyen2:k3/kvcm-v2-pruning-diagnostic
Open

[TRTLLM-15218][chore] KVCacheManagerV2: report the prefix attention alone supports#17448
brnguyen2 wants to merge 2 commits into
NVIDIA:mainfrom
brnguyen2:k3/kvcm-v2-pruning-diagnostic

Conversation

@brnguyen2

@brnguyen2 brnguyen2 commented Aug 9, 2026

Copy link
Copy Markdown
Collaborator

Description

Split out of #17447 so the two fixes there are not held up by a design discussion about a
diagnostic.

When Kimi K3 prefix reuse underperforms, the useful number is the one that separates
"attention pages matched N tokens" from "recurrent-snapshot availability cut it to M".
KVCacheManagerV2 exposed only the final M, so the two causes were indistinguishable from
the outside.

BlockRadixTree::pruneMatch now takes the SSM life cycle as a parameter; passing
std::nullopt skips the recurrent-snapshot constraint and yields the attention-only
prefix. match() records that on ReuseMatch and KvCache carries it to a
_get_num_tokens_before_hybrid_pruning() accessor (C++, nanobind, and the Python runtime
mirror). Models without an SSM life cycle skip the extra prune pass entirely and report
the final match length, so only hybrid models pay for the diagnostic.

Diagnostic only, no behavior change, and reachable only under
use_kv_cache_manager_v2=True.

Reviewer note — this is a redefinition, not a port. An equivalent counter exists
elsewhere with a different meaning, computed at a snapshot point in a staged prune. This
implementation cannot reproduce that: pruneMatch here is a single fixed-point loop with
pageCoverage, so that snapshot point has no equivalent. The counter is defined instead
as the prefix the attention pages alone support, which is slightly narrower — it does
not separate out SWA-window pruning. If a different definition is wanted, this is the
commit to say so on.

Test Coverage

test_num_tokens_before_hybrid_pruning_isolates_recurrent_truncation: attention
partially covers a 48-token lookup while the latest reusable SSM snapshot sits at 32, so
the diagnostic reports 48 where num_committed_tokens is 32. The second half asserts it
collapses onto num_committed_tokens when the snapshot and the attention match agree, so
the test fails if it ever reports the lookup length instead.

Partial reuse must be enabled for the two numbers to differ at all: without it a match is
block-aligned, both are cut at the same block boundary, and the diagnostic is
indistinguishable from num_committed_tokens.

Verified on this branch standalone (not just as part of the combined branch it was split
from): C++ builds clean and tests/unittest/kv_cache_manager_v2_tests/test_kv_cache_manager_v2.py
is 113 passed / 13 skipped / 0 failed.

PR Checklist

  • PR title is [JIRA/NVBUG/None][type] Summary
  • Commits are signed off (DCO)
  • New test added and passing
  • No new dependencies

Dev Engineer Review

  • Adds diagnostic plumbing for attention-only prefix length before hybrid SSM pruning.
  • Updates C++ and Python runtime mirrors consistently.
  • Exposes the value through KvCache and nanobind.
  • Preserves behavior for non-hybrid models and reports the final match length when no SSM lifecycle exists.
  • Adds no configuration changes.
  • No test-list files were modified.
  • The change is diagnostic-only. Performance impact is limited to an additional diagnostic match.
  • The branch has not been independently rebuilt. Rebuild validation is recommended.

QA Engineer Review

  • Added TestSSMSupport.test_num_tokens_before_hybrid_pruning_isolates_recurrent_truncation.
  • The test covers partial reuse where attention supports 48 tokens and recurrent snapshots limit committed reuse to 32 tokens.
  • The test also covers matching attention and recurrent limits at 64 tokens.
  • No corresponding tests/integration/test_lists/ entry is reported for this test.
  • Verdict: needs follow-up.

…KV cache manager V2

When Kimi K3 prefix reuse underperforms, the useful number is the one that
separates "attention pages matched N tokens" from "recurrent-state
snapshot availability cut it to M". V2 exposed only the final M, so the
two causes were indistinguishable from the outside.

BlockRadixTree::pruneMatch now takes the SSM life cycle as a parameter;
passing std::nullopt skips the recurrent-snapshot constraint and yields
the attention-only prefix. match() records that value on ReuseMatch, and
KvCache carries it to a _get_num_tokens_before_hybrid_pruning() accessor
(C++, nanobind and the Python runtime mirror). Models without an SSM life
cycle skip the extra prune pass entirely and report the final match
length, so only hybrid models pay for the diagnostic.

Diagnostic only, no behavior change, and reachable only under
use_kv_cache_manager_v2=True.

Test: test_kv_cache_manager_v2.py::test_ssm_reuse_keeps_snapshots_from_multiple_commits
asserts the diagnostic reports 48 where the committed reuse is 32, i.e.
that recurrent pruning rather than a short attention match caused the
truncation.

Signed-off-by: Brian Nguyen <brnguyen@nvidia.com>
… differ

The assertion added to test_ssm_reuse_keeps_snapshots_from_multiple_commits
could not hold. That test runs without partial reuse, so a match is
block-aligned: with tokens_per_block=32 a 48-token lookup matches only the
one complete block, the attention-only prefix is 32, and the diagnostic is
indistinguishable from num_committed_tokens.

Restore that test to its original assertions and cover the diagnostic in a
test that configures enable_partial_reuse=True, where attention partially
covers 48 tokens while the latest reusable SSM snapshot sits at 32. That is
the case the counter exists to explain. The second half asserts the
diagnostic collapses onto num_committed_tokens when the snapshot and the
attention match agree, so the test fails if it ever reports the lookup
length instead.

Signed-off-by: Brian Nguyen <brnguyen@nvidia.com>
@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 80d885e2-8c3f-4ba3-aed9-1a409d836240

📥 Commits

Reviewing files that changed from the base of the PR and between 1cef02e and 59f1ccc.

📒 Files selected for processing (8)
  • cpp/tensorrt_llm/batch_manager/kv_cache_manager_v2/blockRadixTree.cpp
  • cpp/tensorrt_llm/batch_manager/kv_cache_manager_v2/blockRadixTree.h
  • cpp/tensorrt_llm/batch_manager/kv_cache_manager_v2/kvCache.cpp
  • cpp/tensorrt_llm/batch_manager/kv_cache_manager_v2/kvCache.h
  • cpp/tensorrt_llm/nanobind/batch_manager/kvCacheManagerV2.cpp
  • tensorrt_llm/runtime/kv_cache_manager_v2/_block_radix_tree.py
  • tensorrt_llm/runtime/kv_cache_manager_v2/_core/_kv_cache.py
  • tests/unittest/kv_cache_manager_v2_tests/test_kv_cache_manager_v2.py

Walkthrough

Hybrid KV-cache matching now records the attention prefix before SSM pruning. C++ and Python cache APIs expose this count, and an SSM partial-reuse test validates the diagnostic values.

Changes

Hybrid pruning diagnostics

Layer / File(s) Summary
Separate attention and SSM pruning
cpp/tensorrt_llm/batch_manager/kv_cache_manager_v2/blockRadixTree.*, tensorrt_llm/runtime/kv_cache_manager_v2/_block_radix_tree.py
Matching computes the attention-only prefix before applying SSM snapshot constraints and stores both token counts in ReuseMatch.
Propagate the diagnostic count
cpp/tensorrt_llm/batch_manager/kv_cache_manager_v2/kvCache.*, cpp/tensorrt_llm/nanobind/batch_manager/kvCacheManagerV2.cpp, tensorrt_llm/runtime/kv_cache_manager_v2/_core/_kv_cache.py
KV-cache objects initialize and expose the pre-hybrid-pruning token count through C++ and Python bindings.
Validate partial SSM reuse
tests/unittest/kv_cache_manager_v2_tests/test_kv_cache_manager_v2.py
The regression test checks 32 committed tokens versus 48 pre-hybrid-pruning tokens for partial reuse, and 64 for an exact snapshot match.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant BlockRadixTree
  participant KvCache
  participant KVCacheBinding
  BlockRadixTree->>BlockRadixTree: compute attention-only prefix
  BlockRadixTree->>BlockRadixTree: apply SSM snapshot pruning
  BlockRadixTree-->>KvCache: return ReuseMatch with both counts
  KvCache->>KVCacheBinding: expose diagnostic accessor
  KVCacheBinding-->>KvCache: return pre-hybrid-pruning count
Loading

Possibly related PRs

  • NVIDIA/TensorRT-LLM#17447: Directly overlaps the hybrid-pruning diagnostic changes across matching, cache state, bindings, and regression tests.

Suggested reviewers: schetlur-nv, lowsfer

🚥 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
Title check ✅ Passed The title clearly identifies the diagnostic change that reports the attention-only prefix supported by KVCacheManagerV2.
Description check ✅ Passed The description explains the problem, implementation, diagnostic scope, test coverage, and checklist status in the required sections.
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.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

@brnguyen2

Copy link
Copy Markdown
Collaborator Author

/bot run

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