Skip to content

fix tp capture in vit cuda graph#17255

Merged
JustinTong0323 merged 4 commits intosgl-project:mainfrom
narutolhy:fix_vit_cuda_graph_all_reduce
Mar 27, 2026
Merged

fix tp capture in vit cuda graph#17255
JustinTong0323 merged 4 commits intosgl-project:mainfrom
narutolhy:fix_vit_cuda_graph_all_reduce

Conversation

@narutolhy
Copy link
Copy Markdown
Contributor

Motivation

When using the ViT CUDA Graph runner with Tensor Parallelism enabled, CUDA Graph capture may implicitly involve TP communication paths (e.g., all-reduce or capture-aware communicators). However, the existing implementation does not align the communicator’s capture lifecycle with torch.cuda.graph(...), which can lead to incorrect capture behavior, runtime errors, or hangs during replay in TP setups.

This PR ensures that TP communication (when capture-aware) is properly included in the CUDA Graph capture phase, improving correctness and stability for ViT graph execution under Tensor Parallelism.

Modifications

Updated python/sglang/srt/multimodal/vit_cuda_graph_runner.py to correctly handle TP communication during CUDA Graph capture.

Introduced nullcontext to provide a zero-overhead fallback when no capture-aware communicator is present.

Retrieved the current TP group via get_tp_group() and conditionally wrapped CUDA Graph capture with tp_group.ca_comm.capture() when available.

The final capture logic becomes:

Use ca_comm.capture() only if the TP group provides a capture-aware communicator.

Preserve the original behavior when no such communicator exists.

This change is strictly scoped to the CUDA Graph creation path and does not affect non-TP or non-graph execution.

Accuracy Tests

covered in test_vlm_vit_cuda_graph.py

Benchmarking and Profiling

Checklist

Review Process

  1. Ping Merge Oncalls to start the PR flow. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • /tag-run-ci-label, /rerun-failed-ci, /tag-and-rerun-ci
  4. After green CI and required approvals, ask Merge Oncalls to merge.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @narutolhy, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves an issue in the ViT CUDA Graph runner where Tensor Parallelism communication was not correctly integrated into the CUDA Graph capture process, leading to potential instability. By conditionally incorporating capture-aware communicators during graph creation, it ensures proper synchronization of TP communication within the graph, thereby improving the reliability and correctness of ViT model execution under Tensor Parallelism without affecting non-TP or non-graph execution.

Highlights

  • CUDA Graph Capture for Tensor Parallelism: Ensures that Tensor Parallelism (TP) communication paths are correctly included in the CUDA Graph capture phase when using the ViT CUDA Graph runner, preventing runtime errors or hangs due to unaligned capture behavior.
  • Conditional Capture Context: Implements conditional wrapping of the CUDA Graph capture with tp_group.ca_comm.capture() only when a capture-aware communicator is available, otherwise utilizing nullcontext for zero overhead.
  • Integration of nullcontext: Introduces nullcontext from Python's contextlib to provide a lightweight fallback, preserving the original behavior and efficiency when no capture-aware communicator is present.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request effectively addresses a critical issue concerning CUDA Graph capture in ViT models when Tensor Parallelism is enabled. The introduction of nullcontext and the conditional use of tp_group.ca_comm.capture() ensure that TP communication is properly integrated into the CUDA Graph capture phase, enhancing the correctness and stability of ViT graph execution. The changes are well-scoped and directly resolve the problem described in the motivation.

@yuan-luo yuan-luo self-requested a review January 17, 2026 15:05
Copy link
Copy Markdown
Collaborator

@yuan-luo yuan-luo left a comment

Choose a reason for hiding this comment

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

LGTM

@JustinTong0323
Copy link
Copy Markdown
Collaborator

/tag-and-rerun-ci

@JustinTong0323
Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

1 similar comment
@narutolhy
Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

@narutolhy narutolhy force-pushed the fix_vit_cuda_graph_all_reduce branch from 7c3fa93 to 577ea21 Compare February 25, 2026 06:31
@narutolhy
Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

1 similar comment
@narutolhy
Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

@narutolhy narutolhy force-pushed the fix_vit_cuda_graph_all_reduce branch 2 times, most recently from 114e8a8 to 11bbeed Compare March 2, 2026 06:11
@narutolhy
Copy link
Copy Markdown
Contributor Author

/tag-and-rerun-ci

@narutolhy narutolhy force-pushed the fix_vit_cuda_graph_all_reduce branch from 11bbeed to ef6ed25 Compare March 18, 2026 18:43
@narutolhy
Copy link
Copy Markdown
Contributor Author

/tag-and-rerun-ci

@yuan-luo
Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@narutolhy
Copy link
Copy Markdown
Contributor Author

/tag-and-rerun-ci

@narutolhy
Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

@JustinTong0323
Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

10 similar comments
@JustinTong0323
Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@JustinTong0323
Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@JustinTong0323
Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@JustinTong0323
Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@JustinTong0323
Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@JustinTong0323
Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@JustinTong0323
Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@JustinTong0323
Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@JustinTong0323
Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@JustinTong0323
Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@JustinTong0323 JustinTong0323 enabled auto-merge (squash) March 27, 2026 19:19
@JustinTong0323
Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

4 similar comments
@JustinTong0323
Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@JustinTong0323
Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@JustinTong0323
Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@JustinTong0323
Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@JustinTong0323 JustinTong0323 merged commit 9b29131 into sgl-project:main Mar 27, 2026
843 of 921 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants