[Profiler] Unify platform-aware torch profiling (#57460)

This commit is contained in:
czhu-cohere
2026-09-20 19:17:23 -07:00
committed by GitHub
parent 6dfd87f596
commit 3918f3c5a3
9 changed files with 408 additions and 110 deletions
+25 -1
View File
@@ -14,6 +14,7 @@ from vllm.utils.hashing import safe_hash
logger = init_logger(__name__)
ProfilerKind = Literal["torch", "cuda", "proton"]
TorchProfilerActivity = Literal["CPU", "CUDA", "PrivateUse1", "XPU"]
ProtonBackend = Literal["cupti"]
ProtonContext = Literal["shadow", "python"]
ProtonData = Literal["tree", "trace"]
@@ -51,6 +52,12 @@ class ProfilerConfig:
worker's traces (CPU & GPU) will be saved under this directory. Note that
it must be an absolute path."""
torch_profiler_activities: list[TorchProfilerActivity] | None = Field(
default=None, min_length=1
)
"""Activities recorded by workers using the torch profiler. When unset,
each worker uses its platform default: CPU; CPU and CUDA; or CPU and XPU."""
proton_profiler_dir: str = ""
"""Directory to save Triton Proton profiles. Each worker writes a
separate rank-qualified file."""
@@ -170,7 +177,24 @@ class ProfilerConfig:
@model_validator(mode="after")
def _validate_profiler_config(self) -> Self:
has_delay_or_limit = self.delay_iterations > 0 or self.max_iterations > 0
if self.profiler == "torch" and has_delay_or_limit and not self.ignore_frontend:
activities = self.torch_profiler_activities
if activities is not None:
if self.profiler != "torch":
raise ValueError(
"torch_profiler_activities is only applicable when profiler "
"is set to 'torch'"
)
if len(activities) != len(set(activities)):
raise ValueError(
"torch_profiler_activities must not contain duplicates"
)
records_cpu_activity = activities is None or "CPU" in activities
if (
self.profiler == "torch"
and has_delay_or_limit
and not self.ignore_frontend
and records_cpu_activity
):
logger.warning_once(
"Using 'torch' profiler with delay_iterations or max_iterations "
"while ignore_frontend is False may result in high overhead."
+106 -11
View File
@@ -6,10 +6,9 @@ import inspect
import json
import os
from abc import ABC, abstractmethod
from collections.abc import Callable, Iterator
from collections.abc import Callable, Iterator, Sequence
from contextlib import AbstractContextManager, contextmanager, nullcontext, suppress
from glob import glob
from typing import Literal
from uuid import uuid4
import torch
@@ -18,7 +17,7 @@ from typing_extensions import override
import vllm.version
from vllm.config import ProfilerConfig
from vllm.config.profiler import _is_uri_path
from vllm.config.profiler import ProfilerKind, TorchProfilerActivity, _is_uri_path
from vllm.logger import init_logger
from vllm.platforms import current_platform
@@ -57,6 +56,11 @@ class WorkerProfiler(ABC):
"""Whether the underlying profiler is currently collecting data."""
return self._running
@property
def should_annotate(self) -> bool:
"""Whether worker iterations should receive profiler annotations."""
return self._running
@abstractmethod
def _start(self) -> None:
"""Start the profiler."""
@@ -176,7 +180,6 @@ class WorkerProfiler(ABC):
return nullcontext()
TorchProfilerActivity = Literal["CPU", "CUDA", "PrivateUse1", "XPU"]
TorchProfilerActivityMap = {
"CPU": torch.profiler.ProfilerActivity.CPU,
"CUDA": torch.profiler.ProfilerActivity.CUDA,
@@ -191,7 +194,7 @@ class TorchProfilerWrapper(WorkerProfiler):
profiler_config: ProfilerConfig,
worker_name: str,
local_rank: int,
activities: list[TorchProfilerActivity],
activities: Sequence[TorchProfilerActivity],
on_trace_ready: Callable[[torch.profiler.profile], None] | None = None,
) -> None:
super().__init__(profiler_config)
@@ -224,7 +227,12 @@ class TorchProfilerWrapper(WorkerProfiler):
use_gzip=profiler_config.torch_profiler_use_gzip,
)
self.dump_cpu_time_total = "CPU" in activities and len(activities) == 1
self._records_cpu_activity = "CPU" in activities
self.dump_device_time_total = (
any(activity != "CPU" for activity in activities)
and profiler_config.torch_profiler_dump_cuda_time_total
)
self.dump_cpu_time_total = self._records_cpu_activity and len(activities) == 1
# Create profiler schedule if warmup or wait iterations are configured
profiler_schedule = None
@@ -244,7 +252,7 @@ class TorchProfilerWrapper(WorkerProfiler):
profiler_config.active_iterations,
)
self.profiler = torch.profiler.profile(
self._profiler_kwargs = dict(
activities=[TorchProfilerActivityMap[activity] for activity in activities],
schedule=profiler_schedule,
record_shapes=profiler_config.torch_profiler_record_shapes,
@@ -253,6 +261,7 @@ class TorchProfilerWrapper(WorkerProfiler):
with_flops=profiler_config.torch_profiler_with_flops,
on_trace_ready=trace_handler,
)
self.profiler: torch.profiler.profile
# Track if we're using a schedule (need to call step())
self._uses_schedule = profiler_schedule is not None
@@ -260,10 +269,11 @@ class TorchProfilerWrapper(WorkerProfiler):
# Subtract 1 because profiler.start() already consumes step 0
# (WAIT or WARMUP), so only wait + warmup - 1 non-active steps
# remain to be advanced through via profiler.step() calls.
self._warmup_steps_remaining = max(
self._initial_warmup_steps_remaining = max(
profiler_config.wait_iterations + profiler_config.warmup_iterations - 1,
0,
)
self._warmup_steps_remaining = self._initial_warmup_steps_remaining
self._version_metadata_added = False
def _build_profiler_table(
@@ -319,6 +329,9 @@ class TorchProfilerWrapper(WorkerProfiler):
@override
def _start(self) -> None:
self.profiler = torch.profiler.profile(**self._profiler_kwargs)
self._warmup_steps_remaining = self._initial_warmup_steps_remaining
self._version_metadata_added = False
self.profiler.start()
# No-schedule case: Kineto is live immediately. With a schedule this
# no-ops and _profiler_step stamps it once WAIT ends.
@@ -328,10 +341,9 @@ class TorchProfilerWrapper(WorkerProfiler):
def _stop(self) -> None:
self.profiler.stop()
profiler_config = self.profiler_config
rank = self.local_rank
if profiler_config.torch_profiler_dump_cuda_time_total:
table = self._build_profiler_table(sort_key="self_cuda_time_total")
if self.dump_device_time_total:
table = self._build_profiler_table(sort_key="self_device_time_total")
self._write_profiler_table(rank, table)
# only print profiler results on rank 0
@@ -367,8 +379,15 @@ class TorchProfilerWrapper(WorkerProfiler):
return False
return True
@property
@override
def should_annotate(self) -> bool:
return self._running and self._records_cpu_activity
@override
def annotate_context_manager(self, name: str):
if not self.should_annotate:
return nullcontext()
return torch.profiler.record_function(name)
@@ -606,6 +625,82 @@ class CudaProfilerWrapper(WorkerProfiler):
return torch.cuda.nvtx.range(name)
_DEFAULT_TORCH_PROFILER_ACTIVITIES: dict[str, tuple[TorchProfilerActivity, ...]] = {
"cpu": ("CPU",),
"cuda": ("CPU", "CUDA"),
"xpu": ("CPU", "XPU"),
}
_SUPPORTED_TORCH_PROFILER_ACTIVITIES = {
device: frozenset(activities)
for device, activities in _DEFAULT_TORCH_PROFILER_ACTIVITIES.items()
}
_SUPPORTED_PROFILER_KINDS: dict[str, frozenset[ProfilerKind]] = {
"cpu": frozenset(("torch",)),
"cuda": frozenset(("torch", "cuda", "proton")),
"xpu": frozenset(("torch",)),
}
def validate_worker_profiler_config(profiler_config: ProfilerConfig) -> None:
"""Validate profiler selections against the worker's capabilities."""
profiler_type = profiler_config.profiler
if profiler_type is None:
return
device_type = current_platform.device_type
if device_type not in _SUPPORTED_PROFILER_KINDS:
raise ValueError(f"Unsupported profiler device type: {device_type}")
supported_kinds = _SUPPORTED_PROFILER_KINDS[device_type]
if profiler_type not in supported_kinds:
supported_names = ", ".join(sorted(supported_kinds))
raise ValueError(
f"Unsupported profiler type for {device_type}: "
f"{profiler_type}. Supported profiler types: {supported_names}."
)
if profiler_type == "torch":
default_activities = _DEFAULT_TORCH_PROFILER_ACTIVITIES[device_type]
supported_activities = _SUPPORTED_TORCH_PROFILER_ACTIVITIES[device_type]
configured = profiler_config.torch_profiler_activities
activities = default_activities if configured is None else configured
unsupported = set(activities) - supported_activities
if unsupported:
unsupported_names = ", ".join(sorted(unsupported))
supported_names = ", ".join(sorted(supported_activities))
raise ValueError(
f"Unsupported torch profiler activities for "
f"{device_type}: {unsupported_names}. "
f"Supported activities: {supported_names}."
)
def create_worker_profiler(
profiler_config: ProfilerConfig,
*,
worker_name: str,
local_rank: int,
) -> WorkerProfiler:
"""Create a profiler using a validated config and platform defaults."""
profiler_type = profiler_config.profiler
if profiler_type == "torch":
default_activities = _DEFAULT_TORCH_PROFILER_ACTIVITIES[
current_platform.device_type
]
configured = profiler_config.torch_profiler_activities
logger.debug("Starting torch profiler with trace name: %s", worker_name)
return TorchProfilerWrapper(
profiler_config,
worker_name=worker_name,
local_rank=local_rank,
activities=default_activities if configured is None else tuple(configured),
)
if profiler_type == "cuda":
logger.debug("Starting CUDA profiler")
return CudaProfilerWrapper(profiler_config)
assert profiler_type == "proton", f"Unknown profiler type: {profiler_type}"
logger.debug("Starting Proton profiler with trace name: %s", worker_name)
return ProtonProfilerWrapper(profiler_config, worker_name=worker_name)
def create_graph_capture_profiler(
profiler_config: ProfilerConfig, global_rank: int
) -> WorkerProfiler | None:
+2
View File
@@ -208,9 +208,11 @@ class AsyncLLM(EngineClient):
pass
self.profiler = profiler
configured_activities = vllm_config.profiler_config.torch_profiler_activities
if (
vllm_config.profiler_config.profiler == "torch"
and not vllm_config.profiler_config.ignore_frontend
and (configured_activities is None or "CPU" in configured_activities)
):
profiler_dir = vllm_config.profiler_config.torch_profiler_dir
logger.info(
-22
View File
@@ -7,7 +7,6 @@ import vllm.v1.worker.cpu.shm # noqa # isort: skip
import math
import os
import sys
from typing import Any
import psutil
import torch
@@ -16,7 +15,6 @@ from vllm import envs
from vllm.config import CompilationMode, VllmConfig
from vllm.logger import init_logger
from vllm.platforms import CpuArchEnum, current_platform
from vllm.profiler.wrapper import TorchProfilerWrapper
from vllm.utils.cpu_resource_utils import (
get_allowed_cpu_list,
get_memory_node_info,
@@ -106,18 +104,6 @@ class CPUWorker(Worker):
self.parallel_config.disable_custom_all_reduce = True
# Torch profiler. Enabled and configured through profiler_config.
self.profiler: Any | None = None
profiler_config = vllm_config.profiler_config
if profiler_config.profiler == "torch":
worker_name = f"{vllm_config.instance_id}-rank-{self.rank}"
self.profiler = TorchProfilerWrapper(
profiler_config,
worker_name=worker_name,
local_rank=self.local_rank,
activities=["CPU"],
)
def init_device(self):
self.device = torch.device("cpu")
@@ -287,11 +273,3 @@ class CPUWorker(Worker):
language_model=self.compilation_config.compilation_time,
encoder=self.compilation_config.encoder_compilation_time,
)
def profile(self, is_start: bool = True, profile_prefix: str | None = None):
if self.profiler is None:
raise RuntimeError("Profiler is not enabled.")
if is_start:
self.profiler.start()
else:
self.profiler.stop()
+10 -31
View File
@@ -61,10 +61,9 @@ from vllm.model_executor.warmup.kernel_warmup import kernel_warmup
from vllm.multimodal.gpu_ipc_memory import reserve_mm_ipc_gpu_memory
from vllm.platforms import current_platform
from vllm.profiler.wrapper import (
CudaProfilerWrapper,
ProtonProfilerWrapper,
TorchProfilerWrapper,
create_graph_capture_profiler,
create_worker_profiler,
validate_worker_profiler_config,
)
from vllm.sequence import IntermediateTensors
from vllm.tasks import SupportedTask
@@ -222,6 +221,7 @@ class Worker(WorkerBase):
# so we have all the information needed for proper trace naming.
self.profiler: Any | None = None
self.profiler_config = vllm_config.profiler_config
validate_worker_profiler_config(self.profiler_config)
self.use_v2_model_runner = vllm_config.use_v2_model_runner
@@ -1038,7 +1038,7 @@ class Worker(WorkerBase):
return nullcontext()
self.profiler.step()
if not self.profiler.is_running:
if not self.profiler.should_annotate:
return nullcontext()
iteration_details = compute_iteration_details(scheduler_output)
@@ -1262,7 +1262,6 @@ class Worker(WorkerBase):
)
if is_start:
profiler_type = self.profiler_config.profiler
# Generate the trace name by combining prefix with comprehensive rank suffix
from vllm.distributed.utils import get_worker_rank_suffix
@@ -1274,36 +1273,16 @@ class Worker(WorkerBase):
else:
trace_name = rank_suffix
if profiler_type == "proton" and self.profiler is not None:
if self.profiler_config.profiler == "proton" and self.profiler is not None:
self.profiler.set_output_name(trace_name)
# Create the profiler wrapper only on the first start call
if self.profiler is None:
if profiler_type == "torch":
self.profiler = TorchProfilerWrapper(
self.profiler_config,
worker_name=trace_name,
local_rank=self.local_rank,
activities=["CPU", "CUDA"],
)
logger.debug(
"Starting torch profiler with trace name: %s", trace_name
)
elif profiler_type == "cuda":
self.profiler = CudaProfilerWrapper(self.profiler_config)
logger.debug("Starting CUDA profiler")
elif profiler_type == "proton":
self.profiler = ProtonProfilerWrapper(
self.profiler_config, worker_name=trace_name
)
logger.debug(
"Starting Proton profiler with trace name: %s", trace_name
)
else:
# Config validation should prevent this code being reached
raise ValueError(
f"Invalid profiler value of {self.profiler_config.profiler}"
)
self.profiler = create_worker_profiler(
self.profiler_config,
worker_name=trace_name,
local_rank=self.local_rank,
)
self.profiler.start()
else:
-28
View File
@@ -8,7 +8,6 @@ import torch
from vllm.config import VllmConfig
from vllm.logger import init_logger
from vllm.platforms import current_platform
from vllm.profiler.wrapper import TorchProfilerWrapper
from vllm.utils.mem_utils import MemorySnapshot, format_gib
from vllm.utils.torch_utils import set_random_seed
from vllm.v1.utils import report_usage_stats
@@ -225,33 +224,6 @@ class XPUWorker(Worker):
# If usage stat is enabled, collect relevant info.
report_usage_stats(self.vllm_config)
def profile(self, is_start: bool = True, profile_prefix: str | None = None):
if self.profiler_config is None or self.profiler_config.profiler is None:
raise RuntimeError(
"Profiling is not enabled. Please set --profiler-config to enable "
"profiling. Example: "
"'--profiler-config.profiler=torch --profiler-config.torch_profiler_dir"
"=YOUR_DIR_PATH_TO_DUMP_TRACE'"
)
if is_start and self.profiler is None:
from vllm.distributed.utils import get_worker_rank_suffix
rank_suffix = get_worker_rank_suffix(global_rank=self.rank)
trace_name = (
f"{profile_prefix}_{rank_suffix}" if profile_prefix else rank_suffix
)
self.profiler = TorchProfilerWrapper(
self.profiler_config,
worker_name=trace_name,
local_rank=self.local_rank,
activities=["CPU", "XPU"],
)
logger.debug("Starting torch profiler with trace name: %s", trace_name)
super().profile(is_start=is_start, profile_prefix=profile_prefix)
def shutdown(self) -> None:
logger.info(
"XPUWorker shutdown: cleaning up (rank=%d, local_rank=%d)",