[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