Skip to content

[None][fix] Retire the shared CUDA graph pool handle on engine teardown - #15973

Open
yizhang-nv wants to merge 2 commits into
NVIDIA:mainfrom
yizhang-nv:codex/fix-one-engine-spec-scheduler-budget
Open

[None][fix] Retire the shared CUDA graph pool handle on engine teardown#15973
yizhang-nv wants to merge 2 commits into
NVIDIA:mainfrom
yizhang-nv:codex/fix-one-engine-spec-scheduler-budget

Conversation

@yizhang-nv

@yizhang-nv yizhang-nv commented Jul 6, 2026

Copy link
Copy Markdown
Member

Description

accuracy/test_llm_api_pytorch.py::TestDeepSeekV32::test_nvfp4_multi_gpus_piecewise_cuda_graph[mtp3_fp8kv_chunked] has been waived under NVBug 5989920. The scheduler token-budget failure it was originally waived for (total_num_tokens (8195) should be <= max_num_tokens (8192)) was fixed on main by #16101. Re-enabling the test surfaced a second, unrelated failure, which this PR fixes.

The remaining failure

All 8 ranks aborted during executor init:

model_engine._capture_generation_cuda_graphs -> cuda_graph_runner.capture
  -> torch.cuda.graph(graph, pool=self.memory_pool) -> capture_begin
RuntimeError: it->second->use_count > 0 INTERNAL ASSERT FAILED
at c10/cuda/CUDACachingAllocator.cpp:3045

That assert lives in beginAllocateToPool: the private pool id is still present in graph_pools, but its use_count has already fallen to zero.

Backend._graph_pool_handle is class state, so a single private pool is shared by every engine in the process, and with torch.compile enabled the generation CUDAGraphRunner captures into it (model_engine.py). Engine teardown resets those graphs, dropping use_count to zero, but the allocator only erases the entry once the pool has no blocks left. The next engine built in the same worker process then captures into that retired handle and trips the assert.

clear_piecewise_cuda_graphs already rotates the per-runner handles for exactly this reason ("CUDACachingAllocator does not allow a private pool handle to be reused after its last graph is reset", added in #15923). The class-level handle was missed — and it is the generation runner's only consumer, since piecewise_optimizer allocates its own pool and ignores the handle passed to it. This PR rotates it in _release_cuda_graphs once every runner has been cleared.

This is why the test only fails in CI: it passes standalone, but the stage runs [baseline] first and the MPI workers are reused across both.

Test Coverage

8x B200, LLM_MODELS_ROOT set, both params in one pytest session (the ordering that reproduces the bug):

Scenario Result
Before fix, both params 1 failed, 1 passed + allocator assert on all 8 ranks
Before fix, [mtp3_fp8kv_chunked] alone 1 passed (isolates the ordering dependency)
After fix, both params 2 passed in 1180.31s, zero asserts

Post-fix accuracy: [mtp3_fp8kv_chunked] MMLU 87.865 (ref 87.200), GSM8K 95.072 (ref 95.600); [baseline] GSM8K 95.375 (ref 95.600).

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.

Dev Engineer Review

  • Added Backend.rotate_graph_pool_handle() to replace the shared CUDA graph pool handle after graph teardown.
  • Updated _release_cuda_graphs to clear TorchInductor and CUDA graph runners before rotating the handle.
  • This ordering prevents later engines from capturing into a retired pool.
  • The change is scoped to engine teardown and preserves the existing backend API pattern.
  • Removed the waived DeepSeek-V3.2 test entry for nvbug 5989920.

QA Engineer Review

  • Modified tests/integration/test_lists/waives.txt.
  • Removed TestDeepSeekV32::test_nvfp4_multi_gpus_piecewise_cuda_graph[mtp3_fp8kv_chunked].
  • No test-db/ or qa/ files were modified.
  • CBTS coverage data is unavailable.
  • Verdict: needs follow-up.

@yizhang-nv
yizhang-nv requested a review from a team as a code owner July 6, 2026 09:51
@yizhang-nv
yizhang-nv requested a review from achartier July 6, 2026 09:51
@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

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: 6337a04e-5e1b-4047-958e-43cff3b9173e

📥 Commits

Reviewing files that changed from the base of the PR and between 63eb095 and 4faea3e.

📒 Files selected for processing (3)
  • tensorrt_llm/_torch/compilation/backend.py
  • tensorrt_llm/_torch/pyexecutor/model_engine.py
  • tests/integration/test_lists/waives.txt
💤 Files with no reviewable changes (1)
  • tests/integration/test_lists/waives.txt
🚧 Files skipped from review as they are similar to previous changes (2)
  • tensorrt_llm/_torch/pyexecutor/model_engine.py
  • tensorrt_llm/_torch/compilation/backend.py

Walkthrough

The backend now creates a fresh CUDA graph pool handle after graph teardown. Model engine cleanup invokes this rotation after clearing graph runners. One waived DeepSeek integration test entry was removed.

Changes

CUDA graph pool lifecycle

Layer / File(s) Summary
Rotate the shared graph pool during teardown
tensorrt_llm/_torch/compilation/backend.py, tensorrt_llm/_torch/pyexecutor/model_engine.py
Backend.rotate_graph_pool_handle() creates a new CUDA graph pool handle. _release_cuda_graphs invokes it after clearing captured graphs.
Remove the resolved test waiver
tests/integration/test_lists/waives.txt
The waiver for TestDeepSeekV32::test_nvfp4_multi_gpus_piecewise_cuda_graph[mtp3_fp8kv_chunked] was removed.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested reviewers: schetlur-nv, mlefeb01, qijune

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title follows the required format and clearly describes retiring the shared CUDA graph pool handle during engine teardown.
Description check ✅ Passed The description explains the issue, root cause, solution, test coverage, results, and checklist status in sufficient detail.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.

@yizhang-nv

Copy link
Copy Markdown
Member Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #57916 [ run ] triggered by Bot. Commit: de560e6 Link to invocation

if (self.drafter is None
and getattr(self.model_engine, 'spec_config', None) is None):
return

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should also early return if use_spec_decode is False

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

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

@yizhang-nv

Copy link
Copy Markdown
Member Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58156 [ run ] triggered by Bot. Commit: e34fe34 Link to invocation

@yizhang-nv

Copy link
Copy Markdown
Member Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58158 [ run ] triggered by Bot. Commit: b71f9f0 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58156 [ run ] completed with state ABORTED. Commit: e34fe34

Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

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

@yizhang-nv

Copy link
Copy Markdown
Member Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58362 [ run ] triggered by Bot. Commit: b71f9f0 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58362 [ run ] completed with state SUCCESS. Commit: b71f9f0
/LLM/main/L0_MergeRequest_PR pipeline #46988 completed with status: 'SUCCESS'

CI Report

Link to invocation

chungen28 added a commit to deepinfra/TensorRT-LLM that referenced this pull request Jul 24, 2026
…upstream PR NVIDIA#15973)

Squash of upstream PR NVIDIA#15973 (4 commits). One-engine speculative
decoders (MTP / DSpark) keep the draft model inside ModelEngine, so PyExecutor.drafter
is None; under the overlap scheduler the next draft tokens are produced by the previous
batch and their count may not be copied to the C++ request before the next scheduling
pass, so the scheduler can budget a gen request as 1 token though the model consumes
1+K -> oversized mixed context/generation batch (total_num_tokens > max_num_tokens).

Run scheduler-facing draft-token reservation for every speculative decoder immediately
before scheduling (_prepare_scheduler_draft_tokens); placeholder token values are not
consumed, their length reserves the correct micro-batch capacity. Includes: skip when
spec decode is disabled, handle lightweight executor fixtures, and unwaive the
DeepSeekV3 nvfp4 mtp3_fp8kv_chunked test that exposed the bug.

Signed-off-by: chungen28 <chung-en@deepinfra.com>
@yizhang-nv
yizhang-nv force-pushed the codex/fix-one-engine-spec-scheduler-budget branch from b71f9f0 to 1639857 Compare August 5, 2026 06:38
@yizhang-nv
yizhang-nv requested review from a team as code owners August 5, 2026 06:38
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@yizhang-nv yizhang-nv changed the title [None][fix] Reserve one-engine speculative tokens before scheduling [None][test] Unwaive DeepSeekV3.2 nvfp4 mtp3_fp8kv_chunked test Aug 5, 2026
@yizhang-nv

Copy link
Copy Markdown
Member Author

/bot run --disable-fail-fast --stage-list "DGX_B200-8_GPUs-PyTorch-1, DGX_B200-8_GPUs-PyTorch-2, DGX_B200-8_GPUs-PyTorch-3, DGX_B200-8_GPUs-PyTorch-4"

@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown

Removed the "ci: full pre-merge approved" label because @yizhang-nv could not be verified as an active member of NVIDIA/trt-llm-ci-approvers. Ask a member of that team to apply it.

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #63972 [ run ] triggered by Bot. Commit: 1639857 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #63968 [ run ] completed with state ABORTED. Commit: 1639857

Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #63972 [ run ] completed with state FAILURE. Commit: 1639857
/LLM/main/L0_MergeRequest_PR pipeline #51907 completed with status: 'UNSTABLE'

CI Report

⚠️ Multi-GPU Label Required:
Multi-GPU tests require the ci: full pre-merge approved label on this PR. Ask a member of NVIDIA/trt-llm-ci-approvers to add the label, then re-trigger CI with the same bot command (no rebase needed).

⚠️ 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

@yizhang-nv

Copy link
Copy Markdown
Member Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #63992 [ run ] triggered by Bot. Commit: 1639857 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

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

@yizhang-nv
yizhang-nv requested review from a team as code owners August 5, 2026 12:51
@yizhang-nv
yizhang-nv requested a review from Shixiaowei02 August 5, 2026 12:51
@yizhang-nv yizhang-nv changed the title [None][test] Unwaive DeepSeekV3.2 nvfp4 mtp3_fp8kv_chunked test [None][fix] Retire the shared CUDA graph pool handle on engine teardown Aug 5, 2026

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

🧹 Nitpick comments (1)
tests/unittest/_torch/compilation/test_graph_pool_handle_rotation.py (1)

45-66: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Assert teardown order and cover encoder cleanup.

The allocator fix depends on clearing every runner before rotating the pool. This test sets encoder_cuda_graph_runner to None and checks only that calls occurred. It does not detect a future call-order regression or an encoder cleanup regression.

Add an encoder mock and assert this order: piecewise cleanup, decoder cleanup, encoder cleanup, then pool rotation.

Based on the supplied teardown and test code, call order is part of the stale-pool fix.

🤖 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/compilation/test_graph_pool_handle_rotation.py` around
lines 45 - 66, Update test_release_cuda_graphs_rotates_shared_pool to use an
encoder CUDA graph runner mock, then assert the teardown sequence is piecewise
graph cleanup, decoder runner cleanup, encoder runner cleanup, and finally
graph-pool handle rotation. Preserve the existing stale-handle assertion while
making the test fail if any cleanup occurs after rotation or encoder cleanup is
omitted.
🤖 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.

Nitpick comments:
In `@tests/unittest/_torch/compilation/test_graph_pool_handle_rotation.py`:
- Around line 45-66: Update test_release_cuda_graphs_rotates_shared_pool to use
an encoder CUDA graph runner mock, then assert the teardown sequence is
piecewise graph cleanup, decoder runner cleanup, encoder runner cleanup, and
finally graph-pool handle rotation. Preserve the existing stale-handle assertion
while making the test fail if any cleanup occurs after rotation or encoder
cleanup is omitted.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: dd395d9f-f888-40a6-8a00-7f803714700f

📥 Commits

Reviewing files that changed from the base of the PR and between 1639857 and b32b829.

📒 Files selected for processing (3)
  • tensorrt_llm/_torch/compilation/backend.py
  • tensorrt_llm/_torch/pyexecutor/model_engine.py
  • tests/unittest/_torch/compilation/test_graph_pool_handle_rotation.py

@yizhang-nv

Copy link
Copy Markdown
Member Author

/bot run --disable-fail-fast

@yizhang-nv
yizhang-nv force-pushed the codex/fix-one-engine-spec-scheduler-budget branch from b32b829 to c4a5959 Compare August 5, 2026 13:06
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@yizhang-nv

Copy link
Copy Markdown
Member Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #64056 [ run ] triggered by Bot. Commit: c4a5959 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #64057 [ run ] triggered by Bot. Commit: c4a5959 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #64056 [ run ] completed with state ABORTED. Commit: c4a5959

Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

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

Unwaive TestDeepSeekV32::test_nvfp4_multi_gpus_piecewise_cuda_graph[mtp3_fp8kv_chunked]
(nvbugs/5989920) to re-enable it in CI.

Signed-off-by: Yi Zhang <187001205+yizhang-nv@users.noreply.github.com>
Backend._graph_pool_handle is class state, so one private pool is shared
by every engine in the process. With torch.compile enabled the generation
CUDAGraphRunner captures into that pool. Tearing the engine down resets
those graphs, dropping the pool's use_count to zero, but the caching
allocator only erases the entry once the pool has no blocks left. A second
engine built in the same worker process therefore captured into a retired
handle and tripped beginAllocateToPool's use_count > 0 assert.

clear_piecewise_cuda_graphs already rotates the per-runner handles for
exactly this reason; the class-level handle -- whose only consumer is the
generation runner, since piecewise_optimizer allocates its own -- was
missed. Rotate it in _release_cuda_graphs once every runner has been
cleared.

Surfaced by back-to-back DeepSeek-V3.2 runs in one MPI worker:
test_nvfp4_multi_gpus_piecewise_cuda_graph[mtp3_fp8kv_chunked] passed in
isolation but failed after [baseline] in the same session.

Signed-off-by: Yi Zhang <187001205+yizhang-nv@users.noreply.github.com>
@yizhang-nv

Copy link
Copy Markdown
Member Author

/bot run --disable-fail-fast

@yizhang-nv
yizhang-nv force-pushed the codex/fix-one-engine-spec-scheduler-budget branch from c4a5959 to 4faea3e Compare August 6, 2026 02:18
@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #64174 [ run ] triggered by Bot. Commit: 4faea3e Link to invocation

so the next engine built in this process must start from a fresh id.
Same rationale as the per-runner rotation above.
"""
cls._graph_pool_handle = torch.cuda.graph_pool_handle()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I have removed the unnecessary _graph_pool_handle used inside torch compile in #16952.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants