Skip to content

[None][feat] KVCacheManagerV2: suspend/resume observability stat + coverage#16710

Open
eopXD wants to merge 1 commit into
NVIDIA:mainfrom
eopXD:kvcv2-suspend-resume-stat
Open

[None][feat] KVCacheManagerV2: suspend/resume observability stat + coverage#16710
eopXD wants to merge 1 commit into
NVIDIA:mainfrom
eopXD:kvcv2-suspend-resume-stat

Conversation

@eopXD

@eopXD eopXD commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator

Dev Engineer Review

  • Implements Python-only per-iteration suspend/resume observability counters in KVCacheManagerV2 (iterSuspendedRequests / iterResumedRequests) gated by _should_record_stats(), incremented on suspend() / resume() transitions, and drained once per iteration.
  • Extends KVCacheV2IterationStatsReport + serializer export so the counters surface as top-level keys in llm.get_stats() (not nested per pool-group).
  • Adds runtime support in KVCacheManager for recording/reading iteration suspend/resume counters (record_suspended_request, record_resumed_request, get_and_reset_iteration_suspend_resume_stats) with matching typed stubs in tensorrt_llm/runtime/kv_cache_manager_v2/__init__.pyi.
  • Adds/updates unit tests validating: counter increment + reporting, per-iteration reset/emit behavior, disabled-stat suppression, and serializer emission (including defaults and that keys are not emitted under kvCacheIterationStatsByPoolGroup).
  • Adds/updates an integration test for GPT-OSS V2 suspend/resume under capacity pressure; it asserts suspend+resume occurred and validates deterministic output-prefix integrity.

Potential review points

  • Confirm the top-level iteration stats serializer behavior remains stable when pool-group substructures are missing/empty (top-level keys should still emit consistently).
  • Verify counters increment only on actual state transitions (avoid double-counting repeated suspend()/resume() calls that don’t change the cache state).

QA Engineer Review

Test changes (functions added/updated):

  • tests/unittest/kv_cache_manager_v2_tests/test_kv_cache_stats_behavior.py
    • Added test_suspend_resume_iteration_stats_are_reported
  • tests/unittest/executor/test_stats_serializer.py
    • Added test_serializer_emits_v2_suspend_resume_counters
  • tests/integration/defs/accuracy/test_llm_api_pytorch.py
    • Added test_w4_1gpu_suspend_resume (TestGPTOSS)

Coverage in CI test lists (tests/integration/test_lists):

  • tests/integration/test_lists/test-db/l0_b200.yml
    • Modified l0_b200: added accuracy/test_llm_api_pytorch.py::TestGPTOSS::test_w4_1gpu_suspend_resume

Verdict: needs follow-up

Description

Adds a Python-only per-iteration suspend/resume (preemption) observability counter to KVCacheManagerV2iterSuspendedRequests / iterResumedRequests, surfaced as top-level iteration keys via llm.get_stats() — together with the tests that exercise it.

V2's active-request suspend/resume state machine (ACTIVE → SUSPENDED → resume, with host page-index reconnect via _restore_page_index_bufs) previously had no statistic or log, so a black-box test could not confirm it fired. Offload/onboard bytes are the wrong proxy: suspend() keeps HELD pages on GPU and only lazily offloads under further pressure, so iterOffloadBytes/iterOnboardBytes can be 0 while suspend/resume still fired.

Implementation mirrors the #15633 offload/onboard per-iteration counters — no C++/nanobind change:

  • Manager-level counters, incremented at _KVCache.suspend() and on a successful resume(), gated on _should_record_stats().
  • Drained once per iteration-stats fetch into KVCacheV2IterationStatsReport and emitted as top-level iterSuspendedRequests / iterResumedRequests in the serialized stats (sibling to kvCacheIterationStats, not per pool group — suspend/resume is a whole-request event).

Additive stats-dict key only; tests/unittest/api_stability is unaffected (no public API signature change).

Test Coverage

  • tests/unittest/kv_cache_manager_v2_tests/test_kv_cache_stats_behavior.py: the counters increment on suspend+resume, reset each iteration, and are suppressed when stats are disabled (model-free, deterministic).
  • tests/unittest/executor/test_stats_serializer.py
  • tests/integration/defs/accuracy/test_llm_api_pytorch.py::TestGPTOSS::test_w4_1gpu_suspend_resume — the V2-only round trip on GPT-OSS-120B (TP=1); gates on iterSuspendedRequests>0 and iterResumedRequests>0, no crash/deadlock (all requests complete non-empty), and a ≥3-token deterministic prefix vs the uncontended single-request reference.

Verified on the rc21 build (main ef6ebc2f2c + #15633): unit 20/20; I-10 suspended=31 / resumed=35, offload/onboard=0, common-prefix 17/11/19/8; api_stability 64/64. The unit tests are model-free and base-insensitive. Note: I-10's pool/generation sizing was tuned on rc21 and has not yet been re-run on current main — the resume-utilization change (#16484) can shift when suspension fires; a NOTE in the test documents the retune path if the gate-(2) counters come back 0.

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.

@coderabbitai

coderabbitai Bot commented Jul 22, 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: 2c40bc26-4cd0-4a51-ae21-ffce13a3aed6

📥 Commits

Reviewing files that changed from the base of the PR and between 01825e4 and a83f909.

📒 Files selected for processing (9)
  • tensorrt_llm/_torch/pyexecutor/kv_cache_manager_v2.py
  • tensorrt_llm/_torch/pyexecutor/kv_cache_stats.py
  • tensorrt_llm/runtime/kv_cache_manager_v2/__init__.pyi
  • tensorrt_llm/runtime/kv_cache_manager_v2/_core/_kv_cache.py
  • tensorrt_llm/runtime/kv_cache_manager_v2/_core/_kv_cache_manager.py
  • tests/integration/defs/accuracy/test_llm_api_pytorch.py
  • tests/integration/test_lists/test-db/l0_b200.yml
  • tests/unittest/executor/test_stats_serializer.py
  • tests/unittest/kv_cache_manager_v2_tests/test_kv_cache_stats_behavior.py
🚧 Files skipped from review as they are similar to previous changes (8)
  • tensorrt_llm/runtime/kv_cache_manager_v2/init.pyi
  • tests/integration/test_lists/test-db/l0_b200.yml
  • tensorrt_llm/runtime/kv_cache_manager_v2/_core/_kv_cache_manager.py
  • tests/unittest/kv_cache_manager_v2_tests/test_kv_cache_stats_behavior.py
  • tensorrt_llm/runtime/kv_cache_manager_v2/_core/_kv_cache.py
  • tensorrt_llm/_torch/pyexecutor/kv_cache_stats.py
  • tests/unittest/executor/test_stats_serializer.py
  • tensorrt_llm/_torch/pyexecutor/kv_cache_manager_v2.py

Walkthrough

KV cache manager v2 now records suspend/resume transitions as per-iteration counters, exposes them in iteration reports, serializes them as top-level statistics, and validates reporting, reset behavior, and contended KV-cache correctness.

Changes

KV cache suspend/resume statistics

Layer / File(s) Summary
Record suspend/resume transitions
tensorrt_llm/runtime/kv_cache_manager_v2/...
Adds typed manager APIs and counters, with cache suspend/resume paths recording transitions when statistics are enabled.
Drain and serialize iteration counters
tensorrt_llm/_torch/pyexecutor/...
Collects and resets counters in KVCacheV2IterationStatsReport and emits iterSuspendedRequests and iterResumedRequests.
Validate iteration reporting and KV integrity
tests/unittest/..., tests/integration/...
Tests counter reporting, reset behavior, serialization placement, test scheduling, and contended suspend/resume correctness.

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

Sequence Diagram(s)

sequenceDiagram
  participant _KVCache
  participant KVCacheManager
  participant KVCacheManagerV2
  participant BaseWorker
  _KVCache->>KVCacheManager: record suspend/resume transition
  KVCacheManagerV2->>KVCacheManager: get_and_reset_iteration_suspend_resume_stats()
  KVCacheManager-->>KVCacheManagerV2: suspended and resumed counts
  KVCacheManagerV2->>BaseWorker: serialize iteration counters
Loading

Suggested labels: api-compatible

Suggested reviewers: bowenfu, zhanruisunch

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 45.45% 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 is specific, correctly formatted, and matches the main change: KVCacheManagerV2 suspend/resume observability plus tests.
Description check ✅ Passed The description includes the required Description, Test Coverage, and PR Checklist sections and explains the change and validation clearly.
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.

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

🧹 Nitpick comments (1)
tests/unittest/kv_cache_manager_v2_tests/test_kv_cache_stats_behavior.py (1)

818-892: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Good coverage of report/reset/disabled paths — consider adding a failed-resume case.

All three new tests use the default max_util_for_resume=1.0, so resume() always succeeds. There's no test asserting resumed_requests stays unincremented when resume() is refused (e.g. by setting a low max_util_for_resume and driving GPU utilization above it before resuming). Since this counter exists specifically to signal that the preemption state machine "genuinely fired" (per the integration test's own rationale), a silent regression that increments the counter on a failed resume would be exactly the kind of bug this feature is meant to catch, yet nothing here tests it.

🤖 Prompt for 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.

In `@tests/unittest/kv_cache_manager_v2_tests/test_kv_cache_stats_behavior.py`
around lines 818 - 892, Add a failed-resume test alongside
test_suspend_resume_iteration_stats_are_reported using a manager configured with
a low max_util_for_resume and GPU utilization above that threshold before
calling resume. Assert resume is refused, the cache remains suspended,
suspended_requests increments, and resumed_requests remains 0 in
get_iteration_stats.
🤖 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 `@tests/integration/defs/accuracy/test_llm_api_pytorch.py`:
- Around line 5196-5369: Register TestGPTOSS::test_w4_1gpu_suspend_resume in the
appropriate inspected integration test-list files under the QA and test-db
configurations, following the existing syntax and placement conventions for
accuracy tests. Ensure the new test is included in the relevant GPU suites so it
is scheduled and counted for coverage.

---

Nitpick comments:
In `@tests/unittest/kv_cache_manager_v2_tests/test_kv_cache_stats_behavior.py`:
- Around line 818-892: Add a failed-resume test alongside
test_suspend_resume_iteration_stats_are_reported using a manager configured with
a low max_util_for_resume and GPU utilization above that threshold before
calling resume. Assert resume is refused, the cache remains suspended,
suspended_requests increments, and resumed_requests remains 0 in
get_iteration_stats.
🪄 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: 1ce9b0bb-83d7-4c13-bc9b-5bcd1b1d66b9

📥 Commits

Reviewing files that changed from the base of the PR and between 697738c and 6b8e159.

📒 Files selected for processing (8)
  • tensorrt_llm/_torch/pyexecutor/kv_cache_manager_v2.py
  • tensorrt_llm/_torch/pyexecutor/kv_cache_stats.py
  • tensorrt_llm/runtime/kv_cache_manager_v2/__init__.pyi
  • tensorrt_llm/runtime/kv_cache_manager_v2/_core/_kv_cache.py
  • tensorrt_llm/runtime/kv_cache_manager_v2/_core/_kv_cache_manager.py
  • tests/integration/defs/accuracy/test_llm_api_pytorch.py
  • tests/unittest/executor/test_stats_serializer.py
  • tests/unittest/kv_cache_manager_v2_tests/test_kv_cache_stats_behavior.py

Comment thread tests/integration/defs/accuracy/test_llm_api_pytorch.py Outdated
@eopXD
eopXD force-pushed the kvcv2-suspend-resume-stat branch 2 times, most recently from 62284ae to 01825e4 Compare July 22, 2026 05:30

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

🤖 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 `@tests/unittest/kv_cache_manager_v2_tests/test_kv_cache_stats_behavior.py`:
- Around line 818-837: Annotate all newly introduced functions: in
tests/unittest/kv_cache_manager_v2_tests/test_kv_cache_stats_behavior.py:818-837,
add the precise existing type for resource_guard and annotate the test/helper
return types; in tests/unittest/executor/test_stats_serializer.py:429, add a ->
None return annotation; in
tests/integration/defs/accuracy/test_llm_api_pytorch.py:5196-5266, annotate the
test and both local helpers with parameter and return types.
🪄 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: 656700ec-6028-479e-9d31-7da1ff322988

📥 Commits

Reviewing files that changed from the base of the PR and between 6b8e159 and 62284ae.

📒 Files selected for processing (8)
  • tensorrt_llm/_torch/pyexecutor/kv_cache_manager_v2.py
  • tensorrt_llm/_torch/pyexecutor/kv_cache_stats.py
  • tensorrt_llm/runtime/kv_cache_manager_v2/__init__.pyi
  • tensorrt_llm/runtime/kv_cache_manager_v2/_core/_kv_cache.py
  • tensorrt_llm/runtime/kv_cache_manager_v2/_core/_kv_cache_manager.py
  • tests/integration/defs/accuracy/test_llm_api_pytorch.py
  • tests/unittest/executor/test_stats_serializer.py
  • tests/unittest/kv_cache_manager_v2_tests/test_kv_cache_stats_behavior.py
🚧 Files skipped from review as they are similar to previous changes (3)
  • tensorrt_llm/_torch/pyexecutor/kv_cache_stats.py
  • tensorrt_llm/_torch/pyexecutor/kv_cache_manager_v2.py
  • tensorrt_llm/runtime/kv_cache_manager_v2/_core/_kv_cache_manager.py

…verage

Add a Python-only per-iteration suspend/resume (preemption) observability
counter to KVCacheManagerV2 -- iterSuspendedRequests / iterResumedRequests,
surfaced via get_stats()

Signed-off-by: Yueh-Ting Chen <yuehtingc@nvidia.com>
@eopXD
eopXD force-pushed the kvcv2-suspend-resume-stat branch from 01825e4 to a83f909 Compare July 22, 2026 05:41
@eopXD

eopXD commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60902 [ run ] triggered by Bot. Commit: a83f909 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

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

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

Purely additive suspend/resume observability: counters increment after the completed state transition, guarded by _should_record_stats() (default off), no change to logic/return values/hot path. LGTM.

@nvpohanh
nvpohanh requested a review from yizhang-nv July 24, 2026 08:15
@nvpohanh

Copy link
Copy Markdown
Collaborator

[by Codex] @VALLIS-NERIA Could you please review this PR? Thank you!

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.

4 participants