-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathfastspeech.py
More file actions
195 lines (164 loc) · 7.63 KB
/
fastspeech.py
File metadata and controls
195 lines (164 loc) · 7.63 KB
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
# Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the NVIDIA CORPORATION nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from collections import OrderedDict
import numpy as np
import torch
from torch import nn as nn
from fastspeech.model.module import FFTBlocks, LengthRegulator
from fastspeech.utils.pytorch import to_device_async
from fastspeech.utils.nvtx import Nvtx
from torch.nn import functional as F
from fastspeech.utils.logging import tprint
from fastspeech.text_norm.symbols import symbols
class Fastspeech(nn.Module):
""" FastSpeech """
def __init__(self,
max_seq_len,
d_model,
phoneme_side_n_layer,
phoneme_side_head,
phoneme_side_conv1d_filter_size,
phoneme_side_output_size,
mel_side_n_layer,
mel_side_head,
mel_side_conv1d_filter_size,
mel_side_output_size,
fft_conv1d_kernel,
fft_conv1d_padding,
duration_predictor_filter_size,
duration_predictor_kernel_size,
dropout,
n_mels,
fused_layernorm=False):
super(Fastspeech, self).__init__()
self.max_seq_len = max_seq_len
self.d_model = d_model
self.phoneme_side_n_layer = phoneme_side_n_layer
self.phoneme_side_head = phoneme_side_head
self.phoneme_side_conv1d_filter_size = phoneme_side_conv1d_filter_size
self.phoneme_side_output_size = phoneme_side_output_size
self.mel_side_n_layer = mel_side_n_layer
self.mel_side_head = mel_side_head
self.mel_side_conv1d_filter_size = mel_side_conv1d_filter_size
self.mel_side_output_size = mel_side_output_size
self.fft_conv1d_kernel = fft_conv1d_kernel
self.fft_conv1d_padding = fft_conv1d_padding
self.duration_predictor_filter_size = duration_predictor_filter_size
self.duration_predictor_kernel_size = duration_predictor_kernel_size
self.dropout = dropout
self.n_mels = n_mels
self.fused_layernorm = fused_layernorm
self.n_phns = len(symbols)+1
self.word_emb = nn.Embedding(
self.n_phns,
d_model,
padding_idx=0)
self.phoneme_side = FFTBlocks(
max_seq_len=max_seq_len,
n_layers=phoneme_side_n_layer,
n_head=phoneme_side_head,
d_k=64,
d_v=64,
d_model=d_model,
d_inner=phoneme_side_conv1d_filter_size,
fft_conv1d_kernel=fft_conv1d_kernel,
fft_conv1d_padding=fft_conv1d_padding,
dropout=dropout,
name="phoneme_side",
fused_layernorm=fused_layernorm
)
self.length_regulator = LengthRegulator(
input_size=phoneme_side_output_size,
duration_predictor_filter_size=duration_predictor_filter_size,
duration_predictor_kernel_size=duration_predictor_kernel_size,
dropout=dropout,
fused_layernorm=fused_layernorm
)
self.mel_side = FFTBlocks(
max_seq_len=max_seq_len,
n_layers=mel_side_n_layer,
n_head=mel_side_head,
d_k=64,
d_v=64,
d_model=d_model,
d_inner=mel_side_conv1d_filter_size,
fft_conv1d_kernel=fft_conv1d_kernel,
fft_conv1d_padding=fft_conv1d_padding,
dropout=dropout,
name="mel_side",
fused_layernorm=fused_layernorm
)
self.mel_linear = nn.Linear(mel_side_output_size, n_mels, bias=True)
def forward(self, seq, pos, duration_target=None, alpha=1.0, seq_output_len=None, use_fp16=False, acts=None):
# Phoneme Embedding
output = self.word_emb(seq)
if acts is not None:
acts["act.emb"] = output
if use_fp16:
output = output.half()
# Phoneme Side FFT Blocks
output, output_mask = self.phoneme_side(output, pos, acts=acts)
if acts is not None:
acts["act.phoneme_side.seq"] = output
# Length Regulator
output, pos, duration = self.length_regulator(
output,
output_mask,
target=duration_target,
alpha=alpha)
if seq_output_len:
output = F.pad(output, pad=(0, 0, 0, seq_output_len - output.size(1)))
pos = F.pad(pos, pad=(0, seq_output_len - pos.size(1)))
# length of output mel shouldn't exceed max_seq_len
output = output[:, :self.max_seq_len]
pos = pos[:, :self.max_seq_len]
if acts is not None:
acts["act.length_regulator.seq"] = output
acts["act.length_regulator.dur"] = torch.round(duration)
if self.training or output.bool().any():
# Mel Side FFT Blocks
output, output_mask = self.mel_side(output, pos, acts=acts)
if acts is not None:
acts["act.mel_side.seq"] = output
# Linear Layer
output = self.mel_linear(output)
if acts is not None:
acts["out.seq_mask"] = output_mask
acts["out.seq"] = output
else:
# seq length could be zero, in case duration predictor outputs all zeros.
# In this case, skip feed-forwarding.
tprint("Duration Predictor outputs all zeros. Output will be zero length.")
output_shape = (output.size(0), 0, output_mask.size(2))
output = torch.zeros(size=(output_shape))
output_mask = torch.ones(size=(output_shape))
if torch.cuda.device_count() > 1:
# In a multi-gpu setting, all output mels from devices must have the same length.
# otherwise, an error occurs in process of gathering output.
if not seq_output_len:
seq_output_len = self.max_seq_len
padding = (0, 0, 0, seq_output_len - output.size(1))
output = F.pad(output, padding)
output = output[:, :seq_output_len, :]
output_mask = F.pad(output_mask, padding)
output_mask = output_mask[:, :seq_output_len, :]
return output, output_mask, duration