[Feature] Add precomputed custom voices and Qwen3-TTS ref-context cache (#3492)
Signed-off-by: Sy03 <1370724210@qq.com> Signed-off-by: Yueqian Lin <linyueqian@outlook.com> Co-authored-by: Dan250124 <416947747@qq.com> Co-authored-by: Yueqian Lin <linyueqian@outlook.com> Co-authored-by: Yueqian Lin <70319226+linyueqian@users.noreply.github.com>
This commit is contained in:
23 files changed
+2362
-145
No files matched your search
@@ -8,6 +8,7 @@ import os
|
||||
import re
|
||||
import struct
|
||||
import time
|
||||
from collections import OrderedDict
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from http import HTTPStatus
|
||||
from pathlib import Path
|
||||
@@ -55,7 +56,11 @@ from vllm_omni.model_executor.models.ming_flash_omni.prompt_utils import (
|
||||
create_instruction as ming_create_instruction,
|
||||
)
|
||||
from vllm_omni.outputs import OmniRequestOutput
|
||||
from vllm_omni.utils.speaker_cache import get_speaker_cache
|
||||
from vllm_omni.utils.speaker_cache import (
|
||||
get_speaker_cache,
|
||||
iter_custom_voice_profiles,
|
||||
load_validated_profile_tensors,
|
||||
)
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
@@ -107,6 +112,9 @@ _TTS_LANGUAGES: set[str] = {
|
||||
}
|
||||
_REF_AUDIO_MIN_DURATION = 1.0 # seconds
|
||||
_REF_AUDIO_MAX_DURATION = 30.0 # seconds
|
||||
_REF_AUDIO_RESOLVE_CACHE_MAX_ENTRIES = 256
|
||||
_REF_AUDIO_RESOLVE_CACHE_MAX_BYTES = 256 * 1024 * 1024
|
||||
_QWEN3_TTS_REF_AUDIO_CACHE_KEY = "_qwen3_tts_ref_audio_cache_key"
|
||||
_TTS_MAX_INSTRUCTIONS_LENGTH = 500
|
||||
_TTS_MAX_NEW_TOKENS_MIN = 1
|
||||
_TTS_MAX_NEW_TOKENS_MAX = 4096
|
||||
@@ -261,8 +269,15 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
|
||||
logger.warning("Invalid SPEAKER_MAX_UPLOADED=%r; using default 1000", _raw_cap)
|
||||
self._max_uploaded_speakers = 1000
|
||||
self.uploaded_speakers: dict[str, dict] = {}
|
||||
self.precomputed_speakers: dict[str, dict[str, Any]] = {}
|
||||
self.supported_speakers: set[str] = set()
|
||||
self._ref_audio_data_url_cache: dict[str, str] = {}
|
||||
self._ref_audio_resolve_cache: OrderedDict[str, tuple[list[float], int, int, str]] = OrderedDict()
|
||||
self._ref_audio_resolve_cache_bytes = 0
|
||||
self._ref_audio_resolve_cache_max_entries = _REF_AUDIO_RESOLVE_CACHE_MAX_ENTRIES
|
||||
self._ref_audio_resolve_cache_max_bytes = _REF_AUDIO_RESOLVE_CACHE_MAX_BYTES
|
||||
self._ref_audio_model_artifact_ready: set[str] = set()
|
||||
self._request_ref_audio_artifact_keys: dict[str, str] = {}
|
||||
self._speaker_cache = get_speaker_cache()
|
||||
self._last_upload_ts = 0
|
||||
self._upload_lock = asyncio.Lock()
|
||||
@@ -395,6 +410,7 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
|
||||
)
|
||||
# Determine TTS model type or None
|
||||
self._tts_model_type = self._detect_tts_model_type()
|
||||
self.precomputed_speakers = self._load_precomputed_speakers()
|
||||
|
||||
# GLM-TTS lazy-cached resources (populated on first GLM-TTS request)
|
||||
self._glm_tts_text_tokenizer: object | None = None
|
||||
@@ -405,6 +421,7 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
|
||||
|
||||
# Merge built-in speakers into the set initialized by _init_speaker_storage.
|
||||
self.supported_speakers |= self._load_supported_speakers()
|
||||
self.supported_speakers |= set(self.precomputed_speakers)
|
||||
self._tts_tokenizer = None
|
||||
self._voxcpm2_tokenizer = None
|
||||
self._voxcpm2_split_map: dict[int, list[int]] = {}
|
||||
@@ -570,6 +587,45 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
|
||||
return "glm_tts"
|
||||
return None
|
||||
|
||||
def _get_custom_voice_dir(self) -> str | None:
|
||||
try:
|
||||
value = getattr(self.engine_client.model_config.hf_config, "custom_voice_dir", None)
|
||||
except AttributeError:
|
||||
return None
|
||||
if isinstance(value, os.PathLike):
|
||||
return os.fspath(value)
|
||||
if isinstance(value, str) and value:
|
||||
return value
|
||||
return None
|
||||
|
||||
def _load_precomputed_speakers(self) -> dict[str, dict[str, Any]]:
|
||||
"""Load precomputed voice names from ``custom_voice_dir`` for API validation."""
|
||||
if self._tts_model_type not in ("qwen3_tts", "voxcpm2"):
|
||||
return {}
|
||||
custom_voice_dir = self._get_custom_voice_dir()
|
||||
if not custom_voice_dir:
|
||||
return {}
|
||||
|
||||
profiles: dict[str, dict[str, Any]] = {}
|
||||
qwen3_embedding_dim = self._get_qwen_tts_expected_speaker_embedding_dim()
|
||||
for profile in iter_custom_voice_profiles(custom_voice_dir, expected_model_type=self._tts_model_type):
|
||||
tensors = load_validated_profile_tensors(
|
||||
profile,
|
||||
expected_model_type=self._tts_model_type,
|
||||
qwen3_embedding_dim=qwen3_embedding_dim,
|
||||
)
|
||||
if tensors is None:
|
||||
continue
|
||||
profiles[profile["voice_name_lower"]] = profile
|
||||
if profiles:
|
||||
logger.info(
|
||||
"Loaded %d precomputed %s voice profile(s) from %s",
|
||||
len(profiles),
|
||||
self._tts_model_type,
|
||||
custom_voice_dir,
|
||||
)
|
||||
return profiles
|
||||
|
||||
def _compute_max_instructions_length(self) -> int:
|
||||
"""Compute max instructions length with precedence: CLI > stage config > default.
|
||||
|
||||
@@ -748,11 +804,14 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
|
||||
self._voxcpm2_encode("") # lazy-init tokenizer + split_map
|
||||
ref_audio = None
|
||||
ref_sr = None
|
||||
voice_profile = None
|
||||
if request.ref_audio is not None:
|
||||
ref_audio, ref_sr = await self._resolve_ref_audio(request.ref_audio)
|
||||
elif uploaded_ref is not None:
|
||||
wav_np, ref_sr = uploaded_ref
|
||||
ref_audio = wav_np.tolist()
|
||||
elif request.voice is not None:
|
||||
voice_profile = self.precomputed_speakers.get(request.voice.lower())
|
||||
return build_voxcpm2_prompt(
|
||||
hf_config=self.engine_client.model_config.hf_config,
|
||||
tokenizer=self._voxcpm2_tokenizer,
|
||||
@@ -761,6 +820,7 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
|
||||
ref_audio=ref_audio,
|
||||
ref_sr=ref_sr,
|
||||
ref_text=request.ref_text,
|
||||
voice_profile=voice_profile,
|
||||
)
|
||||
|
||||
def _load_uploaded_audio(self, voice_name: str) -> tuple[np.ndarray, int] | None:
|
||||
@@ -1205,12 +1265,7 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
|
||||
if self._tts_model_type == "cosyvoice3":
|
||||
return self._validate_cosyvoice3_request(request)
|
||||
if self._tts_model_type == "voxcpm2":
|
||||
if request.max_new_tokens is not None:
|
||||
if request.max_new_tokens < _TTS_MAX_NEW_TOKENS_MIN:
|
||||
return f"max_new_tokens must be at least {_TTS_MAX_NEW_TOKENS_MIN}"
|
||||
if request.max_new_tokens > _TTS_MAX_NEW_TOKENS_MAX:
|
||||
return f"max_new_tokens cannot exceed {_TTS_MAX_NEW_TOKENS_MAX}"
|
||||
return None # VoxCPM2 accepts any text input
|
||||
return self._validate_voxcpm2_request(request)
|
||||
if self._tts_model_type == "ming_flash_omni_tts":
|
||||
return self._validate_ming_tts_request(request)
|
||||
if self._tts_model_type == "moss_tts_nano":
|
||||
@@ -1219,6 +1274,26 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
|
||||
return self._validate_glm_tts_request(request)
|
||||
return self._validate_qwen_tts_request(request)
|
||||
|
||||
def _validate_voxcpm2_request(self, request: OpenAICreateSpeechRequest) -> str | None:
|
||||
"""Validate VoxCPM2 request parameters. Returns error message or None."""
|
||||
if not request.input or not request.input.strip():
|
||||
return "Input text cannot be empty"
|
||||
|
||||
if request.voice is not None:
|
||||
request.voice = request.voice.lower()
|
||||
available_voices = set(self.uploaded_speakers) | set(self.precomputed_speakers) | {"default"}
|
||||
if request.voice not in available_voices:
|
||||
supported = ", ".join(sorted(available_voices)) or "none"
|
||||
return f"Invalid voice '{request.voice}'. Supported: {supported}"
|
||||
|
||||
if request.max_new_tokens is not None:
|
||||
if request.max_new_tokens < _TTS_MAX_NEW_TOKENS_MIN:
|
||||
return f"max_new_tokens must be at least {_TTS_MAX_NEW_TOKENS_MIN}"
|
||||
if request.max_new_tokens > _TTS_MAX_NEW_TOKENS_MAX:
|
||||
return f"max_new_tokens cannot exceed {_TTS_MAX_NEW_TOKENS_MAX}"
|
||||
|
||||
return None
|
||||
|
||||
def _voxcpm2_encode(self, text: str) -> list[int]:
|
||||
"""Tokenize text for VoxCPM2, splitting multichar Chinese tokens."""
|
||||
from vllm_omni.model_executor.models.voxcpm2.voxcpm2_talker import (
|
||||
@@ -1315,11 +1390,13 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
|
||||
# Infer Base task when ref_audio or ref_text is provided without explicit task_type.
|
||||
if request.task_type is None and (request.ref_audio is not None or request.ref_text is not None):
|
||||
request.task_type = "Base"
|
||||
task_type = request.task_type or "CustomVoice"
|
||||
|
||||
# Normalize voice to lowercase for case-insensitive matching
|
||||
if request.voice is not None:
|
||||
request.voice = request.voice.lower()
|
||||
if request.task_type is None and request.voice in self.precomputed_speakers:
|
||||
request.task_type = "Base"
|
||||
task_type = request.task_type or "CustomVoice"
|
||||
|
||||
# Validate input is not empty
|
||||
if not request.input or not request.input.strip():
|
||||
@@ -1379,6 +1456,15 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
|
||||
file_path = Path(speaker_info["file_path"])
|
||||
if not file_path.exists():
|
||||
return f"Data file for uploaded speaker '{request.voice}' not found on disk"
|
||||
elif voice_lower in self.precomputed_speakers:
|
||||
profile = self.precomputed_speakers[voice_lower]
|
||||
mode = str(profile.get("mode") or "xvec").lower()
|
||||
ref_text = request.ref_text or profile.get("ref_text")
|
||||
if mode == "icl" and (not isinstance(ref_text, str) or not ref_text.strip()):
|
||||
return (
|
||||
f"Precomputed voice '{request.voice}' uses ICL mode but has no ref_text in "
|
||||
"the request or custom voice manifest"
|
||||
)
|
||||
else:
|
||||
# need ref_audio for built-in speaker
|
||||
if request.ref_audio is None:
|
||||
@@ -1677,6 +1763,19 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
|
||||
URLs, ``data:`` base64 URIs, and ``file:`` local paths (the latter
|
||||
gated by ``--allowed-local-media-path``).
|
||||
"""
|
||||
cache_key = hashlib.sha1(ref_audio_str.encode("utf-8")).hexdigest()
|
||||
cached = self._ref_audio_resolve_cache.get(cache_key)
|
||||
if cached is not None:
|
||||
self._ref_audio_resolve_cache.move_to_end(cache_key)
|
||||
wav_list, sr, _, _ = cached
|
||||
logger.debug(
|
||||
"Resolved ref_audio from cache: samples=%d sr=%d duration_s=%.3f",
|
||||
len(wav_list),
|
||||
sr,
|
||||
len(wav_list) / sr if sr > 0 else 0.0,
|
||||
)
|
||||
return wav_list, sr
|
||||
|
||||
# In diffusion mode, model_config may not be available
|
||||
if self._diffusion_mode:
|
||||
connector = MediaConnector()
|
||||
@@ -1686,11 +1785,14 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
|
||||
allowed_local_media_path=model_config.allowed_local_media_path,
|
||||
allowed_media_domains=model_config.allowed_media_domains,
|
||||
)
|
||||
fetch_start_s = time.perf_counter()
|
||||
wav_np, sr = await connector.fetch_audio_async(ref_audio_str)
|
||||
fetch_decode_ms = (time.perf_counter() - fetch_start_s) * 1000.0
|
||||
wav_np = np.asarray(wav_np, dtype=np.float32)
|
||||
if wav_np.ndim > 1:
|
||||
wav_np = np.mean(wav_np, axis=-1)
|
||||
sr = int(sr)
|
||||
artifact_key = self._make_ref_audio_artifact_cache_key(wav_np, sr)
|
||||
duration = len(wav_np) / sr if sr > 0 else 0.0
|
||||
if duration < _REF_AUDIO_MIN_DURATION:
|
||||
raise ValueError(
|
||||
@@ -1702,7 +1804,83 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
|
||||
f"Reference audio too long ({duration:.1f}s). "
|
||||
f"Maximum {_REF_AUDIO_MAX_DURATION:.0f}s supported — use a shorter clip."
|
||||
)
|
||||
return wav_np.tolist(), sr
|
||||
tolist_start_s = time.perf_counter()
|
||||
wav_list = wav_np.tolist()
|
||||
tolist_ms = (time.perf_counter() - tolist_start_s) * 1000.0
|
||||
logger.debug(
|
||||
"Resolved ref_audio: fetch_decode_ms=%.3f tolist_ms=%.3f samples=%d sr=%d duration_s=%.3f",
|
||||
fetch_decode_ms,
|
||||
tolist_ms,
|
||||
len(wav_np),
|
||||
sr,
|
||||
duration,
|
||||
)
|
||||
self._put_resolved_ref_audio(cache_key, wav_list, sr, artifact_key)
|
||||
return wav_list, sr
|
||||
|
||||
@staticmethod
|
||||
def _make_ref_audio_artifact_cache_key(wav: np.ndarray, sr: int) -> str:
|
||||
wav_f32 = wav.astype(np.float32, copy=False).reshape(-1)
|
||||
h = hashlib.sha1()
|
||||
h.update(int(sr).to_bytes(4, byteorder="little", signed=False))
|
||||
h.update(int(wav_f32.size).to_bytes(8, byteorder="little", signed=False))
|
||||
h.update(wav_f32.tobytes(order="C"))
|
||||
return h.hexdigest()
|
||||
|
||||
def _get_resolved_ref_audio_artifact_key(self, ref_audio_str: str) -> str | None:
|
||||
source_key = hashlib.sha1(ref_audio_str.encode("utf-8")).hexdigest()
|
||||
cached = self._ref_audio_resolve_cache.get(source_key)
|
||||
if cached is None:
|
||||
return None
|
||||
self._ref_audio_resolve_cache.move_to_end(source_key)
|
||||
return cached[3]
|
||||
|
||||
def _put_resolved_ref_audio(self, cache_key: str, wav_list: list[float], sr: int, artifact_key: str) -> None:
|
||||
if self._ref_audio_resolve_cache_max_entries <= 0 or self._ref_audio_resolve_cache_max_bytes <= 0:
|
||||
return
|
||||
# Approximate list[float] storage. CPython float objects add per-element
|
||||
# overhead, so max_entries remains the hard cache cap.
|
||||
size = len(wav_list) * 40
|
||||
if size > self._ref_audio_resolve_cache_max_bytes:
|
||||
return
|
||||
previous = self._ref_audio_resolve_cache.pop(cache_key, None)
|
||||
if previous is not None:
|
||||
self._ref_audio_resolve_cache_bytes -= previous[2]
|
||||
if previous[3] != artifact_key:
|
||||
self._discard_ref_audio_artifact_ready_if_unreferenced(previous[3])
|
||||
self._ref_audio_resolve_cache[cache_key] = (wav_list, int(sr), size, artifact_key)
|
||||
self._ref_audio_resolve_cache_bytes += size
|
||||
while len(self._ref_audio_resolve_cache) > self._ref_audio_resolve_cache_max_entries:
|
||||
_, (_, _, old_size, old_artifact_key) = self._ref_audio_resolve_cache.popitem(last=False)
|
||||
self._ref_audio_resolve_cache_bytes -= old_size
|
||||
self._discard_ref_audio_artifact_ready_if_unreferenced(old_artifact_key)
|
||||
while self._ref_audio_resolve_cache_bytes > self._ref_audio_resolve_cache_max_bytes:
|
||||
_, (_, _, old_size, old_artifact_key) = self._ref_audio_resolve_cache.popitem(last=False)
|
||||
self._ref_audio_resolve_cache_bytes -= old_size
|
||||
self._discard_ref_audio_artifact_ready_if_unreferenced(old_artifact_key)
|
||||
|
||||
def _discard_ref_audio_artifact_ready_if_unreferenced(self, artifact_key: str) -> None:
|
||||
if artifact_key and all(entry[3] != artifact_key for entry in self._ref_audio_resolve_cache.values()):
|
||||
self._ref_audio_model_artifact_ready.discard(artifact_key)
|
||||
|
||||
def _qwen3_tts_can_use_ref_audio_artifact_only(self, tts_params: dict[str, Any], artifact_key: str | None) -> bool:
|
||||
if self._tts_model_type != "qwen3_tts":
|
||||
return False
|
||||
if not artifact_key or artifact_key not in self._ref_audio_model_artifact_ready:
|
||||
return False
|
||||
return (tts_params.get("task_type") or ["CustomVoice"])[0] == "Base"
|
||||
|
||||
def _track_ref_audio_artifact_warmup(self, request_id: str, artifact_key: str | None) -> None:
|
||||
if artifact_key:
|
||||
self._request_ref_audio_artifact_keys[request_id] = artifact_key
|
||||
|
||||
def _mark_ref_audio_artifact_ready_for_request(self, request_id: str) -> None:
|
||||
artifact_key = self._request_ref_audio_artifact_keys.pop(request_id, None)
|
||||
if artifact_key and any(entry[3] == artifact_key for entry in self._ref_audio_resolve_cache.values()):
|
||||
self._ref_audio_model_artifact_ready.add(artifact_key)
|
||||
|
||||
def _discard_ref_audio_artifact_warmup(self, request_id: str) -> None:
|
||||
self._request_ref_audio_artifact_keys.pop(request_id, None)
|
||||
|
||||
async def _generate_audio_chunks(
|
||||
self,
|
||||
@@ -1733,6 +1911,7 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
|
||||
first_chunk = True
|
||||
first_audio_chunk_s: float | None = None
|
||||
stream_start_s = request_start_s if request_start_s is not None else time.perf_counter()
|
||||
artifact_ready = False
|
||||
|
||||
try:
|
||||
async for res in generator:
|
||||
@@ -1787,6 +1966,8 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
|
||||
if first_audio_chunk_s is None:
|
||||
first_audio_chunk_s = time.perf_counter()
|
||||
yield self.create_audio(audio_obj).audio_data
|
||||
self._mark_ref_audio_artifact_ready_for_request(request_id)
|
||||
artifact_ready = True
|
||||
total_ms = (time.perf_counter() - stream_start_s) * 1000.0
|
||||
if first_audio_chunk_s is not None:
|
||||
first_chunk_ms = (first_audio_chunk_s - stream_start_s) * 1000.0
|
||||
@@ -1840,6 +2021,9 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
|
||||
)
|
||||
logger.exception("Streaming speech generation failed for %s: %s", request_id, e)
|
||||
raise
|
||||
finally:
|
||||
if not artifact_ready:
|
||||
self._discard_ref_audio_artifact_warmup(request_id)
|
||||
|
||||
@staticmethod
|
||||
def _extract_audio_output(res) -> tuple[dict | None, str | None]:
|
||||
@@ -1893,13 +2077,14 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
|
||||
|
||||
# Speaker (voice)
|
||||
if request.voice is not None:
|
||||
voice_lower = request.voice.lower()
|
||||
params["speaker"] = [request.voice]
|
||||
params["voice_created_at"] = [self._voice_created_at(request.voice.lower())]
|
||||
params["voice_created_at"] = [self._voice_created_at(voice_lower)]
|
||||
|
||||
# Uploaded voices use task_type="Base" (CustomVoice requires built-in spk_id).
|
||||
# If ref_text was provided at upload time, use in-context cloning; otherwise x_vector only.
|
||||
if request.voice.lower() in self.uploaded_speakers and request.ref_audio is None:
|
||||
speaker_info = self.uploaded_speakers[request.voice.lower()]
|
||||
if voice_lower in self.uploaded_speakers and request.ref_audio is None:
|
||||
speaker_info = self.uploaded_speakers[voice_lower]
|
||||
|
||||
# Check if this voice was uploaded with a pre-computed embedding.
|
||||
# Populate request.speaker_embedding so the existing code path
|
||||
@@ -1907,6 +2092,7 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
|
||||
embedding = self._get_uploaded_speaker_embedding(request.voice)
|
||||
if embedding is not None:
|
||||
request.speaker_embedding = embedding
|
||||
params["speaker"] = [voice_lower]
|
||||
params["task_type"] = ["Base"]
|
||||
logger.info("Auto-set speaker_embedding for uploaded voice: %s", request.voice)
|
||||
else:
|
||||
@@ -1914,6 +2100,7 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
|
||||
if not audio_data:
|
||||
raise ValueError(f"Audio file for uploaded voice '{request.voice}' is missing or corrupted")
|
||||
stored_ref_text = speaker_info.get("ref_text")
|
||||
params["speaker"] = [voice_lower]
|
||||
params["ref_audio"] = [audio_data]
|
||||
params["task_type"] = ["Base"]
|
||||
if stored_ref_text:
|
||||
@@ -1924,6 +2111,19 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
|
||||
logger.info(
|
||||
"Auto-set ref_audio for uploaded voice: %s (icl=%s)", request.voice, bool(stored_ref_text)
|
||||
)
|
||||
elif voice_lower in self.precomputed_speakers and request.ref_audio is None:
|
||||
profile = self.precomputed_speakers[voice_lower]
|
||||
mode = str(profile.get("mode") or "xvec").lower()
|
||||
params["speaker"] = [voice_lower]
|
||||
params["task_type"] = ["Base"]
|
||||
params["x_vector_only_mode"] = [mode != "icl"]
|
||||
ref_text = request.ref_text or profile.get("ref_text")
|
||||
if isinstance(ref_text, str) and ref_text.strip():
|
||||
params["ref_text"] = [ref_text]
|
||||
ref_code_length = profile.get("ref_code_length")
|
||||
if mode == "icl" and ref_code_length:
|
||||
params["ref_code_length"] = [int(ref_code_length)]
|
||||
logger.info("Using precomputed Qwen3-TTS custom voice profile: %s (mode=%s)", voice_lower, mode)
|
||||
|
||||
elif params["task_type"][0] == "CustomVoice":
|
||||
params["speaker"] = ["Vivian"] # Default for CustomVoice
|
||||
@@ -2334,6 +2534,9 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
|
||||
if self.engine_client.errored:
|
||||
raise self.engine_client.dead_error
|
||||
|
||||
request_id = request_id or f"speech-{random_uuid()}"
|
||||
qwen3_ref_audio_warmup_artifact_key: str | None = None
|
||||
|
||||
# If this is a streaming request, we need to coerce
|
||||
# cumulative outputs to delta outputs; this ensures
|
||||
# we don't emit redundant MM data & drain after emitting.
|
||||
@@ -2412,12 +2615,13 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
|
||||
elif self._tts_model_type == "voxcpm2":
|
||||
# voxcpm2 doesn't use `_apply_uploaded_speaker` because the prompt builder needs the
|
||||
# raw waveform tuple for prefill-length accounting, not a base64 data URL.
|
||||
validation_error = self._validate_voxcpm2_request(request)
|
||||
if validation_error:
|
||||
raise ValueError(validation_error)
|
||||
|
||||
uploaded_ref: tuple[np.ndarray, int] | None = None
|
||||
if request.voice:
|
||||
voice_lower = request.voice.lower()
|
||||
if voice_lower not in self.uploaded_speakers and voice_lower not in self.supported_speakers:
|
||||
all_voices = sorted(self.uploaded_speakers.keys() | self.supported_speakers)
|
||||
raise ValueError(f"Invalid voice '{request.voice}'. Supported: {', '.join(all_voices) or 'none'}")
|
||||
if voice_lower in self.uploaded_speakers and not has_inline_ref_audio:
|
||||
if self.uploaded_speakers[voice_lower].get("embedding_source") == "direct":
|
||||
raise ValueError(
|
||||
@@ -2430,7 +2634,7 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
|
||||
tts_params = {}
|
||||
if request.voice:
|
||||
voice_lower = request.voice.lower()
|
||||
if voice_lower in self.uploaded_speakers and not has_inline_ref_audio:
|
||||
if voice_lower in self.uploaded_speakers or voice_lower in self.precomputed_speakers:
|
||||
additional = prompt.setdefault("additional_information", {})
|
||||
additional["voice_name"] = voice_lower
|
||||
additional["voice_created_at"] = self._voice_created_at(voice_lower)
|
||||
@@ -2478,7 +2682,18 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
|
||||
ref_audio_source = tts_params["ref_audio"][0]
|
||||
if ref_audio_source is not None and isinstance(ref_audio_source, str):
|
||||
wav_list, sr = await self._resolve_ref_audio(ref_audio_source)
|
||||
tts_params["ref_audio"] = [[wav_list, sr]]
|
||||
artifact_key = self._get_resolved_ref_audio_artifact_key(ref_audio_source)
|
||||
if self._tts_model_type == "qwen3_tts" and artifact_key:
|
||||
tts_params[_QWEN3_TTS_REF_AUDIO_CACHE_KEY] = [artifact_key]
|
||||
ref_code_length = self._estimate_ref_code_len([wav_list, sr])
|
||||
if self._tts_model_type == "qwen3_tts" and ref_code_length is not None:
|
||||
tts_params["ref_code_length"] = [int(ref_code_length)]
|
||||
if self._qwen3_tts_can_use_ref_audio_artifact_only(tts_params, artifact_key):
|
||||
logger.debug("Using Qwen3-TTS ref_audio artifact-only path: %s", artifact_key)
|
||||
else:
|
||||
tts_params["ref_audio"] = [[wav_list, sr]]
|
||||
if self._tts_model_type == "qwen3_tts":
|
||||
qwen3_ref_audio_warmup_artifact_key = artifact_key
|
||||
|
||||
ph_len = await self._estimate_prompt_len_async(tts_params)
|
||||
prompt = tokens_input(prompt_token_ids=[1] * ph_len)
|
||||
@@ -2506,7 +2721,6 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
|
||||
tts_params = {}
|
||||
prompt = {"prompt": request.input}
|
||||
|
||||
request_id = request_id or f"speech-{random_uuid()}"
|
||||
if self._is_fish_speech:
|
||||
model_type = "fish_speech"
|
||||
elif self._tts_model_type == "covo_audio":
|
||||
@@ -2643,6 +2857,7 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
|
||||
sampling_params_list=sampling_params_list,
|
||||
output_modalities=["audio"],
|
||||
)
|
||||
self._track_ref_audio_artifact_warmup(request_id, qwen3_ref_audio_warmup_artifact_key)
|
||||
return request_id, generator, tts_params
|
||||
|
||||
async def _generate_pcm_chunks(self, generator, request_id: str):
|
||||
@@ -2657,8 +2872,11 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
|
||||
async def _iter_pcm_audio_bytes(self, request: OpenAICreateSpeechRequest):
|
||||
"""Yield raw PCM bytes for a speech request as soon as chunks are decoded."""
|
||||
request_id, generator, _ = await self._prepare_speech_generation(request)
|
||||
async for chunk in self._generate_pcm_chunks(generator, request_id):
|
||||
yield chunk
|
||||
try:
|
||||
async for chunk in self._generate_pcm_chunks(generator, request_id):
|
||||
yield chunk
|
||||
finally:
|
||||
self._discard_ref_audio_artifact_warmup(request_id)
|
||||
|
||||
async def _generate_audio_bytes(
|
||||
self,
|
||||
@@ -2667,101 +2885,108 @@ class OmniOpenAIServingSpeech(OpenAIServing, AudioMixin):
|
||||
request_id: str | None = None,
|
||||
) -> tuple[bytes | str, str]:
|
||||
request_id, generator, _ = await self._prepare_speech_generation(request, request_id=request_id)
|
||||
artifact_ready = False
|
||||
|
||||
# MOSS-TTS-Nano emits delta chunks per yield (single-stage,
|
||||
# async_chunk=false). The engine surfaces each yield as its own
|
||||
# RequestOutput, so we need to accumulate across the async-for loop —
|
||||
# final_output alone only carries the last (often empty) sentinel.
|
||||
is_moss = self._tts_model_type == "moss_tts_nano"
|
||||
moss_chunks: list[Any] = []
|
||||
moss_sample_rate: int | None = None
|
||||
try:
|
||||
# MOSS-TTS-Nano emits delta chunks per yield (single-stage,
|
||||
# async_chunk=false). The engine surfaces each yield as its own
|
||||
# RequestOutput, so we need to accumulate across the async-for loop —
|
||||
# final_output alone only carries the last (often empty) sentinel.
|
||||
is_moss = self._tts_model_type == "moss_tts_nano"
|
||||
moss_chunks: list[Any] = []
|
||||
moss_sample_rate: int | None = None
|
||||
|
||||
final_output: OmniRequestOutput | None = None
|
||||
async for res in generator:
|
||||
final_output = res
|
||||
if not is_moss:
|
||||
continue
|
||||
try:
|
||||
step_audio, step_key = self._extract_audio_output(res)
|
||||
except Exception:
|
||||
continue
|
||||
if step_key is None:
|
||||
continue
|
||||
chunk = step_audio[step_key]
|
||||
candidates = chunk if isinstance(chunk, list) else [chunk]
|
||||
for cand in candidates:
|
||||
if hasattr(cand, "numel") and cand.numel() > 0:
|
||||
moss_chunks.append(cand)
|
||||
sr_step = step_audio.get("sr")
|
||||
if sr_step is not None:
|
||||
sr_val_step = sr_step[-1] if isinstance(sr_step, list) and sr_step else sr_step
|
||||
moss_sample_rate = int(sr_val_step.item()) if hasattr(sr_val_step, "item") else int(sr_val_step)
|
||||
final_output: OmniRequestOutput | None = None
|
||||
async for res in generator:
|
||||
final_output = res
|
||||
if not is_moss:
|
||||
continue
|
||||
try:
|
||||
step_audio, step_key = self._extract_audio_output(res)
|
||||
except Exception:
|
||||
continue
|
||||
if step_key is None:
|
||||
continue
|
||||
chunk = step_audio[step_key]
|
||||
candidates = chunk if isinstance(chunk, list) else [chunk]
|
||||
for cand in candidates:
|
||||
if hasattr(cand, "numel") and cand.numel() > 0:
|
||||
moss_chunks.append(cand)
|
||||
sr_step = step_audio.get("sr")
|
||||
if sr_step is not None:
|
||||
sr_val_step = sr_step[-1] if isinstance(sr_step, list) and sr_step else sr_step
|
||||
moss_sample_rate = int(sr_val_step.item()) if hasattr(sr_val_step, "item") else int(sr_val_step)
|
||||
|
||||
if final_output is None:
|
||||
raise ValueError("No output generated from the model.")
|
||||
if final_output is None:
|
||||
raise ValueError("No output generated from the model.")
|
||||
|
||||
audio_output, audio_key = self._extract_audio_output(final_output)
|
||||
if audio_key is None:
|
||||
raise ValueError("TTS model did not produce audio output.")
|
||||
audio_output, audio_key = self._extract_audio_output(final_output)
|
||||
if audio_key is None:
|
||||
raise ValueError("TTS model did not produce audio output.")
|
||||
|
||||
audio_tensor = audio_output[audio_key]
|
||||
sr_raw = audio_output.get("sr", 24000)
|
||||
sr_val = sr_raw[-1] if isinstance(sr_raw, list) and sr_raw else sr_raw
|
||||
sample_rate = sr_val.item() if hasattr(sr_val, "item") else int(sr_val)
|
||||
audio_tensor = audio_output[audio_key]
|
||||
sr_raw = audio_output.get("sr", 24000)
|
||||
sr_val = sr_raw[-1] if isinstance(sr_raw, list) and sr_raw else sr_raw
|
||||
sample_rate = sr_val.item() if hasattr(sr_val, "item") else int(sr_val)
|
||||
|
||||
if is_moss:
|
||||
# Prefer the engine's own consolidated audio when present. After the
|
||||
# vllm 0.20 rebase non-stream requests resolve to FINAL_ONLY, so
|
||||
# final_output already carries the full concatenated waveform; the
|
||||
# delta-accumulator below is kept as a fallback for DELTA-style
|
||||
# engines that surface chunks one yield at a time.
|
||||
if isinstance(audio_tensor, list):
|
||||
non_empty_final = [c for c in audio_tensor if hasattr(c, "numel") and c.numel() > 0]
|
||||
final_audio = torch.cat(non_empty_final, dim=-1) if non_empty_final else None
|
||||
elif hasattr(audio_tensor, "numel") and audio_tensor.numel() > 0:
|
||||
final_audio = audio_tensor
|
||||
else:
|
||||
final_audio = None
|
||||
if is_moss:
|
||||
# Prefer the engine's own consolidated audio when present. After the
|
||||
# vllm 0.20 rebase non-stream requests resolve to FINAL_ONLY, so
|
||||
# final_output already carries the full concatenated waveform; the
|
||||
# delta-accumulator below is kept as a fallback for DELTA-style
|
||||
# engines that surface chunks one yield at a time.
|
||||
if isinstance(audio_tensor, list):
|
||||
non_empty_final = [c for c in audio_tensor if hasattr(c, "numel") and c.numel() > 0]
|
||||
final_audio = torch.cat(non_empty_final, dim=-1) if non_empty_final else None
|
||||
elif hasattr(audio_tensor, "numel") and audio_tensor.numel() > 0:
|
||||
final_audio = audio_tensor
|
||||
else:
|
||||
final_audio = None
|
||||
|
||||
if final_audio is not None:
|
||||
audio_tensor = final_audio
|
||||
elif moss_chunks:
|
||||
audio_tensor = torch.cat(moss_chunks, dim=-1)
|
||||
else:
|
||||
audio_tensor = np.zeros((0,), dtype=np.float32)
|
||||
if moss_sample_rate is not None:
|
||||
sample_rate = moss_sample_rate
|
||||
elif isinstance(audio_tensor, list):
|
||||
async_chunk = bool(getattr(self.engine_client.model_config, "async_chunk", False))
|
||||
if async_chunk:
|
||||
non_empty_chunks = [candidate for candidate in audio_tensor if candidate.numel() > 0]
|
||||
audio_tensor = (
|
||||
torch.cat(non_empty_chunks, dim=-1) if non_empty_chunks else np.zeros((0,), dtype=np.float32)
|
||||
)
|
||||
else:
|
||||
audio_history = audio_tensor
|
||||
audio_tensor = np.zeros((0,), dtype=np.float32)
|
||||
# Non-async Qwen3-TTS returns cumulative history snapshots, so keep the latest non-empty tensor.
|
||||
for candidate in reversed(audio_history):
|
||||
if candidate.numel() > 0:
|
||||
audio_tensor = candidate
|
||||
break
|
||||
if hasattr(audio_tensor, "float"):
|
||||
audio_tensor = audio_tensor.float().detach().cpu().numpy()
|
||||
if final_audio is not None:
|
||||
audio_tensor = final_audio
|
||||
elif moss_chunks:
|
||||
audio_tensor = torch.cat(moss_chunks, dim=-1)
|
||||
else:
|
||||
audio_tensor = np.zeros((0,), dtype=np.float32)
|
||||
if moss_sample_rate is not None:
|
||||
sample_rate = moss_sample_rate
|
||||
elif isinstance(audio_tensor, list):
|
||||
async_chunk = bool(getattr(self.engine_client.model_config, "async_chunk", False))
|
||||
if async_chunk:
|
||||
non_empty_chunks = [candidate for candidate in audio_tensor if candidate.numel() > 0]
|
||||
audio_tensor = (
|
||||
torch.cat(non_empty_chunks, dim=-1) if non_empty_chunks else np.zeros((0,), dtype=np.float32)
|
||||
)
|
||||
else:
|
||||
audio_history = audio_tensor
|
||||
audio_tensor = np.zeros((0,), dtype=np.float32)
|
||||
# Non-async Qwen3-TTS returns cumulative history snapshots, so keep the latest non-empty tensor.
|
||||
for candidate in reversed(audio_history):
|
||||
if candidate.numel() > 0:
|
||||
audio_tensor = candidate
|
||||
break
|
||||
if hasattr(audio_tensor, "float"):
|
||||
audio_tensor = audio_tensor.float().detach().cpu().numpy()
|
||||
|
||||
if audio_tensor.ndim > 1:
|
||||
audio_tensor = audio_tensor.squeeze()
|
||||
if audio_tensor.ndim > 1:
|
||||
audio_tensor = audio_tensor.squeeze()
|
||||
|
||||
audio_obj = CreateAudio(
|
||||
audio_tensor=audio_tensor,
|
||||
sample_rate=sample_rate,
|
||||
response_format=request.response_format or "wav",
|
||||
speed=request.speed or 1.0,
|
||||
stream_format=request.stream_format,
|
||||
base64_encode=base64_encode,
|
||||
)
|
||||
audio_response: AudioResponse = self.create_audio(audio_obj)
|
||||
return audio_response.audio_data, audio_response.media_type
|
||||
audio_obj = CreateAudio(
|
||||
audio_tensor=audio_tensor,
|
||||
sample_rate=sample_rate,
|
||||
response_format=request.response_format or "wav",
|
||||
speed=request.speed or 1.0,
|
||||
stream_format=request.stream_format,
|
||||
base64_encode=base64_encode,
|
||||
)
|
||||
audio_response: AudioResponse = self.create_audio(audio_obj)
|
||||
self._mark_ref_audio_artifact_ready_for_request(request_id)
|
||||
artifact_ready = True
|
||||
return audio_response.audio_data, audio_response.media_type
|
||||
finally:
|
||||
if not artifact_ready:
|
||||
self._discard_ref_audio_artifact_warmup(request_id)
|
||||
|
||||
async def _create_diffusion_speech(
|
||||
self,
|
||||
|
||||
Reference in new issue
Block a user