[Core][Frontend] Bound duplex output delivery and reject cancelled audio (#7644)
Signed-off-by: Nolen Liang <nliang@nvidia.com> Signed-off-by: bcsdhjew <nliang@nvidia.com>
This commit is contained in:
1 parent
3f20252151
commit
f781f7bb08
26 files changed
+1170
-147
No files matched your search
@@ -22,6 +22,7 @@ import json
|
||||
from collections.abc import Mapping
|
||||
from contextlib import suppress
|
||||
from dataclasses import dataclass, replace
|
||||
from functools import partial
|
||||
|
||||
from fastapi import WebSocket, WebSocketDisconnect
|
||||
from vllm.logger import init_logger
|
||||
@@ -478,7 +479,7 @@ class OmniDuplexSessionHandler:
|
||||
attachment_generation=credentials.attachment_generation,
|
||||
resume_token=credentials.resume_token,
|
||||
)
|
||||
await self._send_event(session_id, event)
|
||||
await self._send_event(session_id, event, handle=handle)
|
||||
if isinstance(event, SessionClosed):
|
||||
close_reason = event.reason or event.type
|
||||
break
|
||||
@@ -523,19 +524,22 @@ class OmniDuplexSessionHandler:
|
||||
with suppress(Exception):
|
||||
await asyncio.wait_for(asyncio.shield(pump), _PUMP_DRAIN_TIMEOUT_S)
|
||||
|
||||
async def _send_event(self, session_id: str, event: DuplexEvent) -> None:
|
||||
async def _send_event(self, session_id: str, event: DuplexEvent, *, handle: DuplexSessionHandle) -> None:
|
||||
payload = event.to_realtime()
|
||||
journal = not isinstance(event, _UNJOURNALED_EVENTS) and session_id not in self._resync_required_sessions
|
||||
event_guard = partial(handle.output_guard, event)
|
||||
try:
|
||||
try:
|
||||
await self._attachment_registry.send_event(session_id, payload, journal=journal)
|
||||
await self._attachment_registry.send_event(
|
||||
session_id, payload, journal=journal, event_guard=event_guard
|
||||
)
|
||||
except DuplexJournalOverflowError:
|
||||
first_overflow = session_id not in self._resync_required_sessions
|
||||
self._resync_required_sessions.add(session_id)
|
||||
if first_overflow:
|
||||
resync = SessionResyncRequired(session_id=session_id, reason="journal_overflow")
|
||||
await self._attachment_registry.send_event(session_id, resync.to_realtime(), journal=False)
|
||||
await self._attachment_registry.send_event(session_id, payload, journal=False)
|
||||
await self._attachment_registry.send_event(session_id, payload, journal=False, event_guard=event_guard)
|
||||
except KeyError:
|
||||
# Attachment already closed (takeover or teardown); the journal is gone.
|
||||
pass
|
||||
|
||||
@@ -11,6 +11,7 @@ import secrets
|
||||
import time
|
||||
from collections import deque
|
||||
from collections.abc import Awaitable, Callable, Mapping
|
||||
from contextlib import AbstractContextManager, nullcontext
|
||||
from dataclasses import dataclass, field
|
||||
from types import MappingProxyType
|
||||
|
||||
@@ -291,6 +292,7 @@ class DuplexSessionAttachmentRegistry:
|
||||
*,
|
||||
journal: bool = True,
|
||||
on_accepted: Callable[[], None] | None = None,
|
||||
event_guard: Callable[[], AbstractContextManager[bool]] | None = None,
|
||||
) -> JournalEntry | None:
|
||||
"""Sequence and dispatch one event to the current attachment.
|
||||
|
||||
@@ -309,9 +311,15 @@ class DuplexSessionAttachmentRegistry:
|
||||
async with self._lock:
|
||||
if self._sessions.get(session_id) is not state:
|
||||
raise KeyError(f"unknown duplex attachment session: {session_id}")
|
||||
entry = state.journal.record(payload) if journal else None
|
||||
attachment = state.attachment
|
||||
wire_payload = dict(entry.payload) if entry is not None else dict(payload)
|
||||
# The producer may invalidate held audio while this coroutine
|
||||
# waits for an attachment lock. Commit order before sequencing;
|
||||
# release the cross-thread guard before awaiting network I/O.
|
||||
with event_guard() if event_guard is not None else nullcontext(True) as valid:
|
||||
if not valid:
|
||||
return None
|
||||
entry = state.journal.record(payload) if journal else None
|
||||
attachment = state.attachment
|
||||
wire_payload = dict(entry.payload) if entry is not None else dict(payload)
|
||||
if entry is not None and on_accepted is not None:
|
||||
on_accepted()
|
||||
if attachment is not None:
|
||||
|
||||
@@ -27,7 +27,7 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from collections.abc import AsyncIterator, Awaitable, Callable, Mapping, Sequence
|
||||
from contextlib import suppress
|
||||
from contextlib import AbstractContextManager, suppress
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
@@ -37,6 +37,7 @@ from vllm_omni.config.stage_config import DuplexSessionRuntimeConfig
|
||||
from vllm_omni.engine.duplex import commands as duplex_commands
|
||||
from vllm_omni.engine.duplex.commands import DuplexCommand
|
||||
from vllm_omni.engine.duplex.config import DuplexCapabilities, DuplexSessionConfig, ResponseCreateOptions
|
||||
from vllm_omni.engine.duplex.delivery import DuplexOutputBuffer
|
||||
from vllm_omni.engine.duplex.events import DuplexEvent, SessionClosed
|
||||
from vllm_omni.engine.duplex.messages import (
|
||||
DuplexControlResultMessage,
|
||||
@@ -66,7 +67,11 @@ class DuplexSessionHandle:
|
||||
self.capabilities: DuplexCapabilities = DuplexCapabilities()
|
||||
self.public_session: dict[str, object] = {}
|
||||
self.lease_generation: int = 0
|
||||
self._outbox: asyncio.Queue[DuplexEvent | None] = asyncio.Queue()
|
||||
limits = omni.duplex_session_config
|
||||
self._outbox = DuplexOutputBuffer(
|
||||
max_bytes=limits.max_pending_output_bytes_per_session,
|
||||
max_events=limits.max_pending_output_events_per_session,
|
||||
)
|
||||
self._closed = False
|
||||
self._close_reason: str | None = None
|
||||
self._closed_event = asyncio.Event()
|
||||
@@ -219,12 +224,10 @@ class DuplexSessionHandle:
|
||||
self._consumer_active = True
|
||||
try:
|
||||
while True:
|
||||
if self._closed and self._outbox.empty():
|
||||
return
|
||||
event = await self._outbox.get()
|
||||
if event is None:
|
||||
if self._closed and self._outbox.empty():
|
||||
return
|
||||
return
|
||||
if not self._outbox.is_valid(event):
|
||||
continue
|
||||
yield event
|
||||
if event.is_terminal:
|
||||
@@ -236,6 +239,10 @@ class DuplexSessionHandle:
|
||||
await self._closed_event.wait()
|
||||
return self._close_reason or "closed"
|
||||
|
||||
def output_guard(self, event: DuplexEvent) -> AbstractContextManager[bool]:
|
||||
"""Recheck held audio while committing its delivery; never await inside this guard."""
|
||||
return self._outbox.guard(event)
|
||||
|
||||
async def __aenter__(self) -> DuplexSessionHandle:
|
||||
return self
|
||||
|
||||
@@ -256,7 +263,7 @@ class DuplexSessionHandle:
|
||||
self.lease_generation = int(result.lease_generation)
|
||||
|
||||
def _deliver(self, event: DuplexEvent) -> None:
|
||||
self._outbox.put_nowait(event)
|
||||
self._outbox.put(event)
|
||||
if isinstance(event, SessionClosed): # SessionExpired is a SessionClosed
|
||||
self._mark_closed(event.reason or event.type)
|
||||
|
||||
@@ -266,7 +273,7 @@ class DuplexSessionHandle:
|
||||
self._closed = True
|
||||
self._close_reason = reason
|
||||
self._closed_event.set()
|
||||
self._outbox.put_nowait(None)
|
||||
self._outbox.close()
|
||||
|
||||
|
||||
class DuplexOmni(AsyncOmni):
|
||||
@@ -356,8 +363,11 @@ class DuplexOmni(AsyncOmni):
|
||||
self._handles[session_id] = handle
|
||||
self._final_output_handler()
|
||||
try:
|
||||
result = await self.engine.open_session_async(session_id, session_config, timeout=timeout)
|
||||
result = await self.engine.open_session_async(
|
||||
session_id, session_config, output_buffer=handle._outbox, timeout=timeout
|
||||
)
|
||||
except BaseException:
|
||||
handle._mark_closed("open_failed")
|
||||
if self._handles.get(session_id) is handle:
|
||||
self._handles.pop(session_id, None)
|
||||
# Dropping the handle only forgets the id here. The engine may still
|
||||
|
||||
Reference in new issue
Block a user