Skip to content

[None][fix] Fix bugs in WindowBlockManager destructor statistics#12448

Merged
eopXD merged 1 commit intoNVIDIA:mainfrom
eopXD:fix/window-block-manager-stats-bugs
Apr 27, 2026
Merged

[None][fix] Fix bugs in WindowBlockManager destructor statistics#12448
eopXD merged 1 commit intoNVIDIA:mainfrom
eopXD:fix/window-block-manager-stats-bugs

Conversation

@eopXD
Copy link
Copy Markdown
Collaborator

@eopXD eopXD commented Mar 23, 2026

Description

Fix several bugs in the KV cache statistics logged during WindowBlockManager destruction:

  • Division-by-zero: mTotalInputTokens could be 0 when computing reused tokens percentage
  • Wrong zero-guard: reusedUniqueBlocksPercentage guarded mAllocTotalBlocks instead of the actual denominator mAllocNewBlocks
  • Potential integer overflow: cacheHitRate denominator sum was computed as int32_t before cast to floating point
  • Precision loss: float (23-bit mantissa) replaced with double for computed ratios to avoid precision loss with large counter values
  • Format specifier mismatch: %lu replaced with %d to match SizeType32 (int32_t)

Test Coverage

  • Compilation verification -- changes are debug-only logging in a destructor with no testable return value
  • Existing unit tests: pytest tests/unittest/

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)

  • 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

@eopXD eopXD requested a review from a team as a code owner March 23, 2026 09:13
@eopXD eopXD force-pushed the fix/window-block-manager-stats-bugs branch from 613657e to 06dfda8 Compare March 23, 2026 09:16
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 23, 2026

📝 Walkthrough

Walkthrough

Modified the WindowBlockManager destructor's statistics computation and logging in a KV cache manager. Changes include switching intermediate calculation types from float to double, adjusting zero-division guards for percentage and hit-rate calculations, and updating format specifiers for block count logging.

Changes

Cohort / File(s) Summary
KV Cache Statistics Logging
cpp/tensorrt_llm/batch_manager/kvCacheManager.cpp
Updated destructor statistics computation: changed percentage intermediate types to double, corrected zero-division guards for reusedUniqueBlocksPercentage (denominator changed to mAllocNewBlocks == 0) and cacheHitRate (denominator changed to (mReusedBlocks + mMissedBlocks) == 0), updated log format specifiers from %lu to %d, and added zero-division guard for token reuse ratio.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically summarizes the main change: fixing bugs in WindowBlockManager destructor statistics, which is the primary focus of the changeset.
Description check ✅ Passed The PR description comprehensively addresses all required sections with clear explanations of bugs fixed, test coverage details, and a completed checklist.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

@eopXD
Copy link
Copy Markdown
Collaborator Author

eopXD commented Mar 23, 2026

/bot run

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

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)
cpp/tensorrt_llm/batch_manager/kvCacheManager.cpp (1)

757-762: Mark write-once local variables as const.

Lines 757 and 760 declare reusedUniqueBlocksPercentage and cacheHitRate which are initialized once and never reassigned. They should be declared const to improve intent and align with the coding guideline: "A variable that is not modified after its initialization should be declared as const."

Suggested refactor
-    double reusedUniqueBlocksPercentage = mAllocNewBlocks == 0
+    double const reusedUniqueBlocksPercentage = mAllocNewBlocks == 0
         ? 0.0
         : static_cast<double>(mReusedUniqueBlocks) / static_cast<double>(mAllocNewBlocks) * 100.0;
-    double cacheHitRate = (mReusedBlocks + mMissedBlocks) == 0 ? 0.0
+    double const cacheHitRate = (mReusedBlocks + mMissedBlocks) == 0 ? 0.0
                                                                : static_cast<double>(mReusedBlocks)
             / (static_cast<double>(mReusedBlocks) + static_cast<double>(mMissedBlocks));
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@cpp/tensorrt_llm/batch_manager/kvCacheManager.cpp` around lines 757 - 762,
The two local variables reusedUniqueBlocksPercentage and cacheHitRate in
kvCacheManager.cpp are initialized once and never mutated, so mark them const to
express intent; locate their declarations (reusedUniqueBlocksPercentage and
cacheHitRate) inside the method in class/namespace handling KV cache statistics
(around the block computing reused vs new/missed blocks) and change their
declarations from "double" to "const double" without altering the existing
initialization expressions or numeric casts.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@cpp/tensorrt_llm/batch_manager/kvCacheManager.cpp`:
- Around line 760-762: Compute the denominator using a wider integer type to
avoid 32-bit signed overflow: cast mReusedBlocks and mMissedBlocks to a 64-bit
unsigned or signed integer (e.g., std::uint64_t or std::int64_t) before summing
and use that sum for the zero check and division; then compute cacheHitRate by
dividing static_cast<double>(mReusedBlocks) by the wider-typed denominator (or
convert the denominator to double) to preserve precision. Also mark
reusedUniqueBlocksPercentage and cacheHitRate as const since they are assigned
only once. Update references to mReusedBlocks and mMissedBlocks in this
expression so the check uses the wider-typed sum rather than adding two
SizeType32 values directly.

---

Nitpick comments:
In `@cpp/tensorrt_llm/batch_manager/kvCacheManager.cpp`:
- Around line 757-762: The two local variables reusedUniqueBlocksPercentage and
cacheHitRate in kvCacheManager.cpp are initialized once and never mutated, so
mark them const to express intent; locate their declarations
(reusedUniqueBlocksPercentage and cacheHitRate) inside the method in
class/namespace handling KV cache statistics (around the block computing reused
vs new/missed blocks) and change their declarations from "double" to "const
double" without altering the existing initialization expressions or numeric
casts.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: e096c772-9a0e-47a4-b073-d63e99e1ea86

📥 Commits

Reviewing files that changed from the base of the PR and between 4f929fe and 06dfda8.

📒 Files selected for processing (1)
  • cpp/tensorrt_llm/batch_manager/kvCacheManager.cpp

Comment thread cpp/tensorrt_llm/batch_manager/kvCacheManager.cpp
@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

PR_Github #39913 [ run ] triggered by Bot. Commit: 06dfda8 Link to invocation

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

PR_Github #39913 [ run ] completed with state SUCCESS. Commit: 06dfda8
/LLM/main/L0_MergeRequest_PR pipeline #31080 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

Link to invocation

@eopXD eopXD force-pushed the fix/window-block-manager-stats-bugs branch from 06dfda8 to 7f3ed29 Compare March 24, 2026 02:43
@eopXD
Copy link
Copy Markdown
Collaborator Author

eopXD commented Mar 24, 2026

/bot run --disable-fail-fast

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

PR_Github #40035 [ run ] triggered by Bot. Commit: 7f3ed29 Link to invocation

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

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

Link to invocation

@eopXD
Copy link
Copy Markdown
Collaborator Author

eopXD commented Mar 25, 2026

/bot run --disable-fail-fast

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

PR_Github #40264 [ run ] triggered by Bot. Commit: 7f3ed29 Link to invocation

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

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

Link to invocation

@eopXD eopXD force-pushed the fix/window-block-manager-stats-bugs branch from 7f3ed29 to 5a81a82 Compare March 26, 2026 02:45
@eopXD
Copy link
Copy Markdown
Collaborator Author

eopXD commented Mar 26, 2026

/bot run --disable-fail-fast

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

PR_Github #40412 [ run ] triggered by Bot. Commit: 5a81a82 Link to invocation

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

PR_Github #40412 [ run ] completed with state SUCCESS. Commit: 5a81a82
/LLM/main/L0_MergeRequest_PR pipeline #31505 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

Link to invocation

@eopXD eopXD force-pushed the fix/window-block-manager-stats-bugs branch from 5a81a82 to d9d08bc Compare March 30, 2026 08:39
@eopXD
Copy link
Copy Markdown
Collaborator Author

eopXD commented Mar 30, 2026

/bot run --disable-fail-fast

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

PR_Github #40706 [ run ] triggered by Bot. Commit: d9d08bc Link to invocation

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

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

Link to invocation

@eopXD
Copy link
Copy Markdown
Collaborator Author

eopXD commented Mar 31, 2026

/bot run --disable-fail-fast

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

PR_Github #40829 [ run ] triggered by Bot. Commit: d9d08bc Link to invocation

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

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

Link to invocation

@eopXD eopXD force-pushed the fix/window-block-manager-stats-bugs branch from d9d08bc to b36b0b1 Compare April 7, 2026 07:39
@eopXD
Copy link
Copy Markdown
Collaborator Author

eopXD commented Apr 7, 2026

/bot run --disable-fail-fast

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

PR_Github #42101 [ run ] triggered by Bot. Commit: b36b0b1 Link to invocation

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

PR_Github #42101 [ run ] completed with state FAILURE. Commit: b36b0b1
/LLM/main/L0_MergeRequest_PR pipeline #32939 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

Link to invocation

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

PR_Github #43215 [ run ] triggered by Bot. Commit: b36b0b1 Link to invocation

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

PR_Github #43215 [ run ] completed with state DISABLED
Freeze main and open the PR merge only after CI is back to healthy https://nvidia.slack.com/archives/C059LSY62BT/p1776141760843319?thread_ts=1775985925.442509&cid=C059LSY62BT

Link to invocation

@eopXD
Copy link
Copy Markdown
Collaborator Author

eopXD commented Apr 15, 2026

/bot run --disable-fail-fast

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

PR_Github #43329 [ run ] triggered by Bot. Commit: b36b0b1 Link to invocation

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

PR_Github #43329 [ run ] completed with state FAILURE. Commit: b36b0b1
/LLM/main/L0_MergeRequest_PR pipeline #33871 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

Link to invocation

@eopXD eopXD force-pushed the fix/window-block-manager-stats-bugs branch from b36b0b1 to e5ac14d Compare April 16, 2026 02:38
@eopXD
Copy link
Copy Markdown
Collaborator Author

eopXD commented Apr 16, 2026

/bot run --disable-fail-fast

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

PR_Github #43624 [ run ] triggered by Bot. Commit: e5ac14d Link to invocation

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

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

Link to invocation

@eopXD eopXD force-pushed the fix/window-block-manager-stats-bugs branch from e5ac14d to fdaa56e Compare April 20, 2026 05:34
@eopXD
Copy link
Copy Markdown
Collaborator Author

eopXD commented Apr 20, 2026

/bot run --disable-fail-fast

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

PR_Github #44325 [ run ] triggered by Bot. Commit: fdaa56e Link to invocation

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

PR_Github #44325 [ run ] completed with state FAILURE. Commit: fdaa56e
/LLM/main/L0_MergeRequest_PR pipeline #34743 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

Link to invocation

@eopXD
Copy link
Copy Markdown
Collaborator Author

eopXD commented Apr 20, 2026

/bot run --disable-fail-fast

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

PR_Github #44456 [ run ] triggered by Bot. Commit: fdaa56e Link to invocation

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

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

Link to invocation

@eopXD
Copy link
Copy Markdown
Collaborator Author

eopXD commented Apr 21, 2026

/bot run --disable-fail-fast

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

PR_Github #44602 [ run ] triggered by Bot. Commit: fdaa56e Link to invocation

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

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

Link to invocation

…rting

Fix several bugs in the KV cache statistics logged during
WindowBlockManager destruction:

1. Division-by-zero when mTotalInputTokens is 0 (reused tokens %).
2. Wrong zero-guard for reusedUniqueBlocksPercentage: guarded
   mAllocTotalBlocks instead of the actual denominator mAllocNewBlocks.
3. Potential integer overflow in cacheHitRate denominator: the sum
   mReusedBlocks + mMissedBlocks was computed as int32 before cast.
4. Use double instead of float for computed ratios to avoid precision
   loss for large counter values (float has only 23-bit mantissa).
5. Fix format specifiers from %lu to %d to match SizeType32 (int32_t).

Signed-off-by: Yueh-Ting Chen <yueh.ting.chen@gmail.com>
@eopXD eopXD force-pushed the fix/window-block-manager-stats-bugs branch from fdaa56e to 9e2590e Compare April 22, 2026 06:18
@eopXD
Copy link
Copy Markdown
Collaborator Author

eopXD commented Apr 22, 2026

/bot run --disable-fail-fast

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

PR_Github #44916 [ run ] triggered by Bot. Commit: 9e2590e Link to invocation

@eopXD
Copy link
Copy Markdown
Collaborator Author

eopXD commented Apr 23, 2026

/bot run --disable-fail-fast

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

PR_Github #45118 [ run ] triggered by Bot. Commit: 9e2590e Link to invocation

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

PR_Github #45118 [ run ] completed with state SUCCESS. Commit: 9e2590e
/LLM/main/L0_MergeRequest_PR pipeline #35412 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

Link to invocation

@eopXD
Copy link
Copy Markdown
Collaborator Author

eopXD commented Apr 24, 2026

/bot run --disable-fail-fast

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

PR_Github #45324 [ run ] triggered by Bot. Commit: 9e2590e Link to invocation

@tensorrt-cicd
Copy link
Copy Markdown
Collaborator

PR_Github #45324 [ run ] completed with state SUCCESS. Commit: 9e2590e
/LLM/main/L0_MergeRequest_PR pipeline #35577 completed with status: 'SUCCESS'

CI Report

Link to invocation

@eopXD eopXD merged commit 6119440 into NVIDIA:main Apr 27, 2026
5 checks passed
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.

3 participants