-
-
Notifications
You must be signed in to change notification settings - Fork 15k
[cudagraphs] Refactor cudagraph capture loop #32946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
LucasWilkinson
merged 2 commits into
vllm-project:main
from
neuralmagic:lwilkinson/cg-refactor
Jan 23, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,6 @@ | |
| from contextlib import contextmanager | ||
| from copy import copy, deepcopy | ||
| from functools import reduce | ||
| from itertools import product | ||
| from typing import TYPE_CHECKING, Any, NamedTuple, TypeAlias, cast | ||
|
|
||
| import numpy as np | ||
|
|
@@ -4809,50 +4808,14 @@ def freeze_gc(): | |
| set_cudagraph_capturing_enabled(True) | ||
| with freeze_gc(), graph_capture(device=self.device): | ||
| start_free_gpu_memory = torch.cuda.mem_get_info()[0] | ||
| cudagraph_mode = self.compilation_config.cudagraph_mode | ||
| assert cudagraph_mode is not None | ||
|
|
||
| if self.lora_config: | ||
| if self.compilation_config.cudagraph_specialize_lora: | ||
| lora_cases = [True, False] | ||
| else: | ||
| lora_cases = [True] | ||
| else: | ||
| lora_cases = [False] | ||
|
|
||
| if cudagraph_mode.mixed_mode() != CUDAGraphMode.NONE: | ||
| cudagraph_runtime_mode = cudagraph_mode.mixed_mode() | ||
| # make sure we capture the largest batch size first | ||
| compilation_cases = list( | ||
| product(reversed(self.cudagraph_batch_sizes), lora_cases) | ||
| ) | ||
| self._capture_cudagraphs( | ||
| compilation_cases, | ||
| cudagraph_runtime_mode=cudagraph_runtime_mode, | ||
| uniform_decode=False, | ||
| ) | ||
|
|
||
| # Capture full cudagraph for uniform decode batches if we | ||
| # don't already have full mixed prefill-decode cudagraphs. | ||
| if ( | ||
| cudagraph_mode.decode_mode() == CUDAGraphMode.FULL | ||
| and cudagraph_mode.separate_routine() | ||
| ): | ||
| max_num_tokens = ( | ||
| self.scheduler_config.max_num_seqs * self.uniform_decode_query_len | ||
| ) | ||
| decode_cudagraph_batch_sizes = [ | ||
| x | ||
| for x in self.cudagraph_batch_sizes | ||
| if max_num_tokens >= x >= self.uniform_decode_query_len | ||
| ] | ||
| compilation_cases_decode = list( | ||
| product(reversed(decode_cudagraph_batch_sizes), lora_cases) | ||
| ) | ||
| for ( | ||
| runtime_mode, | ||
| batch_descs, | ||
| ) in self.cudagraph_dispatcher.get_capture_descs(): | ||
| self._capture_cudagraphs( | ||
| compilation_cases=compilation_cases_decode, | ||
| cudagraph_runtime_mode=CUDAGraphMode.FULL, | ||
| uniform_decode=True, | ||
| batch_descriptors=batch_descs, | ||
| cudagraph_runtime_mode=runtime_mode, | ||
| ) | ||
|
|
||
| torch.cuda.synchronize() | ||
|
|
@@ -4883,19 +4846,32 @@ def freeze_gc(): | |
|
|
||
| def _capture_cudagraphs( | ||
| self, | ||
| compilation_cases: list[tuple[int, bool]], | ||
| batch_descriptors: list[BatchDescriptor], | ||
| cudagraph_runtime_mode: CUDAGraphMode, | ||
| uniform_decode: bool, | ||
| ): | ||
| assert ( | ||
| cudagraph_runtime_mode != CUDAGraphMode.NONE | ||
| and cudagraph_runtime_mode.valid_runtime_modes() | ||
| ), f"Invalid cudagraph runtime mode: {cudagraph_runtime_mode}" | ||
|
|
||
| if not batch_descriptors: | ||
| return | ||
|
|
||
| uniform_decode = batch_descriptors[0].uniform | ||
| force_attention = cudagraph_runtime_mode == CUDAGraphMode.FULL | ||
|
|
||
| dummy_run = functools.partial( | ||
| self._dummy_run, | ||
| uniform_decode=uniform_decode, | ||
| skip_eplb=True, | ||
| remove_lora=False, | ||
| force_attention=force_attention, | ||
| ) | ||
|
|
||
LucasWilkinson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Only rank 0 should print progress bar during capture | ||
| if is_global_first_rank(): | ||
| compilation_cases = tqdm( | ||
| compilation_cases, | ||
| batch_descriptors = tqdm( | ||
| batch_descriptors, | ||
| disable=not self.load_config.use_tqdm_on_load, | ||
| desc="Capturing CUDA graphs ({}, {})".format( | ||
| "decode" if uniform_decode else "mixed prefill-decode", | ||
|
|
@@ -4904,7 +4880,10 @@ def _capture_cudagraphs( | |
| ) | ||
|
|
||
| # We skip EPLB here since we don't want to record dummy metrics | ||
| for num_tokens, activate_lora in compilation_cases: | ||
| for batch_desc in batch_descriptors: | ||
| num_tokens = batch_desc.num_tokens | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like we're moving closer and closer to passing
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. next 😄 |
||
| activate_lora = batch_desc.has_lora | ||
|
|
||
| # We currently only capture ubatched graphs when its a FULL | ||
| # cudagraph, a uniform decode batch, and the number of tokens | ||
| # is above the threshold. Otherwise we just capture a non-ubatched | ||
|
|
@@ -4922,28 +4901,22 @@ def _capture_cudagraphs( | |
|
|
||
| for _ in range(self.compilation_config.cudagraph_num_of_warmups): | ||
| # Use CUDAGraphRuntimeStyle.NONE (default) for warmup. | ||
| # But be careful, warm up with `NONE`is orthogonal to | ||
| # But be careful, warm up with `NONE` is orthogonal to | ||
| # if we want to warm up attention or not. This is | ||
| # different from the case where `FULL` implies capture | ||
| # attention while `PIECEWISE` implies no attention. | ||
| force_attention = cudagraph_runtime_mode == CUDAGraphMode.FULL | ||
| self._dummy_run( | ||
| dummy_run( | ||
| num_tokens, | ||
| cudagraph_runtime_mode=CUDAGraphMode.NONE, | ||
| force_attention=force_attention, | ||
| uniform_decode=uniform_decode, | ||
| allow_microbatching=allow_microbatching, | ||
| skip_eplb=True, | ||
| remove_lora=False, | ||
| activate_lora=activate_lora, | ||
| ) | ||
| self._dummy_run( | ||
|
|
||
| # Capture run | ||
| dummy_run( | ||
| num_tokens, | ||
| cudagraph_runtime_mode=cudagraph_runtime_mode, | ||
| uniform_decode=uniform_decode, | ||
| allow_microbatching=allow_microbatching, | ||
| skip_eplb=True, | ||
| remove_lora=False, | ||
| activate_lora=activate_lora, | ||
| is_graph_capturing=True, | ||
| ) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.