Spaces:
Running
Running
# Copyright (c) 2023 Amphion. | |
# | |
# This source code is licensed under the MIT license found in the | |
# LICENSE file in the root directory of this source tree. | |
# This code is modified from https://github.com/jaywalnut310/vits/blob/main/models.py | |
import math | |
import torch | |
from torch import nn | |
from torch.nn import functional as F | |
from utils.util import * | |
from modules.flow.modules import * | |
from modules.base.base_module import * | |
from modules.transformer.attentions import Encoder | |
from modules.duration_predictor.standard_duration_predictor import DurationPredictor | |
from modules.duration_predictor.stochastic_duration_predictor import ( | |
StochasticDurationPredictor, | |
) | |
from models.vocoders.gan.generator.hifigan import HiFiGAN_vits as Generator | |
try: | |
from modules import monotonic_align | |
except ImportError: | |
print("Monotonic align not found. Please make sure you have compiled it.") | |
class TextEncoder(nn.Module): | |
def __init__( | |
self, | |
n_vocab, | |
out_channels, | |
hidden_channels, | |
filter_channels, | |
n_heads, | |
n_layers, | |
kernel_size, | |
p_dropout, | |
): | |
super().__init__() | |
self.n_vocab = n_vocab | |
self.out_channels = out_channels | |
self.hidden_channels = hidden_channels | |
self.filter_channels = filter_channels | |
self.n_heads = n_heads | |
self.n_layers = n_layers | |
self.kernel_size = kernel_size | |
self.p_dropout = p_dropout | |
self.emb = nn.Embedding(n_vocab, hidden_channels) | |
nn.init.normal_(self.emb.weight, 0.0, hidden_channels**-0.5) | |
self.encoder = Encoder( | |
hidden_channels, filter_channels, n_heads, n_layers, kernel_size, p_dropout | |
) | |
self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1) | |
def forward(self, x, x_lengths): | |
x = self.emb(x) * math.sqrt(self.hidden_channels) # [b, t, h] | |
x = torch.transpose(x, 1, -1) # [b, h, t] | |
x_mask = torch.unsqueeze(sequence_mask(x_lengths, x.size(2)), 1).to(x.dtype) | |
x = self.encoder(x * x_mask, x_mask) | |
stats = self.proj(x) * x_mask | |
m, logs = torch.split(stats, self.out_channels, dim=1) | |
return x, m, logs, x_mask | |
class ResidualCouplingBlock(nn.Module): | |
def __init__( | |
self, | |
channels, | |
hidden_channels, | |
kernel_size, | |
dilation_rate, | |
n_layers, | |
n_flows=4, | |
gin_channels=0, | |
): | |
super().__init__() | |
self.channels = channels | |
self.hidden_channels = hidden_channels | |
self.kernel_size = kernel_size | |
self.dilation_rate = dilation_rate | |
self.n_layers = n_layers | |
self.n_flows = n_flows | |
self.gin_channels = gin_channels | |
self.flows = nn.ModuleList() | |
for i in range(n_flows): | |
self.flows.append( | |
ResidualCouplingLayer( | |
channels, | |
hidden_channels, | |
kernel_size, | |
dilation_rate, | |
n_layers, | |
gin_channels=gin_channels, | |
mean_only=True, | |
) | |
) | |
self.flows.append(Flip()) | |
def forward(self, x, x_mask, g=None, reverse=False): | |
if not reverse: | |
for flow in self.flows: | |
x, _ = flow(x, x_mask, g=g, reverse=reverse) | |
else: | |
for flow in reversed(self.flows): | |
x = flow(x, x_mask, g=g, reverse=reverse) | |
return x | |
class PosteriorEncoder(nn.Module): | |
def __init__( | |
self, | |
in_channels, | |
out_channels, | |
hidden_channels, | |
kernel_size, | |
dilation_rate, | |
n_layers, | |
gin_channels=0, | |
): | |
super().__init__() | |
self.in_channels = in_channels | |
self.out_channels = out_channels | |
self.hidden_channels = hidden_channels | |
self.kernel_size = kernel_size | |
self.dilation_rate = dilation_rate | |
self.n_layers = n_layers | |
self.gin_channels = gin_channels | |
self.pre = nn.Conv1d(in_channels, hidden_channels, 1) | |
self.enc = WN( | |
hidden_channels, | |
kernel_size, | |
dilation_rate, | |
n_layers, | |
gin_channels=gin_channels, | |
) | |
self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1) | |
def forward(self, x, x_lengths, g=None): | |
x_mask = torch.unsqueeze(sequence_mask(x_lengths, x.size(2)), 1).to(x.dtype) | |
x = self.pre(x) * x_mask | |
x = self.enc(x, x_mask, g=g) | |
stats = self.proj(x) * x_mask | |
m, logs = torch.split(stats, self.out_channels, dim=1) | |
z = (m + torch.randn_like(m) * torch.exp(logs)) * x_mask | |
return z, m, logs, x_mask | |
class SynthesizerTrn(nn.Module): | |
""" | |
Synthesizer for Training | |
""" | |
def __init__( | |
self, | |
n_vocab, | |
spec_channels, | |
segment_size, | |
inter_channels, | |
hidden_channels, | |
filter_channels, | |
n_heads, | |
n_layers, | |
kernel_size, | |
p_dropout, | |
resblock, | |
resblock_kernel_sizes, | |
resblock_dilation_sizes, | |
upsample_rates, | |
upsample_initial_channel, | |
upsample_kernel_sizes, | |
n_speakers=0, | |
gin_channels=0, | |
use_sdp=True, | |
**kwargs, | |
): | |
super().__init__() | |
self.n_vocab = n_vocab | |
self.spec_channels = spec_channels | |
self.inter_channels = inter_channels | |
self.hidden_channels = hidden_channels | |
self.filter_channels = filter_channels | |
self.n_heads = n_heads | |
self.n_layers = n_layers | |
self.kernel_size = kernel_size | |
self.p_dropout = p_dropout | |
self.resblock = resblock | |
self.resblock_kernel_sizes = resblock_kernel_sizes | |
self.resblock_dilation_sizes = resblock_dilation_sizes | |
self.upsample_rates = upsample_rates | |
self.upsample_initial_channel = upsample_initial_channel | |
self.upsample_kernel_sizes = upsample_kernel_sizes | |
self.segment_size = segment_size | |
self.n_speakers = n_speakers | |
self.gin_channels = gin_channels | |
self.use_sdp = use_sdp | |
self.enc_p = TextEncoder( | |
n_vocab, | |
inter_channels, | |
hidden_channels, | |
filter_channels, | |
n_heads, | |
n_layers, | |
kernel_size, | |
p_dropout, | |
) | |
self.dec = Generator( | |
inter_channels, | |
resblock, | |
resblock_kernel_sizes, | |
resblock_dilation_sizes, | |
upsample_rates, | |
upsample_initial_channel, | |
upsample_kernel_sizes, | |
gin_channels=gin_channels, | |
) | |
self.enc_q = PosteriorEncoder( | |
spec_channels, | |
inter_channels, | |
hidden_channels, | |
5, | |
1, | |
16, | |
gin_channels=gin_channels, | |
) | |
self.flow = ResidualCouplingBlock( | |
inter_channels, hidden_channels, 5, 1, 4, gin_channels=gin_channels | |
) | |
if use_sdp: | |
self.dp = StochasticDurationPredictor( | |
hidden_channels, 192, 3, 0.5, 4, gin_channels=gin_channels | |
) | |
else: | |
self.dp = DurationPredictor( | |
hidden_channels, 256, 3, 0.5, gin_channels=gin_channels | |
) | |
if n_speakers >= 1: | |
self.emb_g = nn.Embedding(n_speakers, gin_channels) | |
def forward(self, data): | |
x = data["phone_seq"] | |
x_lengths = data["phone_len"] | |
y = data["linear"] | |
y_lengths = data["target_len"] | |
x, m_p, logs_p, x_mask = self.enc_p(x, x_lengths) | |
if self.n_speakers > 0: | |
g = self.emb_g(data["spk_id"].squeeze(-1)).unsqueeze(-1) # [b, h, 1] | |
else: | |
g = None | |
z, m_q, logs_q, y_mask = self.enc_q(y, y_lengths, g=g) | |
z_p = self.flow(z, y_mask, g=g) | |
with torch.no_grad(): | |
# negative cross-entropy | |
s_p_sq_r = torch.exp(-2 * logs_p) # [b, d, t] | |
neg_cent1 = torch.sum( | |
-0.5 * math.log(2 * math.pi) - logs_p, [1], keepdim=True | |
) # [b, 1, t_s] | |
neg_cent2 = torch.matmul( | |
-0.5 * (z_p**2).transpose(1, 2), s_p_sq_r | |
) # [b, t_t, d] x [b, d, t_s] = [b, t_t, t_s] | |
neg_cent3 = torch.matmul( | |
z_p.transpose(1, 2), (m_p * s_p_sq_r) | |
) # [b, t_t, d] x [b, d, t_s] = [b, t_t, t_s] | |
neg_cent4 = torch.sum( | |
-0.5 * (m_p**2) * s_p_sq_r, [1], keepdim=True | |
) # [b, 1, t_s] | |
neg_cent = neg_cent1 + neg_cent2 + neg_cent3 + neg_cent4 | |
attn_mask = torch.unsqueeze(x_mask, 2) * torch.unsqueeze(y_mask, -1) | |
attn = ( | |
monotonic_align.maximum_path(neg_cent, attn_mask.squeeze(1)) | |
.unsqueeze(1) | |
.detach() | |
) | |
w = attn.sum(2) | |
if self.use_sdp: | |
l_length = self.dp(x, x_mask, w, g=g) | |
l_length = l_length / torch.sum(x_mask) | |
else: | |
logw_ = torch.log(w + 1e-6) * x_mask | |
logw = self.dp(x, x_mask, g=g) | |
l_length = torch.sum((logw - logw_) ** 2, [1, 2]) / torch.sum(x_mask) | |
# expand prior | |
m_p = torch.matmul(attn.squeeze(1), m_p.transpose(1, 2)).transpose(1, 2) | |
logs_p = torch.matmul(attn.squeeze(1), logs_p.transpose(1, 2)).transpose(1, 2) | |
z_slice, ids_slice = rand_slice_segments(z, y_lengths, self.segment_size) | |
o = self.dec(z_slice, g=g) | |
outputs = { | |
"y_hat": o, | |
"l_length": l_length, | |
"attn": attn, | |
"ids_slice": ids_slice, | |
"x_mask": x_mask, | |
"z_mask": y_mask, | |
"z": z, | |
"z_p": z_p, | |
"m_p": m_p, | |
"logs_p": logs_p, | |
"m_q": m_q, | |
"logs_q": logs_q, | |
} | |
return outputs | |
def infer( | |
self, | |
x, | |
x_lengths, | |
sid=None, | |
noise_scale=1, | |
length_scale=1, | |
noise_scale_w=1.0, | |
max_len=None, | |
): | |
x, m_p, logs_p, x_mask = self.enc_p(x, x_lengths) | |
if self.n_speakers > 0: | |
sid = sid.squeeze(-1) | |
g = self.emb_g(sid).unsqueeze(-1) # [b, h, 1] | |
else: | |
g = None | |
if self.use_sdp: | |
logw = self.dp(x, x_mask, g=g, reverse=True, noise_scale=noise_scale_w) | |
else: | |
logw = self.dp(x, x_mask, g=g) | |
w = torch.exp(logw) * x_mask * length_scale | |
w_ceil = torch.ceil(w) | |
y_lengths = torch.clamp_min(torch.sum(w_ceil, [1, 2]), 1).long() | |
y_mask = torch.unsqueeze(sequence_mask(y_lengths, None), 1).to(x_mask.dtype) | |
attn_mask = torch.unsqueeze(x_mask, 2) * torch.unsqueeze(y_mask, -1) | |
attn = generate_path(w_ceil, attn_mask) | |
m_p = torch.matmul(attn.squeeze(1), m_p.transpose(1, 2)).transpose( | |
1, 2 | |
) # [b, t', t], [b, t, d] -> [b, d, t'] | |
logs_p = torch.matmul(attn.squeeze(1), logs_p.transpose(1, 2)).transpose( | |
1, 2 | |
) # [b, t', t], [b, t, d] -> [b, d, t'] | |
z_p = m_p + torch.randn_like(m_p) * torch.exp(logs_p) * noise_scale | |
z = self.flow(z_p, y_mask, g=g, reverse=True) | |
o = self.dec((z * y_mask)[:, :, :max_len], g=g) | |
outputs = { | |
"y_hat": o, | |
"attn": attn, | |
"mask": y_mask, | |
"z": z, | |
"z_p": z_p, | |
"m_p": m_p, | |
"logs_p": logs_p, | |
} | |
return outputs | |
def voice_conversion(self, y, y_lengths, sid_src, sid_tgt): | |
assert self.n_speakers > 0, "n_speakers have to be larger than 0." | |
g_src = self.emb_g(sid_src).unsqueeze(-1) | |
g_tgt = self.emb_g(sid_tgt).unsqueeze(-1) | |
z, m_q, logs_q, y_mask = self.enc_q(y, y_lengths, g=g_src) | |
z_p = self.flow(z, y_mask, g=g_src) | |
z_hat = self.flow(z_p, y_mask, g=g_tgt, reverse=True) | |
o_hat = self.dec(z_hat * y_mask, g=g_tgt) | |
return o_hat, y_mask, (z, z_p, z_hat) | |