1313
1414
1515import math
16+
1617import numpy as np
1718import torch
1819import torch .nn as nn
1920import torch .nn .functional as F
21+
2022import tqdm
2123
22- from ..modeling_utils import ModelMixin
2324from ..configuration_utils import ConfigMixin
25+ from ..modeling_utils import ModelMixin
2426from ..pipeline_utils import DiffusionPipeline
2527
2628
@@ -46,8 +48,7 @@ def calc_diffusion_step_embedding(diffusion_steps, diffusion_step_embed_dim_in):
4648 _embed = np .log (10000 ) / (half_dim - 1 )
4749 _embed = torch .exp (torch .arange (half_dim ) * - _embed ).cuda ()
4850 _embed = diffusion_steps * _embed
49- diffusion_step_embed = torch .cat ((torch .sin (_embed ),
50- torch .cos (_embed )), 1 )
51+ diffusion_step_embed = torch .cat ((torch .sin (_embed ), torch .cos (_embed )), 1 )
5152 return diffusion_step_embed
5253
5354
@@ -67,8 +68,7 @@ class Conv(nn.Module):
6768 def __init__ (self , in_channels , out_channels , kernel_size = 3 , dilation = 1 ):
6869 super ().__init__ ()
6970 self .padding = dilation * (kernel_size - 1 ) // 2
70- self .conv = nn .Conv1d (in_channels , out_channels , kernel_size ,
71- dilation = dilation , padding = self .padding )
71+ self .conv = nn .Conv1d (in_channels , out_channels , kernel_size , dilation = dilation , padding = self .padding )
7272 self .conv = nn .utils .weight_norm (self .conv )
7373 nn .init .kaiming_normal_ (self .conv .weight )
7474
@@ -94,24 +94,20 @@ def forward(self, x):
9494# every residual block (named residual layer in paper)
9595# contains one noncausal dilated conv
9696class ResidualBlock (nn .Module ):
97- def __init__ (self , res_channels , skip_channels , dilation ,
98- diffusion_step_embed_dim_out ):
97+ def __init__ (self , res_channels , skip_channels , dilation , diffusion_step_embed_dim_out ):
9998 super ().__init__ ()
10099 self .res_channels = res_channels
101100
102101 # Use a FC layer for diffusion step embedding
103102 self .fc_t = nn .Linear (diffusion_step_embed_dim_out , self .res_channels )
104103
105104 # Dilated conv layer
106- self .dilated_conv_layer = Conv (self .res_channels , 2 * self .res_channels ,
107- kernel_size = 3 , dilation = dilation )
105+ self .dilated_conv_layer = Conv (self .res_channels , 2 * self .res_channels , kernel_size = 3 , dilation = dilation )
108106
109107 # Add mel spectrogram upsampler and conditioner conv1x1 layer
110108 self .upsample_conv2d = nn .ModuleList ()
111109 for s in [16 , 16 ]:
112- conv_trans2d = nn .ConvTranspose2d (1 , 1 , (3 , 2 * s ),
113- padding = (1 , s // 2 ),
114- stride = (1 , s ))
110+ conv_trans2d = nn .ConvTranspose2d (1 , 1 , (3 , 2 * s ), padding = (1 , s // 2 ), stride = (1 , s ))
115111 conv_trans2d = nn .utils .weight_norm (conv_trans2d )
116112 nn .init .kaiming_normal_ (conv_trans2d .weight )
117113 self .upsample_conv2d .append (conv_trans2d )
@@ -157,7 +153,7 @@ def forward(self, input_data):
157153 h += mel_spec
158154
159155 # Gated-tanh nonlinearity
160- out = torch .tanh (h [:, :self .res_channels , :]) * torch .sigmoid (h [:, self .res_channels :, :])
156+ out = torch .tanh (h [:, : self .res_channels , :]) * torch .sigmoid (h [:, self .res_channels :, :])
161157
162158 # Residual and skip outputs
163159 res = self .res_conv (out )
@@ -169,10 +165,16 @@ def forward(self, input_data):
169165
170166
171167class ResidualGroup (nn .Module ):
172- def __init__ (self , res_channels , skip_channels , num_res_layers , dilation_cycle ,
173- diffusion_step_embed_dim_in ,
174- diffusion_step_embed_dim_mid ,
175- diffusion_step_embed_dim_out ):
168+ def __init__ (
169+ self ,
170+ res_channels ,
171+ skip_channels ,
172+ num_res_layers ,
173+ dilation_cycle ,
174+ diffusion_step_embed_dim_in ,
175+ diffusion_step_embed_dim_mid ,
176+ diffusion_step_embed_dim_out ,
177+ ):
176178 super ().__init__ ()
177179 self .num_res_layers = num_res_layers
178180 self .diffusion_step_embed_dim_in = diffusion_step_embed_dim_in
@@ -185,16 +187,19 @@ def __init__(self, res_channels, skip_channels, num_res_layers, dilation_cycle,
185187 self .residual_blocks = nn .ModuleList ()
186188 for n in range (self .num_res_layers ):
187189 self .residual_blocks .append (
188- ResidualBlock (res_channels , skip_channels ,
189- dilation = 2 ** (n % dilation_cycle ),
190- diffusion_step_embed_dim_out = diffusion_step_embed_dim_out ))
190+ ResidualBlock (
191+ res_channels ,
192+ skip_channels ,
193+ dilation = 2 ** (n % dilation_cycle ),
194+ diffusion_step_embed_dim_out = diffusion_step_embed_dim_out ,
195+ )
196+ )
191197
192198 def forward (self , input_data ):
193199 x , mel_spectrogram , diffusion_steps = input_data
194200
195201 # Embed diffusion step t
196- diffusion_step_embed = calc_diffusion_step_embedding (
197- diffusion_steps , self .diffusion_step_embed_dim_in )
202+ diffusion_step_embed = calc_diffusion_step_embedding (diffusion_steps , self .diffusion_step_embed_dim_in )
198203 diffusion_step_embed = swish (self .fc_t1 (diffusion_step_embed ))
199204 diffusion_step_embed = swish (self .fc_t2 (diffusion_step_embed ))
200205
@@ -239,20 +244,24 @@ def __init__(
239244 diffusion_step_embed_dim_out = diffusion_step_embed_dim_out ,
240245 )
241246
242-
243247 # Initial conv1x1 with relu
244248 self .init_conv = nn .Sequential (Conv (in_channels , res_channels , kernel_size = 1 ), nn .ReLU (inplace = False ))
245249 # All residual layers
246- self .residual_layer = ResidualGroup (res_channels ,
247- skip_channels ,
248- num_res_layers ,
249- dilation_cycle ,
250- diffusion_step_embed_dim_in ,
251- diffusion_step_embed_dim_mid ,
252- diffusion_step_embed_dim_out )
250+ self .residual_layer = ResidualGroup (
251+ res_channels ,
252+ skip_channels ,
253+ num_res_layers ,
254+ dilation_cycle ,
255+ diffusion_step_embed_dim_in ,
256+ diffusion_step_embed_dim_mid ,
257+ diffusion_step_embed_dim_out ,
258+ )
253259 # Final conv1x1 -> relu -> zeroconv1x1
254- self .final_conv = nn .Sequential (Conv (skip_channels , skip_channels , kernel_size = 1 ),
255- nn .ReLU (inplace = False ), ZeroConv1d (skip_channels , out_channels ))
260+ self .final_conv = nn .Sequential (
261+ Conv (skip_channels , skip_channels , kernel_size = 1 ),
262+ nn .ReLU (inplace = False ),
263+ ZeroConv1d (skip_channels , out_channels ),
264+ )
256265
257266 def forward (self , input_data ):
258267 audio , mel_spectrogram , diffusion_steps = input_data
@@ -267,12 +276,12 @@ def __init__(self, diffwave, noise_scheduler):
267276 super ().__init__ ()
268277 noise_scheduler = noise_scheduler .set_format ("pt" )
269278 self .register_modules (diffwave = diffwave , noise_scheduler = noise_scheduler )
270-
279+
271280 @torch .no_grad ()
272281 def __call__ (self , mel_spectrogram , generator ):
273282 if torch_device is None :
274283 torch_device = "cuda" if torch .cuda .is_available () else "cpu"
275-
284+
276285 self .diffwave .to (torch_device )
277286
278287 audio_length = mel_spectrogram .size (- 1 ) * self .config .hop_len
@@ -301,4 +310,4 @@ def __call__(self, mel_spectrogram, generator):
301310 # 4. set current audio to prev_audio: x_t -> x_t-1
302311 audio = pred_prev_audio + variance
303312
304- return audio
313+ return audio
0 commit comments