Repository navigation
[Profiler][GPU] Extend CUDA graph capture profiling to the V2 model runner #54061
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
Open
devalshahamd
wants to merge
29
commits into
vllm-project:main
Choose a base branch
from
devalshahamd:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+455
−67
Open
Changes from 5 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
7144935
Add cuda graph capture profiling support for v2 runner
devalshahamd ca2cf3a
Merge branch 'main' into main
devalshahamd c8f8829
Merge remote-tracking branch 'upstream/main'
devalshahamd 5268c4f
Merge branch 'main' into main
tjtanaa ccf4db5
Merge branch 'main' into main
devalshahamd 03b525d
Merge branch 'main' of https://github.com/vllm-project/vllm into main
devalshahamd 809c115
Use TorchProfilerWrapper to create capture profiler
devalshahamd 2e1a22d
Moved profiler to the base capture function
devalshahamd 8fb58bc
Merge branch 'main' into main
devalshahamd de8403b
Merge branch 'main' into main
devalshahamd f4a6cbb
Sort stdlib imports in profiler wrapper.
devalshahamd f0d20a1
Merge branch 'main' into main
devalshahamd 6bcee19
Merge branch 'main' into main
devalshahamd e47d513
Restore single-line use_aux_hidden_state_outputs in V2 capture.
devalshahamd ac837b6
Merge branch 'main' into main
devalshahamd 6da05d3
Merge branch 'main' into main
devalshahamd d9c4f84
Document vllm_config in graph_capture_profiler docstring.
devalshahamd cfec17a
Merge branch 'main' into main
devalshahamd 1393529
Merge branch 'main' into main
devalshahamd 0f23134
Merge branch 'main' into main
devalshahamd 7e2ebd9
Merge branch 'main' into main
devalshahamd e66c4ce
Merge branch 'main' of https://github.com/devalshahamd/vllm
devalshahamd 063d504
Merge branch 'main' into main
devalshahamd a063047
[Bugfix][Profiler] Build an unstarted profiler for graph capture
devalshahamd d1eb3d4
Merge branch 'main' into main
devalshahamd 83312ea
[Profiler] Skip capture binding when graph-capture profiling is disabled
devalshahamd 9cb7aae
[Profiler] Reuse serving's activity selection for graph-capture profi…
devalshahamd 11bd32f
Merge branch 'main' into main
mergify[bot] 632a3d2
Merge branch 'main' into main
mergify[bot] 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
| """Torch profiler helpers for CUDA/HIP graph capture tracing. | ||
|
|
||
| Graph capture is driven by three subsystems (encoder, decoder and speculator) | ||
| that reach the same handful of capture loops by different routes. | ||
| ``graph_capture_profiler`` binds one profiler around a subsystem's capture, | ||
| and ``graph_capture_step`` picks that binding up inside whichever capture loop | ||
| ends up running. Capture loops therefore carry no profiler plumbing, and a new | ||
| speculator built on an existing capture loop is traced without further changes. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import contextvars | ||
| from collections.abc import Iterator | ||
| from contextlib import AbstractContextManager, contextmanager, nullcontext | ||
| from dataclasses import dataclass | ||
| from typing import Any | ||
|
|
||
| import torch | ||
|
|
||
| from vllm.config import VllmConfig | ||
| from vllm.distributed.parallel_state import get_world_group | ||
| from vllm.logger import init_logger | ||
|
|
||
| logger = init_logger(__name__) | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class _CaptureBinding: | ||
| profiler: AbstractContextManager[Any] | ||
| label_prefix: str | None | ||
|
|
||
|
|
||
| _active_binding: contextvars.ContextVar[_CaptureBinding | None] = ( | ||
| contextvars.ContextVar("vllm_graph_capture_binding", default=None) | ||
| ) | ||
|
|
||
|
|
||
| def _make_profiler( | ||
| vllm_config: VllmConfig, | ||
| subsystem: str | None, | ||
| ) -> AbstractContextManager[Any]: | ||
| profiler_config = vllm_config.profiler_config | ||
| local_rank = get_world_group().local_rank | ||
| if local_rank != 0 or not profiler_config.capture_torch_profiler: | ||
| logger.info_once( | ||
| "Rank %d: Torch profiler disabled for CUDA graph capture", local_rank | ||
| ) | ||
| return nullcontext() | ||
|
|
||
| trace_dir = profiler_config.torch_profiler_dir + "/capture_traces" | ||
| worker_name = f"graph_capture_rank_{local_rank}" | ||
| if subsystem: | ||
| worker_name += f"_{subsystem}" | ||
| logger.info_once( | ||
| "Rank %d: Torch profiler enabled for %s CUDA graph capture, " | ||
| "traces will be saved to: %s", | ||
| local_rank, | ||
| subsystem or "decoder", | ||
| trace_dir, | ||
| ) | ||
| return torch.profiler.profile( | ||
| activities=[ | ||
| torch.profiler.ProfilerActivity.CPU, | ||
| torch.profiler.ProfilerActivity.CUDA, | ||
| ], | ||
| record_shapes=True, | ||
| profile_memory=True, | ||
| with_stack=True, | ||
| on_trace_ready=torch.profiler.tensorboard_trace_handler( | ||
| trace_dir, | ||
| worker_name=worker_name, | ||
| use_gzip=True, | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| @contextmanager | ||
| def graph_capture_profiler( | ||
| vllm_config: VllmConfig, | ||
| subsystem: str | None = None, | ||
| label_prefix: str | None = None, | ||
| ) -> Iterator[None]: | ||
| """Bind a graph-capture profiler for one subsystem's capture. | ||
|
|
||
| Args: | ||
| subsystem: Suffix for the trace file name. ``"encoder"`` writes | ||
| ``graph_capture_rank_0_encoder.<timestamp>.pt.trace.json.gz``; | ||
| the main decoder passes ``None`` and keeps the unsuffixed name. | ||
| label_prefix: Inserted into every annotation recorded inside the | ||
| block, so that e.g. speculator graphs are labelled | ||
| ``capture_32_draft_FULL`` rather than reusing the decoder's | ||
| ``capture_32_FULL``. | ||
| """ | ||
| binding = _CaptureBinding( | ||
| profiler=_make_profiler(vllm_config, subsystem), | ||
| label_prefix=label_prefix, | ||
| ) | ||
| token = _active_binding.set(binding) | ||
| try: | ||
| yield | ||
| finally: | ||
| _active_binding.reset(token) | ||
|
|
||
|
|
||
| @contextmanager | ||
| def graph_capture_step(num_tokens: int, mode: str) -> Iterator[None]: | ||
| """Profile the capture of a single graph shape. | ||
|
|
||
| Records a ``capture_{num_tokens}_{mode}`` annotation, or | ||
| ``capture_{num_tokens}_{label_prefix}_{mode}`` when the active binding | ||
| sets a prefix. Does nothing outside a ``graph_capture_profiler`` block, so | ||
| capture loops can call this unconditionally. | ||
| """ | ||
| binding = _active_binding.get() | ||
| if binding is None: | ||
| yield | ||
| return | ||
|
|
||
| if binding.label_prefix: | ||
| label = f"capture_{num_tokens}_{binding.label_prefix}_{mode}" | ||
| else: | ||
| label = f"capture_{num_tokens}_{mode}" | ||
| with binding.profiler, torch.profiler.record_function(label): | ||
| yield |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can the changes in this file be moved inside the respective capture methods?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @njhill Thanks for the suggestion!
I moved the binding into the capture methods themselves (CudaGraphManager.capture, the encoder/speculator managers, and V1 _capture_cudagraphs), so capture_model() no longer has to wrap anything. Nested speculator/encoder capture inherits the outer binding, and the throwaway memory-estimate run skips tracing.