Skip to content
Open
Show file tree
Hide file tree
Changes from 13 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 Aug 27, 2026
ca2cf3a
Merge branch 'main' into main
devalshahamd Aug 31, 2026
c8f8829
Merge remote-tracking branch 'upstream/main'
devalshahamd Sep 3, 2026
5268c4f
Merge branch 'main' into main
tjtanaa Sep 7, 2026
ccf4db5
Merge branch 'main' into main
devalshahamd Sep 11, 2026
03b525d
Merge branch 'main' of https://github.com/vllm-project/vllm into main
devalshahamd Sep 14, 2026
809c115
Use TorchProfilerWrapper to create capture profiler
devalshahamd Sep 14, 2026
2e1a22d
Moved profiler to the base capture function
devalshahamd Sep 15, 2026
8fb58bc
Merge branch 'main' into main
devalshahamd Sep 15, 2026
de8403b
Merge branch 'main' into main
devalshahamd Sep 15, 2026
f4a6cbb
Sort stdlib imports in profiler wrapper.
devalshahamd Sep 15, 2026
f0d20a1
Merge branch 'main' into main
devalshahamd Sep 15, 2026
6bcee19
Merge branch 'main' into main
devalshahamd Sep 15, 2026
e47d513
Restore single-line use_aux_hidden_state_outputs in V2 capture.
devalshahamd Sep 15, 2026
ac837b6
Merge branch 'main' into main
devalshahamd Sep 16, 2026
6da05d3
Merge branch 'main' into main
devalshahamd Sep 17, 2026
d9c4f84
Document vllm_config in graph_capture_profiler docstring.
devalshahamd Sep 17, 2026
cfec17a
Merge branch 'main' into main
devalshahamd Sep 21, 2026
1393529
Merge branch 'main' into main
devalshahamd Sep 24, 2026
0f23134
Merge branch 'main' into main
devalshahamd Oct 1, 2026
7e2ebd9
Merge branch 'main' into main
devalshahamd Oct 20, 2026
e66c4ce
Merge branch 'main' of https://github.com/devalshahamd/vllm
devalshahamd Oct 20, 2026
063d504
Merge branch 'main' into main
devalshahamd Oct 28, 2026
a063047
[Bugfix][Profiler] Build an unstarted profiler for graph capture
devalshahamd Oct 29, 2026
d1eb3d4
Merge branch 'main' into main
devalshahamd Oct 29, 2026
83312ea
[Profiler] Skip capture binding when graph-capture profiling is disabled
devalshahamd Oct 29, 2026
9cb7aae
[Profiler] Reuse serving's activity selection for graph-capture profi…
devalshahamd Oct 29, 2026
11bd32f
Merge branch 'main' into main
mergify[bot] Oct 30, 2026
632a3d2
Merge branch 'main' into main
mergify[bot] Nov 2, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
216 changes: 204 additions & 12 deletions tests/v1/worker/test_gpu_profiler.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import os
from contextlib import nullcontext
from contextlib import contextmanager, nullcontext
from types import SimpleNamespace
from unittest.mock import MagicMock, Mock, call, patch
from uuid import UUID

import pytest
import torch
from pydantic import ValidationError

from vllm.config import (
Expand All @@ -17,7 +18,18 @@
)
from vllm.config.profiler import _is_uri_path
from vllm.platforms import current_platform
from vllm.profiler.wrapper import ProtonProfilerWrapper, WorkerProfiler
from vllm.profiler import graph_capture
from vllm.profiler.graph_capture import (
graph_capture_profiler,
graph_capture_step,
skip_graph_capture_tracing,
)
from vllm.profiler.wrapper import (
ProtonProfilerWrapper,
WorkerProfiler,
default_torch_profiler_activities,
graph_capture_profiler_config,
)
from vllm.v1.core.sched.output import CachedRequestData
from vllm.v1.worker.gpu_model_runner import GPUModelRunner
from vllm.v1.worker.gpu_worker import Worker
Expand Down Expand Up @@ -332,21 +344,201 @@ def test_skips_annotation_work_after_profiler_stops(self):


def test_profiler_entered_during_capture():
"""Profiler is used as a context manager in _warmup_and_capture,
confirming it is active during the actual graph capture run."""
"""graph_capture_step enters the bound profiler during V1 capture."""
runner = MagicMock()
runner.compilation_config.cudagraph_num_of_warmups = 0
mock_profiler = MagicMock()

GPUModelRunner._warmup_and_capture(
runner,
desc=MagicMock(num_tokens=4, uniform=True),
cudagraph_runtime_mode=CUDAGraphMode.FULL,
profiler=mock_profiler,
with bound_graph_capture() as profiler:
GPUModelRunner._warmup_and_capture(
runner,
desc=MagicMock(num_tokens=4, uniform=True),
cudagraph_runtime_mode=CUDAGraphMode.FULL,
)

profiler.__enter__.assert_called_once()
profiler.__exit__.assert_called_once()


@contextmanager
def bound_graph_capture(label_prefix=None):
"""Enter a graph-capture binding backed by a mock profiler."""
profiler = MagicMock()
with (
patch.object(
graph_capture, "make_graph_capture_profiler", return_value=profiler
),
graph_capture_profiler(MagicMock(), label_prefix=label_prefix),
):
yield profiler


class TestGraphCaptureProfiler:
"""V2 binds one profiler per subsystem; capture loops only call
graph_capture_step and inherit whichever binding is active."""

@pytest.fixture
def annotation(self):
with patch.object(torch.profiler, "record_function") as record_function:
yield record_function

def test_step_is_noop_without_binding(self, annotation):
with graph_capture_step(32, "FULL"):
pass

annotation.assert_not_called()

@pytest.mark.parametrize(
"label_prefix,num_tokens,mode,expected",
[
(None, 512, "PIECEWISE", "capture_512_PIECEWISE"),
("draft", 32, "FULL", "capture_32_draft_FULL"),
("encoder", 1024, "default", "capture_1024_encoder_default"),
],
)
def test_annotation_label(
self, annotation, label_prefix, num_tokens, mode, expected
):
with (
bound_graph_capture(label_prefix) as profiler,
graph_capture_step(num_tokens, mode),
):
pass

annotation.assert_called_once_with(expected)
profiler.__enter__.assert_called_once()
profiler.__exit__.assert_called_once()

def test_profiler_cycles_once_per_shape(self, annotation):
"""Each shape re-enters the profiler, which is what splits capture
into one trace file per shape."""
with bound_graph_capture() as profiler:
for num_tokens in (32, 64, 128):
with graph_capture_step(num_tokens, "FULL"):
pass

assert profiler.__enter__.call_count == 3
assert profiler.__exit__.call_count == 3
assert annotation.call_count == 3

def test_binding_cleared_when_capture_raises(self):
with pytest.raises(RuntimeError), bound_graph_capture():
raise RuntimeError("capture failed")

assert graph_capture._active_binding.get() is None

@pytest.mark.parametrize(
"subsystem,expected",
[
(None, "graph_capture_rank_0"),
("encoder", "graph_capture_rank_0_encoder"),
("speculator", "graph_capture_rank_0_speculator"),
],
)
def test_trace_file_name_per_subsystem(self, subsystem, expected):
config = MagicMock(
profiler_config=ProfilerConfig(
profiler="torch",
torch_profiler_dir="/traces",
capture_torch_profiler=True,
),
device_config=SimpleNamespace(device_type="xpu"),
)
profiler = MagicMock()
wrapper = MagicMock(profiler=profiler)

with (
patch.object(
graph_capture, "get_world_group", return_value=MagicMock(local_rank=0)
),
patch.object(
graph_capture, "TorchProfilerWrapper", return_value=wrapper
) as wrapper_cls,
):
result = graph_capture.make_graph_capture_profiler(config, subsystem)

assert result is profiler
capture_config = wrapper_cls.call_args.args[0]
assert capture_config.torch_profiler_dir == "/traces/capture_traces"
assert wrapper_cls.call_args.kwargs["worker_name"] == expected
# Activities follow the runner device, not a hardcoded CUDA list.
assert wrapper_cls.call_args.kwargs["activities"] == ["CPU", "XPU"]
wrapper.start.assert_not_called()

def test_capture_config_overrides(self):
config = ProfilerConfig(
profiler="torch",
torch_profiler_dir="/traces",
delay_iterations=2,
max_iterations=3,
warmup_iterations=4,
wait_iterations=5,
torch_profiler_record_shapes=False,
torch_profiler_with_stack=False,
torch_profiler_with_flops=True,
torch_profiler_with_memory=True,
torch_profiler_use_gzip=False,
ignore_frontend=True,
)

capture_config = graph_capture_profiler_config(config)

assert capture_config.delay_iterations == 0
assert capture_config.max_iterations == 0
assert capture_config.warmup_iterations == 0
assert capture_config.wait_iterations == 0
assert capture_config.torch_profiler_record_shapes
assert capture_config.torch_profiler_with_stack
assert capture_config.torch_profiler_with_flops
assert capture_config.torch_profiler_with_memory
assert not capture_config.torch_profiler_use_gzip

@pytest.mark.parametrize(
"device_type,expected",
[
("cuda", ["CPU", "CUDA"]),
("xpu", ["CPU", "XPU"]),
("cpu", ["CPU"]),
("tpu", ["CPU"]),
],
)
def test_activities_follow_runner_device(self, device_type, expected):
assert default_torch_profiler_activities(device_type) == expected

@pytest.mark.parametrize("local_rank,enabled", [(0, False), (1, True)])
def test_no_profiler_when_disabled_or_off_rank(self, local_rank, enabled):
config = MagicMock()
config.profiler_config.capture_torch_profiler = enabled

with patch.object(
graph_capture,
"get_world_group",
return_value=MagicMock(local_rank=local_rank),
):
profiler = graph_capture.make_graph_capture_profiler(config, None)

assert isinstance(profiler, nullcontext)

def test_nested_bind_keeps_outer_prefix(self, annotation):
with (
bound_graph_capture("draft") as profiler,
graph_capture_profiler(MagicMock(), label_prefix="ignored"),
graph_capture_step(32, "FULL"),
):
pass

annotation.assert_called_once_with("capture_32_draft_FULL")
profiler.__enter__.assert_called_once()

def test_skip_tracing_is_noop_even_if_capture_binds(self, annotation):
with (
skip_graph_capture_tracing(),
graph_capture_profiler(MagicMock(), label_prefix="encoder"),
graph_capture_step(32, "FULL"),
):
pass

mock_profiler.__enter__.assert_called_once()
mock_profiler.__exit__.assert_called_once()
annotation.assert_not_called()
assert graph_capture._active_binding.get() is None


def make_proton(session_id: int | None = 7):
Expand Down
3 changes: 2 additions & 1 deletion vllm/config/profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ class ProfilerConfig:
Disabled by default."""

capture_torch_profiler: bool = False
"""If `True`, enables a torch profiler during CUDA graph capture on rank 0.
"""If `True`, enables a torch profiler during GPU graph capture on rank 0.
Activities include CPU and the runner device (CUDA/ROCm or XPU).
Traces are saved to a `capture_traces` subdirectory under `torch_profiler_dir`.
Requires `profiler` to be set to 'torch'."""

Expand Down
Loading
Loading