[Profiler] Add per-session profiling controls to Python and Rust frontends (#57875)

Signed-off-by: czhu <conway.zhu@cohere.com>
Signed-off-by: Conway Zhu <conway.zhu@cohere.com>
Co-authored-by: aoshen02 <aoshen@inferact.ai>
This commit is contained in:
czhu-cohere
2026-10-06 05:21:19 +00:00
committed by GitHub
co-authored by aoshen02
parent 9a1f6fd4cb
commit 6fddc9b21e
25 changed files with 1497 additions and 164 deletions
+65 -26
View File
@@ -13,6 +13,10 @@ import vllm.envs as envs
from vllm import TokensPrompt
from vllm.config import VllmConfig
from vllm.config.kv_events import KVEventsConfig
from vllm.config.profiler import (
validate_profile_iteration_bounds,
validate_profile_prefix,
)
from vllm.distributed.weight_transfer.base import (
WeightTransferInitRequest,
WeightTransferUpdateRequest,
@@ -23,6 +27,7 @@ from vllm.entrypoints.serve.elastic_ep.middleware import set_scaling_elastic_ep
from vllm.exceptions import (
GracefulHTTPError,
MaxQueuedTokensError,
ProfilerAlreadyActiveError,
QueueOverflowError,
VLLMClientError,
VLLMValidationError,
@@ -33,7 +38,7 @@ from vllm.lora.request import LoRARequest
from vllm.multimodal import MULTIMODAL_REGISTRY, MultiModalRegistry
from vllm.outputs import STREAM_FINISHED, PoolingRequestOutput, RequestOutput
from vllm.pooling_params import PoolingParams
from vllm.profiler.wrapper import TorchProfilerWrapper
from vllm.profiler.wrapper import TorchProfilerWrapper, create_frontend_profiler
from vllm.renderers import renderer_from_config
from vllm.renderers.inputs.preprocess import extract_prompt_components
from vllm.sampling_params import RequestOutputKind, SamplingParams
@@ -211,24 +216,27 @@ class AsyncLLM(EngineClient):
pass
self.profiler = profiler
configured_activities = vllm_config.profiler_config.torch_profiler_activities
self._frontend_profiler_injected = profiler is not None
self._profile_lock = asyncio.Lock()
self._profile_session_guard_enabled = client_count == 1
self._profile_session_active = False
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)
not self._frontend_profiler_injected
and vllm_config.profiler_config.should_profile_frontend
):
profiler_dir = vllm_config.profiler_config.torch_profiler_dir
logger.info(
"Torch profiler enabled. AsyncLLM CPU traces will be collected under %s", # noqa: E501
profiler_dir,
)
worker_name = f"{socket.gethostname()}_{os.getpid()}.async_llm"
self.profiler = TorchProfilerWrapper(
vllm_config.profiler_config,
worker_name=worker_name,
local_rank=0,
activities=["CPU"],
)
if self._profile_session_guard_enabled:
self._frontend_profiler_worker_name = (
f"{socket.gethostname()}_{os.getpid()}.async_llm"
)
self.profiler = create_frontend_profiler(
vllm_config.profiler_config,
worker_name=self._frontend_profiler_worker_name,
)
else:
logger.warning(
"Frontend CPU profiling is disabled when multiple API server "
"processes share an engine."
)
@classmethod
def from_vllm_config(
@@ -1081,17 +1089,48 @@ class AsyncLLM(EngineClient):
if self.errored:
raise self.dead_error
async def start_profile(self, profile_prefix: str | None = None) -> None:
coros = [self.engine_core.profile_async(True, profile_prefix)]
if self.profiler is not None:
coros.append(asyncio.to_thread(self.profiler.start))
await asyncio.gather(*coros)
async def start_profile(
self,
profile_prefix: str | None = None,
*,
delay_iterations: int | None = None,
max_iterations: int | None = None,
) -> None:
async with self._profile_lock:
if self._profile_session_guard_enabled and self._profile_session_active:
raise ProfilerAlreadyActiveError()
validate_profile_prefix(profile_prefix)
validate_profile_iteration_bounds(delay_iterations, max_iterations)
if self._profile_session_guard_enabled:
self._profile_session_active = True
# A cancelled start may still take effect, so only a reported
# engine failure clears the session; otherwise /stop_profile is needed.
try:
await self.engine_core.profile_async(
True,
profile_prefix,
delay_iterations,
max_iterations,
)
except Exception:
self._profile_session_active = False
raise
if self.profiler is not None:
if not self._frontend_profiler_injected:
worker_name = self._frontend_profiler_worker_name
if profile_prefix is not None:
worker_name = f"{profile_prefix}_{worker_name}"
self.profiler.set_output_name(worker_name)
await asyncio.to_thread(self.profiler.start)
async def stop_profile(self) -> None:
coros = [self.engine_core.profile_async(False)]
if self.profiler is not None:
coros.append(asyncio.to_thread(self.profiler.stop))
await asyncio.gather(*coros)
async with self._profile_lock:
coros = [self.engine_core.profile_async(False)]
if self.profiler is not None:
coros.append(asyncio.to_thread(self.profiler.stop))
await asyncio.gather(*coros)
self._profile_session_active = False
async def reset_mm_cache(self) -> None:
# Join the background MM warmup first: the mm_processor_cache is not
+13 -2
View File
@@ -810,8 +810,19 @@ class EngineCore:
cleanup_dist_env_and_memory()
logger.debug_once("[shutdown] EngineCore: local resource teardown complete")
def profile(self, is_start: bool = True, profile_prefix: str | None = None):
self.model_executor.profile(is_start, profile_prefix)
def profile(
self,
is_start: bool = True,
profile_prefix: str | None = None,
delay_iterations: int | None = None,
max_iterations: int | None = None,
):
self.model_executor.profile(
is_start,
profile_prefix,
delay_iterations=delay_iterations,
max_iterations=max_iterations,
)
def reset_mm_cache(self):
# NOTE: Since this is mainly for debugging, we don't attempt to
+51 -8
View File
@@ -181,7 +181,13 @@ class EngineCoreClient(ABC):
def add_request(self, request: EngineCoreRequest) -> None:
raise NotImplementedError
def profile(self, is_start: bool = True, profile_prefix: str | None = None) -> None:
def profile(
self,
is_start: bool = True,
profile_prefix: str | None = None,
delay_iterations: int | None = None,
max_iterations: int | None = None,
) -> None:
raise NotImplementedError
def reset_mm_cache(self) -> None:
@@ -275,7 +281,11 @@ class EngineCoreClient(ABC):
raise NotImplementedError
async def profile_async(
self, is_start: bool = True, profile_prefix: str | None = None
self,
is_start: bool = True,
profile_prefix: str | None = None,
delay_iterations: int | None = None,
max_iterations: int | None = None,
) -> None:
raise NotImplementedError
@@ -392,8 +402,19 @@ class InprocClient(EngineCoreClient):
if self._finalizer.detach() is not None:
self.engine_core.shutdown()
def profile(self, is_start: bool = True, profile_prefix: str | None = None) -> None:
self.engine_core.profile(is_start, profile_prefix)
def profile(
self,
is_start: bool = True,
profile_prefix: str | None = None,
delay_iterations: int | None = None,
max_iterations: int | None = None,
) -> None:
self.engine_core.profile(
is_start,
profile_prefix,
delay_iterations=delay_iterations,
max_iterations=max_iterations,
)
def reset_mm_cache(self) -> None:
self.engine_core.reset_mm_cache()
@@ -1019,8 +1040,20 @@ class SyncMPClient(MPClient):
if request_ids and not self.resources.engine_dead:
self._send_input(EngineCoreRequestType.ABORT, request_ids)
def profile(self, is_start: bool = True, profile_prefix: str | None = None) -> None:
self.call_utility("profile", is_start, profile_prefix)
def profile(
self,
is_start: bool = True,
profile_prefix: str | None = None,
delay_iterations: int | None = None,
max_iterations: int | None = None,
) -> None:
self.call_utility(
"profile",
is_start,
profile_prefix,
delay_iterations,
max_iterations,
)
def reset_mm_cache(self) -> None:
self.call_utility("reset_mm_cache")
@@ -1294,9 +1327,19 @@ class AsyncMPClient(MPClient):
return await self.call_utility_consensus_async("is_scheduler_paused")
async def profile_async(
self, is_start: bool = True, profile_prefix: str | None = None
self,
is_start: bool = True,
profile_prefix: str | None = None,
delay_iterations: int | None = None,
max_iterations: int | None = None,
) -> None:
await self.call_utility_async("profile", is_start, profile_prefix)
await self.call_utility_async(
"profile",
is_start,
profile_prefix,
delay_iterations,
max_iterations,
)
async def reset_mm_cache_async(self) -> None:
await self.call_utility_async("reset_mm_cache")
+30 -2
View File
@@ -12,9 +12,14 @@ from typing_extensions import TypeVar
import vllm.envs as envs
from vllm.config import ParallelConfig, VllmConfig
from vllm.config.profiler import (
validate_profile_iteration_bounds,
validate_profile_prefix,
)
from vllm.distributed import stateless_destroy_torch_distributed_process_group
from vllm.distributed.parallel_state import get_dp_group
from vllm.engine.arg_utils import EngineArgs
from vllm.exceptions import ProfilerAlreadyActiveError
from vllm.inputs import EngineInput, PromptType
from vllm.logger import configure_logging_if_needed, init_logger
from vllm.lora.request import LoRARequest
@@ -116,6 +121,7 @@ class LLMEngine:
log_stats=self.log_stats,
renderer=renderer,
)
self._profile_session_active = False
self.logger_manager: StatLoggerManager | None = None
if self.log_stats:
@@ -358,11 +364,33 @@ class LLMEngine:
return processed_outputs.request_outputs
def start_profile(self, profile_prefix: str | None = None):
self.engine_core.profile(True, profile_prefix)
def start_profile(
self,
profile_prefix: str | None = None,
*,
delay_iterations: int | None = None,
max_iterations: int | None = None,
):
if self._profile_session_active:
raise ProfilerAlreadyActiveError()
validate_profile_prefix(profile_prefix)
validate_profile_iteration_bounds(delay_iterations, max_iterations)
self._profile_session_active = True
try:
self.engine_core.profile(
True,
profile_prefix,
delay_iterations,
max_iterations,
)
except BaseException:
self._profile_session_active = False
raise
def stop_profile(self):
self.engine_core.profile(False)
self._profile_session_active = False
def reset_mm_cache(self):
# Join the background MM warmup first: the mm_processor_cache is not