[tts][bugfix] Fix CosyVoice3 sampling and stage handoff (#6424)
Signed-off-by: akshatvishu <akshatnayak197@gmail.com>
This commit is contained in:
1 parent
0db262de2a
commit
39c16d75a9
11 files changed
+221
-68
No files matched your search
@@ -494,7 +494,7 @@ class HiFTGenerator(nn.Module):
|
||||
self.istft_params["n_fft"],
|
||||
self.istft_params["hop_len"],
|
||||
self.istft_params["n_fft"],
|
||||
window=self.stft_window,
|
||||
window=self._get_stft_window(x),
|
||||
return_complex=True,
|
||||
)
|
||||
spec = torch.view_as_real(spec) # [B, F, TT, 2]
|
||||
@@ -509,10 +509,15 @@ class HiFTGenerator(nn.Module):
|
||||
self.istft_params["n_fft"],
|
||||
self.istft_params["hop_len"],
|
||||
self.istft_params["n_fft"],
|
||||
window=self.stft_window,
|
||||
window=self._get_stft_window(magnitude),
|
||||
)
|
||||
return inverse_transform
|
||||
|
||||
def _get_stft_window(self, tensor: torch.Tensor) -> torch.Tensor:
|
||||
if self.stft_window.device != tensor.device:
|
||||
self.stft_window = self.stft_window.to(tensor.device)
|
||||
return self.stft_window
|
||||
|
||||
def _decode_pre_istft(
|
||||
self,
|
||||
x: torch.Tensor,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
|
||||
import os
|
||||
from collections.abc import Iterable, Mapping, Sequence
|
||||
from functools import partial
|
||||
@@ -35,6 +35,7 @@ from vllm.multimodal.processing import (
|
||||
from vllm.sequence import IntermediateTensors
|
||||
from vllm.v1.outputs import SamplerOutput
|
||||
from vllm.v1.sample.metadata import SamplingMetadata
|
||||
from vllm.v1.sample.ops.topk_topp_sampler import random_sample
|
||||
from vllm.v1.sample.sampler import Sampler
|
||||
|
||||
from vllm_omni.data_entry_keys import EmbeddingsStruct, OmniPayloadStruct, to_dict, to_struct
|
||||
@@ -566,8 +567,8 @@ class CosyVoice3Model(
|
||||
return float(value)
|
||||
|
||||
@staticmethod
|
||||
def _multinomial_sample(probs: torch.Tensor, generator: torch.Generator | None = None) -> torch.Tensor:
|
||||
return torch.multinomial(probs, 1, replacement=True, generator=generator).reshape(())
|
||||
def _random_sample_one(probs: torch.Tensor, generator: torch.Generator | None = None) -> torch.Tensor:
|
||||
return random_sample(probs.unsqueeze(0), {} if generator is None else {0: generator}).reshape(())
|
||||
|
||||
@classmethod
|
||||
def _nucleus_sample_one(
|
||||
@@ -602,7 +603,7 @@ class CosyVoice3Model(
|
||||
# First token always passes (cum_before[0] = 0 < top_p for any top_p > 0),
|
||||
# so ``weights`` is guaranteed to have at least one nonzero entry. The
|
||||
# final ``.item()`` is the ONLY D2H sync per call.
|
||||
sample_idx = torch.multinomial(weights, 1, replacement=True, generator=generator)
|
||||
sample_idx = cls._random_sample_one(weights, generator=generator)
|
||||
return int(sorted_idx[sample_idx].item())
|
||||
|
||||
@classmethod
|
||||
@@ -632,9 +633,14 @@ class CosyVoice3Model(
|
||||
rep_num = int((recent == top_id).sum().item())
|
||||
if rep_num >= win_size * tau_r:
|
||||
weighted_scores = weighted_scores.clone()
|
||||
original_score = weighted_scores[top_id].clone()
|
||||
weighted_scores[top_id] = float("-inf")
|
||||
fallback_probs = weighted_scores.softmax(dim=0)
|
||||
top_id = int(cls._multinomial_sample(fallback_probs, generator=generator).item())
|
||||
weighted_scores[top_id] = torch.where(
|
||||
torch.isfinite(weighted_scores).any(),
|
||||
weighted_scores[top_id],
|
||||
original_score,
|
||||
)
|
||||
top_id = int(cls._random_sample_one(weighted_scores.softmax(dim=0), generator=generator).item())
|
||||
return top_id
|
||||
|
||||
def _cosyvoice3_ras_enabled(self, sampling_metadata: SamplingMetadata) -> bool:
|
||||
@@ -677,6 +683,10 @@ class CosyVoice3Model(
|
||||
logits.masked_fill_(sampling_metadata.allowed_token_ids_mask, float("-inf"))
|
||||
for processor in sampling_metadata.logitsprocs.non_argmax_invariant:
|
||||
logits = processor.apply(logits)
|
||||
finite_logits = torch.isfinite(logits)
|
||||
if not finite_logits.any(dim=-1).all().item():
|
||||
raise ValueError("CosyVoice3 sampling received a row with no finite logits")
|
||||
logits.masked_fill_(~finite_logits, float("-inf"))
|
||||
|
||||
sampling_cfg = dict(self.config.llm.get("sampling", {}))
|
||||
default_top_p = float(sampling_cfg.get("top_p", 0.8))
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
|
||||
from collections import defaultdict
|
||||
from collections.abc import Mapping
|
||||
from contextlib import nullcontext
|
||||
from typing import Any
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
from vllm.inputs import TextPrompt
|
||||
from vllm.logger import init_logger
|
||||
@@ -16,6 +15,7 @@ from vllm_omni.data_entry_keys import (
|
||||
MetaStruct,
|
||||
OmniPayloadStruct,
|
||||
)
|
||||
from vllm_omni.engine.serialization import deserialize_additional_information
|
||||
from vllm_omni.inputs.data import OmniTokensPrompt
|
||||
from vllm_omni.model_executor.models.cosyvoice3.utils import unpad_prompt_conditioning
|
||||
|
||||
@@ -93,32 +93,6 @@ def _to_cpu_tensor(x: Any) -> torch.Tensor | None:
|
||||
return None
|
||||
|
||||
|
||||
def _decode_additional_information(raw_info: Any) -> dict[str, Any]:
|
||||
if raw_info is None:
|
||||
return {}
|
||||
if isinstance(raw_info, dict):
|
||||
return raw_info
|
||||
|
||||
entries = getattr(raw_info, "entries", None)
|
||||
if not isinstance(entries, dict):
|
||||
return {}
|
||||
|
||||
decoded: dict[str, Any] = {}
|
||||
for key, entry in entries.items():
|
||||
tensor_data = getattr(entry, "tensor_data", None)
|
||||
if tensor_data is not None:
|
||||
dtype_name = getattr(entry, "tensor_dtype", "float32")
|
||||
tensor_shape = getattr(entry, "tensor_shape", None)
|
||||
if tensor_shape is None:
|
||||
continue
|
||||
dt = np.dtype(dtype_name)
|
||||
arr = np.frombuffer(tensor_data, dtype=dt).reshape(tensor_shape)
|
||||
decoded[key] = torch.from_numpy(arr.copy())
|
||||
else:
|
||||
decoded[key] = getattr(entry, "list_data", None)
|
||||
return decoded
|
||||
|
||||
|
||||
def talker2code2wav_async_chunk(
|
||||
transfer_manager: Any,
|
||||
multimodal_output: dict[str, Any] | None,
|
||||
@@ -149,7 +123,7 @@ def talker2code2wav_async_chunk(
|
||||
request_state = transfer_manager.request_payload.get(request_id)
|
||||
if not isinstance(request_state, dict) or "_cosyvoice3_async_state" not in request_state:
|
||||
with nullcontext():
|
||||
info = _decode_additional_information(getattr(request, "additional_information", None))
|
||||
info = deserialize_additional_information(getattr(request, "additional_information", None))
|
||||
info_embed = info.get("embed", {}) if isinstance(info, dict) else {}
|
||||
prompt_payload = {}
|
||||
cond_keys = ("speech_token", "speech_feat", "embedding", "speech_token_len")
|
||||
|
||||
Reference in new issue
Block a user