[Bugfix][MRV2] Support encoder timing stats in model runner V2 (#50020)
Signed-off-by: Guan-Ming (Wesley) Chiu <105915352+guan404ming@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent
5426311d91
commit
457a5f34db
@@ -1,5 +1,10 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
import threading
|
||||
import time
|
||||
from collections.abc import Collection
|
||||
from contextlib import contextmanager
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
|
||||
@@ -13,7 +18,10 @@ from vllm.multimodal.utils import (
|
||||
set_mm_embedding_modality,
|
||||
)
|
||||
from vllm.v1.worker.gpu.mm.encoder_cache import EncoderCache
|
||||
from vllm.v1.worker.utils import sanity_check_mm_encoder_outputs
|
||||
from vllm.v1.worker.utils import (
|
||||
EncoderTimingStats,
|
||||
sanity_check_mm_encoder_outputs,
|
||||
)
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
@@ -27,6 +35,7 @@ class EncoderRunner:
|
||||
encoder_cache: EncoderCache,
|
||||
dtype: torch.dtype,
|
||||
device: torch.device,
|
||||
enable_timing: bool = False,
|
||||
):
|
||||
self.model = model
|
||||
self.max_num_tokens = max_num_tokens
|
||||
@@ -35,6 +44,9 @@ class EncoderRunner:
|
||||
self.dtype = dtype
|
||||
self.device = device
|
||||
self.is_realtime = supports_realtime(model)
|
||||
self.enable_timing = enable_timing
|
||||
self.encoder_timing_registry: dict[str, EncoderTimingStats] = {}
|
||||
self._timing_lock = threading.Lock()
|
||||
|
||||
self.inputs_embeds = torch.zeros(
|
||||
max_num_tokens, hidden_size, dtype=dtype, device=device
|
||||
@@ -110,6 +122,36 @@ class EncoderRunner:
|
||||
encoder_outputs.extend(batch_outputs)
|
||||
return encoder_outputs
|
||||
|
||||
@contextmanager
|
||||
def timed_encoder_operation(self, request_ids: Collection[str]):
|
||||
if not (self.enable_timing and request_ids):
|
||||
yield
|
||||
return
|
||||
|
||||
torch.accelerator.synchronize()
|
||||
start_time = time.perf_counter()
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
torch.accelerator.synchronize()
|
||||
per_request_time = (time.perf_counter() - start_time) / len(request_ids)
|
||||
with self._timing_lock:
|
||||
for req_id in request_ids:
|
||||
stats = self.encoder_timing_registry.setdefault(
|
||||
req_id, EncoderTimingStats()
|
||||
)
|
||||
stats.encoder_forward_secs += per_request_time
|
||||
stats.num_encoder_calls += 1
|
||||
|
||||
def get_encoder_timing_stats(self) -> dict[str, dict[str, float | int]]:
|
||||
with self._timing_lock:
|
||||
stats = {
|
||||
req_id: stats_obj.to_dict()
|
||||
for req_id, stats_obj in self.encoder_timing_registry.items()
|
||||
}
|
||||
self.encoder_timing_registry.clear()
|
||||
return stats
|
||||
|
||||
def gather_mm_embeddings(
|
||||
self,
|
||||
req_ids: list[str],
|
||||
|
||||
Reference in New Issue
Block a user