MCPcopy Create free account
hub / github.com/MoonInTheRiver/DiffSinger / forward

Method forward

usr/diff/diffusion.py:296–318  ·  view source on GitHub ↗
(self, txt_tokens, mel2ph=None, spk_embed=None,
                ref_mels=None, f0=None, uv=None, energy=None, infer=False)

Source from the content-addressed store, hash-verified

294 return loss
295
296 def forward(self, txt_tokens, mel2ph=None, spk_embed=None,
297 ref_mels=None, f0=None, uv=None, energy=None, infer=False):
298 b, *_, device = *txt_tokens.shape, txt_tokens.device
299 ret = self.fs2(txt_tokens, mel2ph, spk_embed, ref_mels, f0, uv, energy,
300 skip_decoder=True, infer=infer)
301 cond = ret['decoder_inp'].transpose(1, 2)
302 if not infer:
303 t = torch.randint(0, self.num_timesteps, (b,), device=device).long()
304 x = ref_mels
305 x = self.norm_spec(x)
306 x = x.transpose(1, 2)[:, None, :, :] # [B, 1, M, T]
307 nonpadding = (mel2ph != 0).float()
308 ret['diff_loss'] = self.p_losses(x, t, cond, nonpadding=nonpadding)
309 else:
310 t = self.num_timesteps
311 shape = (cond.shape[0], 1, self.mel_bins, cond.shape[2])
312 x = torch.randn(shape, device=device)
313 for i in tqdm(reversed(range(0, t)), desc='sample time step', total=t):
314 x = self.p_sample(x, torch.full((b,), i, device=device, dtype=torch.long), cond)
315 x = x[:, 0].transpose(1, 2)
316 ret['mel_out'] = self.denorm_spec(x)
317
318 return ret
319
320 def norm_spec(self, x):
321 return (x - self.spec_min) / (self.spec_max - self.spec_min) * 2 - 1

Callers

nothing calls this directly

Calls 4

norm_specMethod · 0.95
p_lossesMethod · 0.95
p_sampleMethod · 0.95
denorm_specMethod · 0.95

Tested by

no test coverage detected