[Feature] support to change the speaker of qwen3-omni (#1963)

Signed-off-by: Rein Yang <ruiruyang2@gmail.com>
This commit is contained in:
rein yang authored and GitHub committed 2026-03-24 13:53:24 +08:00
1 parent d16be833e1
commit 3cdf28828e
18 files changed
+486 -533

No files matched your search

@@ -697,7 +697,7 @@ class Qwen2_5OmniForConditionalGeneration(
# TODO(Peiqi): add voice_type support
req_input_ids, req_embeds = self._thinker_to_talker_prefill(
voice_type=self.voice_type,
speaker=self.voice_type,
output_prompt_embeds=thinker_result.to(input_embeds.dtype).to(self._module_device(self.model)),
output_token_ids=thinker_output_token_ids,
thinker_prompt_embeds=prompt_embeds.to(input_embeds.dtype).to(self._module_device(self.model)),
@@ -711,7 +711,7 @@ class Qwen2_5OmniForConditionalGeneration(
def _thinker_to_talker_prefill(
self,
voice_type: str,
speaker: str,
output_prompt_embeds,
output_token_ids,
thinker_prompt_embeds,
@@ -726,7 +726,7 @@ class Qwen2_5OmniForConditionalGeneration(
prompt_embeds = torch.cat(
[
thinker_prompt_embeds,
self._get_embed_text_spk_token(voice_type) + self.embed_codec_pad_token,
self._get_embed_text_spk_token(speaker) + self.embed_codec_pad_token,
output_prompt_embeds[:1] + self.embed_codec_bos_token,
],
dim=0,
@@ -322,10 +322,6 @@ class Qwen3OmniMoeForConditionalGeneration(
if inputs_embeds is None and input_ids is not None:
inputs_embeds = self.talker.embed_input_ids(input_ids)
# TODO(Peiqi): temporal hack here to support voice_type.
if not hasattr(self, "voice_type"):
self.voice_type = voice_type
# Run talker forward
with torch.inference_mode():
talker_hidden = self.talker.forward(
@@ -377,7 +373,7 @@ class Qwen3OmniMoeForConditionalGeneration(
left_context_size.append(info["left_context_size"])
else:
logger.debug("No additional_information provided to code2wav stage.")
audio_tensors = self.generate_audio(codes, voice_type, left_context_size, seq_token_counts)
audio_tensors = self.generate_audio(codes, left_context_size, seq_token_counts)
return audio_tensors
@@ -457,7 +453,6 @@ class Qwen3OmniMoeForConditionalGeneration(
def generate_audio(
self,
code: torch.Tensor,
voice_type: str,
left_context_size: list[int] | None = None,
seq_token_counts: list[int] | None = None,
) -> list[torch.Tensor]:
@@ -466,7 +461,6 @@ class Qwen3OmniMoeForConditionalGeneration(
Args:
code: [batch, num_quantizers, T] - RVQ codec codes
voice_type: Voice type (not used in Qwen3, kept for compatibility)
left_context_size: Left context size for streaming decode
seq_token_counts: Token count for each request in batch
@@ -678,8 +672,16 @@ class Qwen3OmniMoeForConditionalGeneration(
def talker_preprocess_prefill(self, input_ids: torch.Tensor, input_embeds: torch.Tensor, **info_dict: dict):
# Containers to return per-request updates (e.g., code_predictor_hidden_per_request)
update_dict: dict[str, dict] = {}
# TODO(Peiqi): add voice_type support
voice_type = self.voice_type
voice_type = info_dict.get("speaker")
logger.info("talker_preprocess_prefill speaker: %s", voice_type)
if voice_type is not None and isinstance(voice_type, (list, tuple)) and len(voice_type) > 0:
voice_type = voice_type[0]
if not isinstance(voice_type, str) or not voice_type.strip():
# Fall back to model default; speaker is per-request.
voice_type = self.default_tts_text_spk_type
else:
voice_type = str(voice_type).lower().strip()
start_index = info_dict.get("num_processed_tokens", 0)
end_index = start_index + input_embeds.shape[0]
# Read thinker outputs for prefill
@@ -700,7 +700,7 @@ class Qwen3TTSTalkerForConditionalGeneration(nn.Module):
info: dict[str, Any] = additional_information or {}
text = _first(info.get("text"), "")
language = _first(info.get("language"), "Auto")
speaker = _first(info.get("speaker"), "")
speaker = _first(info.get("speaker"), "").lower().strip()
instruct = _first(info.get("instruct"), "")
non_streaming_mode_raw = _first(info.get("non_streaming_mode"), None)
@@ -1438,11 +1438,14 @@ class Qwen3TTSTalkerForConditionalGeneration(nn.Module):
)
elif task_type == "CustomVoice":
speaker = (info_dict.get("speaker") or [""])[0]
if not isinstance(speaker, str) or not speaker.strip():
_speaker_raw = info_dict.get("speaker") or [""]
speaker = (
((_speaker_raw[0] if isinstance(_speaker_raw, (list, tuple)) else _speaker_raw) or "").lower().strip()
)
if not speaker:
raise ValueError("CustomVoice requires additional_information.speaker.")
spk_id_map = getattr(self.talker_config, "spk_id", None) or {}
if speaker.lower() not in spk_id_map:
if speaker not in spk_id_map:
raise ValueError(f"Unsupported speaker: {speaker}")
spk_id = spk_id_map[speaker.lower()]
# Keep it at least 1D; embedding on a 0-d tensor can return 1D.