def forward(self, x, x_lengths, speaker_embedding, g=None): 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(commons.sequence_mask(x_lengths, x.size(2)), 1).to(x.dtype) # Append speaker embeddings speaker_embedding = speaker_embedding.unsqueeze(2) speaker_embedding = speaker_embedding.repeat(1, 1, x.shape[2]) x = torch.cat((x, speaker_embedding), dim=1) if self.prenet: x = self.pre(x, x_mask) x = self.encoder(x, x_mask) if g is not None: g_exp = g.expand(-1, -1, x.size(-1)) x_dp = torch.cat([torch.detach(x), g_exp], 1) else: x_dp = torch.detach(x) x_m = self.proj_m(x) * x_mask if not self.mean_only: x_logs = self.proj_s(x) * x_mask else: x_logs = torch.zeros_like(x_m) logw = self.proj_w(x_dp, x_mask) return x_m, x_logs, logw, x_mask
def infer(self, x, x_lengths, sid=None, noise_scale=1, length_scale=1, noise_scale_w=1., max_len=None): x, m_p, logs_p, x_mask = self.enc_p(x, x_lengths) if self.n_speakers > 0: 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(commons.sequence_mask(y_lengths, None), 1).to(x_mask.dtype) attn_mask = torch.unsqueeze(x_mask, 2) * torch.unsqueeze(y_mask, -1) attn = commons.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) return o, attn, y_mask, (z, z_p, m_p, logs_p)
def forward(self, x, x_lengths, g=None): x_mask = torch.unsqueeze(commons.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
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(commons.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
def forward(self, x, x_lengths, speaker_embedding, y=None, y_lengths=None, g=None, gen=False, noise_scale=1., length_scale=1.): if g is not None: g = F.normalize(self.emb_g(g)).unsqueeze(-1) # [b, h] x_m, x_logs, logw, x_mask = self.encoder(x, x_lengths, speaker_embedding, g=g) if gen: 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_max_length = None else: y_max_length = y.size(2) y, y_lengths, y_max_length = self.preprocess(y, y_lengths, y_max_length) y_mask = torch.unsqueeze(commons.sequence_mask(y_lengths, y_max_length), 1).to(x_mask.dtype) attn_mask = torch.unsqueeze(x_mask, -1) * torch.unsqueeze(y_mask, 2) if gen: attn = commons.generate_path(w_ceil.squeeze(1), attn_mask.squeeze(1)).unsqueeze(1) y_m = torch.matmul(attn.squeeze(1).transpose(1, 2), x_m.transpose(1, 2)).transpose(1, 2) # [b, t', t], [b, t, d] -> [b, d, t'] y_logs = torch.matmul(attn.squeeze(1).transpose(1, 2), x_logs.transpose(1, 2)).transpose(1, 2) # [b, t', t], [b, t, d] -> [b, d, t'] logw_ = torch.log(1e-8 + torch.sum(attn, -1)) * x_mask z = (y_m + torch.exp(y_logs) * torch.randn_like(y_m) * noise_scale) * y_mask y, logdet = self.decoder(z, y_mask, g=g, reverse=True) return (y, y_m, y_logs, logdet), attn, logw, logw_, x_m, x_logs else: z, logdet = self.decoder(y, y_mask, g=g, reverse=False) with torch.no_grad(): x_s_sq_r = torch.exp(-2 * x_logs) logp1 = torch.sum(-0.5 * math.log(2 * math.pi) - x_logs, [1]).unsqueeze(-1) # [b, t, 1] logp2 = torch.matmul(x_s_sq_r.transpose(1, 2), -0.5 * (z ** 2)) # [b, t, d] x [b, d, t'] = [b, t, t'] logp3 = torch.matmul((x_m * x_s_sq_r).transpose(1, 2), z) # [b, t, d] x [b, d, t'] = [b, t, t'] logp4 = torch.sum(-0.5 * (x_m ** 2) * x_s_sq_r, [1]).unsqueeze(-1) # [b, t, 1] logp = logp1 + logp2 + logp3 + logp4 # [b, t, t'] # Only used during training, which is done on linux # imports cython function which will not work on windows import tts_dev.glow_model.monotonic_align as monotonic_align attn = monotonic_align.maximum_path(logp, attn_mask.squeeze(1)).unsqueeze(1).detach() y_m = torch.matmul(attn.squeeze(1).transpose(1, 2), x_m.transpose(1, 2)).transpose(1, 2) # [b, t', t], [b, t, d] -> [b, d, t'] y_logs = torch.matmul(attn.squeeze(1).transpose(1, 2), x_logs.transpose(1, 2)).transpose(1, 2) # [b, t', t], [b, t, d] -> [b, d, t'] logw_ = torch.log(1e-8 + torch.sum(attn, -1)) * x_mask return (z, y_m, y_logs, logdet), attn, logw, logw_, x_m, x_logs
def forward(self, x, x_lengths,g=None): g=None x = self.pre_enc(x) x_mask = torch.unsqueeze(commons.sequence_mask(x_lengths, x.size(2)), 1).to(x.dtype) x = self.encoder(x * x_mask, x_mask,g) stats = self.proj(x) * x_mask m, logs = torch.split(stats, self.out_channels, dim=1) return x, m, logs, x_mask
def forward(self, x, x_lengths, y=None, y_lengths=None, g=None, gen=False, noise_scale=1., length_scale=1.): if g is not None: g = F.normalize(self.emb_g(g)).unsqueeze(-1) # [b, h] x_m, x_logs, logw, x_mask = self.encoder(x, x_lengths, g=g) if gen: 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_max_length = None else: y_max_length = y.size(2) y, y_lengths, y_max_length = self.preprocess(y, y_lengths, y_max_length) z_mask = torch.unsqueeze( commons.sequence_mask(y_lengths, y_max_length), 1).to(x_mask.dtype) attn_mask = torch.unsqueeze(x_mask, -1) * torch.unsqueeze(z_mask, 2) if gen: attn = commons.generate_path(w_ceil.squeeze(1), attn_mask.squeeze(1)).unsqueeze(1) z_m = torch.matmul( attn.squeeze(1).transpose(1, 2), x_m.transpose( 1, 2)).transpose(1, 2) # [b, t', t], [b, t, d] -> [b, d, t'] z_logs = torch.matmul( attn.squeeze(1).transpose(1, 2), x_logs.transpose( 1, 2)).transpose(1, 2) # [b, t', t], [b, t, d] -> [b, d, t'] logw_ = torch.log(1e-8 + torch.sum(attn, -1)) * x_mask z = (z_m + torch.exp(z_logs) * torch.randn_like(z_m) * noise_scale) * z_mask y, logdet = self.decoder(z, z_mask, g=g, reverse=True) return (y, z_m, z_logs, logdet, z_mask), (x_m, x_logs, x_mask), (attn, logw, logw_) else: z, logdet = self.decoder(y, z_mask, g=g, reverse=False) with torch.no_grad(): x_s_sq_r = torch.exp(-2 * x_logs) logp1 = torch.sum(-0.5 * math.log(2 * math.pi) - x_logs, [1]).unsqueeze(-1) # [b, t, 1] logp2 = torch.matmul( x_s_sq_r.transpose(1, 2), -0.5 * (z**2)) # [b, t, d] x [b, d, t'] = [b, t, t'] logp3 = torch.matmul((x_m * x_s_sq_r).transpose(1, 2), z) # [b, t, d] x [b, d, t'] = [b, t, t'] logp4 = torch.sum(-0.5 * (x_m**2) * x_s_sq_r, [1]).unsqueeze(-1) # [b, t, 1] logp = logp1 + logp2 + logp3 + logp4 # [b, t, t'] attn = monotonic_align.maximum_path( logp, attn_mask.squeeze(1)).unsqueeze(1).detach() z_m = torch.matmul( attn.squeeze(1).transpose(1, 2), x_m.transpose( 1, 2)).transpose(1, 2) # [b, t', t], [b, t, d] -> [b, d, t'] z_logs = torch.matmul( attn.squeeze(1).transpose(1, 2), x_logs.transpose( 1, 2)).transpose(1, 2) # [b, t', t], [b, t, d] -> [b, d, t'] logw_ = torch.log(1e-8 + torch.sum(attn, -1)) * x_mask return (z, z_m, z_logs, logdet, z_mask), (x_m, x_logs, x_mask), (attn, logw, logw_)