Skip to content

[None][fix] avoid attention workspace resize during CUDA graph capture - #16319

Merged
yuxianq merged 8 commits into
NVIDIA:mainfrom
yuxianq:fix/cuda-graph-warmup-workspace
Jul 21, 2026
Merged

[None][fix] avoid attention workspace resize during CUDA graph capture#16319
yuxianq merged 8 commits into
NVIDIA:mainfrom
yuxianq:fix/cuda-graph-warmup-workspace

Conversation

@yuxianq

@yuxianq yuxianq commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • Performance
    • Improved CUDA Graph warmup and capture workflows for decoder and encoder operations.
    • Enhanced handling of graph-shaped workloads to support more reliable graph reuse.
  • Bug Fixes
    • Improved detection of unavailable graph configurations and clearer error handling.
    • Corrected output handling during warmup and capture phases.
    • Improved handling of workloads without available KV-cache space.

Description

CUDA graph warmup processes generation graph shapes from larger to smaller batch sizes so later captures can reuse the attention workspace. This assumption does not always hold: an attention kernel can switch implementations at a smaller batch size and require a larger workspace. Resizing the shared workspace after an earlier graph was captured invalidates the workspace address stored in that graph.

Separate eager warmup from graph capture for both generation and encoder CUDA graph runners. PyTorchModelEngine first runs every graph shape in warmup-only mode. This performs the normal eager warmup iterations, grows the shared attention workspace to the maximum required size, and retains the prepared graph metadata, but does not create CUDA graphs or a graph memory pool. It then runs the same shapes in capture-only mode, reuses the prepared metadata, and captures the runtime graphs without repeating the eager warmups or resizing the workspace. Piecewise CUDA graphs are captured only during the capture-only pass.

This keeps the same two eager warmups plus one capture per graph shape as before the PR while avoiding the previous double-capture approach. It also removes the need to disable replay, clear captured graph state, or replace and restore the CUDA graph memory pool, avoiding allocator pool lifetime conflicts.

Test Coverage

  • Passed single-GPU small-model PyTorch LLM API integration coverage from tests/integration/defs/accuracy/test_llm_api_pytorch.py on H200.
  • Profiled three alternating before-PR/current trials with Gemma-3-1B on one H200 at maximum batch size 64. Median CUDA graph warmup/capture time changed from 4.090 s to 3.463 s (-15.3%), and total engine warmup changed from 7.990 s to 7.272 s (-9.0%). Full LLM lifecycle time remained comparable.
  • Targeted pre-commit checks passed for both modified files.

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.

@yuxianq

yuxianq commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@yuxianq
yuxianq requested a review from haow-nv July 13, 2026 09:35
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58950 [ run ] triggered by Bot. Commit: 28bbed9 Link to invocation

@yuxianq

yuxianq commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58980 [ run ] triggered by Bot. Commit: f46dc2f Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58950 [ run ] completed with state ABORTED. Commit: 28bbed9
/LLM/main/L0_MergeRequest_PR pipeline #47482 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

@yuxianq
yuxianq marked this pull request as ready for review July 14, 2026 04:50
@yuxianq
yuxianq requested review from a team as code owners July 14, 2026 04:50
@yuxianq
yuxianq requested review from chienchunhung and nv-xtf July 14, 2026 04:50
@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

CUDA graph execution now uses explicit warmup-only and capture-only phases for decoder and encoder paths, validates prepared graph metadata, captures and returns outputs, and distinguishes direct warmup outputs from graph replay.

Changes

CUDA graph capture flow

Layer / File(s) Summary
Capture modes and decoder runner
tensorrt_llm/_torch/pyexecutor/cuda_graph_runner.py
Adds shared warmup-only and capture-only modes, metadata-based graph checks, capture validation, and weak-referenced captured outputs for the decoder runner.
Encoder graph runner
tensorrt_llm/_torch/pyexecutor/cuda_graph_runner.py
Applies the same mode handling, metadata validation, capture flow, and output storage to encoder graphs.
Decoder warmup and replay integration
tensorrt_llm/_torch/pyexecutor/model_engine.py
Runs decoder warmup in two phases, limits piecewise capture to capture-only mode, handles unavailable KV-cache shapes, and returns warmup outputs directly.
Encoder warmup and replay integration
tensorrt_llm/_torch/pyexecutor/model_engine.py
Runs encoder warmup in two phases, updates operation logging, and separates warmup output returns from captured-graph replay.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant ModelEngine
  participant CUDAGraphRunner
  participant EncoderCUDAGraphRunner
  participant CUDAGraph
  ModelEngine->>CUDAGraphRunner: run decoder warmup-only pass
  CUDAGraphRunner->>CUDAGraph: prepare graph metadata
  ModelEngine->>CUDAGraphRunner: run decoder capture-only pass
  CUDAGraphRunner->>CUDAGraph: capture and store graph output
  ModelEngine->>EncoderCUDAGraphRunner: run encoder warmup-only pass
  EncoderCUDAGraphRunner->>CUDAGraph: prepare graph metadata
  ModelEngine->>EncoderCUDAGraphRunner: run encoder capture-only pass
  EncoderCUDAGraphRunner->>CUDAGraph: capture and store graph output
Loading

Suggested reviewers: haow-nv, cascade812, yechank-nvidia

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise, follows the required template, and matches the main fix around CUDA graph capture and workspace resizing.
Description check ✅ Passed The description follows the template with Description, Test Coverage, and PR Checklist sections filled in.
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.

@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)
tensorrt_llm/_torch/pyexecutor/model_engine.py (1)

5267-5292: 🩺 Stability & Availability | 🔴 Critical | ⚡ Quick win

Warmup-only execution can read an uninitialized capture result.

Existing or duplicate graph keys make needs_capture false, but both warmup-only branches still consume capture_outputs, causing UnboundLocalError.

  • tensorrt_llm/_torch/pyexecutor/model_engine.py#L5267-L5292: invoke capture() whenever encoder warmup-only mode is active.
  • tensorrt_llm/_torch/pyexecutor/model_engine.py#L5466-L5488: apply the same condition to decoder capture.
🤖 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 `@tensorrt_llm/_torch/pyexecutor/model_engine.py` around lines 5267 - 5292, The
encoder capture flow around encoder_cuda_graph_runner.capture and the
corresponding decoder flow must invoke capture whenever warmup-only mode is
active, even when needs_capture is false; update both sites in
tensorrt_llm/_torch/pyexecutor/model_engine.py (5267-5292 and 5466-5488) so
capture runs when needs_capture or is_warmup_only is true, while preserving
normal replay behavior otherwise.
🤖 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 `@tensorrt_llm/_torch/pyexecutor/model_engine.py`:
- Around line 5267-5292: The encoder capture flow around
encoder_cuda_graph_runner.capture and the corresponding decoder flow must invoke
capture whenever warmup-only mode is active, even when needs_capture is false;
update both sites in tensorrt_llm/_torch/pyexecutor/model_engine.py (5267-5292
and 5466-5488) so capture runs when needs_capture or is_warmup_only is true,
while preserving normal replay behavior otherwise.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: d1feb614-cdd3-4ddf-9677-211ccaed2e72

📥 Commits

Reviewing files that changed from the base of the PR and between 11a880f and 5b71382.

📒 Files selected for processing (2)
  • tensorrt_llm/_torch/pyexecutor/cuda_graph_runner.py
  • tensorrt_llm/_torch/pyexecutor/model_engine.py

@yuxianq
yuxianq requested review from Tabrizian and tingyangk and removed request for chienchunhung and nv-xtf July 14, 2026 04:55
@yuxianq

yuxianq commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59109 [ run ] triggered by Bot. Commit: 5b71382 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58980 [ run ] completed with state ABORTED. Commit: f46dc2f

Link to invocation

@haow-nv haow-nv left a comment

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.

It works in my local testing(DeepSeekV3 bf16) with cutedsl mla attention fmha lib(#15138).

@yuxianq

yuxianq commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59128 [ run ] triggered by Bot. Commit: 5b71382 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59109 [ run ] completed with state ABORTED. Commit: 5b71382

Link to invocation

@yuxianq

yuxianq commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59137 [ run ] triggered by Bot. Commit: 0bcfd5e Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59128 [ run ] completed with state ABORTED. Commit: 5b71382

Link to invocation

yuxianq added 2 commits July 14, 2026 09:17
Signed-off-by: Yuxian Qiu <142763828+yuxianq@users.noreply.github.com>
Signed-off-by: Yuxian Qiu <142763828+yuxianq@users.noreply.github.com>
Signed-off-by: Yuxian Qiu <142763828+yuxianq@users.noreply.github.com>
@yuxianq

yuxianq commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59413 [ run ] triggered by Bot. Commit: 3c24394 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59413 [ run ] completed with state SUCCESS. Commit: 3c24394
/LLM/main/L0_MergeRequest_PR pipeline #47884 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

@yuxianq

yuxianq commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59469 [ run ] triggered by Bot. Commit: 3c24394 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59469 [ run ] completed with state SUCCESS. Commit: 3c24394
/LLM/main/L0_MergeRequest_PR pipeline #47936 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

@yuxianq

yuxianq commented Jul 16, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59684 [ run ] triggered by Bot. Commit: b612dc1 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

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

@yuxianq

yuxianq commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59875 [ run ] triggered by Bot. Commit: 41d1a92 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59875 [ run ] completed with state SUCCESS. Commit: 41d1a92
/LLM/main/L0_MergeRequest_PR pipeline #48278 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

@yuxianq

yuxianq commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59962 [ run ] triggered by Bot. Commit: 41d1a92 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59962 [ run ] completed with state FAILURE. Commit: 41d1a92
/LLM/main/L0_MergeRequest_PR pipeline #48356 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

@yuxianq

yuxianq commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60240 [ run ] triggered by Bot. Commit: 41d1a92 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60240 [ run ] completed with state SUCCESS. Commit: 41d1a92
/LLM/main/L0_MergeRequest_PR pipeline #48606 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

@yuxianq

yuxianq commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60340 [ run ] triggered by Bot. Commit: 41d1a92 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60340 [ run ] completed with state SUCCESS. Commit: 41d1a92
/LLM/main/L0_MergeRequest_PR pipeline #48687 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

@yuxianq

yuxianq commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60573 [ run ] triggered by Bot. Commit: 41d1a92 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60573 [ run ] completed with state SUCCESS. Commit: 41d1a92
/LLM/main/L0_MergeRequest_PR pipeline #48887 completed with status: 'SUCCESS'

CI Report

Link to invocation

@yuxianq
yuxianq merged commit 6fec819 into NVIDIA:main Jul 21, 2026
7 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.

6 participants