[Feature][Voxtral TTS] Support per-model extra sampling params & add cfg_alpha for Voxtral TTS (#2338)
Signed-off-by: Chen-Yo Sun <chenyo.sun@mistral.ai> Signed-off-by: Yueqian Lin <linyueqian@outlook.com> Co-authored-by: Yueqian Lin <linyueqian@outlook.com>
This commit is contained in:
1 parent
2903f528d2
commit
8e1bbc9ab5
13 files changed
+160
-37
No files matched your search
+25
-13
@@ -48,13 +48,13 @@ class CUDAGraphAcousticTransformerWrapper:
|
||||
self.n_acoustic_codebook = self.acoustic_transformer.model_args.n_acoustic_codebook
|
||||
self.acoustic_embeddings_levels = self.acoustic_transformer.acoustic_embeddings_levels
|
||||
|
||||
self.cfg_alpha = 1.2
|
||||
self.n_steps = self.acoustic_transformer.acoustic_transformer_args.n_decoding_steps
|
||||
|
||||
# Graph storage
|
||||
self.graphs: dict[int, CUDAGraph] = {}
|
||||
self.static_inputs: dict[int, torch.Tensor] = {}
|
||||
self.static_noise: dict[int, torch.Tensor] = {}
|
||||
self.static_cfg_alpha: dict[int, torch.Tensor] = {}
|
||||
self.static_fake_eos: dict[int, torch.Tensor] = {}
|
||||
self.static_audio_codes: dict[int, torch.Tensor] = {}
|
||||
|
||||
@@ -80,8 +80,10 @@ class CUDAGraphAcousticTransformerWrapper:
|
||||
# Phase 1: Eager warmup for ALL capture sizes
|
||||
for size in self.capture_sizes:
|
||||
dummy = torch.zeros(size, hidden_dim, device=device, dtype=dtype)
|
||||
dummy_cfg_alpha = torch.full((size, 1), 1.2, device=device, dtype=dtype)
|
||||
dummy_noise = torch.randn(size, self.n_acoustic_codebook, device=device, dtype=dtype)
|
||||
with torch.no_grad():
|
||||
self._forward_cudagraph_compatible(dummy)
|
||||
self._forward_cudagraph_compatible(dummy, cfg_alpha=dummy_cfg_alpha, noise=dummy_noise)
|
||||
|
||||
torch.cuda.synchronize(device)
|
||||
|
||||
@@ -105,7 +107,12 @@ class CUDAGraphAcousticTransformerWrapper:
|
||||
len(self.capture_sizes),
|
||||
)
|
||||
|
||||
def _forward_cudagraph_compatible(self, hidden_states: torch.Tensor, noise: torch.Tensor | None = None):
|
||||
def _forward_cudagraph_compatible(
|
||||
self,
|
||||
hidden_states: torch.Tensor,
|
||||
cfg_alpha: torch.Tensor,
|
||||
noise: torch.Tensor,
|
||||
):
|
||||
"""
|
||||
The actual computation captured by the CUDA graph.
|
||||
|
||||
@@ -117,6 +124,7 @@ class CUDAGraphAcousticTransformerWrapper:
|
||||
- Calls _predict_velocity directly
|
||||
- Uses a pre-allocated noise buffer to avoid baking random state
|
||||
into the CUDA graph
|
||||
- Uses a pre-allocated cfg_alpha buffer for per-request CFG strength
|
||||
"""
|
||||
at = self.acoustic_transformer
|
||||
B = hidden_states.shape[0]
|
||||
@@ -132,10 +140,7 @@ class CUDAGraphAcousticTransformerWrapper:
|
||||
# --- Flow matching: Euler ODE ---
|
||||
should_decode = semantic_code.squeeze(1) != self.end_audio_token_id
|
||||
|
||||
if noise is not None:
|
||||
x = noise
|
||||
else:
|
||||
x = torch.randn(B, self.n_acoustic_codebook, device=hidden_states.device, dtype=hidden_states.dtype)
|
||||
x = noise
|
||||
|
||||
# Pre-compute zero hidden states for unconditional CFG branch
|
||||
hidden_states_zero = torch.zeros_like(hidden_states)
|
||||
@@ -154,8 +159,8 @@ class CUDAGraphAcousticTransformerWrapper:
|
||||
v_all = at._predict_velocity(x_t=x_batched, llm_output=llm_batched, t_emb=t_emb_batched)
|
||||
v_t, uncond_v_t = v_all[:B], v_all[B:]
|
||||
|
||||
# CFG combination
|
||||
v_t = self.cfg_alpha * v_t + (1 - self.cfg_alpha) * uncond_v_t
|
||||
# CFG combination (cfg_alpha is (B, 1), v_t is (B, C))
|
||||
v_t = cfg_alpha * v_t + (1 - cfg_alpha) * uncond_v_t
|
||||
|
||||
x = x + v_t * dt
|
||||
|
||||
@@ -188,10 +193,11 @@ class CUDAGraphAcousticTransformerWrapper:
|
||||
"""Capture a CUDA graph for a specific batch size."""
|
||||
static_input = torch.zeros(size, hidden_dim, device=device, dtype=dtype)
|
||||
static_noise = torch.randn(size, self.n_acoustic_codebook, device=device, dtype=dtype)
|
||||
static_cfg_alpha = torch.full((size, 1), 1.2, device=device, dtype=dtype)
|
||||
|
||||
# Stabilizing eager run
|
||||
with torch.no_grad():
|
||||
_ = self._forward_cudagraph_compatible(static_input, noise=static_noise)
|
||||
_ = self._forward_cudagraph_compatible(static_input, cfg_alpha=static_cfg_alpha, noise=static_noise)
|
||||
|
||||
torch.cuda.synchronize(device)
|
||||
|
||||
@@ -199,12 +205,13 @@ class CUDAGraphAcousticTransformerWrapper:
|
||||
with torch.no_grad():
|
||||
with torch.cuda.graph(graph, pool=current_platform.get_global_graph_pool()):
|
||||
static_fake_eos, static_audio_codes = self._forward_cudagraph_compatible(
|
||||
static_input, noise=static_noise
|
||||
static_input, cfg_alpha=static_cfg_alpha, noise=static_noise
|
||||
)
|
||||
|
||||
self.graphs[size] = graph
|
||||
self.static_inputs[size] = static_input
|
||||
self.static_noise[size] = static_noise
|
||||
self.static_cfg_alpha[size] = static_cfg_alpha
|
||||
self.static_fake_eos[size] = static_fake_eos
|
||||
self.static_audio_codes[size] = static_audio_codes
|
||||
|
||||
@@ -218,6 +225,7 @@ class CUDAGraphAcousticTransformerWrapper:
|
||||
def __call__(
|
||||
self,
|
||||
hidden_states: torch.Tensor,
|
||||
cfg_alpha: torch.Tensor,
|
||||
) -> tuple[torch.Tensor, dict[str, list[torch.Tensor]] | None]:
|
||||
"""
|
||||
Drop-in replacement for model.compute_mm_logits().
|
||||
@@ -229,16 +237,20 @@ class CUDAGraphAcousticTransformerWrapper:
|
||||
actual_size = hidden_states.shape[0]
|
||||
|
||||
if not self.enabled or not self._warmed_up:
|
||||
return self.model.compute_mm_logits(hidden_states)
|
||||
return self.model.compute_mm_logits(hidden_states, cfg_alpha=cfg_alpha)
|
||||
|
||||
padded_size = self._get_padded_size(actual_size)
|
||||
if padded_size is None or padded_size not in self.graphs:
|
||||
return self.model.compute_mm_logits(hidden_states)
|
||||
return self.model.compute_mm_logits(hidden_states, cfg_alpha=cfg_alpha)
|
||||
|
||||
# Zero static input, then copy actual data
|
||||
self.static_inputs[padded_size].zero_()
|
||||
self.static_inputs[padded_size][:actual_size] = hidden_states
|
||||
|
||||
# Copy per-request cfg_alpha into static buffer (pad with 1.2 default)
|
||||
self.static_cfg_alpha[padded_size].fill_(1.2)
|
||||
self.static_cfg_alpha[padded_size][:actual_size, 0] = cfg_alpha
|
||||
|
||||
# Fill noise buffer with fresh random values before replay so the
|
||||
# flow-matching ODE starts from different initial noise each time.
|
||||
self.static_noise[padded_size].normal_()
|
||||
|
||||
@@ -283,6 +283,30 @@ class VoxtralTTSForConditionalGeneration(
|
||||
multimodal_outputs={"audio": batch_audio_arrays},
|
||||
)
|
||||
|
||||
_DEFAULT_CFG_ALPHA = 1.2
|
||||
|
||||
def _extract_cfg_alpha(self, input_hidden_states: torch.Tensor, **kwargs) -> torch.Tensor:
|
||||
"""Extract per-request cfg_alpha from sampling_extra_args.
|
||||
|
||||
Returns a 1-D tensor of shape (B,) with per-request cfg_alpha values.
|
||||
Falls back to default if sampling_extra_args is missing or incomplete.
|
||||
"""
|
||||
B = input_hidden_states.shape[0]
|
||||
sampling_extra_args = kwargs.get("sampling_extra_args")
|
||||
if sampling_extra_args is None:
|
||||
return torch.full(
|
||||
(B,),
|
||||
self._DEFAULT_CFG_ALPHA,
|
||||
device=input_hidden_states.device,
|
||||
dtype=input_hidden_states.dtype,
|
||||
)
|
||||
cfg_alpha_values = [ea.get("cfg_alpha", self._DEFAULT_CFG_ALPHA) for ea in sampling_extra_args]
|
||||
return torch.tensor(
|
||||
cfg_alpha_values,
|
||||
device=input_hidden_states.device,
|
||||
dtype=input_hidden_states.dtype,
|
||||
)
|
||||
|
||||
def make_omni_output(
|
||||
self, model_outputs: torch.Tensor | OmniOutput | tuple, logits_index: int | None = None, **kwargs
|
||||
) -> OmniOutput:
|
||||
@@ -291,10 +315,15 @@ class VoxtralTTSForConditionalGeneration(
|
||||
hidden_states = model_outputs
|
||||
assert logits_index is not None
|
||||
input_hidden_states = hidden_states[logits_index]
|
||||
cfg_alpha = self._extract_cfg_alpha(input_hidden_states, **kwargs)
|
||||
if self._cudagraph_acoustic_transformer is not None:
|
||||
fake_eos, multimodal_outputs = self._cudagraph_acoustic_transformer(input_hidden_states)
|
||||
fake_eos, multimodal_outputs = self._cudagraph_acoustic_transformer(
|
||||
input_hidden_states, cfg_alpha=cfg_alpha
|
||||
)
|
||||
else:
|
||||
fake_eos, multimodal_outputs = self.model.compute_mm_logits(input_hidden_states)
|
||||
fake_eos, multimodal_outputs = self.model.compute_mm_logits(
|
||||
input_hidden_states, cfg_alpha=cfg_alpha
|
||||
)
|
||||
hidden_states[logits_index, 0] = fake_eos
|
||||
return OmniOutput(
|
||||
text_hidden_states=hidden_states,
|
||||
|
||||
@@ -438,8 +438,6 @@ class FlowMatchingAudioTransformer(nn.Module):
|
||||
|
||||
# Flow matching constants
|
||||
self._n_steps = args.n_decoding_steps
|
||||
# TODO(chenyo): hardcoded, need to fix
|
||||
self._cfg_alpha = 1.2
|
||||
self._noise_scale = 1.0
|
||||
self.register_buffer(
|
||||
"_timesteps",
|
||||
@@ -512,6 +510,7 @@ class FlowMatchingAudioTransformer(nn.Module):
|
||||
self,
|
||||
semantic_code: torch.Tensor,
|
||||
llm_hidden: torch.Tensor,
|
||||
cfg_alpha: torch.Tensor,
|
||||
) -> torch.Tensor:
|
||||
B = semantic_code.shape[0]
|
||||
|
||||
@@ -525,6 +524,10 @@ class FlowMatchingAudioTransformer(nn.Module):
|
||||
timesteps = self._timesteps.to(dtype=llm_hidden.dtype)
|
||||
llm_hidden_zero = torch.zeros_like(llm_hidden)
|
||||
|
||||
# Reshape cfg_alpha for broadcasting: (B,) -> (B, 1)
|
||||
cfg_alpha = cfg_alpha.to(dtype=llm_hidden.dtype, device=llm_hidden.device)
|
||||
cfg_alpha = cfg_alpha.unsqueeze(1) # (B, 1) for broadcasting with (B, C)
|
||||
|
||||
# Euler integration with batched conditional + unconditional velocity
|
||||
sampled = x_0
|
||||
for i in range(len(timesteps) - 1):
|
||||
@@ -544,7 +547,7 @@ class FlowMatchingAudioTransformer(nn.Module):
|
||||
t_emb=t_emb_batched,
|
||||
)
|
||||
v_t, uncond_v_t = v_all[:B], v_all[B:]
|
||||
v_t = self._cfg_alpha * v_t + (1 - self._cfg_alpha) * uncond_v_t
|
||||
v_t = cfg_alpha * v_t + (1 - cfg_alpha) * uncond_v_t
|
||||
|
||||
sampled = sampled + v_t * dt
|
||||
|
||||
@@ -585,6 +588,7 @@ class FlowMatchingAudioTransformer(nn.Module):
|
||||
def forward(
|
||||
self,
|
||||
llm_hidden: torch.Tensor,
|
||||
cfg_alpha: torch.Tensor,
|
||||
) -> torch.Tensor:
|
||||
# llm_hidden: BxD
|
||||
semantic_logit = self.semantic_codebook_output(llm_hidden).float()
|
||||
@@ -594,10 +598,10 @@ class FlowMatchingAudioTransformer(nn.Module):
|
||||
# semantic_logit: Bx1
|
||||
semantic_code = semantic_logit.argmax(dim=-1, keepdim=True)
|
||||
|
||||
# acoustic codes, TODO(@chenyo): config sampling
|
||||
acoustic_codes = self.decode_one_frame(
|
||||
semantic_code.squeeze(1),
|
||||
llm_hidden,
|
||||
cfg_alpha=cfg_alpha,
|
||||
)
|
||||
|
||||
audio_codes = torch.concatenate(
|
||||
@@ -1035,11 +1039,13 @@ class VoxtralTTSAudioGenerationForConditionalGeneration(nn.Module, SupportsMulti
|
||||
def compute_mm_logits(
|
||||
self,
|
||||
hidden_states: torch.Tensor,
|
||||
cfg_alpha: torch.Tensor,
|
||||
) -> tuple[torch.Tensor, torch.Tensor | None]:
|
||||
audio_codes = None
|
||||
mm_tokens = None
|
||||
audio_codes = self.acoustic_transformer(
|
||||
llm_hidden=hidden_states,
|
||||
cfg_alpha=cfg_alpha,
|
||||
)
|
||||
fake_eos = torch.where(
|
||||
audio_codes[:, 0] == AudioSpecialTokens.id(AudioSpecialTokens.end_audio),
|
||||
|
||||
Reference in new issue
Block a user