File size: 4,728 Bytes
cc5e704 fad5e83 cc5e704 fad5e83 cc5e704 fad5e83 cc5e704 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | """
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
|