[Profiler] Add minimal Triton Proton profiling backend (#48789)
Signed-off-by: Luossu <zhluosuu@outlook.com>
This commit is contained in:
@@ -1,13 +1,18 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
import importlib
|
||||
import inspect
|
||||
import json
|
||||
import os
|
||||
from abc import ABC, abstractmethod
|
||||
from collections.abc import Callable
|
||||
from contextlib import nullcontext
|
||||
from typing import Literal
|
||||
from uuid import uuid4
|
||||
|
||||
import torch
|
||||
from packaging.version import InvalidVersion, Version
|
||||
from typing_extensions import override
|
||||
|
||||
import vllm.version
|
||||
@@ -17,6 +22,8 @@ from vllm.logger import init_logger
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
_TRITON_PROTON_3_7_VERSION = Version("3.7.0")
|
||||
|
||||
|
||||
class WorkerProfiler(ABC):
|
||||
def __init__(self, profiler_config: ProfilerConfig) -> None:
|
||||
@@ -43,6 +50,11 @@ class WorkerProfiler(ABC):
|
||||
self._profiling_for_iters = 0
|
||||
self._running = False
|
||||
|
||||
@property
|
||||
def is_running(self) -> bool:
|
||||
"""Whether the underlying profiler is currently collecting data."""
|
||||
return self._running
|
||||
|
||||
@abstractmethod
|
||||
def _start(self) -> None:
|
||||
"""Start the profiler."""
|
||||
@@ -340,6 +352,123 @@ class TorchProfilerWrapper(WorkerProfiler):
|
||||
return torch.profiler.record_function(name)
|
||||
|
||||
|
||||
class ProtonProfilerWrapper(WorkerProfiler):
|
||||
"""Worker profiler backed by :mod:`triton.profiler` (Proton)."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
profiler_config: ProfilerConfig,
|
||||
worker_name: str,
|
||||
) -> None:
|
||||
super().__init__(profiler_config)
|
||||
|
||||
try:
|
||||
self._proton = importlib.import_module("triton.profiler")
|
||||
triton = importlib.import_module("triton")
|
||||
except ImportError as exc:
|
||||
raise RuntimeError(
|
||||
"The Proton profiler requires a Triton installation with "
|
||||
"triton.profiler support."
|
||||
) from exc
|
||||
|
||||
self._output_dir = profiler_config.proton_profiler_dir
|
||||
self._output_path = os.path.join(self._output_dir, f"proton_{worker_name}")
|
||||
self._context = profiler_config.proton_context
|
||||
self._data = profiler_config.proton_data
|
||||
self._backend = profiler_config.proton_backend
|
||||
self._mode = profiler_config.proton_mode
|
||||
self._hook = profiler_config.proton_hook
|
||||
self._output_format = profiler_config.proton_output_format
|
||||
self._triton_version_string = getattr(triton, "__version__", "unknown")
|
||||
try:
|
||||
self._triton_version = Version(self._triton_version_string)
|
||||
except InvalidVersion:
|
||||
self._triton_version = None
|
||||
self._validate_capabilities()
|
||||
self._session_id: int | None = None
|
||||
# Qualify output names by process and wrapper instance so a new
|
||||
# worker cannot overwrite profiles left by an earlier server process.
|
||||
self._instance_id = f"pid{os.getpid()}_{uuid4().hex}"
|
||||
self._run_id = 0
|
||||
|
||||
logger.info_once(
|
||||
"Proton profiling enabled. Output will be saved under: %s",
|
||||
self._output_dir,
|
||||
)
|
||||
|
||||
def _require_triton_version(self, feature: str, minimum: Version) -> None:
|
||||
if self._triton_version is None or self._triton_version < minimum:
|
||||
raise RuntimeError(
|
||||
f"Proton {feature} requires Triton >= {minimum}; found "
|
||||
f"{self._triton_version_string}."
|
||||
)
|
||||
|
||||
def _validate_capabilities(self) -> None:
|
||||
if self._output_format is not None:
|
||||
parameters = inspect.signature(self._proton.finalize).parameters
|
||||
supports_output_format = "output_format" in parameters or any(
|
||||
parameter.kind == inspect.Parameter.VAR_KEYWORD
|
||||
for parameter in parameters.values()
|
||||
)
|
||||
if not supports_output_format:
|
||||
raise RuntimeError(
|
||||
"The installed Triton Proton does not support selecting "
|
||||
"an output format during finalize."
|
||||
)
|
||||
|
||||
if self._output_format == "hatchet_msgpack":
|
||||
self._require_triton_version(
|
||||
"hatchet_msgpack output", _TRITON_PROTON_3_7_VERSION
|
||||
)
|
||||
if self._mode and self._mode.split(":", 1)[0] == "periodic_flushing":
|
||||
self._require_triton_version(
|
||||
"periodic flushing", _TRITON_PROTON_3_7_VERSION
|
||||
)
|
||||
|
||||
def _create_session(self, output_path: str) -> int:
|
||||
os.makedirs(self._output_dir, exist_ok=True)
|
||||
session_id = self._proton.start(
|
||||
name=output_path,
|
||||
context=self._context,
|
||||
data=self._data,
|
||||
backend=self._backend,
|
||||
mode=self._mode,
|
||||
hook=self._hook,
|
||||
)
|
||||
if session_id is None:
|
||||
raise RuntimeError("Proton did not create a profiling session")
|
||||
return session_id
|
||||
|
||||
@override
|
||||
def _start(self) -> None:
|
||||
output_path = f"{self._output_path}_{self._instance_id}_run{self._run_id}"
|
||||
self._session_id = self._create_session(output_path)
|
||||
self._run_id += 1
|
||||
|
||||
@override
|
||||
def _stop(self) -> None:
|
||||
assert self._session_id is not None
|
||||
session_id = self._session_id
|
||||
try:
|
||||
self._proton.deactivate(session=session_id)
|
||||
finally:
|
||||
try:
|
||||
if self._output_format is None:
|
||||
self._proton.finalize(session=session_id)
|
||||
else:
|
||||
self._proton.finalize(
|
||||
session=session_id, output_format=self._output_format
|
||||
)
|
||||
finally:
|
||||
self._session_id = None
|
||||
|
||||
@override
|
||||
def annotate_context_manager(self, name: str):
|
||||
if not self._running:
|
||||
return nullcontext()
|
||||
return self._proton.scope(name)
|
||||
|
||||
|
||||
class CudaProfilerWrapper(WorkerProfiler):
|
||||
def __init__(self, profiler_config: ProfilerConfig) -> None:
|
||||
super().__init__(profiler_config)
|
||||
|
||||
Reference in New Issue
Block a user