Skip to content

[https://nvbugs/6438658][fix] Fix KV cache estimation capacity - #16545

Merged
jiaganc merged 3 commits into
NVIDIA:mainfrom
jiaganc:codex/fix-kv-estimation-cuda-graph-warmup
Jul 22, 2026
Merged

[https://nvbugs/6438658][fix] Fix KV cache estimation capacity#16545
jiaganc merged 3 commits into
NVIDIA:mainfrom
jiaganc:codex/fix-kv-estimation-cuda-graph-warmup

Conversation

@jiaganc

@jiaganc jiaganc commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • Improvements

    • Improved KV-cache capacity and resumable-capacity calculations across multi-device setups.
    • Refined cache configuration construction and warmup handling for more consistent sizing.
    • DeepSeek-V4 cache sizing now uses average sequence length when available.
    • Added support for previously unavailable DeepSeek-V4 layer-size queries.
  • Bug Fixes

    • Invalid max_util_for_resume=0 values are now rejected.
    • Improved cache initialization fallback behavior when host-tier setup requires retrying.
  • Documentation

    • Clarified cache buffer registration and configuration behavior.

Description

Fix KV cache capacity estimation so the resulting cache can satisfy runtime warmup and resume requirements.

Without these constraints, pool-ratio estimation can allocate a pool layout that cannot run CUDA graph warmup. Ignoring resume utilization during distributed quota synchronization can also remove the reserved headroom and overestimate usable token capacity.

  • Preserve max_util_for_resume headroom when synchronizing KV cache capacity across distributed ranks.
  • Cover the full CUDA graph generation warmup batch and the per-iteration context token budget when estimating pool ratios.
  • Refactor: Create the generic V2 cache configuration, standard layers, and estimation constraints in KVCacheManagerV2. Let specialized cache managers update the base configuration.

Test Coverage

  • Added and updated unit coverage for distributed resume headroom, generic estimation constraints, DeepSeek V4 typical-step configuration, and max_util_for_resume validation.
  • pre-commit passed for all changed files.
  • python3 -m py_compile passed for the changed Python files.
  • Focused pytest execution was not available locally because the environment lacks compatible compiled tensorrt_llm.bindings.

PR Checklist

  • 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, comment /bot help.

@jiaganc
jiaganc force-pushed the codex/fix-kv-estimation-cuda-graph-warmup branch 2 times, most recently from fa320a4 to 5e8eea7 Compare July 20, 2026 06:35
@jiaganc jiaganc added the api-compatible Accepted LLM API contract change that is backwards-compatible label Jul 20, 2026
@jiaganc
jiaganc marked this pull request as ready for review July 20, 2026 06:43
@jiaganc
jiaganc requested review from a team as code owners July 20, 2026 06:43
@coderabbitai

coderabbitai Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

KV cache configuration construction is split into base and subclass stages. Resumable quota synchronization and max_util_for_resume validation are updated. DeepSeek-V4 uses average sequence length for typical-step sizing, and tests cover the revised configuration paths.

Changes

KV cache configuration changes

Layer / File(s) Summary
Base configuration and customization flow
tensorrt_llm/_torch/pyexecutor/kv_cache_manager_v2.py, tensorrt_llm/_torch/attention_backend/sparse/minimax_m3/cache_manager.py, tests/unittest/_torch/executor/test_kv_cache_manager_v2.py
KVCacheManagerV2 now builds base cache configuration, warmup constraints, scratch reuse settings, and tier wiring before applying the subclass customization hook. Tests cover pool-ratio, warmup, partial-reuse, and block-reuse configuration.
Quota synchronization and validation
tensorrt_llm/_torch/pyexecutor/kv_cache_manager_v2.py, tensorrt_llm/llmapi/llm_args.py, tests/unittest/llmapi/test_llm_args.py
Resumable quota synchronization applies max_util_for_resume consistently, while zero is rejected by KvCacheConfig validation.
DeepSeek-V4 cache customization
tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/cache_manager.py, tests/unittest/_torch/attention/sparse/deepseek_v4/test_deepseek_v4_cache_manager.py
DeepSeek-V4 uses stored average sequence length for typical-step sizing, updates configurations with replace, and returns 1 from get_layer_bytes_per_token.
Estimated code review effort: 4 (Complex) ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant KVCacheManagerV2
  participant BaseConfigBuilder
  participant DeepseekV4CacheManager
  participant CacheConfig
  KVCacheManagerV2->>BaseConfigBuilder: build base cache configuration
  BaseConfigBuilder->>CacheConfig: create layers and constraints
  KVCacheManagerV2->>DeepseekV4CacheManager: apply subclass customization
  DeepseekV4CacheManager->>CacheConfig: set layers and typical_step
  DeepseekV4CacheManager-->>KVCacheManagerV2: return updated configuration
Loading

Possibly related PRs

Suggested reviewers: lfr-0531, kaiyux, reasonsolo

🚥 Pre-merge checks | ✅ 4 | ❌ 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 (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise, correctly formatted, and matches the main change: fixing KV cache estimation capacity.
Description check ✅ Passed The description covers the issue, solution, test coverage, and checklist items in the required template 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.

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
tests/unittest/_torch/executor/test_kv_cache_manager_v2.py (1)

34-59: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Cover nonzero extra-KV-token warmup capacity.

The helper fixes num_extra_kv_tokens to zero, so the new constraints never verify either added extra-token term. Parameterize it and add a case asserting both decode and context capacity.

Suggested coverage
 def _make_cache_config_for_test(
     kv_cache_config: KvCacheConfig,
     *,
     ...
     max_draft_len: int = 0,
+    num_extra_kv_tokens: int = 0,
 ) -> KVCacheManagerConfig:
     ...
-    cache_manager.num_extra_kv_tokens = 0
+    cache_manager.num_extra_kv_tokens = num_extra_kv_tokens

Add a test in tests/unittest/_torch/executor/test_kv_cache_manager_v2.py with max_draft_len=2 and num_extra_kv_tokens=4; it should expect minimal decode capacities of 7 and a context constraint capacity of 2052.

As per path instructions, tests/** feedback must identify concrete files and whether coverage is sufficient; coverage is insufficient here.

🤖 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/_torch/executor/test_kv_cache_manager_v2.py` around lines 34 -
59, Update _make_cache_config_for_test to accept a num_extra_kv_tokens parameter
and assign it to cache_manager instead of always using zero. Add the requested
test case in test_kv_cache_manager_v2.py with max_draft_len=2 and
num_extra_kv_tokens=4, asserting minimal decode capacity 7 and context
constraint capacity 2052.

Source: Path instructions

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

Outside diff comments:
In `@tests/unittest/_torch/executor/test_kv_cache_manager_v2.py`:
- Around line 34-59: Update _make_cache_config_for_test to accept a
num_extra_kv_tokens parameter and assign it to cache_manager instead of always
using zero. Add the requested test case in test_kv_cache_manager_v2.py with
max_draft_len=2 and num_extra_kv_tokens=4, asserting minimal decode capacity 7
and context constraint capacity 2052.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 167c3e6e-1c4d-4930-8017-818c68545fe2

📥 Commits

Reviewing files that changed from the base of the PR and between f354b77 and 5e8eea7.

📒 Files selected for processing (7)
  • tensorrt_llm/_torch/attention_backend/sparse/deepseek_v4/cache_manager.py
  • tensorrt_llm/_torch/attention_backend/sparse/minimax_m3/cache_manager.py
  • tensorrt_llm/_torch/pyexecutor/kv_cache_manager_v2.py
  • tensorrt_llm/llmapi/llm_args.py
  • tests/unittest/_torch/attention/sparse/deepseek_v4/test_deepseek_v4_cache_manager.py
  • tests/unittest/_torch/executor/test_kv_cache_manager_v2.py
  • tests/unittest/llmapi/test_llm_args.py

@jiaganc
jiaganc force-pushed the codex/fix-kv-estimation-cuda-graph-warmup branch from 5e8eea7 to 745369c Compare July 20, 2026 06:53
@jiaganc

jiaganc commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60299 [ run ] triggered by Bot. Commit: 745369c Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60299 [ run ] completed with state FAILURE. Commit: 745369c
/LLM/main/L0_MergeRequest_PR pipeline #48650 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 #60315 [ run ] triggered by Bot. Commit: 745369c Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60315 [ run ] completed with state FAILURE. Commit: 745369c
/LLM/main/L0_MergeRequest_PR pipeline #48661 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 #60396 [ run ] triggered by Bot. Commit: 5d4390f Link to invocation

@mikeiovine
mikeiovine removed the request for review from Tabrizian July 20, 2026 15:44
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60396 [ run ] completed with state FAILURE. Commit: 5d4390f
/LLM/main/L0_MergeRequest_PR pipeline #48734 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 21, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run

jiaganc added 2 commits July 20, 2026 20:10
Signed-off-by: Jiagan Cheng <jiaganc@nvidia.com>
Signed-off-by: Jiagan Cheng <jiaganc@nvidia.com>
@jiaganc
jiaganc force-pushed the codex/fix-kv-estimation-cuda-graph-warmup branch from 5d4390f to 8604e52 Compare July 21, 2026 03:10
@jiaganc

jiaganc commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60555 [ run ] triggered by Bot. Commit: 8604e52 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60555 [ run ] completed with state SUCCESS. Commit: 8604e52
/LLM/main/L0_MergeRequest_PR pipeline #48866 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 21, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60639 [ run ] triggered by Bot. Commit: 8604e52 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60639 [ run ] completed with state SUCCESS. Commit: 8604e52
/LLM/main/L0_MergeRequest_PR pipeline #48942 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 21, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60664 [ run ] triggered by Bot. Commit: a119c9b Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60664 [ run ] completed with state FAILURE. Commit: a119c9b
/LLM/main/L0_MergeRequest_PR pipeline #48959 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 21, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60679 [ run ] triggered by Bot. Commit: a119c9b Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60679 [ run ] completed with state FAILURE. Commit: a119c9b
/LLM/main/L0_MergeRequest_PR pipeline #48974 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 21, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60698 [ run ] triggered by Bot. Commit: a119c9b Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60698 [ run ] completed with state FAILURE. Commit: a119c9b
/LLM/main/L0_MergeRequest_PR pipeline #48989 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 22, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60864 [ run ] triggered by Bot. Commit: a119c9b Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60864 [ run ] completed with state SUCCESS. Commit: a119c9b
/LLM/main/L0_MergeRequest_PR pipeline #49136 completed with status: 'SUCCESS'

CI Report

Link to invocation

@jiaganc
jiaganc merged commit 697738c into NVIDIA:main Jul 22, 2026
7 checks passed
@jiaganc
jiaganc deleted the codex/fix-kv-estimation-cuda-graph-warmup branch July 22, 2026 05:11
yuanjingx87 pushed a commit to yuanjingx87/TensorRT-LLM that referenced this pull request Jul 26, 2026
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.

6 participants