[Perf] Fix Qwen3-TTS latency regression (#3485)

Signed-off-by: Sy03 <1370724210@qq.com>
This commit is contained in:
Sy03 authored and GitHub committed 2026-05-10 23:07:38 +00:00
1 parent 857356d5b7
commit 2c73cf3522
12 files changed
+315 -85

No files matched your search

@@ -89,6 +89,8 @@ class CUDAGraphDecoderWrapper:
dtype: torch.dtype = torch.long,
codec_chunk_frames: int = 0,
codec_left_context_frames: int = 0,
decode_chunk_size: int = 300,
decode_left_context: int = 25,
):
if device.type != "cuda" or not self.enabled or self._warmed_up:
return
@@ -100,6 +102,8 @@ class CUDAGraphDecoderWrapper:
self.capture_sizes = self.compute_capture_sizes(
codec_chunk_frames=codec_chunk_frames,
codec_left_context_frames=codec_left_context_frames,
decode_chunk_size=decode_chunk_size,
decode_left_context=decode_left_context,
)
logger.info("Starting CUDA Graph warmup for %d sizes: %s", len(self.capture_sizes), self.capture_sizes)
@@ -286,9 +286,11 @@ class Qwen3TTSCode2Wav(nn.Module):
if hasattr(self.decoder, "precompute_snake_caches"):
self.decoder.precompute_snake_caches()
# Read chunk config from stage connector and update decode params
chunk_frames = 0
left_frames = 0
# The connector codec chunk settings control inter-stage streaming
# windows. Keep decoder-internal chunking separate; using the small
# streaming window here causes repeated overlap decode in Code2Wav.
codec_chunk_frames = 0
codec_left_context_frames = 0
model_cfg = getattr(self.vllm_config, "model_config", None)
connector_cfg = getattr(model_cfg, "stage_connector_config", None)
extra_cfg = (
@@ -296,20 +298,40 @@ class Qwen3TTSCode2Wav(nn.Module):
if isinstance(connector_cfg, dict)
else getattr(connector_cfg, "extra", None)
)
def _get_int_config(name: str, default: int) -> int:
value = extra_cfg.get(name, default)
if value is None:
return default
try:
return int(value)
except (TypeError, ValueError) as exc:
raise ValueError(f"Invalid Qwen3-TTS Code2Wav config {name}={value!r}") from exc
if isinstance(extra_cfg, dict):
chunk_frames = int(extra_cfg.get("codec_chunk_frames") or 0)
left_frames = int(extra_cfg.get("codec_left_context_frames") or 0)
if getattr(model_cfg, "async_chunk", False) and chunk_frames > 0:
self._decode_chunk_frames = chunk_frames
self._decode_left_context_frames = left_frames if left_frames > 0 else 0
codec_chunk_frames = int(extra_cfg.get("codec_chunk_frames") or 0)
codec_left_context_frames = int(extra_cfg.get("codec_left_context_frames") or 0)
decode_chunk_frames = _get_int_config("decode_chunk_frames", self._decode_chunk_frames)
decode_left_context_frames = _get_int_config(
"decode_left_context_frames",
self._decode_left_context_frames,
)
if decode_chunk_frames <= 0 or decode_left_context_frames < 0:
raise ValueError(
"Invalid Qwen3-TTS Code2Wav decode chunk config: "
f"decode_chunk_frames={decode_chunk_frames}, "
f"decode_left_context_frames={decode_left_context_frames}"
)
self._decode_chunk_frames = decode_chunk_frames
self._decode_left_context_frames = decode_left_context_frames
if hasattr(self.decoder, "enable_cudagraph") and device.type == "cuda":
try:
if (
chunk_frames > 0
and left_frames > 0
codec_chunk_frames > 0
and codec_left_context_frames > 0
and self._decoder_sliding_window
and left_frames < self._decoder_sliding_window
and codec_left_context_frames < self._decoder_sliding_window
):
logger.warning(
"Qwen3-TTS streaming codec_left_context_frames=%d "
@@ -317,15 +339,17 @@ class Qwen3TTSCode2Wav(nn.Module):
"chunk-boundary distortion may occur. "
"Increase codec_left_context_frames to at least "
"%d for streaming.",
left_frames,
codec_left_context_frames,
self._decoder_sliding_window,
self._decoder_sliding_window,
)
self.decoder.enable_cudagraph(
device=device,
codec_chunk_frames=chunk_frames,
codec_left_context_frames=left_frames,
codec_chunk_frames=codec_chunk_frames,
codec_left_context_frames=codec_left_context_frames,
decode_chunk_size=self._decode_chunk_frames,
decode_left_context=self._decode_left_context_frames,
)
logger.info("Code2Wav decoder CUDA Graph enabled")
except Exception:
@@ -851,6 +851,8 @@ class Qwen3TTSTokenizerV2Decoder(Qwen3TTSTokenizerV2DecoderPreTrainedModel):
device: torch.device | None = None,
codec_chunk_frames: int = 0,
codec_left_context_frames: int = 0,
decode_chunk_size: int = 300,
decode_left_context: int = 25,
):
from ..cuda_graph_decoder_wrapper import CUDAGraphDecoderWrapper
@@ -871,6 +873,8 @@ class Qwen3TTSTokenizerV2Decoder(Qwen3TTSTokenizerV2DecoderPreTrainedModel):
dtype=torch.long,
codec_chunk_frames=codec_chunk_frames,
codec_left_context_frames=codec_left_context_frames,
decode_chunk_size=decode_chunk_size,
decode_left_context=decode_left_context,
)
self._cudagraph_enabled = True
logger.info(
@@ -38,7 +38,7 @@ stage_args:
devices: "0"
engine_args:
model_stage: code2wav
max_num_seqs: 1
max_num_seqs: 10
model_arch: Qwen3TTSCode2Wav
worker_type: generation
scheduler_cls: vllm_omni.core.sched.omni_generation_scheduler.OmniGenerationScheduler
@@ -87,6 +87,8 @@ runtime:
# Match the decoder sliding attention window to avoid chunk-boundary noise.
codec_chunk_frames: 25
codec_left_context_frames: 72
# Emit only the first audio chunk early, then return to codec_chunk_frames.
initial_codec_chunk_frames: 1
edges:
- from: 0
@@ -161,10 +161,11 @@ def talker2code2wav_async_chunk(
cfg = raw_cfg.get("extra", raw_cfg) if isinstance(raw_cfg, dict) else {}
chunk_size = int(cfg.get("codec_chunk_frames", 25))
left_context_size_config = int(cfg.get("codec_left_context_frames", 25))
configured_initial_chunk_size = int(cfg.get("initial_codec_chunk_frames") or 0)
# Per-request override takes priority over dynamic IC.
per_request_override = False
initial_chunk_size = 0
fixed_initial_chunk_size = configured_initial_chunk_size > 0
initial_chunk_size = configured_initial_chunk_size
additional_information = getattr(request, "additional_information", None)
if (
@@ -175,10 +176,10 @@ def talker2code2wav_async_chunk(
entry = additional_information.entries["initial_codec_chunk_frames"]
if entry.list_data is not None and len(entry.list_data) == 1:
initial_chunk_size = int(entry.list_data[0])
per_request_override = True
fixed_initial_chunk_size = True
# Dynamic IC: cache per request so boundaries stay stable for its lifetime.
if not per_request_override:
if not fixed_initial_chunk_size:
_ic_cache = getattr(transfer_manager, "_cached_ic", None)
if _ic_cache is None:
_ic_cache = {}
@@ -190,7 +191,7 @@ def talker2code2wav_async_chunk(
_ic_cache[request_id] = compute_dynamic_initial_chunk_size(active, capacity, max_ic)
initial_chunk_size = _ic_cache[request_id]
if chunk_size <= 0 or left_context_size_config < 0 or initial_chunk_size < 0:
if chunk_size <= 0 or left_context_size_config < 0 or configured_initial_chunk_size < 0 or initial_chunk_size < 0:
raise ValueError(
f"Invalid codec chunk config: codec_chunk_frames={chunk_size}, "
f"codec_left_context_frames={left_context_size_config}, "
@@ -214,21 +215,16 @@ def talker2code2wav_async_chunk(
}
return None
in_initial_phase = initial_chunk_size > 0 and initial_chunk_size < chunk_size and length <= chunk_size
use_first_chunk = initial_chunk_size > 0 and initial_chunk_size < chunk_size
if in_initial_phase:
# IC phase: emit every initial_chunk_size frames with growing left context.
if not finished and length % initial_chunk_size != 0:
if use_first_chunk and length <= initial_chunk_size:
if not finished and length < initial_chunk_size:
return None
context_length = (
length % initial_chunk_size if (finished and length % initial_chunk_size != 0) else initial_chunk_size
)
context_length = length if finished and length < initial_chunk_size else initial_chunk_size
else:
# Normal phase: offset so the first normal emit picks up after IC phase.
# IC is stateless (may change with load); any mismatch is absorbed by left_context.
initial_coverage = (
(chunk_size // initial_chunk_size) * initial_chunk_size if 0 < initial_chunk_size < chunk_size else 0
)
# The initial chunk is only for TTFA. After that, return to the normal
# codec chunk size so Code2Wav is not flooded by repeated tiny windows.
initial_coverage = initial_chunk_size if use_first_chunk else 0
adjusted = length - initial_coverage
if not finished and adjusted % chunk_size != 0:
return None