[Bugfix] Fix video prewarm cache retention and cancel-restart delay (#7363)
Signed-off-by: psv666 <2693925048@qq.com>
This commit is contained in:
1 parent
e540dfb72f
commit
c8bef008eb
3 files changed
+182
-18
No files matched your search
@@ -1,5 +1,5 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
|
||||
"""Qwen-Omni streaming video WebSocket handler.
|
||||
|
||||
Accepts video frames incrementally via WebSocket, buffers them, and
|
||||
@@ -26,12 +26,14 @@ Protocol:
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Mapping
|
||||
from typing import Any
|
||||
|
||||
from vllm_omni.entrypoints.openai.video_stream_base import (
|
||||
_BAD_FRAME,
|
||||
_DEFAULT_CONFIG_TIMEOUT,
|
||||
_DEFAULT_IDLE_TIMEOUT,
|
||||
PrewarmedFrame,
|
||||
StreamingVideoSessionConfig,
|
||||
VideoStreamTurnTrigger,
|
||||
)
|
||||
@@ -59,7 +61,7 @@ class QwenOmniStreamingVideoHandler(OmniStreamingVideoHandlerBase):
|
||||
audio_buffer: bytearray,
|
||||
message_history: list[dict[str, Any]],
|
||||
query_text: str,
|
||||
prewarmed_frames: dict[str, tuple[Any, str]],
|
||||
prewarmed_frames: Mapping[str, PrewarmedFrame],
|
||||
) -> tuple[list[dict[str, Any]], dict[str, Any]]:
|
||||
n_buf = len(frame_buffer)
|
||||
if n_buf <= config.num_frames:
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
|
||||
"""Base WebSocket handler for streaming video input understanding.
|
||||
|
||||
Shared session loop, frame/audio buffering, EVS pre-filter, prewarm,
|
||||
@@ -37,7 +37,8 @@ import uuid
|
||||
import wave
|
||||
from collections.abc import Mapping
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Protocol, runtime_checkable
|
||||
from enum import Enum
|
||||
from typing import Any, Final, Protocol, TypeAlias, runtime_checkable
|
||||
|
||||
import torch
|
||||
from fastapi import WebSocket, WebSocketDisconnect
|
||||
@@ -61,7 +62,14 @@ _MAX_BUFFER_FRAMES = 64
|
||||
_MAX_AUDIO_BUFFER_BYTES = 4 * 1024 * 1024
|
||||
_MAX_MSG_QUEUE = 200
|
||||
_CODEC_FRAME_SAMPLES = 1920 # CausalConv leading-edge artifact length
|
||||
_BAD_FRAME = object()
|
||||
|
||||
|
||||
class _FrameStatus(Enum):
|
||||
BAD = "bad"
|
||||
|
||||
|
||||
_BAD_FRAME: Final = _FrameStatus.BAD
|
||||
PrewarmedFrame: TypeAlias = tuple[Any, str] | _FrameStatus
|
||||
|
||||
|
||||
def _decode_frame_bytes(raw_bytes: bytes) -> Any:
|
||||
@@ -83,7 +91,7 @@ class VideoStreamPipelineHooks(Protocol):
|
||||
audio_buffer: bytearray,
|
||||
message_history: list[dict[str, Any]],
|
||||
query_text: str,
|
||||
prewarmed_frames: dict[str, tuple[Any, str]],
|
||||
prewarmed_frames: Mapping[str, PrewarmedFrame],
|
||||
) -> tuple[list[dict[str, Any]], dict[str, Any]]:
|
||||
"""Build OpenAI-style messages and the current user message."""
|
||||
...
|
||||
@@ -169,7 +177,7 @@ class OmniStreamingVideoHandler:
|
||||
audio_buffer: bytearray,
|
||||
message_history: list[dict[str, Any]],
|
||||
query_text: str,
|
||||
prewarmed_frames: dict[str, tuple[Any, str]],
|
||||
prewarmed_frames: Mapping[str, PrewarmedFrame],
|
||||
) -> tuple[list[dict[str, Any]], dict[str, Any]]:
|
||||
raise NotImplementedError
|
||||
|
||||
@@ -219,7 +227,7 @@ class OmniStreamingVideoHandler:
|
||||
frame_buffer: list[str] = [] # base64-encoded JPEG frames
|
||||
frame_metadata: list[dict[str, Any]] = []
|
||||
# Per-frame PIL cache + uuid for mm_hash reuse. Aligned with frame_buffer by index.
|
||||
frame_pil_cache: dict[str, tuple[Any, str] | object] = {} # b64 -> (PIL.Image, uuid) or _BAD_FRAME
|
||||
frame_pil_cache: dict[str, PrewarmedFrame] = {} # b64 -> (PIL.Image, uuid) or _BAD_FRAME
|
||||
frame_filter = (
|
||||
FrameSimilarityFilter(threshold=config.frame_filter_threshold) if config.enable_frame_filter else None
|
||||
)
|
||||
@@ -306,7 +314,6 @@ class OmniStreamingVideoHandler:
|
||||
await self._engine_client.abort(prev_request_id)
|
||||
except Exception:
|
||||
pass
|
||||
await asyncio.sleep(0.1)
|
||||
prev_was_interrupted = False
|
||||
|
||||
request_id = f"video-{uuid.uuid4().hex[:12]}"
|
||||
@@ -429,8 +436,12 @@ class OmniStreamingVideoHandler:
|
||||
async def _prewarm(b64: str, b: bytes, u: str) -> None:
|
||||
try:
|
||||
pil = await asyncio.to_thread(_decode_frame_bytes, b)
|
||||
frame_pil_cache[b64] = (pil, u)
|
||||
# The frame may have been evicted while decoding.
|
||||
if b64 in frame_buffer:
|
||||
frame_pil_cache[b64] = (pil, u)
|
||||
except Exception:
|
||||
if b64 not in frame_buffer:
|
||||
return
|
||||
frame_pil_cache[b64] = _BAD_FRAME
|
||||
logger.warning("prewarm decode failed for frame (len=%d)", len(b))
|
||||
try:
|
||||
@@ -577,7 +588,7 @@ class OmniStreamingVideoHandler:
|
||||
query_text: str,
|
||||
request_id: str,
|
||||
interrupt_event: asyncio.Event,
|
||||
prewarmed_frames: dict[str, tuple[Any, str]],
|
||||
prewarmed_frames: Mapping[str, PrewarmedFrame],
|
||||
frame_metadata: list[dict[str, Any]] | None = None,
|
||||
) -> None:
|
||||
"""Build prompt, run inference, stream text + audio response."""
|
||||
@@ -616,10 +627,13 @@ class OmniStreamingVideoHandler:
|
||||
query_text: str,
|
||||
request_id: str,
|
||||
interrupt_event: asyncio.Event,
|
||||
prewarmed_frames: dict[str, tuple[Any, str]],
|
||||
prewarmed_frames: Mapping[str, PrewarmedFrame],
|
||||
frame_metadata: list[dict[str, Any]] | None = None,
|
||||
) -> None:
|
||||
"""Direct engine_client.generate() path for async_chunk audio."""
|
||||
engine_client = self._engine_client
|
||||
assert engine_client is not None, "_process_query must validate the engine client"
|
||||
|
||||
from vllm.entrypoints.openai.chat_completion.protocol import (
|
||||
ChatCompletionRequest,
|
||||
)
|
||||
@@ -686,7 +700,7 @@ class OmniStreamingVideoHandler:
|
||||
audio_tail_tensors: list[Any] = []
|
||||
|
||||
try:
|
||||
result_gen = self._engine_client.generate(
|
||||
result_gen = engine_client.generate(
|
||||
prompt=engine_prompt,
|
||||
request_id=request_id,
|
||||
output_modalities=config.modalities,
|
||||
@@ -989,6 +1003,8 @@ class OmniStreamingVideoHandler:
|
||||
)
|
||||
mixin = AudioMixin()
|
||||
resp = mixin.create_audio(audio_obj)
|
||||
# base64_encode=True selects the string result of create_audio().
|
||||
assert isinstance(resp.audio_data, str)
|
||||
return resp.audio_data
|
||||
|
||||
@staticmethod
|
||||
|
||||
Reference in new issue
Block a user