-
Notifications
You must be signed in to change notification settings - Fork 37
/
module.py
471 lines (375 loc) · 16.3 KB
/
module.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
import torch.nn as nn
import torch as t
import torch.nn.functional as F
import math
import hyperparams as hp
from text.symbols import symbols
import numpy as np
import copy
from collections import OrderedDict
def clones(module, N):
return nn.ModuleList([copy.deepcopy(module) for _ in range(N)])
def fused_add_tanh_sigmoid_multiply(input_a, input_b, n_channels=hp.hidden_size):
n_channels_int = n_channels
assert n_channels_int == input_a.size(1)/2
in_act = input_a+input_b
t_act = torch.tanh(in_act[:, :n_channels_int, :])
s_act = torch.sigmoid(in_act[:, n_channels_int:, :])
acts = t_act * s_act
return acts
class Linear(nn.Module):
"""
Linear Module
"""
def __init__(self, in_dim, out_dim, bias=True, w_init='linear'):
"""
:param in_dim: dimension of input
:param out_dim: dimension of output
:param bias: boolean. if True, bias is included.
:param w_init: str. weight inits with xavier initialization.
"""
super(Linear, self).__init__()
self.linear_layer = nn.Linear(in_dim, out_dim, bias=bias)
nn.init.xavier_uniform_(
self.linear_layer.weight,
gain=nn.init.calculate_gain(w_init))
def forward(self, x):
return self.linear_layer(x)
class Conv(nn.Module):
"""
Convolution Module
"""
def __init__(self, in_channels, out_channels, kernel_size=1, stride=1,
padding=0, dilation=1, bias=True, w_init='linear'):
"""
:param in_channels: dimension of input
:param out_channels: dimension of output
:param kernel_size: size of kernel
:param stride: size of stride
:param padding: size of padding
:param dilation: dilation rate
:param bias: boolean. if True, bias is included.
:param w_init: str. weight inits with xavier initialization.
"""
super(Conv, self).__init__()
self.conv = nn.Conv1d(in_channels, out_channels,
kernel_size=kernel_size, stride=stride,
padding=padding, dilation=dilation,
bias=bias)
nn.init.xavier_uniform_(
self.conv.weight, gain=nn.init.calculate_gain(w_init))
def forward(self, x):
x = self.conv(x)
return x
class EncoderPrenet(nn.Module):
"""
Pre-network for Encoder consists of convolution networks.
"""
def __init__(self, embedding_size, num_hidden):
super(EncoderPrenet, self).__init__()
self.embedding_size = embedding_size
self.embed = nn.Embedding(len(symbols), embedding_size, padding_idx=0)
self.conv1 = Conv(in_channels=embedding_size,
out_channels=num_hidden,
kernel_size=5,
padding=int(np.floor(5 / 2)),
w_init='relu')
self.conv2 = Conv(in_channels=num_hidden,
out_channels=num_hidden,
kernel_size=5,
padding=int(np.floor(5 / 2)),
w_init='relu')
self.conv3 = Conv(in_channels=num_hidden,
out_channels=num_hidden,
kernel_size=5,
padding=int(np.floor(5 / 2)),
w_init='relu')
self.batch_norm1 = nn.BatchNorm1d(num_hidden)
self.batch_norm2 = nn.BatchNorm1d(num_hidden)
self.batch_norm3 = nn.BatchNorm1d(num_hidden)
self.dropout1 = nn.Dropout(p=0.2)
self.dropout2 = nn.Dropout(p=0.2)
self.dropout3 = nn.Dropout(p=0.2)
self.projection = Linear(num_hidden, num_hidden)
def forward(self, input_):
input_ = self.embed(input_)
input_ = input_.transpose(1, 2)
input_ = self.dropout1(t.relu(self.batch_norm1(self.conv1(input_))))
input_ = self.dropout2(t.relu(self.batch_norm2(self.conv2(input_))))
input_ = self.dropout3(t.relu(self.batch_norm3(self.conv3(input_))))
input_ = input_.transpose(1, 2)
input_ = self.projection(input_)
return input_
class FFN(nn.Module):
"""
Positionwise Feed-Forward Network
"""
def __init__(self, num_hidden):
"""
:param num_hidden: dimension of hidden
"""
super(FFN, self).__init__()
self.w_1 = Conv(num_hidden, num_hidden * 4, kernel_size=1, w_init='relu')
self.w_2 = Conv(num_hidden * 4, num_hidden, kernel_size=1)
self.dropout = nn.Dropout(p=0.1)
self.layer_norm = nn.LayerNorm(num_hidden)
def forward(self, input_):
# FFN Network
x = input_.transpose(1, 2)
x = self.w_2(t.relu(self.w_1(x)))
x = x.transpose(1, 2)
# residual connection
x = x + input_
# dropout
# x = self.dropout(x)
# layer normalization
x = self.layer_norm(x)
return x
class PostConvNet(nn.Module):
"""
Post Convolutional Network (mel --> mel)
"""
def __init__(self, num_hidden):
"""
:param num_hidden: dimension of hidden
"""
super(PostConvNet, self).__init__()
self.conv1 = Conv(in_channels=hp.num_mels * hp.outputs_per_step,
out_channels=num_hidden,
kernel_size=5,
padding=4,
w_init='tanh')
self.conv_list = clones(Conv(in_channels=num_hidden,
out_channels=num_hidden,
kernel_size=5,
padding=4,
w_init='tanh'), 3)
self.conv2 = Conv(in_channels=num_hidden,
out_channels=hp.num_mels * hp.outputs_per_step,
kernel_size=5,
padding=4)
self.batch_norm_list = clones(nn.BatchNorm1d(num_hidden), 3)
self.pre_batchnorm = nn.BatchNorm1d(num_hidden)
self.dropout1 = nn.Dropout(p=0.1)
self.dropout_list = nn.ModuleList([nn.Dropout(p=0.1) for _ in range(3)])
def forward(self, input_, mask=None):
# Causal Convolution (for auto-regressive)
input_ = self.dropout1(t.tanh(self.pre_batchnorm(self.conv1(input_)[:, :, :-4])))
for batch_norm, conv, dropout in zip(self.batch_norm_list, self.conv_list, self.dropout_list):
input_ = dropout(t.tanh(batch_norm(conv(input_)[:, :, :-4])))
input_ = self.conv2(input_)[:, :, :-4]
return input_
class MultiheadAttention(nn.Module):
"""
Multihead attention mechanism (dot attention)
"""
def __init__(self, num_hidden_k):
"""
:param num_hidden_k: dimension of hidden
"""
super(MultiheadAttention, self).__init__()
self.num_hidden_k = num_hidden_k #64
self.attn_dropout = nn.Dropout(p=0.1)
def forward(self, key, value, query, mask=None, query_mask=None, kv_mask=None):
# Get attention score
attn = t.bmm(query, key.transpose(1, 2)) #batch matrix-matrix product
attn = attn / math.sqrt(self.num_hidden_k)
# Masking to ignore padding (key side)
if mask is not None:
attn = attn.masked_fill(mask, -2 ** 32 + 1)
attn = t.softmax(attn, dim=-1)
else:
if kv_mask is not None:
attn[-2:] = attn[-2:].masked_fill(kv_mask, -2 ** 32 + 1)
attn = t.softmax(attn, dim=-1)
# Masking to ignore padding (query side)
if query_mask is not None:
attn = attn * query_mask
# Dropout
# attn = self.attn_dropout(attn)
# Get Context Vector
result = t.bmm(attn, value)
return result, attn
class Attention(nn.Module):
"""
Attention Network
"""
def __init__(self, kv_num_hidden, q_num_hidden, num_hidden, h=4):
"""
:param num_hidden: dimension of hidden #256
:param h: num of heads
"""
super(Attention, self).__init__()
self.num_hidden_per_attn = num_hidden // h #64
self.h = h #4
self.key = Linear(kv_num_hidden, num_hidden, bias=False) #256 --> 256
self.value = Linear(kv_num_hidden, num_hidden, bias=False)
self.query = Linear(q_num_hidden, num_hidden, bias=False)
self.multihead = MultiheadAttention(self.num_hidden_per_attn)
self.residual_dropout = nn.Dropout(p=0.1)
self.final_linear = Linear(num_hidden * 2, num_hidden) #512, 256
self.layer_norm_1 = nn.LayerNorm(num_hidden)
def forward(self, memory, decoder_input, mask=None, query_mask=None, kv_mask=None):
batch_size = memory.size(0)
seq_k = memory.size(1)
seq_q = decoder_input.size(1)
# Repeat masks h times
if query_mask is not None:
query_mask = query_mask.unsqueeze(-1).repeat(1, 1, seq_k)
query_mask = query_mask.repeat(self.h, 1, 1)
if mask is not None:
mask = mask.repeat(self.h, 1, 1)
if kv_mask is not None:
kv_mask = kv_mask.repeat(2, 1, 1) # [2*B, t', N]
# Make multihead
key = self.key(memory).view(batch_size, seq_k, self.h, self.num_hidden_per_attn)
value = self.value(memory).view(batch_size, seq_k, self.h, self.num_hidden_per_attn)
query = self.query(decoder_input).view(batch_size, seq_q, self.h, self.num_hidden_per_attn)
key = key.permute(2, 0, 1, 3).contiguous().view(-1, seq_k, self.num_hidden_per_attn)
value = value.permute(2, 0, 1, 3).contiguous().view(-1, seq_k, self.num_hidden_per_attn)
query = query.permute(2, 0, 1, 3).contiguous().view(-1, seq_q, self.num_hidden_per_attn)
# print("at Attention, key shape : ", key.shape)
# Get context vector
result, attns = self.multihead(key, value, query, mask=mask, query_mask=query_mask, kv_mask=kv_mask)
# Concatenate all multihead context vector
result = result.view(self.h, batch_size, seq_q, self.num_hidden_per_attn)
result = result.permute(1, 2, 0, 3).contiguous().view(batch_size, seq_q, -1)
# Concatenate context vector with input (most important)
result = t.cat([decoder_input, result], dim=-1)
# Final linear
result = self.final_linear(result)
# Residual dropout & connection
result = result + decoder_input
# result = self.residual_dropout(result)
# Layer normalization
result = self.layer_norm_1(result)
# print("at Attention, attention shape : ", attns.shape)
return result, attns
class Prenet(nn.Module):
"""
Prenet before passing through the network
"""
def __init__(self, input_size, hidden_size, output_size, p=0.5):
"""
:param input_size: dimension of input
:param hidden_size: dimension of hidden unit
:param output_size: dimension of output
"""
super(Prenet, self).__init__()
self.input_size = input_size
self.output_size = output_size
self.hidden_size = hidden_size
self.layer = nn.Sequential(OrderedDict([
('fc1', Linear(self.input_size, self.hidden_size)),
('relu1', nn.ReLU()),
('dropout1', nn.Dropout(p)),
('fc2', Linear(self.hidden_size, self.output_size)),
('relu2', nn.ReLU()),
('dropout2', nn.Dropout(p)),
]))
def forward(self, input_):
out = self.layer(input_)
return out
class CBHG(nn.Module):
"""
CBHG Module
"""
def __init__(self, hidden_size, K=8, projection_size = 256, num_gru_layers=2, max_pool_kernel_size=2, highwaynet_size=128, is_post=False):
"""
:param hidden_size: dimension of hidden unit
:param K: # of convolution banks
:param projection_size: dimension of projection unit
:param num_gru_layers: # of layers of GRUcell
:param max_pool_kernel_size: max pooling kernel size
:param is_post: whether post processing or not
"""
super(CBHG, self).__init__()
self.hidden_size = hidden_size
self.projection_size = projection_size
self.highwaynet_size = highwaynet_size
self.convbank_list = nn.ModuleList()
self.convbank_list.append(nn.Conv1d(in_channels=projection_size,
out_channels=hidden_size,
kernel_size=1,
padding=int(np.floor(1/2))))
for i in range(2, K+1):
self.convbank_list.append(nn.Conv1d(in_channels=hidden_size,
out_channels=hidden_size,
kernel_size=i,
padding=int(np.floor(i/2))))
self.batchnorm_list = nn.ModuleList()
for i in range(1, K+1):
self.batchnorm_list.append(nn.BatchNorm1d(hidden_size))
convbank_outdim = hidden_size * K
self.conv_projection_1 = nn.Conv1d(in_channels=convbank_outdim,
out_channels=hidden_size,
kernel_size=3,
padding=int(np.floor(3 / 2)))
self.conv_projection_2 = nn.Conv1d(in_channels=hidden_size,
out_channels=projection_size,
kernel_size=3,
padding=int(np.floor(3 / 2)))
self.batchnorm_proj_1 = nn.BatchNorm1d(hidden_size)
self.batchnorm_proj_2 = nn.BatchNorm1d(projection_size)
self.max_pool = nn.MaxPool1d(max_pool_kernel_size, stride=1, padding=1)
self.dim_match = Linear(projection_size, highwaynet_size)
self.highway = Highwaynet()
self.gru = nn.GRU(self.highwaynet_size, self.hidden_size // 2, num_layers=num_gru_layers,
batch_first=True,
bidirectional=True)
def _conv_fit_dim(self, x, kernel_size=3):
if kernel_size % 2 == 0:
return x[:,:,:-1]
else:
return x
def forward(self, input_):
input_ = input_.contiguous()
batch_size = input_.size(0)
total_length = input_.size(-1)
convbank_list = list()
convbank_input = input_
# Convolution bank filters
for k, (conv, batchnorm) in enumerate(zip(self.convbank_list, self.batchnorm_list)):
convbank_input = t.relu(batchnorm(self._conv_fit_dim(conv(convbank_input), k+1).contiguous()))
convbank_list.append(convbank_input)
# Concatenate all features
conv_cat = t.cat(convbank_list, dim=1)
# Max pooling
conv_cat = self.max_pool(conv_cat)[:,:,:-1]
# Projection
conv_projection = t.relu(self.batchnorm_proj_1(self._conv_fit_dim(self.conv_projection_1(conv_cat))))
conv_projection = self.batchnorm_proj_2(self._conv_fit_dim(self.conv_projection_2(conv_projection))) + input_
# Dimension match
conv_projection = self.dim_match(conv_projection.transpose(1,2))
# Highway networks
highway = self.highway.forward(conv_projection)
# Bidirectional GRU
self.gru.flatten_parameters()
out, _ = self.gru(highway)
return out
class Highwaynet(nn.Module):
"""
Highway network
"""
def __init__(self, num_units=128, num_layers=4):
"""
:param num_units: dimension of hidden unit
:param num_layers: # of highway layers
"""
super(Highwaynet, self).__init__()
self.num_units = num_units
self.num_layers = num_layers
self.gates = nn.ModuleList()
self.linears = nn.ModuleList()
for _ in range(self.num_layers):
self.linears.append(Linear(num_units, num_units))
self.gates.append(Linear(num_units, num_units))
def forward(self, input_):
out = input_
# highway gated function
for fc1, fc2 in zip(self.linears, self.gates):
h = t.relu(fc1.forward(out))
t_ = t.sigmoid(fc2.forward(out))
c = 1. - t_
out = h * t_ + out * c
return out