forked from MoonInTheRiver/DiffSinger
-
Notifications
You must be signed in to change notification settings - Fork 285
/
toplevel.py
188 lines (159 loc) · 6.96 KB
/
toplevel.py
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import torch
import torch.nn as nn
import torch.nn.functional as F
from basics.base_module import CategorizedModule
from modules.commons.common_layers import (
XavierUniformInitLinear as Linear,
NormalInitEmbedding as Embedding
)
from modules.diffusion.ddpm import (
GaussianDiffusion, PitchDiffusion
)
from modules.fastspeech.acoustic_encoder import FastSpeech2Acoustic
from modules.fastspeech.param_adaptor import ParameterAdaptorModule
from modules.fastspeech.tts_modules import RhythmRegulator, LengthRegulator
from modules.fastspeech.variance_encoder import FastSpeech2Variance
from utils.hparams import hparams
class DiffSingerAcoustic(ParameterAdaptorModule, CategorizedModule):
@property
def category(self):
return 'acoustic'
def __init__(self, vocab_size, out_dims):
super().__init__()
self.fs2 = FastSpeech2Acoustic(
vocab_size=vocab_size
)
self.diffusion = GaussianDiffusion(
out_dims=out_dims,
num_feats=1,
timesteps=hparams['timesteps'],
k_step=hparams['K_step'],
denoiser_type=hparams['diff_decoder_type'],
denoiser_args={
'n_layers': hparams['residual_layers'],
'n_chans': hparams['residual_channels'],
'n_dilates': hparams['dilation_cycle_length'],
},
spec_min=hparams['spec_min'],
spec_max=hparams['spec_max']
)
def forward(
self, txt_tokens, mel2ph, f0, key_shift=None, speed=None,
spk_embed_id=None, gt_mel=None, infer=True, **kwargs
):
condition = self.fs2(
txt_tokens, mel2ph, f0, key_shift=key_shift, speed=speed,
spk_embed_id=spk_embed_id, **kwargs
)
if infer:
mel_pred = self.diffusion(condition, infer=True)
mel_pred *= ((mel2ph > 0).float()[:, :, None])
return mel_pred
else:
x_recon, noise = self.diffusion(condition, gt_spec=gt_mel, infer=False)
return x_recon, noise
class DiffSingerVariance(ParameterAdaptorModule, CategorizedModule):
@property
def category(self):
return 'variance'
def __init__(self, vocab_size):
super().__init__()
self.predict_dur = hparams['predict_dur']
self.use_spk_id = hparams['use_spk_id']
if self.use_spk_id:
self.spk_embed = Embedding(hparams['num_spk'], hparams['hidden_size'])
self.fs2 = FastSpeech2Variance(
vocab_size=vocab_size
)
self.rr = RhythmRegulator()
self.lr = LengthRegulator()
self.predict_pitch = hparams['predict_pitch']
if self.predict_pitch or self.predict_variances:
self.retake_embed = Embedding(2, hparams['hidden_size'])
if self.predict_pitch:
pitch_hparams = hparams['pitch_prediction_args']
self.base_pitch_embed = Linear(1, hparams['hidden_size'])
self.pitch_predictor = PitchDiffusion(
vmin=pitch_hparams['pitd_norm_min'],
vmax=pitch_hparams['pitd_norm_max'],
cmin=pitch_hparams['pitd_clip_min'],
cmax=pitch_hparams['pitd_clip_max'],
repeat_bins=pitch_hparams['repeat_bins'],
timesteps=hparams['timesteps'],
k_step=hparams['K_step'],
denoiser_type=hparams['diff_decoder_type'],
denoiser_args={
'n_layers': pitch_hparams['residual_layers'],
'n_chans': pitch_hparams['residual_channels'],
'n_dilates': pitch_hparams['dilation_cycle_length'],
}
)
if self.predict_variances:
self.pitch_embed = Linear(1, hparams['hidden_size'])
self.variance_embeds = nn.ModuleDict({
v_name: Linear(1, hparams['hidden_size'])
for v_name in self.variance_prediction_list
})
self.variance_predictor = self.build_adaptor()
def forward(
self, txt_tokens, midi, ph2word, ph_dur=None, word_dur=None, mel2ph=None,
base_pitch=None, pitch=None, retake=None, spk_id=None, infer=True, **kwargs
):
if self.use_spk_id:
spk_embed = self.spk_embed(spk_id)[:, None, :] # [B,] => [B, T=1, H]
else:
spk_embed = None
encoder_out, dur_pred_out = self.fs2(
txt_tokens, midi=midi, ph2word=ph2word,
ph_dur=ph_dur, word_dur=word_dur,
spk_embed=spk_embed, infer=infer
)
if not self.predict_pitch and not self.predict_variances:
return dur_pred_out, None, None, ({} if infer else None)
if mel2ph is None and word_dur is not None: # inference from file
dur_pred_align = self.rr(dur_pred_out, ph2word, word_dur)
mel2ph = self.lr(dur_pred_align)
mel2ph = F.pad(mel2ph, [0, base_pitch.shape[1] - mel2ph.shape[1]])
encoder_out = F.pad(encoder_out, [0, 0, 1, 0])
mel2ph_ = mel2ph[..., None].repeat([1, 1, hparams['hidden_size']])
condition = torch.gather(encoder_out, 1, mel2ph_)
if self.use_spk_id:
condition += spk_embed
if retake is None:
retake_embed = self.retake_embed(torch.ones_like(mel2ph))
else:
retake_embed = self.retake_embed(retake.long())
condition += retake_embed
if self.predict_pitch:
if retake is not None:
base_pitch = base_pitch * retake + pitch * ~retake
pitch_cond = condition + self.base_pitch_embed(base_pitch[:, :, None])
if infer:
pitch_pred_out = self.pitch_predictor(pitch_cond, infer=True)
else:
pitch_pred_out = self.pitch_predictor(pitch_cond, pitch - base_pitch, infer=False)
else:
pitch_pred_out = None
if not self.predict_variances:
return dur_pred_out, pitch_pred_out, ({} if infer else None)
if pitch is None:
pitch = base_pitch + pitch_pred_out
condition += self.pitch_embed(pitch[:, :, None])
variance_inputs = self.collect_variance_inputs(**kwargs)
if retake is None:
variance_embeds = [
self.variance_embeds[v_name](torch.zeros_like(pitch)[:, :, None])
for v_name in self.variance_prediction_list
]
else:
variance_embeds = [
self.variance_embeds[v_name]((v_input * ~retake)[:, :, None])
for v_name, v_input in zip(self.variance_prediction_list, variance_inputs)
]
condition += torch.stack(variance_embeds, dim=-1).sum(-1)
variance_outputs = self.variance_predictor(condition, variance_inputs, infer)
if infer:
variances_pred_out = self.collect_variance_outputs(variance_outputs)
else:
variances_pred_out = variance_outputs
return dur_pred_out, pitch_pred_out, variances_pred_out