-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmodules.py
721 lines (633 loc) · 27.8 KB
/
modules.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
import os
import json
import copy
import math
from collections import OrderedDict
import torch
import torch.nn as nn
from torch.nn import Conv1d, ConvTranspose1d
from torch.nn.utils import weight_norm, remove_weight_norm
from numba import jit, prange
import numpy as np
import torch.nn.functional as F
from utils.pitch_tools import f0_to_coarse, denorm_f0
from utils.tools import get_mask_from_lengths, pad, init_weights
from .blocks import (
Embedding,
SinusoidalPositionalEmbedding,
LayerNorm,
LinearNorm,
SwishBlock,
ConvBlock,
ConvNorm,
BatchNorm1dTBC,
EncSALayer,
ResBlock1,
ResBlock2,
)
from text.symbols import symbols
@jit(nopython=True)
def mas_width1(attn_map):
"""mas with hardcoded width=1"""
# assumes mel x text
opt = np.zeros_like(attn_map)
attn_map = np.log(attn_map)
attn_map[0, 1:] = -np.inf
log_p = np.zeros_like(attn_map)
log_p[0, :] = attn_map[0, :]
prev_ind = np.zeros_like(attn_map, dtype=np.int64)
for i in range(1, attn_map.shape[0]):
for j in range(attn_map.shape[1]): # for each text dim
prev_log = log_p[i - 1, j]
prev_j = j
if j - 1 >= 0 and log_p[i - 1, j - 1] >= log_p[i - 1, j]:
prev_log = log_p[i - 1, j - 1]
prev_j = j - 1
log_p[i, j] = attn_map[i, j] + prev_log
prev_ind[i, j] = prev_j
# now backtrack
curr_text_idx = attn_map.shape[1] - 1
for i in range(attn_map.shape[0] - 1, -1, -1):
opt[i, curr_text_idx] = 1
curr_text_idx = prev_ind[i, curr_text_idx]
opt[0, curr_text_idx] = 1
return opt
@jit(nopython=True, parallel=True)
def b_mas(b_attn_map, in_lens, out_lens, width=1):
assert width == 1
attn_out = np.zeros_like(b_attn_map)
for b in prange(b_attn_map.shape[0]):
out = mas_width1(b_attn_map[b, 0, : out_lens[b], : in_lens[b]])
attn_out[b, 0, : out_lens[b], : in_lens[b]] = out
return attn_out
class TransformerEncoderLayer(nn.Module):
def __init__(self, hidden_size, dropout, kernel_size=None, num_heads=2, norm="ln", ffn_padding="SAME", ffn_act="gelu"):
super().__init__()
self.hidden_size = hidden_size
self.dropout = dropout
self.num_heads = num_heads
self.op = EncSALayer(
hidden_size, num_heads, dropout=dropout,
attention_dropout=0.0, relu_dropout=dropout,
kernel_size=kernel_size,
padding=ffn_padding,
norm=norm, act=ffn_act)
def forward(self, x, **kwargs):
return self.op(x, **kwargs)
class FFTBlocks(nn.Module):
def __init__(self, hidden_size, num_layers, max_seq_len=2000, ffn_kernel_size=9, dropout=None, num_heads=2,
use_pos_embed=True, use_last_norm=True, norm="ln", ffn_padding="SAME", ffn_act="gelu", use_pos_embed_alpha=True):
super().__init__()
self.num_layers = num_layers
embed_dim = self.hidden_size = hidden_size
self.dropout = dropout
self.use_pos_embed = use_pos_embed
self.use_last_norm = use_last_norm
if use_pos_embed:
self.max_source_positions = max_seq_len
self.padding_idx = 0
self.pos_embed_alpha = nn.Parameter(torch.Tensor([1])) if use_pos_embed_alpha else 1
self.embed_positions = SinusoidalPositionalEmbedding(
embed_dim, self.padding_idx, init_size=max_seq_len,
)
self.layers = nn.ModuleList([])
self.layers.extend([
TransformerEncoderLayer(self.hidden_size, self.dropout,
kernel_size=ffn_kernel_size, num_heads=num_heads, ffn_padding=ffn_padding, ffn_act=ffn_act)
for _ in range(self.num_layers)
])
if self.use_last_norm:
if norm == "ln":
self.layer_norm = nn.LayerNorm(embed_dim)
elif norm == "bn":
self.layer_norm = BatchNorm1dTBC(embed_dim)
else:
self.layer_norm = None
def forward(self, x, padding_mask=None, attn_mask=None, return_hiddens=False):
"""
:param x: [B, T, C]
:param padding_mask: [B, T]
:return: [B, T, C] or [L, B, T, C]
"""
padding_mask = x.abs().sum(-1).eq(0).data if padding_mask is None else padding_mask
nonpadding_mask_TB = 1 - padding_mask.transpose(0, 1).float()[:, :, None] # [T, B, 1]
if self.use_pos_embed:
positions = self.pos_embed_alpha * self.embed_positions(x[..., 0])
x = x + positions
x = F.dropout(x, p=self.dropout, training=self.training)
# B x T x C -> T x B x C
x = x.transpose(0, 1) * nonpadding_mask_TB
hiddens = []
for layer in self.layers:
x = layer(x, encoder_padding_mask=padding_mask, attn_mask=attn_mask) * nonpadding_mask_TB
hiddens.append(x)
if self.use_last_norm:
x = self.layer_norm(x) * nonpadding_mask_TB
if return_hiddens:
x = torch.stack(hiddens, 0) # [L, T, B, C]
x = x.transpose(1, 2) # [L, B, T, C]
else:
x = x.transpose(0, 1) # [B, T, C]
return x, padding_mask
class TextEncoder(FFTBlocks):
def __init__(self, config):
max_seq_len = config["max_seq_len"]
hidden_size = config["transformer"]["encoder_hidden"]
super().__init__(
hidden_size,
config["transformer"]["encoder_layer"],
max_seq_len=max_seq_len * 2,
ffn_kernel_size=config["transformer"]["ffn_kernel_size"],
dropout=config["transformer"]["encoder_dropout"],
num_heads=config["transformer"]["encoder_head"],
use_pos_embed=False, # use_pos_embed_alpha for compatibility
ffn_padding=config["transformer"]["ffn_padding"],
ffn_act=config["transformer"]["ffn_act"],
)
self.padding_idx = 0
self.embed_tokens = Embedding(
len(symbols) + 1, hidden_size, self.padding_idx
)
self.embed_scale = math.sqrt(hidden_size)
self.embed_positions = SinusoidalPositionalEmbedding(
hidden_size, self.padding_idx, init_size=max_seq_len,
)
def forward(self, txt_tokens, encoder_padding_mask):
"""
:param txt_tokens: [B, T]
:param encoder_padding_mask: [B, T]
:return: {
"encoder_out": [T x B x C]
}
"""
x, src_word_emb = self.forward_embedding(txt_tokens) # [B, T, H]
x, _ = super(TextEncoder, self).forward(x, encoder_padding_mask)
return x, src_word_emb
def forward_embedding(self, txt_tokens):
# embed tokens and positions
txt_embs = self.embed_scale * self.embed_tokens(txt_tokens)
positions = self.embed_positions(txt_tokens)
x = txt_embs + positions
x = F.dropout(x, p=self.dropout, training=self.training)
return x, txt_embs
class Decoder(FFTBlocks):
def __init__(self, config):
super().__init__(
config["transformer"]["decoder_hidden"],
config["transformer"]["decoder_layer"],
max_seq_len=config["max_seq_len"] * 2,
ffn_kernel_size=config["transformer"]["ffn_kernel_size"],
dropout=config["transformer"]["decoder_dropout"],
num_heads=config["transformer"]["decoder_head"],
ffn_padding=config["transformer"]["ffn_padding"],
ffn_act=config["transformer"]["ffn_act"],
)
class Upsampler(torch.nn.Module):
def __init__(self, preprocess_config, model_config, train_config):
super(Upsampler, self).__init__()
self.lrelu_slope = model_config["generator"]["lrelu_slope"]
in_channels = model_config["transformer"]["decoder_hidden"]
# in_channels = preprocess_config["preprocessing"]["mel"]["n_mel_channels"]
resblock_kernel_sizes = model_config["generator"]["resblock_kernel_sizes"]
upsample_rates = model_config["generator"]["upsample_rates"]
upsample_initial_channel = model_config["generator"]["upsample_initial_channel"]
resblock = model_config["generator"]["resblock"]
upsample_kernel_sizes = model_config["generator"]["upsample_kernel_sizes"]
resblock_dilation_sizes = model_config["generator"]["resblock_dilation_sizes"]
self.num_kernels = len(resblock_kernel_sizes)
self.num_upsamples = len(upsample_rates)
self.conv_pre = weight_norm(Conv1d(in_channels, upsample_initial_channel, 7, 1, padding=3))
resblock = ResBlock1 if resblock == '1' else ResBlock2
self.ups = nn.ModuleList()
for i, (u, k) in enumerate(zip(upsample_rates, upsample_kernel_sizes)):
self.ups.append(weight_norm(
ConvTranspose1d(upsample_initial_channel//(2**i), upsample_initial_channel//(2**(i+1)),
k, u, padding=(k-u)//2)))
self.resblocks = nn.ModuleList()
for i in range(len(self.ups)):
ch = upsample_initial_channel//(2**(i+1))
for j, (k, d) in enumerate(zip(resblock_kernel_sizes, resblock_dilation_sizes)):
self.resblocks.append(resblock(ch, k, d, self.lrelu_slope))
self.conv_post = weight_norm(Conv1d(ch, 1, 7, 1, padding=3))
self.ups.apply(init_weights)
self.conv_post.apply(init_weights)
def forward(self, x):
x = self.conv_pre(x)
for i in range(self.num_upsamples):
x = F.leaky_relu(x, self.lrelu_slope)
x = self.ups[i](x)
xs = None
for j in range(self.num_kernels):
if xs is None:
xs = self.resblocks[i*self.num_kernels+j](x)
else:
xs += self.resblocks[i*self.num_kernels+j](x)
x = xs / self.num_kernels
x = F.leaky_relu(x)
x = self.conv_post(x)
x = torch.tanh(x)
return x
def remove_weight_norm(self):
print('Removing weight norm...')
for l in self.ups:
remove_weight_norm(l)
for l in self.resblocks:
l.remove_weight_norm()
remove_weight_norm(self.conv_pre)
remove_weight_norm(self.conv_post)
class VarianceAdaptor(nn.Module):
""" Variance Adaptor """
def __init__(self, preprocess_config, model_config, train_config):
super(VarianceAdaptor, self).__init__()
self.preprocess_config = preprocess_config
# self.var_start_steps = train_config["step"]["var_start_steps"]
self.binarization_start_steps = train_config["duration"]["binarization_start_steps"]
self.use_pitch_embed = model_config["variance_embedding"]["use_pitch_embed"]
self.use_energy_embed = model_config["variance_embedding"]["use_energy_embed"]
self.predictor_grad = model_config["variance_predictor"]["predictor_grad"]
self.hidden_size = model_config["transformer"]["encoder_hidden"]
self.filter_size = model_config["variance_predictor"]["filter_size"]
self.predictor_layers = model_config["variance_predictor"]["predictor_layers"]
self.dropout = model_config["variance_predictor"]["dropout"]
self.ffn_padding = model_config["transformer"]["ffn_padding"]
self.kernel = model_config["variance_predictor"]["predictor_kernel"]
self.aligner = AlignmentEncoder(
n_mel_channels=preprocess_config["preprocessing"]["mel"]["n_mel_channels"],
n_att_channels=preprocess_config["preprocessing"]["mel"]["n_mel_channels"],
n_text_channels=model_config["transformer"]["encoder_hidden"],
temperature=model_config["duration_modeling"]["aligner_temperature"],
multi_speaker=model_config["multi_speaker"],
)
self.duration_predictor = DurationPredictor(
self.hidden_size,
n_chans=self.filter_size,
n_layers=model_config["variance_predictor"]["dur_predictor_layers"],
dropout_rate=self.dropout, padding=self.ffn_padding,
kernel_size=model_config["variance_predictor"]["dur_predictor_kernel"],
dur_loss=train_config["loss"]["dur_loss"])
self.length_regulator = LengthRegulator()
if self.use_pitch_embed:
n_bins = model_config["variance_embedding"]["pitch_n_bins"]
self.pitch_type = preprocess_config["preprocessing"]["pitch"]["pitch_type"]
self.use_uv = preprocess_config["preprocessing"]["pitch"]["use_uv"]
self.pitch_predictor = PitchPredictor(
self.hidden_size,
n_chans=self.filter_size,
n_layers=self.predictor_layers,
dropout_rate=self.dropout,
odim=2 if self.pitch_type == "frame" else 1,
padding=self.ffn_padding, kernel_size=self.kernel)
self.pitch_embedding = Embedding(n_bins, self.hidden_size, padding_idx=0)
if self.use_energy_embed:
energy_quantization = model_config["variance_embedding"]["energy_quantization"]
assert energy_quantization in ["linear", "log"]
n_bins = model_config["variance_embedding"]["energy_n_bins"]
with open(
os.path.join(preprocess_config["path"]["preprocessed_path"], "stats.json")
) as f:
stats = json.load(f)
energy_min, energy_max = stats[f"energy"][:2]
self.energy_predictor = EnergyPredictor(
self.hidden_size,
n_chans=self.filter_size,
n_layers=self.predictor_layers,
dropout_rate=self.dropout, odim=1,
padding=self.ffn_padding, kernel_size=self.kernel)
if energy_quantization == "log":
self.energy_bins = nn.Parameter(
torch.exp(
torch.linspace(np.log(energy_min), np.log(energy_max), n_bins - 1)
),
requires_grad=False,
)
else:
self.energy_bins = nn.Parameter(
torch.linspace(energy_min, energy_max, n_bins - 1),
requires_grad=False,
)
self.energy_embedding = Embedding(n_bins, self.hidden_size, padding_idx=0)
def binarize_attention_parallel(self, attn, in_lens, out_lens):
"""For training purposes only. Binarizes attention with MAS.
These will no longer recieve a gradient.
Args:
attn: B x 1 x max_mel_len x max_text_len
"""
with torch.no_grad():
attn_cpu = attn.data.cpu().numpy()
attn_out = b_mas(attn_cpu, in_lens.cpu().numpy(), out_lens.cpu().numpy(), width=1)
return torch.from_numpy(attn_out).to(attn.device)
def get_pitch_embedding(self, decoder_inp, f0, uv, control):
decoder_inp = decoder_inp.detach() + self.predictor_grad * (decoder_inp - decoder_inp.detach())
pitch_pred = self.pitch_predictor(decoder_inp) * control
if f0 is None:
f0 = pitch_pred[:, :, 0]
if self.use_uv and uv is None:
uv = pitch_pred[:, :, 1] > 0
f0_denorm = denorm_f0(f0, uv, self.preprocess_config["preprocessing"]["pitch"])
pitch = f0_to_coarse(f0_denorm) # start from 0
pitch_embed = self.pitch_embedding(pitch)
pitch_pred = {
"pitch_pred": pitch_pred,
"f0_denorm": f0_denorm,
}
return pitch_pred, pitch_embed
def get_energy_embedding(self, x, target, control):
x.detach() + self.predictor_grad * (x - x.detach())
prediction = self.energy_predictor(x, squeeze=True)
if target is not None:
embedding = self.energy_embedding(torch.bucketize(target, self.energy_bins))
else:
prediction = prediction * control
embedding = self.energy_embedding(
torch.bucketize(prediction, self.energy_bins)
)
return prediction, embedding
def forward(
self,
x,
text_embedding,
src_len,
max_src_len,
src_mask,
mel=None,
mel_len=None,
max_mel_len=None,
mel_mask=None,
pitch_target=None,
energy_target=None,
seq_start=None,
attn_prior=None,
speaker_embedding=None,
step=1,
p_control=1.0,
e_control=1.0,
d_control=1.0,
):
if speaker_embedding is not None:
x = x + speaker_embedding.unsqueeze(1).expand(
-1, x.shape[1], -1
)
# Duration Prediction
log_duration_prediction = self.duration_predictor(
x.detach() + self.predictor_grad * (x - x.detach()), src_mask
)
# Differential Duration
attn_out = None
if attn_prior is not None:
attn_soft, attn_logprob = self.aligner(
mel.transpose(1, 2),
text_embedding.transpose(1, 2),
src_mask.unsqueeze(-1),
attn_prior.transpose(1, 2),
speaker_embedding,
)
attn_hard = self.binarize_attention_parallel(attn_soft, src_len, mel_len)
attn_hard_dur = attn_hard.sum(2)[:, 0, :]
attn_out = (attn_soft, attn_hard, attn_hard_dur, attn_logprob)
# Upsampling
if attn_prior is not None: # Trainig of unsupervised duration modeling
if step < self.binarization_start_steps:
A_soft = attn_soft.squeeze(1)
x = torch.bmm(A_soft,x)
else:
x, mel_len = self.length_regulator(x, attn_hard_dur, max_mel_len)
duration_rounded = attn_hard_dur
else: # Inference
duration_rounded = torch.clamp(
(torch.round(torch.exp(log_duration_prediction) - 1) * d_control),
min=0,
)
x, mel_len = self.length_regulator(x, duration_rounded, max_mel_len)
mel_mask = get_mask_from_lengths(mel_len)
# Variances
pitch_prediction = energy_prediction = None
x_temp = x.clone()
if self.use_pitch_embed:
if pitch_target is not None:
pitch_prediction, pitch_embedding = self.get_pitch_embedding(
x, pitch_target["f0"], pitch_target["uv"], p_control
)
else:
pitch_prediction, pitch_embedding = self.get_pitch_embedding(
x, None, None, p_control
)
x_temp = x_temp + pitch_embedding
if self.use_energy_embed:
energy_prediction, energy_embedding = self.get_energy_embedding(x, energy_target, e_control)
x_temp = x_temp + energy_embedding
x = x_temp.clone()
return (
x,
log_duration_prediction,
duration_rounded,
mel_len,
mel_mask,
pitch_prediction,
energy_prediction,
attn_out,
)
class AlignmentEncoder(torch.nn.Module):
""" Alignment Encoder for Unsupervised Duration Modeling """
def __init__(self,
n_mel_channels,
n_att_channels,
n_text_channels,
temperature,
multi_speaker):
super().__init__()
self.temperature = temperature
self.softmax = torch.nn.Softmax(dim=3)
self.log_softmax = torch.nn.LogSoftmax(dim=3)
self.key_proj = nn.Sequential(
ConvNorm(
n_text_channels,
n_text_channels * 2,
kernel_size=3,
bias=True,
w_init_gain='relu'
),
torch.nn.ReLU(),
ConvNorm(
n_text_channels * 2,
n_att_channels,
kernel_size=1,
bias=True,
),
)
self.query_proj = nn.Sequential(
ConvNorm(
n_mel_channels,
n_mel_channels * 2,
kernel_size=3,
bias=True,
w_init_gain='relu',
),
torch.nn.ReLU(),
ConvNorm(
n_mel_channels * 2,
n_mel_channels,
kernel_size=1,
bias=True,
),
torch.nn.ReLU(),
ConvNorm(
n_mel_channels,
n_att_channels,
kernel_size=1,
bias=True,
),
)
if multi_speaker:
self.key_spk_proj = LinearNorm(n_text_channels, n_text_channels)
self.query_spk_proj = LinearNorm(n_text_channels, n_mel_channels)
def forward(self, queries, keys, mask=None, attn_prior=None, speaker_embed=None):
"""Forward pass of the aligner encoder.
Args:
queries (torch.tensor): B x C x T1 tensor (probably going to be mel data).
keys (torch.tensor): B x C2 x T2 tensor (text data).
mask (torch.tensor): uint8 binary mask for variable length entries (should be in the T2 domain).
attn_prior (torch.tensor): prior for attention matrix.
speaker_embed (torch.tensor): B x C tnesor of speaker embedding for multi-speaker scheme.
Output:
attn (torch.tensor): B x 1 x T1 x T2 attention mask. Final dim T2 should sum to 1.
attn_logprob (torch.tensor): B x 1 x T1 x T2 log-prob attention mask.
"""
if speaker_embed is not None:
keys = keys + self.key_spk_proj(speaker_embed.unsqueeze(1).expand(
-1, keys.shape[-1], -1
)).transpose(1, 2)
queries = queries + self.query_spk_proj(speaker_embed.unsqueeze(1).expand(
-1, queries.shape[-1], -1
)).transpose(1, 2)
keys_enc = self.key_proj(keys) # B x n_attn_dims x T2
queries_enc = self.query_proj(queries)
# Simplistic Gaussian Isotopic Attention
attn = (queries_enc[:, :, :, None] - keys_enc[:, :, None]) ** 2 # B x n_attn_dims x T1 x T2
attn = -self.temperature * attn.sum(1, keepdim=True)
if attn_prior is not None:
#print(f"AlignmentEncoder \t| mel: {queries.shape} phone: {keys.shape} mask: {mask.shape} attn: {attn.shape} attn_prior: {attn_prior.shape}")
attn = self.log_softmax(attn) + torch.log(attn_prior[:, None] + 1e-8)
#print(f"AlignmentEncoder \t| After prior sum attn: {attn.shape}")
attn_logprob = attn.clone()
if mask is not None:
attn.data.masked_fill_(mask.permute(0, 2, 1).unsqueeze(2), -float("inf"))
attn = self.softmax(attn) # softmax along T2
return attn, attn_logprob
class DurationPredictor(torch.nn.Module):
"""Duration predictor module.
This is a module of duration predictor described in `FastSpeech: Fast, Robust and Controllable Text to Speech`_.
The duration predictor predicts a duration of each frame in log domain from the hidden embeddings of encoder.
.. _`FastSpeech: Fast, Robust and Controllable Text to Speech`:
https://arxiv.org/pdf/1905.09263.pdf
Note:
The outputs are calculated in log domain.
"""
def __init__(self, idim, n_layers=2, n_chans=384, kernel_size=3, dropout_rate=0.1, offset=1.0, padding="SAME", dur_loss="mse"):
"""Initilize duration predictor module.
Args:
idim (int): Input dimension.
n_layers (int, optional): Number of convolutional layers.
n_chans (int, optional): Number of channels of convolutional layers.
kernel_size (int, optional): Kernel size of convolutional layers.
dropout_rate (float, optional): Dropout rate.
offset (float, optional): Offset value to avoid nan in log domain.
"""
super(DurationPredictor, self).__init__()
self.offset = offset
self.conv = torch.nn.ModuleList()
self.kernel_size = kernel_size
self.padding = padding
self.dur_loss = dur_loss
for idx in range(n_layers):
in_chans = idim if idx == 0 else n_chans
self.conv += [torch.nn.Sequential(
torch.nn.ConstantPad1d(((kernel_size - 1) // 2, (kernel_size - 1) // 2)
if padding == "SAME"
else (kernel_size - 1, 0), 0),
Conv1d(in_chans, n_chans, kernel_size, stride=1, padding=0),
torch.nn.ReLU(),
LayerNorm(n_chans, dim=1),
torch.nn.Dropout(dropout_rate)
)]
self.linear = torch.nn.Linear(n_chans, 1)
def forward(self, xs, x_masks=None):
xs = xs.transpose(1, -1) # (B, idim, Tmax)
for f in self.conv:
xs = f(xs) # (B, C, Tmax)
if x_masks is not None:
xs = xs * (1 - x_masks.float())[:, None, :]
xs = self.linear(xs.transpose(1, -1)) # [B, T, C]
xs = xs * (1 - x_masks.float())[:, :, None] # (B, T, C)
if self.dur_loss in ["mse"]:
xs = xs.squeeze(-1) # (B, Tmax)
return xs
class PitchPredictor(torch.nn.Module):
def __init__(self, idim, n_layers=5, n_chans=384, odim=2, kernel_size=5,
dropout_rate=0.1, padding="SAME"):
"""Initilize pitch predictor module.
Args:
idim (int): Input dimension.
n_layers (int, optional): Number of convolutional layers.
n_chans (int, optional): Number of channels of convolutional layers.
kernel_size (int, optional): Kernel size of convolutional layers.
dropout_rate (float, optional): Dropout rate.
"""
super(PitchPredictor, self).__init__()
self.conv = torch.nn.ModuleList()
self.kernel_size = kernel_size
self.padding = padding
for idx in range(n_layers):
in_chans = idim if idx == 0 else n_chans
self.conv += [torch.nn.Sequential(
torch.nn.ConstantPad1d(((kernel_size - 1) // 2, (kernel_size - 1) // 2)
if padding == "SAME"
else (kernel_size - 1, 0), 0),
Conv1d(in_chans, n_chans, kernel_size, stride=1, padding=0),
torch.nn.ReLU(),
LayerNorm(n_chans, dim=1),
torch.nn.Dropout(dropout_rate)
)]
self.linear = torch.nn.Linear(n_chans, odim)
self.embed_positions = SinusoidalPositionalEmbedding(idim, 0, init_size=4096)
self.pos_embed_alpha = nn.Parameter(torch.Tensor([1]))
def forward(self, xs, squeeze=False):
"""
:param xs: [B, T, H]
:return: [B, T, H]
"""
positions = self.pos_embed_alpha * self.embed_positions(xs[..., 0])
xs = xs + positions
xs = xs.transpose(1, -1) # (B, idim, Tmax)
for f in self.conv:
xs = f(xs) # (B, C, Tmax)
# NOTE: calculate in log domain
xs = self.linear(xs.transpose(1, -1)) # (B, Tmax, H)
return xs.squeeze(-1) if squeeze else xs
class EnergyPredictor(PitchPredictor):
pass
class LengthRegulator(nn.Module):
""" Length Regulator """
def __init__(self):
super(LengthRegulator, self).__init__()
def LR(self, x, duration, max_len):
output = list()
mel_len = list()
for batch, expand_target in zip(x, duration):
expanded = self.expand(batch, expand_target)
output.append(expanded)
mel_len.append(expanded.shape[0])
if max_len is not None:
output = pad(output, max_len)
else:
output = pad(output)
return output, torch.LongTensor(mel_len).to(x.device)
def expand(self, batch, predicted):
out = list()
for i, vec in enumerate(batch):
expand_size = predicted[i].item()
out.append(vec.expand(max(int(expand_size), 0), -1))
out = torch.cat(out, 0)
return out
def forward(self, x, duration, max_len):
output, mel_len = self.LR(x, duration, max_len)
return output, mel_len