[VLM] Refactor ViT CUDA Graph key generation#16902
[VLM] Refactor ViT CUDA Graph key generation#16902yuan-luo wants to merge 1 commit intosgl-project:mainfrom
Conversation
Summary of ChangesHello @yuan-luo, 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 improves the robustness and correctness of CUDA Graph utilization for visual language models by refining the graph key generation process. It moves beyond simple sequence length-based keys to incorporate detailed structural information of the input sequences through a new hashing mechanism. This ensures that CUDA graphs are correctly cached and reused only when the underlying computational structure is identical, preventing potential runtime errors and maintaining performance integrity. Highlights
🧠 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 AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request refactors the CUDA graph key generation for Vision Transformer (ViT) models to make it more robust. Previously, the key was based only on seq_len, which could lead to incorrect graph sharing when sequences had the same length but different internal structures (e.g., different cu_seqlens). The new approach introduces a graph_key_hint derived from a hash of cu_seqlens and cu_window_seqlens, ensuring a unique key for each distinct graph structure. The changes are implemented across qwen2_5_vl.py, qwen3_vl.py, and the core vit_cuda_graph_runner.py. The implementation correctly computes the hash on the CPU to avoid performance degradation from GPU synchronization. The code is well-structured and the changes are sound. I have one minor suggestion to remove a redundant operation.
|
/tag-and-rerun-ci |
|
/rerun-failed-ci |
2 similar comments
|
/rerun-failed-ci |
|
/rerun-failed-ci |
1bc30e2 to
7292246
Compare
|
/rerun-failed-ci |
Motivation
Currently ViT CUDA Graph table key uses seq_len. There's a case that two sequences with identical seq_len, but
cu_seqlensare different.cu_seqlensis the segmentation contract for packed sequences: if the contract changes, the graph cannot be reused.For example we have 2 sequences, each seq has 3 images, the token num of the images are:
seq1: [4, 6, 8]
seq2: [5, 5, 8]
In this case the sequence length are the same, but the cu_seqlens are different.
cu_seqlens1: [0, 4, 10, 18]
cu_seqlens2:[0, 5, 10, 18]
We should use two separate graphs for each sequence.
This PR is to introduce a new
graph_key_hint, which is a model-specific hash value related with cu_seqlens and cu_window_seqlens (example for Qwen2.5-VL).After thinking twice, I reckon it is a general issue for CUDA Graph scenarios such as spec decode, normal decode as well as Piecewise CUDA Graph. The CUDA Graph key uses batch-size, which adopts varlen mechanism in attention backend. They probably encounter the same issue. In this case, the graph buffer has change be ruined, but the case is very rare. So it is extremely difficult to reproduce.
Manually test passed.
Baseline:
PR:
Modifications
Accuracy Tests
Benchmarking and Profiling
Checklist
Review Process
/tag-run-ci-label,/rerun-failed-ci,/tag-and-rerun-ci