-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightspeech.py
233 lines (198 loc) · 7.89 KB
/
lightspeech.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
from typing import List, Optional, Tuple
import math
import torch
from torch import nn, Tensor
class LayerNorm1d(nn.LayerNorm):
def forward(self, x: Tensor) -> Tensor:
return super().forward(x.transpose(1, 2)).transpose(1, 2)
class ConvSeparable(nn.Module):
def __init__(
self,
in_channels: int,
out_channels: int,
kernel_size: int,
dropout: float = 0,
):
super().__init__()
self.depthwise_conv = nn.Conv1d(
in_channels,
in_channels,
kernel_size,
padding="same",
groups=in_channels,
bias=False,
)
self.pointwise_conv = nn.Conv1d(in_channels, out_channels, 1)
std = math.sqrt((4 * (1.0 - dropout)) / (kernel_size * out_channels))
nn.init.normal_(self.depthwise_conv.weight, mean=0, std=std)
nn.init.normal_(self.pointwise_conv.weight, mean=0, std=std)
nn.init.zeros_(self.pointwise_conv.bias)
def forward(self, x: Tensor) -> Tensor:
return self.pointwise_conv(self.depthwise_conv(x))
class SepConvLayer(nn.Module):
def __init__(self, channels: int, kernel_size: int, dropout: float):
super().__init__()
self.layer_norm = LayerNorm1d(channels)
self.dropout = nn.Dropout(dropout)
self.activation_fn = nn.ReLU(inplace=True)
self.conv1 = ConvSeparable(channels, channels, kernel_size, dropout=dropout)
self.conv2 = ConvSeparable(channels, channels, kernel_size, dropout=dropout)
def forward(self, x: Tensor) -> Tensor:
residual = x
x = self.layer_norm(x)
x = self.activation_fn(self.conv1(x))
x = self.dropout(x)
x = self.activation_fn(self.conv2(x))
x = self.dropout(x)
return residual + x
class Model(nn.Module):
def __init__(
self,
num_phones: int,
num_speakers: int,
num_mel_bins: int,
num_tones: int = 7,
tone_embedding: int = 16,
d_model: int = 512,
layer_dropout: float = 0.2,
encoder_kernel_sizes: List[int] = [5, 25, 13, 9],
decoder_kernel_sizes: List[int] = [17, 21, 9, 3],
duration_layers: int = 1,
duration_kernel_size: int = 3,
duration_dropout: float = 0.25,
pitch_layers: int = 6,
pitch_kernel_size: int = 5,
pitch_dropout: float = 0.25,
padding_idx: int = 0,
):
super().__init__()
self.padding_idx = padding_idx
self.d_model = d_model
self.num_speakers = num_speakers
if self.num_speakers > 1:
self.speaker_embedding = nn.Embedding(self.num_speakers, d_model)
self.embed_tokens = nn.Embedding(
num_phones, d_model - tone_embedding, padding_idx=self.padding_idx
)
self.embed_tones = nn.Embedding(
num_tones, tone_embedding, padding_idx=self.padding_idx
)
self.dropout = nn.Dropout(layer_dropout)
self.embed_pitch = nn.Conv1d(2, d_model, kernel_size=1)
self.encoder = nn.ModuleList(
[
SepConvLayer(d_model, kernel_size, layer_dropout)
for kernel_size in encoder_kernel_sizes
]
)
self.decoder = nn.ModuleList(
[
SepConvLayer(d_model, kernel_size, layer_dropout)
for kernel_size in decoder_kernel_sizes
]
)
self.duration_predictor = self._make_predictor(
hidden_size=d_model,
out_dim=1,
num_layers=duration_layers,
kernel_size=duration_kernel_size,
dropout=duration_dropout,
)
self.pitch_predictor = self._make_predictor(
hidden_size=d_model,
out_dim=2,
num_layers=pitch_layers,
kernel_size=pitch_kernel_size,
dropout=pitch_dropout,
)
self.layer_norm = LayerNorm1d(d_model)
self.layer_norm2 = LayerNorm1d(d_model)
self.mel_out = nn.Conv1d(d_model, num_mel_bins, kernel_size=1)
@staticmethod
def _make_predictor(
hidden_size: int,
out_dim: int,
num_layers: int,
dropout: float = 0.5,
kernel_size: int = 3,
):
layers = []
for _ in range(num_layers):
layers.extend(
[
ConvSeparable(hidden_size, hidden_size, kernel_size),
nn.ReLU(inplace=True),
LayerNorm1d(hidden_size),
nn.Dropout(dropout),
]
)
layers.append(nn.Conv1d(hidden_size, out_dim, kernel_size=1))
return nn.Sequential(*layers)
def _length_regulator(self, x: Tensor, mel_time: int, durations: Tensor) -> Tensor:
bsz, time, feats = x.shape
if bsz > 1:
cumulative_durations = torch.cumsum(durations, dim=1)
# Create a range tensor for each batch item
expanded_range = (
torch.arange(mel_time, device=x.device).unsqueeze(0).expand(bsz, -1)
)
# Create a mask for valid positions
mask = expanded_range.unsqueeze(1) >= cumulative_durations.unsqueeze(2)
# Calculate source indices
source_indices = mask.long().sum(dim=1)
# Clamp the indices to handle cases where mel_time > total_duration
source_indices = torch.clamp(source_indices, 0, time - 1)
# Create the gather indices tensor
gather_indices = source_indices.unsqueeze(-1).expand(-1, -1, feats)
# Gather the input tensor based on the calculated indices
return torch.gather(x, 1, gather_indices)
else:
indices = torch.arange(time, device=x.device)
repeated_indices = torch.repeat_interleave(
indices, durations[0].long(), dim=0
)
return x[:, repeated_indices]
def forward(
self,
speakers: Tensor,
tokens: Tensor,
tones: Tensor,
pitches: Optional[Tensor] = None,
periodicity: Optional[Tensor] = None,
durations: Optional[Tensor] = None,
mels: Optional[Tensor] = None,
) -> Tuple[Tensor, Tensor, Tensor, Tensor]:
x = torch.cat(
(self.embed_tokens(tokens), self.embed_tones(tones)), dim=-1
).transpose(1, 2)
for encoder_layer in self.encoder:
x = encoder_layer(x)
encoder_outputs = self.layer_norm(x).transpose(1, 2)
if self.num_speakers > 1:
encoder_outputs += self.speaker_embedding(speakers.long()).unsqueeze(1)
duration_prediction = self.duration_predictor(
encoder_outputs.transpose(1, 2)
).squeeze(1)
if mels is not None and durations is not None:
durations = torch.clamp(torch.round(durations), min=0).long()
mel_time = mels.shape[1]
assert torch.max(torch.sum(durations, dim=1)).item() == mel_time
else:
duration_prediction = torch.exp(duration_prediction) - 1
durations = torch.clamp(torch.round(duration_prediction), min=0).long()
mel_time = torch.max(torch.sum(durations, dim=1)).long()
decoder_inp = self._length_regulator(encoder_outputs, mel_time, durations)
decoder_inp = self.dropout(decoder_inp).transpose(1, 2)
pitch_feat = self.pitch_predictor(decoder_inp)
new_feat = (
torch.stack((pitches, periodicity), dim=2).transpose(1, 2)
if pitches is not None
else pitch_feat.clone()
)
new_feat = new_feat.detach()
decoder_inp += self.embed_pitch(new_feat)
for decoder_layer in self.decoder:
decoder_inp = decoder_layer(decoder_inp)
decoder_outputs = self.layer_norm2(decoder_inp)
decoder_outputs = self.mel_out(decoder_outputs).transpose(1, 2)
return decoder_outputs, duration_prediction, pitch_feat[:, 0], pitch_feat[:, 1]