[Optim][Qwen3TTS] big boost model throughput+latency high concurrency (#1852)

Signed-off-by: pablo <pablo@agigo.ai>
Signed-off-by: JuanPZuluaga <juanz9312@gmail.com>
Co-authored-by: pablo <pablo@agigo.ai>
Co-authored-by: Hongsheng Liu <liuhongsheng4@huawei.com>
This commit is contained in:
authored and GitHub committed 2026-03-18 12:22:05 -04:00
1 parent 53ebdd0b1d
commit fc5e8f825e
5 files changed
+75 -30

No files matched your search

@@ -393,6 +393,14 @@ class Qwen3TTSTalkerForConditionalGeneration(nn.Module):
codec_mask[self._codec_eos_token_id] = True
self.register_buffer("_codec_allowed_mask", codec_mask, persistent=False)
# Keys that should stay on GPU in model_intermediate_buffer to avoid
# CPU-to-GPU round-trips on every decode step.
self.gpu_resident_buffer_keys: set[str] = {
"last_talker_hidden",
"tts_pad_embed",
"tailing_text_hidden",
}
# Tokenizer for prompt building.
self._tokenizer = None
self._speech_tokenizer: Qwen3TTSTokenizer | None = None
@@ -547,12 +555,13 @@ class Qwen3TTSTalkerForConditionalGeneration(nn.Module):
full_prompt_embeds, tailing_text_hidden, tts_pad_embed, ref_code_len, ref_code = (
self._build_prompt_embeds(task_type=task_type, info_dict=info_dict)
)
# Store full prompt embeddings + trailing queue on CPU for later chunks/steps.
# Store full prompt embeddings on CPU (large, prefill-only).
# tailing_text_hidden and tts_pad_embed stay on GPU (gpu_resident_buffer_keys).
prompt_embeds_cpu = full_prompt_embeds.detach().to("cpu").contiguous()
info_update: dict[str, Any] = {
"talker_prompt_embeds": prompt_embeds_cpu,
"tailing_text_hidden": tailing_text_hidden.detach().to("cpu").contiguous(),
"tts_pad_embed": tts_pad_embed.detach().to("cpu").contiguous(),
"tailing_text_hidden": tailing_text_hidden.detach(),
"tts_pad_embed": tts_pad_embed.detach(),
"talker_prefill_offset": 0,
"codec_streaming": codec_streaming,
}
@@ -568,7 +577,7 @@ class Qwen3TTSTalkerForConditionalGeneration(nn.Module):
take = prompt_embeds_cpu[s:e]
if int(take.shape[0]) < span_len:
pad_n = int(span_len - int(take.shape[0]))
pad_rows = tts_pad_embed.detach().to("cpu").contiguous().reshape(1, -1).expand(pad_n, -1)
pad_rows = tts_pad_embed.reshape(1, -1).to("cpu").expand(pad_n, -1)
take = torch.cat([take, pad_rows], dim=0)
prompt_embeds = take.to(device=input_ids.device, dtype=torch.bfloat16)
info_update["talker_prefill_offset"] = int(offset + span_len)
@@ -584,7 +593,7 @@ class Qwen3TTSTalkerForConditionalGeneration(nn.Module):
take = prompt_embeds_cpu[s:e]
if int(take.shape[0]) < span_len:
pad_n = int(span_len - int(take.shape[0]))
pad_rows = tts_pad_embed.detach().to("cpu").contiguous().reshape(1, -1).expand(pad_n, -1)
pad_rows = tts_pad_embed.reshape(1, -1).to("cpu").expand(pad_n, -1)
take = torch.cat([take, pad_rows], dim=0)
prompt_embeds = take.to(device=input_ids.device, dtype=torch.bfloat16)
info_update = {"talker_prefill_offset": int(offset + span_len)}
@@ -604,23 +613,24 @@ class Qwen3TTSTalkerForConditionalGeneration(nn.Module):
# Decode: span_len == 1
# Pop one text-step vector from tailing_text_hidden queue.
tts_pad_embed_cpu = info_dict.get("tts_pad_embed")
if not isinstance(tts_pad_embed_cpu, torch.Tensor):
# These tensors stay on GPU via gpu_resident_buffer_keys - .to() is a no-op.
tts_pad_embed_buf = info_dict.get("tts_pad_embed")
if not isinstance(tts_pad_embed_buf, torch.Tensor):
raise RuntimeError("Missing `tts_pad_embed` in additional_information; prefill must run first.")
tts_pad_embed = tts_pad_embed_cpu.to(device=input_ids.device, dtype=torch.bfloat16).reshape(1, -1)
tts_pad_embed = tts_pad_embed_buf.to(device=input_ids.device, dtype=torch.bfloat16).reshape(1, -1)
tail_cpu = info_dict.get("tailing_text_hidden")
if isinstance(tail_cpu, torch.Tensor) and tail_cpu.ndim == 2 and tail_cpu.shape[0] > 0:
text_step = tail_cpu[:1].to(device=input_ids.device, dtype=torch.bfloat16).reshape(1, -1)
new_tail = tail_cpu[1:].detach().to("cpu").contiguous() if tail_cpu.shape[0] > 1 else tail_cpu[:0]
tail = info_dict.get("tailing_text_hidden")
if isinstance(tail, torch.Tensor) and tail.ndim == 2 and tail.shape[0] > 0:
text_step = tail[:1].to(device=input_ids.device, dtype=torch.bfloat16).reshape(1, -1)
new_tail = tail[1:] if tail.shape[0] > 1 else tail[:0]
else:
text_step = tts_pad_embed
new_tail = tail_cpu if isinstance(tail_cpu, torch.Tensor) else torch.empty((0, tts_pad_embed.shape[-1]))
new_tail = tail if isinstance(tail, torch.Tensor) else torch.empty((0, tts_pad_embed.shape[-1]))
last_hidden_cpu = info_dict.get("last_talker_hidden")
if not isinstance(last_hidden_cpu, torch.Tensor):
last_hidden = info_dict.get("last_talker_hidden")
if not isinstance(last_hidden, torch.Tensor):
raise RuntimeError("Missing `last_talker_hidden` in additional_information; postprocess must run.")
past_hidden = last_hidden_cpu.to(device=input_ids.device, dtype=torch.bfloat16).reshape(1, -1)
past_hidden = last_hidden.to(device=input_ids.device, dtype=torch.bfloat16).reshape(1, -1)
# Use OmniGPUModelRunner talker_mtp fast-path for residual codebooks and per-step inputs_embeds update.
last_id_hidden = self.embed_input_ids(input_ids.reshape(1, 1).to(torch.long)).to(
@@ -637,10 +647,10 @@ class Qwen3TTSTalkerForConditionalGeneration(nn.Module):
def postprocess(self, hidden_states: torch.Tensor, **_: Any) -> dict[str, Any]:
# Keep the last token hidden for the next decode step's code predictor.
# Stays on GPU - gpu_resident_buffer_keys avoids the CPU round-trip.
if hidden_states.numel() == 0:
return {}
last = hidden_states[-1, :].detach().to("cpu").contiguous()
return {"last_talker_hidden": last}
return {"last_talker_hidden": hidden_states[-1, :].detach()}
# -------------------- prompt construction helpers --------------------