[Feature] Support Qwen3 Omni talker cudagraph (#669)

Signed-off-by: ZeldaHuang <hzm414167@alibaba-inc.com>
This commit is contained in:
Ziming Huang authored and GitHub committed 2026-01-09 11:45:58 +08:00
1 parent 1467b3e2c7
commit 978e3ad7d4
10 files changed
+153 -80

No files matched your search

@@ -8,6 +8,13 @@ from functools import cached_property
import torch
import torch.nn as nn
from transformers.generation.logits_process import (
LogitsProcessorList,
RepetitionPenaltyLogitsProcessor,
SuppressTokensLogitsProcessor,
TemperatureLogitsWarper,
TopKLogitsWarper,
)
from transformers.models.qwen3_omni_moe.configuration_qwen3_omni_moe import (
Qwen3OmniMoeCode2WavConfig,
Qwen3OmniMoeConfig,
@@ -146,13 +153,6 @@ class Qwen3OmniMoeForConditionalGeneration(
self.talker.init_multi_modal(thinker_config)
self.model = self.talker
self.code2wav = None
from transformers.generation.logits_process import (
LogitsProcessorList,
RepetitionPenaltyLogitsProcessor,
SuppressTokensLogitsProcessor,
TemperatureLogitsWarper,
TopKLogitsWarper,
)
self.logits_processors = LogitsProcessorList(
[
@@ -367,46 +367,17 @@ class Qwen3OmniMoeForConditionalGeneration(
**capture_kwargs,
**kwargs,
)
# Compute thinker-side TTS token embeddings for BOS/EOS/PAD and expose via multimodal outputs.
# These will later be projected into talker text space by the talker stage.
multimodal_outputs = captured_layer_dict if captured_layer_dict is not None else {}
try:
thinker_tts_embeds = self.thinker.embed_input_ids(self.tts_tokens) # [1,3,thinker_hidden]
if (
isinstance(thinker_tts_embeds, torch.Tensor)
and thinker_tts_embeds.ndim == 3
and thinker_tts_embeds.shape[1] == 3
):
bos_eos_pad = thinker_tts_embeds.to(text_hidden_states.device).chunk(3, dim=1) # 3 * [1,1,H]
multimodal_outputs["tts_bos_embed"] = [bos_eos_pad[0]]
multimodal_outputs["tts_eos_embed"] = [bos_eos_pad[1]]
multimodal_outputs["tts_pad_embed"] = [bos_eos_pad[2]]
except Exception:
# Best-effort; absence will be handled by talker with fallbacks
pass
# Return text-only output (with multimodal sidecar)
return OmniOutput(
text_hidden_states=(text_hidden_states.reshape(-1, text_hidden_states.shape[-1])),
multimodal_outputs=multimodal_outputs,
)
return text_hidden_states, captured_layer_dict
# ========== Stage 2.1: Talker ==========
elif self.model_stage == "talker":
if input_ids is None:
input_ids = torch.zeros(inputs_embeds.shape[0], dtype=torch.long, device=inputs_embeds.device)
self.code_predictor_hidden_pool = torch.zeros_like(inputs_embeds)
is_profile = True
else:
is_profile = False
# Ensure we have base embeddings when only ids are provided
if inputs_embeds is None and input_ids is not None:
inputs_embeds = self.talker.embed_input_ids(input_ids)
if not is_profile and input_ids.shape[0] > 1:
self.input_ids_list = input_ids.tolist()
# TODO(Peiqi): temporal hack here to support voice_type.
if not hasattr(self, "voice_type"):
self.voice_type = voice_type
@@ -418,19 +389,7 @@ class Qwen3OmniMoeForConditionalGeneration(
positions=positions,
inputs_embeds=inputs_embeds,
)
# merge the code_predictor_codes from the info_dict list into a single tensor
multimodal_outputs: dict = None
if not is_profile:
# Here is the only place to use runtime_additional_information. After MTP in the
# preprocess function, the code_predictor_codes are stored in the info_dict list.
# We need to merge the tensors from different requests into a single tensor.
# In the future, we may allow user to custom an aggregated function.
info_dicts = kwargs.get("runtime_additional_information")
code_predictor_codes = [info.get("code_predictor_codes") for info in info_dicts]
multimodal_outputs = {"code_predictor_codes": torch.cat(code_predictor_codes, dim=0)}
return OmniOutput(text_hidden_states=talker_hidden, multimodal_outputs=multimodal_outputs)
return talker_hidden
# ========== Stage 3: Code2Wav ==========
elif self.model_stage == "code2wav":
@@ -459,10 +418,7 @@ class Qwen3OmniMoeForConditionalGeneration(
"Batched input for code2wav is not supported yet, only the first audio tensor will be returned"
)
return OmniOutput(
text_hidden_states=None,
multimodal_outputs={"model_outputs": audio_tensors[0].reshape(1, -1)},
)
return audio_tensors
# Fallback (shouldn't reach here)
return OmniOutput(
@@ -473,6 +429,63 @@ class Qwen3OmniMoeForConditionalGeneration(
multimodal_outputs=None,
)
def make_omni_output(self, model_outputs: torch.Tensor | OmniOutput, **kwargs) -> OmniOutput:
"""
Make an OmniOutput object from model outputs.
Args:
model_outputs: Model outputs
"""
if isinstance(model_outputs, OmniOutput):
return model_outputs
if self.model_stage == "thinker":
text_hidden_states, captured_layer_dict = model_outputs
# Compute thinker-side TTS token embeddings for BOS/EOS/PAD and expose via multimodal outputs.
# These will later be projected into talker text space by the talker stage.
multimodal_outputs = captured_layer_dict if captured_layer_dict is not None else {}
try:
thinker_tts_embeds = self.thinker.embed_input_ids(self.tts_tokens) # [1,3,thinker_hidden]
if (
isinstance(thinker_tts_embeds, torch.Tensor)
and thinker_tts_embeds.ndim == 3
and thinker_tts_embeds.shape[1] == 3
):
bos_eos_pad = thinker_tts_embeds.to(text_hidden_states.device).chunk(3, dim=1) # 3 * [1,1,H]
multimodal_outputs["tts_bos_embed"] = [bos_eos_pad[0]]
multimodal_outputs["tts_eos_embed"] = [bos_eos_pad[1]]
multimodal_outputs["tts_pad_embed"] = [bos_eos_pad[2]]
except Exception:
# Best-effort; absence will be handled by talker with fallbacks
pass
# Return text-only output (with multimodal sidecar)
return OmniOutput(
text_hidden_states=(text_hidden_states.reshape(-1, text_hidden_states.shape[-1])),
multimodal_outputs=multimodal_outputs,
)
elif self.model_stage == "talker":
talker_hidden = model_outputs
# merge the code_predictor_codes from the info_dict list into a single tensor
multimodal_outputs: dict = None
# Here is the only place to use runtime_additional_information. After MTP in the
# preprocess function, the code_predictor_codes are stored in the info_dict list.
# We need to merge the tensors from different requests into a single tensor.
# In the future, we may allow user to custom an aggregated function.
info_dicts = kwargs.get("runtime_additional_information")
code_predictor_codes = [info.get("code_predictor_codes") for info in info_dicts]
multimodal_outputs = {"code_predictor_codes": torch.cat(code_predictor_codes, dim=0)}
span_len = multimodal_outputs["code_predictor_codes"].shape[0]
talker_hidden = talker_hidden[:span_len]
return OmniOutput(text_hidden_states=talker_hidden, multimodal_outputs=multimodal_outputs)
elif self.model_stage == "code2wav":
audio_tensors = model_outputs
return OmniOutput(
text_hidden_states=None,
multimodal_outputs={"model_outputs": audio_tensors[0].reshape(1, -1)},
)
return model_outputs
# ==================== Audio Generation ====================
def generate_audio(self, code: torch.Tensor, voice_type: str) -> torch.Tensor:
@@ -624,9 +637,7 @@ class Qwen3OmniMoeForConditionalGeneration(
input_ids, input_embeds, **info_dict
)
# execute talker MTP
input_embeds, code_predictor_codes = self.talker_mtp(input_ids, input_embeds, last_talker_hidden, text_step)
update_dict["code_predictor_codes"] = code_predictor_codes
update_dict["mtp_inputs"] = last_talker_hidden, text_step
return input_ids, input_embeds, update_dict
@@ -657,7 +668,7 @@ class Qwen3OmniMoeForConditionalGeneration(
dtype=torch.long,
)
inputs_embeds = (inputs_embeds + text_step).reshape(-1, self.talker_config.text_config.hidden_size)
return inputs_embeds, code_predictor_codes.squeeze(-1).detach().to("cpu").contiguous()
return inputs_embeds, code_predictor_codes.squeeze(-1)
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)
@@ -872,7 +883,6 @@ class Qwen3OmniMoeForConditionalGeneration(
def talker_preprocess_decode(self, input_ids: torch.Tensor, input_embeds: torch.Tensor, **info_dict: dict):
update_dict: dict[str, dict] = {}
try:
q_tail = info_dict.get("tailing_text_hidden")
if isinstance(q_tail, torch.Tensor) and q_tail.numel() > 0:
@@ -14,6 +14,7 @@ import torch.nn as nn
import torch.nn.functional as F
from transformers import Cache, PretrainedConfig
from transformers.models.qwen3_omni_moe.modeling_qwen3_omni_moe import Qwen3OmniMoeRotaryEmbedding
from vllm.compilation.decorators import support_torch_compile
from vllm.config import CacheConfig, ModelConfig, VllmConfig
from vllm.logger import init_logger
from vllm.model_executor.layers.layernorm import RMSNorm
@@ -463,6 +464,7 @@ class Qwen3OmniCodePredictorBaseModel(nn.Module):
return self.codec_embedding
@support_torch_compile
class Qwen3OmniMoeTalkerCodePredictor(nn.Module):
"""
Code predictor wrapper matching HF structure.
@@ -4,6 +4,11 @@ from typing import Any
import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers.generation.logits_process import (
LogitsProcessorList,
TopKLogitsWarper,
TopPLogitsWarper,
)
from transformers.models.qwen3_omni_moe.configuration_qwen3_omni_moe import (
Qwen3OmniMoeTalkerConfig,
)
@@ -134,6 +139,7 @@ class Qwen3OmniMoeTalkerForConditionalGeneration(
self.code_predictor = Qwen3OmniMoeTalkerCodePredictor(
vllm_config=vllm_config, prefix=maybe_prefix(prefix, "code_predictor")
)
self.empty_code = torch.empty((1, 0), dtype=torch.long)
def code_predictor_forward(
self,
@@ -185,6 +191,12 @@ class Qwen3OmniMoeTalkerForConditionalGeneration(
all_codes_per_position = []
middle_hidden_states = [] # Collect hidden states for each position
logits_processors = LogitsProcessorList(
[
TopKLogitsWarper(top_k=top_k),
TopPLogitsWarper(top_p=top_p),
]
)
# Generate residual layers for each position
for pos in range(seq_len):
layer0_code = input_ids[:, pos : pos + 1] # [batch, 1]
@@ -220,19 +232,13 @@ class Qwen3OmniMoeTalkerForConditionalGeneration(
# Use the corresponding lm_head for this layer
logits = self.code_predictor.lm_head[layer_idx](hidden_state[:, -1:, :]) # [batch, 1, vocab_size]
from transformers.generation.logits_process import (
LogitsProcessorList,
TopKLogitsWarper,
TopPLogitsWarper,
)
logits_processors = LogitsProcessorList(
[
TopKLogitsWarper(top_k=top_k),
TopPLogitsWarper(top_p=top_p),
]
)
input_ids_for_logits_processors = torch.tensor([pos_codes[1:]]).to(logits.device, dtype=torch.long)
if len(pos_codes) > 1:
input_ids_for_logits_processors = torch.cat(pos_codes[1:], dim=1).to(
device=logits.device, dtype=torch.long
)
else:
input_ids_for_logits_processors = self.empty_code
logits = logits_processors(input_ids_for_logits_processors, logits.squeeze(0)).unsqueeze(0)
# Sample from the filtered distribution