forked from graphcore/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conformer_blocks.py
446 lines (360 loc) · 19.6 KB
/
conformer_blocks.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
# Copyright (c) 2021 Graphcore Ltd. All rights reserved.
import numpy as np
from scipy.stats import truncnorm
class Block(object):
""" Base class for building blocks of conformer model
:param popart.builder.Builder builder: Popart Builder object
:param type dtype: numpy data type to use for weights
"""
layer_norm_eps: float = 0.001
def __init__(self, builder, dtype):
self.builder = builder
self.dtype = dtype
def xavier_init(self, shape, num_units_in, num_units_out):
""" xavier initializer for given tensor shape """
bound = np.sqrt(6. / (num_units_in + num_units_out))
return np.random.uniform(-bound, bound, shape).astype(self.dtype)
def normal_init(self, shape, mean, std_dev):
""" normal initializer for given tensor shape """
# Truncated random normal between 2 standard deviations
data = truncnorm.rvs(-2, 2, loc=mean,
scale=std_dev, size=np.prod(shape))
data = data.reshape(shape).astype(self.dtype)
return data
def namescope(self, debug_string):
return self.builder.nameScope(debug_string)
def get_constant(self, const_value):
""" returns constant onnx object with given numpy array value """
return self.builder.aiOnnx.constant(np.array(const_value).astype(self.dtype))
def add_tensor(self, var_name, init_weights):
""" adds an initialized weight tensor to the graph """
weights_tensor = self.builder.addInitializedInputTensor(init_weights, var_name)
return weights_tensor
def layer_norm(self, input_x, num_features):
""" Applies layer normalization to input_x """
gamma = self.get_constant(np.ones((num_features,)))
beta = self.get_constant(np.zeros((num_features,)))
# converting to 2d spatial tensor for group-norm to work correctly
input_x = self.builder.aiOnnx.unsqueeze([input_x], axes=[3])
outs = self.builder.aiGraphcore.groupnormalization([input_x, gamma, beta],
num_groups=1, epsilon=self.layer_norm_eps)
out = self.builder.aiOnnx.squeeze([outs[0]], axes=[3])
return out
def batch_norm(self, input_x, num_features):
""" Applies batch normalization to input_x """
init_scale = np.ones([num_features]).astype(self.dtype)
scale = self.add_tensor("scale", init_scale)
init_biases = np.zeros([num_features]).astype(self.dtype)
biases = self.add_tensor("biases", init_biases)
mean = self.add_tensor("mean", np.zeros([num_features]).astype(self.dtype))
var = self.add_tensor("var", np.zeros([num_features]).astype(self.dtype))
(out, *__) = self.builder.aiOnnx.batchnormalization([input_x, scale, biases, mean, var],
num_outputs=5)
return out
def glu_activation(self, input_x):
""" Applies gated-linear-unit activation to input_x """
s1, s2 = self.builder.aiOnnx.split([input_x], num_outputs=2, axis=1)
s2_gated = self.builder.aiOnnx.sigmoid([s2])
out = self.builder.aiOnnx.mul([s1, s2_gated])
return out
def swish_activation(self, input_x):
""" Applies swish activation to input_x """
out = self.builder.aiOnnx.mul([input_x, self.builder.aiOnnx.sigmoid([input_x])])
return out
class Linear(Block):
""" Linear transformation block
:param popart.builder.Builder builder: Popart Builder object
:param int num_in_features: the number of input features
:param int num_out_features: the number of output features
:param bool bias: whether to have bias term or not
:param type dtype: numpy data type to use for weights
"""
def __init__(self, builder, num_in_features, num_out_features, bias=False, dtype=np.float32):
super(Linear, self).__init__(builder, dtype)
self.num_in_features = num_in_features
self.num_out_features = num_out_features
self.bias = bias
def __call__(self, x):
return self.__build_graph(x)
def __build_graph(self, x):
with self.namescope("Linear"):
wshape = [self.num_out_features, self.num_in_features]
init_weights = self.xavier_init(wshape, self.num_in_features, self.num_out_features)
weights = self.add_tensor("weights", init_weights)
out = self.builder.aiOnnx.matmul([weights, x])
if self.bias:
bshape = [self.num_out_features, 1]
init_biases = np.zeros(bshape).astype(self.dtype)
biases = self.add_tensor("bias", init_biases)
out = self.builder.aiOnnx.add([out, biases])
return out
class FeedForwardModule(Block):
""" Feed Forward Module (Figure 4 of conformer paper)
:param popart.builder.Builder builder: Popart Builder object
:param int num_channels: the number of channels
:param float dropout_rate: rate of dropout
:param type dtype: numpy data type to use for weights
"""
def __init__(self, builder, num_channels, dropout_rate=0.05, dtype=np.float32):
super(FeedForwardModule, self).__init__(builder, dtype)
self.num_channels = num_channels
self.dropout_rate = dropout_rate
def __call__(self, x):
return self.__build_graph(x)
def __build_graph(self, x):
builder = self.builder
num_channels = self.num_channels
dropout_rate = self.dropout_rate
with self.namescope("FeedForwardModule"):
out = self.layer_norm(x, self.num_channels)
# first linear transformation (expansion factor of 4)
out = Linear(builder, num_channels, 4 * num_channels, bias=True, dtype=self.dtype)(out)
out = self.swish_activation(out)
out = self.builder.aiOnnx.dropout([out], 1, dropout_rate)[0]
# second linear transformation (project back to model dimension)
out = Linear(builder, 4 * num_channels, num_channels, bias=True, dtype=self.dtype)(out)
out = self.builder.aiOnnx.dropout([out], 1, dropout_rate)[0]
return out
class ConvolutionSubSampler(Block):
""" Convolutional layer with subsampling
:param popart.builder.Builder builder: Popart Builder object
:param int num_channels: the number of channels
:param int kernel_size: kernel size for convolution
:param int subsampling_factor: factor by which to subsample input
:param bool bias: whether to have bias term or not
:param type dtype: numpy data type to use for weights
"""
def __init__(self, builder, num_channels, kernel_size, subsampling_factor, bias=True, dtype=np.float32):
""" Construct a Convolution SubSampler"""
super(ConvolutionSubSampler, self).__init__(builder, dtype)
self.num_channels = num_channels
self.kernel_size = kernel_size
self.subsampling_factor = subsampling_factor
self.bias = bias
def __call__(self, x):
return self.__build_graph(x)
def __build_graph(self, x):
with self.namescope("ConvolutionSubSampler"):
wshape = [self.num_channels, self.num_channels, self.kernel_size]
init_weights = self.xavier_init(wshape, self.num_channels, self.num_channels)
weights = self.add_tensor("weights", init_weights)
pad = int(self.kernel_size / 2)
conv_args = [x, weights]
if self.bias:
bshape = [self.num_channels]
init_biases = np.zeros(bshape).astype(self.dtype)
biases = self.add_tensor("bias", init_biases)
conv_args += [biases]
out = self.builder.aiOnnx.conv(conv_args,
dilations=[1],
kernel_shape=[self.kernel_size],
strides=[self.subsampling_factor],
pads=[pad, pad])
return out
class ConvolutionModule(Block):
""" Convolutional Module of conformer model (Figure 2 of conformer paper)
:param popart.builder.Builder builder: Popart Builder object
:param int num_channels: the number of channels
:param int kernel_size: kernel size for convolution
:param bool bias: whether to have bias term or not
:param float dropout_rate: rate of dropout
:param type dtype: numpy data type to use for weights
"""
def __init__(self, builder, num_channels, kernel_size, bias=True, dropout_rate=0.05, dtype=np.float32):
""" Construct a Convolution Module """
super(ConvolutionModule, self).__init__(builder, dtype)
self.num_channels = num_channels
self.kernel_size = kernel_size
self.bias = bias
self.dropout_rate = dropout_rate
def __call__(self, x):
return self.__build_graph(x)
def pointwise_conv(self, x, num_out_channels):
with self.namescope("PointWiseConv"):
wshape = [num_out_channels, self.num_channels, 1]
init_weights = self.xavier_init(wshape, self.num_channels, num_out_channels)
weights = self.add_tensor("weights", init_weights)
conv_args = [x, weights]
if self.bias:
bshape = [num_out_channels]
init_biases = np.zeros(bshape).astype(self.dtype)
biases = self.add_tensor("bias", init_biases)
conv_args += [biases]
out = self.builder.aiOnnx.conv(conv_args,
dilations=[1],
kernel_shape=[1],
strides=[1],
pads=[0, 0])
return out
def depthwise_conv(self, x):
with self.namescope("DepthWiseConv"):
# 2nd dimension should be 1 for depth-wise convolutions
wshape = [self.num_channels, 1, self.kernel_size]
init_weights = self.xavier_init(wshape, self.num_channels, self.num_channels)
weights = self.add_tensor("weights", init_weights)
pad = int(self.kernel_size / 2)
conv_args = [x, weights]
if self.bias:
bshape = [self.num_channels]
init_biases = np.zeros(bshape).astype(self.dtype)
biases = self.add_tensor("bias", init_biases)
conv_args += [biases]
out = self.builder.aiOnnx.conv(conv_args,
dilations=[1],
kernel_shape=[self.kernel_size],
strides=[1],
group=self.num_channels,
pads=[pad, pad])
return out
def __build_graph(self, x):
with self.namescope("ConvolutionModule"):
out = self.layer_norm(x, self.num_channels)
out = self.pointwise_conv(out, 2 * self.num_channels)
out = self.glu_activation(out)
out = self.depthwise_conv(out)
out = self.batch_norm(out, self.num_channels)
out = self.swish_activation(out)
out = self.pointwise_conv(out, self.num_channels)
out = self.builder.aiOnnx.dropout([out], 1, self.dropout_rate)[0]
return out
class MultiHeadedAttention(Block):
""" Multi-Head Attention Block.
:param popart.builder.Builder builder: Popart Builder object
:param int num_heads: the number of heads
:param int num_features: the number of features
:param int sequence_length: length of sequences
:param float dropout_rate: dropout rate
:param type dtype: numpy data type to use for weights
"""
def __init__(self, builder, num_heads, num_features, sequence_length, dropout_rate=0.05, dtype=np.float32):
""" Construct an MultiHeadedAttention Block """
super(MultiHeadedAttention, self).__init__(builder, dtype)
assert(num_features % num_heads == 0)
self.num_heads = num_heads
self.num_features = num_features
self.sequence_length = self.get_constant(sequence_length)
self.dropout_rate = dropout_rate
def __call__(self, query, key, value):
return self.__build_graph(query, key, value)
def scaled_dot_product_attention(self, Q, K, V):
Q_t = self.builder.aiOnnx.transpose([Q], perm=[0, 2, 1]) # Tq X q
# getting transformed query key dot products (Tq X Tk)
attention_scores = self.builder.aiOnnx.matmul([Q_t, K])
attention_scores = self.builder.aiOnnx.softmax([attention_scores], axis=2)
if self.dropout_rate > 0.0:
attention_scores = self.builder.aiOnnx.dropout([attention_scores], 1, self.dropout_rate)[0]
attention_scores = self.builder.aiOnnx.transpose([attention_scores], perm=[0, 2, 1]) # Tk X Tq
# getting weighted average of value vectors to get context vectors
context_vectors = self.builder.aiOnnx.matmul([V, attention_scores]) # v X Tq
# scale by sqrt of sequence-length (as in deep-voice attention block)
# (this scaling was observed to work well relative to scaling by key-dimension before softmax)
context_vectors = self.builder.aiOnnx.div([context_vectors,
self.builder.aiOnnx.sqrt([self.sequence_length])])
return context_vectors
def __build_graph(self, queries, keys, values):
with self.namescope("MultiHeadedAttention"):
builder = self.builder
num_heads = self.num_heads
num_features = self.num_features
Q = Linear(builder, num_features, num_features, bias=False, dtype=self.dtype)(queries)
K = Linear(builder, num_features, num_features, bias=False, dtype=self.dtype)(keys)
V = Linear(builder, num_features, num_features, bias=False, dtype=self.dtype)(values)
Qs = builder.aiOnnx.split([Q], num_outputs=num_heads, axis=1)
Ks = builder.aiOnnx.split([K], num_outputs=num_heads, axis=1)
Vs = builder.aiOnnx.split([V], num_outputs=num_heads, axis=1)
heads = []
for Qi, Ki, Vi in zip(Qs, Ks, Vs):
heads.append(self.scaled_dot_product_attention(Qi, Ki, Vi))
heads_concat = builder.aiOnnx.concat(heads, axis=1)
context_vecs = Linear(builder, num_features, num_features, bias=False, dtype=self.dtype)(heads_concat)
return context_vecs
class MultiHeadedSelfAttentionModule(Block):
""" Multi-Head Attention Block sandiwched between LayerNorm and Dropout (Figure 3 of conformer paper)
:param popart.builder.Builder builder: Popart Builder object
:param int num_heads: the number of heads
:param int num_features: the number of features
:param int sequence_length: length of sequences
:param float dropout_rate: dropout rate
:param type dtype: numpy data type to use for weights
"""
def __init__(self, builder, num_heads, num_features, sequence_length, dropout_rate=0.05, dtype=np.float32):
""" Construct an MultiHeadedAttention Module """
super(MultiHeadedSelfAttentionModule, self).__init__(builder, dtype)
assert (num_features % num_heads == 0)
self.num_heads = num_heads
self.num_features = num_features
self.sequence_length = sequence_length
self.dropout_rate = dropout_rate
def __call__(self, x):
return self.__build_graph(x)
def __build_graph(self, x):
with self.namescope("MultiHeadedSelfAttentionModule"):
mha = MultiHeadedAttention(self.builder,
self.num_heads,
self.num_features,
self.sequence_length,
dropout_rate=self.dropout_rate,
dtype=self.dtype)
out = self.layer_norm(x, self.num_features)
out = mha(out, out, out)
out = self.builder.aiOnnx.dropout([out], 1, self.dropout_rate)[0]
return out
class ConformerBlock(Block):
""" Conformer Block (Figure 1 of conformer paper)
:param popart.builder.Builder builder: Popart Builder object
:param int num_heads: the number of attention heads
:param int num_features: the number of features
:param int sequence_length: length of sequences
:param int kernel_size: width of kernel
:param bool use_conv_module: whether to include convolution module or not
(if False, reduces to Transformer-lite block)
:param float dropout_rate: dropout rate
:param type dtype: numpy data type to use for weights
"""
def __init__(self, builder, num_heads, num_features, sequence_length, kernel_size=31,
use_conv_module=True, dropout_rate=0.05, dtype=np.float32):
""" Construct a Conformer Block """
super(ConformerBlock, self).__init__(builder, dtype)
assert (num_features % num_heads == 0)
self.num_heads = num_heads
self.num_features = num_features
self.sequence_length = sequence_length
self.kernel_size = kernel_size
self.use_conv_module = use_conv_module
self.dropout_rate = dropout_rate
def __call__(self, x):
return self.__build_graph(x)
def __build_graph(self, x):
with self.namescope("ConformerBlock"):
half_constant = self.get_constant(0.5)
# first feed-forward layer
ffn_1 = FeedForwardModule(self.builder,
self.num_features,
dropout_rate=self.dropout_rate,
dtype=self.dtype)
# second feed-forward layer
ffn_2 = FeedForwardModule(self.builder,
self.num_features,
dropout_rate=self.dropout_rate,
dtype=self.dtype)
# multi-headed self-attention module
mhsa = MultiHeadedSelfAttentionModule(self.builder,
self.num_heads,
self.num_features,
self.sequence_length,
dropout_rate=self.dropout_rate,
dtype=self.dtype)
if self.use_conv_module:
# convolution module
conv_module = ConvolutionModule(self.builder,
self.num_features,
self.kernel_size,
dropout_rate=self.dropout_rate,
dtype=self.dtype)
out = self.builder.aiOnnx.add([x, self.builder.aiOnnx.mul([ffn_1(x), half_constant])])
out = self.builder.aiOnnx.add([out, mhsa(out)])
if self.use_conv_module:
out = self.builder.aiOnnx.add([out, conv_module(out)])
out = self.builder.aiOnnx.add([out, self.builder.aiOnnx.mul([ffn_2(out), half_constant])])
out = self.layer_norm(out, self.num_features)
return out