Murli-V1.5-Code / models /tts_engine.py
ViuAI's picture
Inference quality boost: 32-step Midpoint ODE, cfg=1.5, temp=0.667, whole-paragraph natural phrasing in narrate.py
fad5e83 verified
Raw History Blame Contribute Delete
4.73 kB
"""
Murli V1.5 End-to-End Acoustic TTS Engine (~20M Params).
Single-Speaker Studio-Grade Expressive Hindi + Android ONNX Ready.
"""
from typing import Dict, Optional
import torch
import torch.nn as nn
from config import MurliV15Config
from models.text_encoder import BilingualTextEncoder
from models.prosody_aligner import ProsodyAndAlignmentModule
from models.flow_matching import ConvNeXtCFMVectorField, compute_cfm_loss, sample_ode
class MurliV15Engine(nn.Module):
def __init__(self, cfg: MurliV15Config = MurliV15Config()):
super().__init__()
self.cfg = cfg
m = cfg.model
self.text_encoder = BilingualTextEncoder(
vocab_size=m.vocab_size,
dim=m.text_dim,
num_layers=m.text_layers,
num_heads=m.text_heads,
ff_mult=m.text_ff_mult,
dropout=m.text_dropout,
)
self.prosody_aligner = ProsodyAndAlignmentModule(
text_dim=m.text_dim,
n_mels=m.mel_dim,
num_emotions=m.num_emotions,
kernel_size=m.prosody_kernel_size,
num_layers=m.prosody_layers,
)
self.vector_field = ConvNeXtCFMVectorField(
mel_dim=m.mel_dim,
cond_dim=m.text_dim,
hidden_dim=m.cfm_hidden_dim,
num_blocks=m.cfm_blocks,
kernel_size=m.cfm_kernel_size,
time_dim=m.cfm_time_dim,
)
def forward(
self,
token_ids: torch.Tensor,
text_mask: torch.Tensor,
mel_target: torch.Tensor,
mel_mask: torch.Tensor,
f0_target: torch.Tensor,
energy_target: torch.Tensor,
emotion_ids: Optional[torch.Tensor] = None,
) -> Dict[str, torch.Tensor]:
if emotion_ids is None:
emotion_ids = torch.zeros(token_ids.shape[0], dtype=torch.long, device=token_ids.device)
text_feats = self.text_encoder(token_ids, text_mask=text_mask)
cond_mel, prior_mel, p_losses = self.prosody_aligner.forward_train(
text_feats=text_feats,
text_mask=text_mask,
emotion_ids=emotion_ids,
mel_target=mel_target,
mel_mask=mel_mask,
f0_target=f0_target,
energy_target=energy_target,
)
cfm_loss = compute_cfm_loss(
self.vector_field,
mel_target=mel_target,
cond=cond_mel,
mel_mask=mel_mask,
cfg_drop_prob=self.cfg.model.cfg_drop_prob if self.training else 0.0,
)
tc = self.cfg.train
total_loss = (
tc.cfm_loss_weight * cfm_loss
+ tc.prior_loss_weight * p_losses["prior_loss"]
+ tc.dur_loss_weight * p_losses["dur_loss"]
+ tc.f0_loss_weight * p_losses["f0_loss"]
+ tc.energy_loss_weight * p_losses["energy_loss"]
)
return {
"loss": total_loss,
"cfm_loss": cfm_loss,
"prior_loss": p_losses["prior_loss"],
"dur_loss": p_losses["dur_loss"],
"f0_loss": p_losses["f0_loss"],
"energy_loss": p_losses["energy_loss"],
"prior_mel": prior_mel,
}
@torch.no_grad()
def synthesize(
self,
token_ids: torch.Tensor,
emotion_id: int = 0,
speed: Optional[float] = None,
steps: Optional[int] = None,
cfg_scale: Optional[float] = None,
temperature: Optional[float] = None,
) -> torch.Tensor:
self.eval()
m = self.cfg.model
speed = m.default_speed if speed is None else speed
steps = m.default_steps if steps is None else steps
cfg_scale = m.default_cfg_scale if cfg_scale is None else cfg_scale
temperature = m.default_temperature if temperature is None else temperature
text_mask = token_ids != 0
emotion_ids = torch.full((token_ids.shape[0],), int(emotion_id), dtype=torch.long, device=token_ids.device)
text_feats = self.text_encoder(token_ids, text_mask=text_mask)
cond_mel, prior_mel = self.prosody_aligner.forward_infer(
text_feats=text_feats,
text_mask=text_mask,
emotion_ids=emotion_ids,
speed=speed,
min_frames=m.min_frames,
)
mel_denorm = sample_ode(
self.vector_field,
cond=cond_mel,
prior_mel=prior_mel,
steps=steps,
cfg_scale=cfg_scale,
temperature=temperature,
)
return mel_denorm * self.cfg.audio.mel_std + self.cfg.audio.mel_mean