[Model] Optimize Qwen3-TTS streaming throughput and first audio on CUDA (#8163)

Signed-off-by: Sy03 <1370724210@qq.com>
This commit is contained in:
Sy03 authored and GitHub committed 2026-09-26 22:51:04 +08:00
1 parent 8a49c65bf9
commit e1303e5067
55 files changed
+4222 -230

No files matched your search

+120 -8
View File
@@ -21,7 +21,7 @@ import time as _time
from collections.abc import Awaitable, Callable, Coroutine
from dataclasses import dataclass, field
from http import HTTPStatus
from typing import Any
from typing import TYPE_CHECKING, Any
import janus
import torch
@@ -34,6 +34,7 @@ from vllm.v1.engine import EngineCoreOutputs, FinishReason
from vllm.v1.engine.exceptions import EngineDeadError
from vllm.v1.metrics.stats import IterationStats
from vllm_omni.data_entry_keys import FIRST_AUDIO_KEY, FIRST_AUDIO_REQUIRED_KEY
from vllm_omni.diffusion.data import is_diffusion_request_started_output
from vllm_omni.distributed.omni_connectors.utils.config import stage_receives_chunks
from vllm_omni.engine import OmniEngineCoreRequest
@@ -66,6 +67,9 @@ from vllm_omni.metrics.stat_logger import OmniPrometheusStatLogger
from vllm_omni.metrics.utils import DIFFUSION_METRICS_ONLY_REQUEST_ID
from vllm_omni.outputs import OmniRequestOutput
if TYPE_CHECKING:
from vllm_omni.engine import OmniEngineCoreOutput
logger = init_logger(__name__)
@@ -226,6 +230,10 @@ class OrchestratorRequestState:
running_counter_registered: bool = False
request_artifact_dirs: set[str] = field(default_factory=set)
native_kv_transfer_id: str | None = None
# First-frame delivery and final-stage outputs use different channels.
upstream_first_audio: bool = False
pending_upstream_first_audio: tuple[int, OmniEngineCoreOutput] | None = None
pending_first_audio_outputs: list[tuple[int, int, Any]] = field(default_factory=list)
@dataclass
@@ -272,7 +280,7 @@ class StreamingInputState:
Fresh rather than shared: ``output_metadata`` is handed out by reference.
"""
segment = self.segments.get(stage_id)
segment = self.segments.get(stage_id) if stage_id is not None else None
return segment if segment is not None else StreamingSegmentState()
@@ -440,9 +448,13 @@ class OrchestratorBase:
# Start membership watcher if distributed mode is active.
membership_watcher: asyncio.Task[None] | None = None
if self._membership is not None:
async def cleanup_member_requests(ids: list[str]) -> None:
await self._cleanup_request_ids(ids, abort=True)
self._membership.install_unregister_handlers(
output_queue=self.output_async_queue,
cleanup_callback=lambda ids: self._cleanup_request_ids(ids, abort=True),
cleanup_callback=cleanup_member_requests,
replica_removed_callback=self._remove_stage_replica_waiting,
)
membership_watcher = self._membership.start()
@@ -528,7 +540,7 @@ class OrchestratorBase:
# process exit as EngineDeadError during teardown.
for pool in self.stage_pools:
for client in pool.clients:
if hasattr(client, "_shutting_down"):
if client is not None and hasattr(client, "_shutting_down"):
client._shutting_down = True
# Stage teardown runs once in run()'s finally after the
# orchestration loop observes _shutdown_event and exits.
@@ -786,6 +798,7 @@ class OrchestratorBase:
``_finish_raw_terminal_requests`` once routing is done.
"""
pool = self.stage_pools[stage_id]
await self._route_upstream_first_audio(stage_id, replica_id, raw_outputs)
await self._handle_kv_ready_raw_outputs(stage_id, raw_outputs)
for eco in raw_outputs.outputs:
# Emit kv_wait_s before _handle_kv_ready_raw_outputs'
@@ -800,7 +813,8 @@ class OrchestratorBase:
kv_params.get("connector_type") or "unknown",
float(kv_wait_s),
)
req_state = self.request_states.get(getattr(eco, "request_id", None))
request_id = getattr(eco, "request_id", None)
req_state = self.request_states.get(request_id) if request_id is not None else None
if req_state is None:
continue
if (
@@ -861,6 +875,97 @@ class OrchestratorBase:
)
return processed
async def _route_upstream_first_audio(self, stage_id: int, replica_id: int, raw_outputs: Any) -> None:
"""Order explicitly marked first audio ahead of a codec that skipped it.
The sender and codec run independently. Keep overtaking codec outputs,
including terminal outputs, on the request until its first PCM arrives.
Cancellation and stage errors use ordinary request cleanup.
"""
kept = []
final_output = self.stage_pools[stage_id].final_output
for eco in raw_outputs.outputs:
mm = getattr(eco, "multimodal_output", None)
is_first = isinstance(mm, dict) and bool(mm.pop(FIRST_AUDIO_KEY, False))
requires_first = isinstance(mm, dict) and bool(mm.pop(FIRST_AUDIO_REQUIRED_KEY, False))
request_id = getattr(eco, "request_id", None)
req_state = self.request_states.get(request_id) if request_id is not None else None
if is_first:
assert isinstance(mm, dict)
# Only the dedicated first-frame channel is intercepted.
if (
req_state is None
or req_state.upstream_first_audio
or req_state.pending_upstream_first_audio is not None
or stage_id + 1 > req_state.final_stage_id
):
continue
audio = mm.get("model_outputs")
if not isinstance(audio, torch.Tensor) or audio.numel() == 0:
await self._handle_stage_error(stage_id, eco, error="Invalid or empty first-audio payload")
continue
req_state.pending_upstream_first_audio = (stage_id, eco)
await self._flush_upstream_first_audio(req_state)
continue
if (
req_state is not None
and final_output
and not req_state.upstream_first_audio
and (requires_first or req_state.pending_first_audio_outputs)
and getattr(eco, "finish_reason", None) != FinishReason.ERROR
):
req_state.pending_first_audio_outputs.append((stage_id, replica_id, eco))
continue
kept.append(eco)
raw_outputs.outputs = kept
async def _flush_upstream_first_audio(self, req_state: OrchestratorRequestState) -> None:
"""Feed upstream PCM through the codec's normal output-kind contract.
The source can finish its first frame while downstream prewarm is
still selecting/registering a replica. Retain that frame until the
codec owns an output-processor state, then accumulate it before any
codec output that omitted it. This preserves FINAL_ONLY/CUMULATIVE
waveforms and downstream consumers such as forced alignment.
"""
from vllm_omni.engine import OmniEngineCoreOutputs
pending = req_state.pending_upstream_first_audio
if pending is None or self.request_states.get(req_state.request_id) is not req_state:
return
source_stage, first_output = pending
codec_stage = source_stage + 1
pool = self.stage_pools[codec_stage]
replica_id = pool.get_bound_replica_id(req_state.request_id)
if replica_id is None or req_state.request_id not in pool.output_processor.request_states:
return
req_state.pending_upstream_first_audio = None
req_state.upstream_first_audio = True
try:
processed = await pool.process_llm_raw_outputs(replica_id, OmniEngineCoreOutputs(outputs=[first_output]))
await self._handle_processed_outputs(codec_stage, replica_id, processed)
except EngineDeadError as error:
await self._handle_dead_replica(codec_stage, replica_id, error)
return
except Exception as error:
# A promised first frame must either enter the codec accumulator
# or fail its request; leaving it pending would stall completion.
logger.exception("First-audio output processing failed for %s", req_state.request_id)
await self._handle_stage_error(codec_stage, first_output, error=str(error))
return
if self.request_states.get(req_state.request_id) is not req_state:
return
ready, req_state.pending_first_audio_outputs = req_state.pending_first_audio_outputs, []
for pending_stage, pending_replica, output in ready:
if self.request_states.get(req_state.request_id) is not req_state:
break
terminal: set[str] = set()
processed = await self._process_llm_stage_outputs(
pending_stage, pending_replica, OmniEngineCoreOutputs(outputs=[output]), terminal
)
await self._handle_processed_outputs(pending_stage, pending_replica, processed)
await self._finish_raw_terminal_requests(pending_stage, pending_replica, terminal)
def _absorb_diffusion_metrics(self, stage_id: int, replica_id: int, diffusion_output: Any) -> bool:
"""Drain a diffusion output's piggybacked metrics.
@@ -1543,7 +1648,7 @@ class OrchestratorBase:
self._pd_kv_params.pop(request_id, None)
req_state = self.request_states.pop(request_id, None)
if req_state is not None:
cleanup_request_artifact_dirs(getattr(req_state, "request_artifact_dirs", ()))
cleanup_request_artifact_dirs(getattr(req_state, "request_artifact_dirs", set()))
if req_state is not None and req_state.running_counter_registered and self._running_counter is not None:
self._running_counter.decrement()
req_state.running_counter_registered = False
@@ -2201,7 +2306,7 @@ class OrchestratorBase:
)
return
diffusion_source_outputs = [output, *companion_outputs]
if next_client.custom_process_input_func is not None:
if next_client is not None and next_client.custom_process_input_func is not None:
_t_ar2d = _time.perf_counter()
_fn = next_client.custom_process_input_func
_extra_kwargs: dict[str, Any] = {}
@@ -2386,7 +2491,9 @@ class OrchestratorBase:
req_state.streaming.source_token_decoder = decode
try:
next_inputs = next_client.process_engine_inputs(
assert next_client is not None
process_inputs = getattr(next_client, "process_engine_inputs")
next_inputs = process_inputs(
source_outputs,
req_state.prompt,
streaming_context=req_state.streaming,
@@ -2637,6 +2744,11 @@ class OrchestratorBase:
req_state,
)
if req_state.pending_upstream_first_audio is not None or req_state.upstream_first_audio:
await self._flush_upstream_first_audio(req_state)
if self.request_states.get(request_id) is not req_state:
return False
# async_chunk pre-submit fires per stage edge (N-1 -> N). Source
# replica is stage 0's bound replica (single-replica thinker in
# all current configs); fall back to 0 if unknown.