Fix Base voice clone streaming quality and stop-token crash (#1945)

Signed-off-by: linyueqian <linyueqian@outlook.com>
This commit is contained in:
Yueqian Lin authored and GitHub committed 2026-03-18 06:54:12 +08:00
1 parent cf328eb8e8
commit fe786f1a19
4 files changed
+111 -17

No files matched your search

@@ -284,11 +284,11 @@ class Qwen3TTSCode2Wav(nn.Module):
for j, idx in enumerate(valid_indices):
ctx_frames, actual_frames = parsed[idx]
wav = wav_tensors[j]
expected_len = actual_frames * upsample
if wav.shape[0] > expected_len:
wav = wav[:expected_len]
if ctx_frames > 0:
cut = ctx_frames * upsample
# Proportional trim matching the official HF implementation:
# the decoder output length may not be exactly frames * upsample
# so compute cut as a proportion of total decoded length.
cut = int(ctx_frames / max(actual_frames, 1) * wav.shape[0])
if cut < wav.shape[0]:
wav = wav[cut:]
else:
@@ -298,6 +298,10 @@ class Qwen3TTSCode2Wav(nn.Module):
wav.shape[0],
)
continue
else:
expected_len = actual_frames * upsample
if wav.shape[0] > expected_len:
wav = wav[:expected_len]
if wav.shape[0] > 0:
audios[idx] = wav.to(dtype=torch.float32).reshape(-1)
@@ -29,8 +29,10 @@ def talker2code2wav(
output = talker_output.outputs[0]
# audio_codes shape: [num_frames, Q] where Q=num_quantizers (16)
audio_codes = output.multimodal_output["audio_codes"].to(torch.long)
# Filter zero-padded frames (EOS/invalid steps), matching _extract_last_frame behavior
valid_mask = audio_codes.any(dim=1)
# Filter invalid frames: zero-padded (EOS) and frames containing
# out-of-range values (e.g. stop_token_id=2150 exceeds codebook_size=2048).
_CODEBOOK_SIZE = 2048
valid_mask = audio_codes.any(dim=1) & (audio_codes.max(dim=1).values < _CODEBOOK_SIZE)
audio_codes = audio_codes[valid_mask]
ref_code = output.multimodal_output.get("ref_code")
if isinstance(ref_code, list):
@@ -177,13 +179,17 @@ def talker2code2wav_async_chunk(
left_context_size = max(0, end_index - context_length)
window_frames = transfer_manager.code_prompt_token_ids[request_id][-end_index:]
# Prepend ref_code to the first decoder window only.
if transfer_manager.put_req_chunk[request_id] == 0:
ref_code = request_payload.pop(request_id, None)
if isinstance(ref_code, torch.Tensor) and ref_code.numel() > 0:
ref_frames = ref_code.tolist()
window_frames = ref_frames + window_frames
left_context_size += len(ref_frames)
# Prepend ref_code as decoder context for every chunk so the vocoder
# maintains voice-clone speaker identity throughout the stream. The HF
# reference decodes ref_code + all_codes in one pass; without ref_code
# context on later chunks the decoder loses speaker identity and produces
# distorted audio. Use `.get()` (not `.pop()`) to keep ref_code for
# subsequent chunks.
ref_code = request_payload.get(request_id)
if isinstance(ref_code, torch.Tensor) and ref_code.numel() > 0:
ref_frames = ref_code.tolist()
window_frames = ref_frames + window_frames
left_context_size += len(ref_frames)
code_predictor_codes = torch.tensor(window_frames).transpose(0, 1).reshape(-1).tolist()