-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconformer_encoder.py
2352 lines (2025 loc) · 97.9 KB
/
conformer_encoder.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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import math
import torch
import random
from torch import nn
from typing import Union
import torch.distributed
from torch.nn import Module
from torch.nn import LayerNorm
import torch.nn.functional as F
from omegaconf import ListConfig
from contextlib import nullcontext
"""
Classes and methods from the nemo-toolkit for using the ConformerEncoder module
"""
class MultiHeadAttention(nn.Module):
"""Multi-Head Attention layer of Transformer.
Args:
n_head (int): number of heads
n_feat (int): size of the features
dropout_rate (float): dropout rate
"""
def __init__(self, n_head, n_feat, dropout_rate, max_cache_len=0):
"""Construct an MultiHeadedAttention object."""
super(MultiHeadAttention, self).__init__()
self.cache_drop_size = None
assert n_feat % n_head == 0
# We assume d_v always equals d_k
self.d_k = n_feat // n_head
self.s_d_k = math.sqrt(self.d_k)
self.h = n_head
self.linear_q = nn.Linear(n_feat, n_feat)
self.linear_k = nn.Linear(n_feat, n_feat)
self.linear_v = nn.Linear(n_feat, n_feat)
self.linear_out = nn.Linear(n_feat, n_feat)
self.dropout = nn.Dropout(p=dropout_rate)
self._max_cache_len = max_cache_len
def forward_qkv(self, query, key, value):
"""Transforms query, key and value.
Args:
query (torch.Tensor): (batch, time1, size)
key (torch.Tensor): (batch, time2, size)
value (torch.Tensor): (batch, time2, size)
returns:
q (torch.Tensor): (batch, head, time1, size)
k (torch.Tensor): (batch, head, time2, size)
v (torch.Tensor): (batch, head, time2, size)
"""
n_batch = query.size(0)
q = self.linear_q(query).view(n_batch, -1, self.h, self.d_k)
k = self.linear_k(key).view(n_batch, -1, self.h, self.d_k)
v = self.linear_v(value).view(n_batch, -1, self.h, self.d_k)
q = q.transpose(1, 2)
k = k.transpose(1, 2)
v = v.transpose(1, 2)
return q, k, v
def forward_attention(self, value, scores, mask):
"""Compute attention context vector.
Args:
value (torch.Tensor): (batch, time2, size)
scores(torch.Tensor): (batch, time1, time2)
mask(torch.Tensor): (batch, time1, time2)
returns:
value (torch.Tensor): transformed `value` (batch, time2, d_model) weighted by the attention scores
"""
n_batch = value.size(0)
if mask is not None:
mask = mask.unsqueeze(1) # (batch, 1, time1, time2)
scores = scores.masked_fill(mask, -10000.0)
attn = torch.softmax(scores, dim=-1).masked_fill(mask, 0.0) # (batch, head, time1, time2)
else:
attn = torch.softmax(scores, dim=-1) # (batch, head, time1, time2)
p_attn = self.dropout(attn)
x = torch.matmul(p_attn, value) # (batch, head, time1, d_k)
x = x.transpose(1, 2).reshape(n_batch, -1, self.h * self.d_k) # (batch, time1, d_model)
return self.linear_out(x) # (batch, time1, d_model)
def forward(self, query, key, value, mask, pos_emb=None, cache=None):
"""Compute 'Scaled Dot Product Attention'.
Args:
query (torch.Tensor): (batch, time1, size)
key (torch.Tensor): (batch, time2, size)
value(torch.Tensor): (batch, time2, size)
mask (torch.Tensor): (batch, time1, time2)
cache (torch.Tensor) : (batch, time_cache, size)
returns:
output (torch.Tensor): transformed `value` (batch, time1, d_model) weighted by the query dot key attention
cache (torch.Tensor) : (batch, time_cache_next, size)
"""
key, value, query, cache = self.update_cache(key=key, value=value, query=query, cache=cache)
if torch.is_autocast_enabled():
query, key, value = query.to(torch.float32), key.to(torch.float32), value.to(torch.float32)
# temporary until we solve this more gracefully
with avoid_float16_autocast_context():
q, k, v = self.forward_qkv(query, key, value)
scores = torch.matmul(q, k.transpose(-2, -1)) / self.s_d_k
out = self.forward_attention(v, scores, mask)
if cache is None:
return out
else:
return out, cache
def update_cache(self, key, value, query, cache):
if cache is not None:
key = value = torch.cat([cache, key], dim=1)
q_keep_size = query.shape[1] - self.cache_drop_size
cache = torch.cat([cache[:, q_keep_size:, :], query[:, :q_keep_size, :]], dim=1)
return key, value, query, cache
class RelPositionMultiHeadAttention(MultiHeadAttention):
def __init__(self, n_head, n_feat, dropout_rate, pos_bias_u, pos_bias_v, max_cache_len=0):
"""Construct an RelPositionMultiHeadedAttention object."""
super().__init__(n_head=n_head, n_feat=n_feat, dropout_rate=dropout_rate, max_cache_len=max_cache_len)
# linear transformation for positional encoding
self.linear_pos = nn.Linear(n_feat, n_feat, bias=False)
# these two learnable biases are used in matrix c and matrix d
# as described in https://arxiv.org/abs/1901.02860 Section 3.3
if pos_bias_u is None or pos_bias_v is None:
self.pos_bias_u = nn.Parameter(torch.FloatTensor(self.h, self.d_k))
self.pos_bias_v = nn.Parameter(torch.FloatTensor(self.h, self.d_k))
# nn.init.normal_(self.pos_bias_u, 0.0, 0.02)
# nn.init.normal_(self.pos_bias_v, 0.0, 0.02)
nn.init.zeros_(self.pos_bias_u)
nn.init.zeros_(self.pos_bias_v)
else:
self.pos_bias_u = pos_bias_u
self.pos_bias_v = pos_bias_v
def rel_shift(self, x):
b, h, qlen, pos_len = x.size() # (b, h, t1, t2)
# need to add a column of zeros on the left side of last dimension to perform the relative shifting
x = torch.nn.functional.pad(x, pad=(1, 0)) # (b, h, t1, t2+1)
x = x.view(b, h, -1, qlen) # (b, h, t2+1, t1)
# need to drop the first row
x = x[:, :, 1:].view(b, h, qlen, pos_len) # (b, h, t1, t2)
return x
def forward(self, query, key, value, mask, pos_emb, cache=None):
key, value, query, cache = self.update_cache(key=key, value=value, query=query, cache=cache)
if torch.is_autocast_enabled():
query, key, value = query.to(torch.float32), key.to(torch.float32), value.to(torch.float32)
# temporary until we solve this more gracefully
with avoid_float16_autocast_context():
q, k, v = self.forward_qkv(query, key, value)
q = q.transpose(1, 2) # (batch, time1, head, d_k)
n_batch_pos = pos_emb.size(0)
p = self.linear_pos(pos_emb).view(n_batch_pos, -1, self.h, self.d_k)
p = p.transpose(1, 2) # (batch, head, time1, d_k)
# (batch, head, time1, d_k)
q_with_bias_u = (q + self.pos_bias_u).transpose(1, 2)
# (batch, head, time1, d_k)
q_with_bias_v = (q + self.pos_bias_v).transpose(1, 2)
# compute attention score
# first compute matrix a and matrix c
# as described in https://arxiv.org/abs/1901.02860 Section 3.3
# (batch, head, time1, time2)
matrix_ac = torch.matmul(q_with_bias_u, k.transpose(-2, -1))
# compute matrix b and matrix d
# (batch, head, time1, time2)
matrix_bd = torch.matmul(q_with_bias_v, p.transpose(-2, -1))
matrix_bd = self.rel_shift(matrix_bd)
# drops extra elements in the matrix_bd to match the matrix_ac's size
matrix_bd = matrix_bd[:, :, :, : matrix_ac.size(-1)]
scores = (matrix_ac + matrix_bd) / self.s_d_k # (batch, head, time1, time2)
out = self.forward_attention(v, scores, mask)
if cache is None:
return out
else:
return out, cache
class RelPositionMultiHeadAttentionLongformer(RelPositionMultiHeadAttention):
"""Multi-Head Attention layer of Transformer-XL with sliding window local+global attention from Longformer.
Partially adapted from allenai (https://github.com/allenai/longformer/blob/master/longformer/sliding_chunks.py)
and huggingface (https://github.com/huggingface/transformers/blob/main/src/transformers/models/longformer/modeling_longformer.py)
Paper: https://arxiv.org/abs/1901.02860 (Transformer-XL),
https://arxiv.org/abs/2004.05150 (Longformer)
Args:
n_head (int): number of heads
n_feat (int): size of the features
dropout_rate (float): dropout rate
pos_bias_u (Tensor): the positional bias matrix U
pos_bias_v (Tensor): the positional bias matrix V
att_context_size (List[int]): List of 2 ints corresponding to left and right attention context sizes.
max_cache_len (int): the maximum size of cache
global_tokens (int): number of tokens to be used for global attention
global_tokens_spacing (int): how far apart the global tokens are
global_attn_separate (bool): whether the q, k, v layers used for global tokens should be separate
"""
def __init__(
self,
n_head,
n_feat,
dropout_rate,
pos_bias_u,
pos_bias_v,
att_context_size,
max_cache_len=0,
global_tokens=0,
global_tokens_spacing=1,
global_attn_separate=False,
):
"""Construct an RelPositionMultiHeadAttentionLongformer object."""
super().__init__(
n_head=n_head,
n_feat=n_feat,
dropout_rate=dropout_rate,
pos_bias_u=pos_bias_u,
pos_bias_v=pos_bias_v,
max_cache_len=max_cache_len,
)
self.att_context_size = att_context_size
self.global_tokens = global_tokens
self.global_tokens_spacing = global_tokens_spacing
self.global_attn_separate = global_attn_separate
if self.global_attn_separate:
self.global_q = nn.Linear(n_feat, n_feat)
self.global_k = nn.Linear(n_feat, n_feat)
self.global_v = nn.Linear(n_feat, n_feat)
def forward(self, query, key, value, pad_mask, pos_emb, cache=None):
"""Compute Scaled Dot Product Local Attention with rel. positional encoding. using overlapping chunks
Args:
query (torch.Tensor): (batch, time, size)
key (torch.Tensor): (batch, time, size)
value(torch.Tensor): (batch, time, size)
pad_mask (torch.Tensor): (batch, time)
pos_emb (torch.Tensor) : (batch, 2w + 1, size)
cache (torch.Tensor) : (batch, time_cache, size)
Returns:
output (torch.Tensor): transformed `value` (batch, time1, d_model) weighted by the query dot key attention
cache (torch.Tensor) : (batch, time_cache_next, size)
"""
key, value, query, cache = self.update_cache(key=key, value=value, query=query, cache=cache)
if torch.is_autocast_enabled():
query, key, value = query.to(torch.float32), key.to(torch.float32), value.to(torch.float32)
# temporary until we solve this more gracefully
with avoid_float16_autocast_context():
q, k, v = self.forward_qkv(query, key, value)
n_batch, _, T, _ = q.size()
w = max(self.att_context_size[0], self.att_context_size[1])
if w <= 0:
raise ValueError("When using local attention, context size must be set > 0")
pad_len = (2 * w - T % (2 * w)) % (2 * w) # pad time to 2w
q = F.pad(q, (0, 0, 0, pad_len)) # (batch, head, time, size)
k = F.pad(k, (0, 0, 0, pad_len)) # (batch, head, time, size)
v = F.pad(v, (0, 0, 0, pad_len)) # (batch, head, time, size)
mask = F.pad(pad_mask, (0, pad_len), value=1.0)
q_with_bias_u = q + self.pos_bias_u.unsqueeze(1) # (batch, head, time, size)
q_with_bias_v = q + self.pos_bias_v.unsqueeze(1) # (batch, head, time, size)
diagonal_matrix_ac = self.sliding_chunks_matmul_qk(
q_with_bias_u, k, w, padding_value=0.0
) # (batch, head, time, 2w + 1)
# add relative positional embedding
n_batch_pos = pos_emb.size(0)
p = self.linear_pos(pos_emb).view(n_batch_pos, -1, self.h, self.d_k).transpose(1, 2)
# (batch, head, 2w, size)
diagonal_matrix_bd = torch.matmul(q_with_bias_v, p.transpose(-2, -1))
# (batch, head, time, 2w + 1)
start_pos = w - self.att_context_size[0]
end_pos = w + self.att_context_size[1]
diagonal_matrix_ac[:, :, :, : self.att_context_size[0]] += diagonal_matrix_bd[
:, :, :, : self.att_context_size[0]
]
diagonal_matrix_ac[:, :, :, -(self.att_context_size[1] + 1) :] += diagonal_matrix_bd[
:, :, :, self.att_context_size[0] :
]
scores = diagonal_matrix_ac / self.s_d_k
# (batch, head, time, 2w + 1)
# mask invalid positions
scores[:, :, :, :start_pos] = -10000.0
scores[:, :, :, end_pos + 1 :] = -10000.0
# This implementation is fast and takes very little memory because num_heads x hidden_size = 1
# from (bsz x seq_len) to (bsz x num_heads x seqlen x hidden_size)
mask = mask.unsqueeze(dim=1).unsqueeze(dim=-1)
# cast to float/half then replace 1's with -inf
float_mask = mask.type_as(scores).masked_fill(mask, -10000.0)
ones = float_mask.new_ones(size=float_mask.size()) # tensor of ones
# diagonal mask with zeros everywhere and -inf inplace of padding
d_mask = self.sliding_chunks_matmul_qk(ones, float_mask, w, padding_value=0.0)
# (batch, head, time, 2w + 1)
scores += d_mask
if self.global_tokens > 0:
# create q, k, v for global attn
if self.global_attn_separate:
global_q = self.global_q(query).view(n_batch, -1, self.h, self.d_k)
global_k = self.global_k(key).view(n_batch, -1, self.h, self.d_k)
global_v = self.global_v(value).view(n_batch, -1, self.h, self.d_k)
global_q = global_q.transpose(1, 2)
global_k = global_k.transpose(1, 2)
global_v = global_v.transpose(1, 2)
global_q = F.pad(global_q, (0, 0, 0, pad_len)) # (batch, head, time, size)
global_k = F.pad(global_k, (0, 0, 0, pad_len)) # (batch, head, time, size)
global_v = F.pad(global_v, (0, 0, 0, pad_len)) # (batch, head, time, size)
else:
global_q, global_k, global_v = q, k, v
global_q /= self.s_d_k
# assign which tokens are global
is_index_global_attn = torch.zeros_like(pad_mask)
is_index_global_attn[
:, : self.global_tokens * self.global_tokens_spacing : self.global_tokens_spacing
] = 1.0
# compute global attn indices
(
max_num_global_attn_indices,
is_index_global_attn_nonzero,
is_local_index_global_attn_nonzero,
is_local_index_no_global_attn_nonzero,
) = self._get_global_attn_indices(is_index_global_attn=is_index_global_attn)
# calculate global attn probs with global keys
# (batch, time, head, max_num_global_attn_indices)
global_key_attn = self._compute_global_key_attn(
query=global_q.transpose(1, 2),
key=global_k.transpose(1, 2),
max_num_global_attn_indices=max_num_global_attn_indices,
is_index_global_attn_nonzero=is_index_global_attn_nonzero,
is_local_index_global_attn_nonzero=is_local_index_global_attn_nonzero,
is_local_index_no_global_attn_nonzero=is_local_index_no_global_attn_nonzero,
).transpose(1, 2)
# concat to local_attn_probs
# (batch, time, head, max_num_global_attn_indices + 2*w)
scores = torch.cat((global_key_attn, scores), dim=-1)
# free memory
del global_key_attn
attn = torch.softmax(scores, dim=-1).masked_fill(mask, 0.0)
p_attn = self.dropout(attn)
# (batch, head, time, 2w + 1)
if self.global_tokens > 0:
# compute sum of global and local attn
out = self._compute_attn_output_with_global_indices(
value=v,
attn_probs=p_attn,
max_num_global_attn_indices=max_num_global_attn_indices,
is_index_global_attn_nonzero=is_index_global_attn_nonzero,
is_local_index_global_attn_nonzero=is_local_index_global_attn_nonzero,
w=w,
)
else:
# compute local attn only
out = self.sliding_chunks_matmul_pv(p_attn, v, w)
out = out.reshape(n_batch, -1, self.h * self.d_k)[:, :T]
if self.global_tokens > 0:
out_global_to_all = self._compute_out_global_to_all(
query=global_q,
key=global_k,
value=global_v,
max_num_global_attn_indices=max_num_global_attn_indices,
is_local_index_global_attn_nonzero=is_local_index_global_attn_nonzero,
is_index_global_attn_nonzero=is_index_global_attn_nonzero,
is_local_index_no_global_attn_nonzero=is_local_index_no_global_attn_nonzero,
is_index_masked=mask,
)
# overwrite values with global attention
out[is_index_global_attn_nonzero] = out_global_to_all
ret = self.linear_out(out)
if cache is None:
return ret
else:
return ret, cache
def _get_global_attn_indices(self, is_index_global_attn: torch.Tensor):
"""
Compute global attention indices.
Args:
is_index_global_attn (torch.Tensor): (batch, time) A boolean tensor indicating if an index is a global attention index.
Returns:
max_num_global_attn_indices (int): Maximum number of global attention indices in the batch.
is_index_global_attn_nonzero (tuple): Indices of global attention (non-zero elements).
is_local_index_global_attn_nonzero (tuple): Indices of non-padding values within global attention indices.
is_local_index_no_global_attn_nonzero (tuple): Indices of padding values within global attention indices.
"""
# Calculate the number of global attention indices in the batch
num_global_attn_indices = is_index_global_attn.long().sum(dim=1)
# Find the maximum number of global attention indices in the batch
max_num_global_attn_indices = num_global_attn_indices.max()
# Get the indices of global attention (non-zero elements)
is_index_global_attn_nonzero = is_index_global_attn.nonzero(as_tuple=True)
# Create a helper tensor to find the local indices of global attention
is_local_index_global_attn = torch.arange(
max_num_global_attn_indices, device=is_index_global_attn.device
) < num_global_attn_indices.unsqueeze(dim=-1)
# Find the non-padding values within global attention indices
is_local_index_global_attn_nonzero = is_local_index_global_attn.nonzero(as_tuple=True)
# Find the padding values within global attention indices
is_local_index_no_global_attn_nonzero = (is_local_index_global_attn == 0).nonzero(as_tuple=True)
return (
max_num_global_attn_indices,
is_index_global_attn_nonzero,
is_local_index_global_attn_nonzero,
is_local_index_no_global_attn_nonzero,
)
def _compute_global_key_attn(
self,
key: torch.Tensor,
query: torch.Tensor,
max_num_global_attn_indices: int,
is_index_global_attn_nonzero: tuple,
is_local_index_global_attn_nonzero: tuple,
is_local_index_no_global_attn_nonzero: tuple,
) -> torch.Tensor:
batch_size = key.shape[0]
# create only global key vectors
key_only_global = key.new_zeros(batch_size, max_num_global_attn_indices, self.h, self.d_k)
key_only_global[is_local_index_global_attn_nonzero] = key[is_index_global_attn_nonzero]
# (batch_size, seq_len, head, max_num_global_attn_indices)
attn_probs_from_global_key = torch.einsum("blhd,bshd->blhs", (query, key_only_global))
# need to transpose since ONNX export only supports consecutive indexing: https://pytorch.org/docs/stable/onnx.html#writes-sets
attn_probs_from_global_key = attn_probs_from_global_key.transpose(1, 3)
attn_probs_from_global_key[
is_local_index_no_global_attn_nonzero[0], is_local_index_no_global_attn_nonzero[1], :, :
] = torch.finfo(attn_probs_from_global_key.dtype).min
attn_probs_from_global_key = attn_probs_from_global_key.transpose(1, 3)
return attn_probs_from_global_key
def _compute_attn_output_with_global_indices(
self,
value: torch.Tensor,
attn_probs: torch.Tensor,
max_num_global_attn_indices: int,
is_index_global_attn_nonzero: tuple,
is_local_index_global_attn_nonzero: tuple,
w: int,
) -> torch.Tensor:
"""
Compute the attention output with global indices.
Args:
value (torch.Tensor): (batch, head, time, head_dim) The value vectors for global attention.
attn_probs (torch.Tensor): (batch, time, head, 2w) The attention probabilities.
max_num_global_attn_indices (int): Maximum number of global attention indices in the batch.
is_index_global_attn_nonzero (tuple): Indices of global attention (non-zero elements).
is_local_index_global_attn_nonzero (tuple): Non-padding values within global attention indices.
w (int): Local context size
Returns:
torch.Tensor: (batch, time, head x head_dim) The attention output of all tokens attending to global.
"""
batch_size, time = attn_probs.shape[0], attn_probs.shape[2]
value = value.transpose(1, 2)
# get value vectors for global only
value_vectors_only_global = value.new_zeros(batch_size, max_num_global_attn_indices, self.h, self.d_k)
value_vectors_only_global[is_local_index_global_attn_nonzero] = value[is_index_global_attn_nonzero]
# cut local attn probs to global only
attn_probs_only_global = attn_probs.narrow(-1, 0, max_num_global_attn_indices)
# compute attn output only global
attn_output_only_global = torch.matmul(
attn_probs_only_global.clone(), value_vectors_only_global.transpose(1, 2).clone()
).transpose(1, 2)
# reshape attn probs
attn_probs_without_global = attn_probs.narrow(
-1, max_num_global_attn_indices, attn_probs.size(-1) - max_num_global_attn_indices
).contiguous()
# compute attn output with global
attn_output_without_global = self.sliding_chunks_matmul_pv(attn_probs_without_global, value.transpose(1, 2), w)
return attn_output_only_global + attn_output_without_global
def _compute_out_global_to_all(
self,
query: torch.Tensor,
key: torch.Tensor,
value: torch.Tensor,
max_num_global_attn_indices: int,
is_local_index_global_attn_nonzero: tuple,
is_index_global_attn_nonzero: tuple,
is_local_index_no_global_attn_nonzero: tuple,
is_index_masked: torch.Tensor,
):
"""
Compute the attention output of global tokens attending to all.
Args:
query (torch.Tensor): (batch, head, time, head_dim) The queries for global attention.
key (torch.Tensor): (batch, head, time, head_dim) The keys for global attention.
value (torch.Tensor): (batch, head, time, head_dim) The values for global attention.
max_num_global_attn_indices (int): Maximum number of global attention indices in the batch.
is_local_index_global_attn_nonzero (tuple): Non-padding values within global attention indices.
is_index_global_attn_nonzero (tuple): Indices of global attention (non-zero elements).
is_local_index_no_global_attn_nonzero (tuple): Padding values within global attention indices.
is_index_masked (torch.Tensor): (batch, time) A boolean tensor indicating if an index is masked.
Returns:
global_attn_output (torch.Tensor): (batch, max_num_global_attn_indices, head x head_dim)
The attention output of global tokens attending to all.
"""
batch_size = key.shape[0]
seq_len = key.shape[2]
global_k = key.reshape(batch_size * self.h, -1, self.d_k)
global_v = value.reshape(batch_size * self.h, -1, self.d_k)
global_q = query.transpose(1, 2)
global_q_from_global = global_q.new_zeros(batch_size, max_num_global_attn_indices, self.h, self.d_k)
global_q_from_global[is_local_index_global_attn_nonzero] = global_q[is_index_global_attn_nonzero]
global_q_from_global = global_q_from_global.transpose(0, 1).reshape(batch_size * self.h, -1, self.d_k)
# compute attn scores
global_attn_scores = torch.bmm(global_q_from_global, global_k.transpose(1, 2))
global_attn_scores = global_attn_scores.view(batch_size, self.h, max_num_global_attn_indices, seq_len)
# need to transpose since ONNX export only supports consecutive indexing: https://pytorch.org/docs/stable/onnx.html#writes-sets
global_attn_scores = global_attn_scores.transpose(1, 2)
global_attn_scores[
is_local_index_no_global_attn_nonzero[0], is_local_index_no_global_attn_nonzero[1], :, :
] = torch.finfo(global_attn_scores.dtype).min
global_attn_scores = global_attn_scores.transpose(1, 2)
global_attn_scores = global_attn_scores.masked_fill(
is_index_masked.transpose(2, 3), torch.finfo(global_attn_scores.dtype).min,
)
global_attn_scores = global_attn_scores.view(batch_size * self.h, max_num_global_attn_indices, seq_len)
# compute global attn probs
global_attn_probs_float = nn.functional.softmax(global_attn_scores, dim=-1, dtype=torch.float32)
global_attn_probs = self.dropout(global_attn_probs_float)
# global attn output
global_attn_output = torch.bmm(global_attn_probs, global_v)
global_attn_output = global_attn_output.view(batch_size, self.h, max_num_global_attn_indices, self.d_k)
global_attn_output = global_attn_output[
is_local_index_global_attn_nonzero[0], :, is_local_index_global_attn_nonzero[1]
]
global_attn_output = global_attn_output.reshape(global_attn_output.shape[0], -1)
return global_attn_output
# Longformer implementation for overlap case
#
def _skew(self, x: torch.Tensor, direction, padding_value: float) -> torch.Tensor:
"""Convert diagonals into columns (or columns into diagonals depending on `direction`
Args:
x (torch.Tensor): (batch x head, chunk_count, 2w, 2w)
direction (List[int]): padding directions
padding_value (float): value to pad with
Returns:
output (torch.Tensor): (batch x head, chunk_count, 2w, 2w + 1)
"""
x_padded = F.pad(x, direction, value=padding_value)
x_padded = x_padded.view(*x_padded.size()[:-2], x_padded.size(-1), x_padded.size(-2))
return x_padded
def _skew2(self, x: torch.Tensor, padding_value: float) -> torch.Tensor:
"""Shift every row 1 step to right converting columns into diagonals
Args:
x (torch.Tensor): (batch x head, chunks_count + 1, w, 2w + 1)
padding_value (float): value to pad with
Returns:
output (torch.Tensor): (batch x head, chunks_count + 1, w, 3w)
"""
# X = B x C x M x L
B, C, M, L = x.size()
x = F.pad(x, (0, M + 1), value=padding_value) # B x C x M x (L+M+1)
x = x.view(B, C, -1) # B x C x ML+MM+M
x = x[:, :, :-M] # B x C x ML+MM
x = x.view(B, C, M, M + L) # B x C, M x L+M
x = x[:, :, :, :-1]
return x
def _chunk_overlap(self, x: torch.Tensor, w: int) -> torch.Tensor:
"""Convert into overlapping chunks.
Args:
x (torch.Tensor): # (batch x head, time, size)
w (int): Chunk overlap size
Returns:
output (torch.Tensor): # (batch x head, chunk_count, 2w, size)
"""
# non-overlapping chunks of size = 2w
x = x.view(x.size(0), x.size(1) // (w * 2), w * 2, x.size(2))
# use `as_strided` to make the chunks overlap with an overlap size = w
chunk_size = list(x.size())
chunk_size[1] = chunk_size[1] * 2 - 1
chunk_stride = list(x.stride())
chunk_stride[1] = chunk_stride[1] // 2
return x.as_strided(size=chunk_size, stride=chunk_stride)
def _get_invalid_locations_mask(self, w: int, device: str):
diagonals_list = []
for j in range(-w, 1):
diagonal_mask = torch.zeros(w, device='cpu', dtype=torch.uint8)
diagonal_mask[:-j] = 1
diagonals_list.append(diagonal_mask)
mask = torch.stack(diagonals_list, dim=-1)
mask = mask[None, None, :, :]
ending_mask = mask.flip(dims=(2, 3)).bool().to(device)
return mask.bool().to(device), ending_mask
def mask_invalid_locations(
self, input_tensor: torch.Tensor, w: int,
):
"""
Mask locations invalid for the sliding window attention
Args:
input_tensor (torch.Tensor): # (batch x head, time, size)
w (int): Chunk overlap size
"""
beginning_mask, ending_mask = self._get_invalid_locations_mask(w, input_tensor.device)
seq_len = input_tensor.size(2)
beginning_input = input_tensor[:, :, :w, : w + 1]
beginning_mask = beginning_mask[:, :, :seq_len].expand(beginning_input.size())
beginning_input.masked_fill_(beginning_mask, -float('inf'))
ending_input = input_tensor[:, :, -w:, -(w + 1) :]
ending_mask = ending_mask[:, :, -seq_len:].expand(ending_input.size())
ending_input.masked_fill_(ending_mask, -float('inf'))
def sliding_chunks_matmul_qk(self, q: torch.Tensor, k: torch.Tensor, w: int, padding_value: float) -> torch.Tensor:
"""Matrix multiplication of query x key tensors using with a sliding window attention pattern.
This implementation splits the input into overlapping chunks of size 2w
with an overlap of size w
Args:
q (torch.Tensor): (batch, head, time, size)
k (torch.Tensor): (batch, head, time, size)
w (int): Chunk overlap size
padding_value (float): Value to pad with
Returns:
output (torch.Tensor): (batch, head, time, 2w + 1)
"""
bsz, num_heads, seqlen, head_dim = q.size()
assert seqlen % (w * 2) == 0
assert q.size() == k.size()
chunks_count = seqlen // w - 1
# group bsz and num_heads dimensions into one, then chunk seqlen into chunks of size w * 2
q = q.reshape(bsz * num_heads, seqlen, head_dim)
k = k.reshape(bsz * num_heads, seqlen, head_dim)
chunk_q = self._chunk_overlap(q, w) # (batch x head, chunk_count, 2w, size)
chunk_k = self._chunk_overlap(k, w) # (batch x head, chunk_count, 2w, size)
# matrix multipication
# bcxd: bsz*num_heads x chunks x 2w x head_dim
# bcyd: bsz*num_heads x chunks x 2w x head_dim
# bcxy: bsz*num_heads x chunks x 2w x 2w
chunk_attn = torch.einsum('bcxd,bcyd->bcxy', (chunk_q, chunk_k)) # multiply
# (batch x head, chunk_count, 2w, 2w)
# convert diagonals into columns
diagonal_chunk_attn = self._skew(chunk_attn, direction=(0, 0, 0, 1), padding_value=padding_value)
# (batch x head, chunk_count, 2w, 2w + 1)
# allocate space for the overall attention matrix where the chunks are combined. The last dimension
# has (w * 2 + 1) columns. The first (w) columns are the w lower triangles (attention from a word to
# w previous words). The following column is attention score from each word to itself, then
# followed by w columns for the upper triangle.
diagonal_attn = diagonal_chunk_attn.new_empty((bsz * num_heads, chunks_count + 1, w, w * 2 + 1))
# (batch x head, chunk_count + 1, w, 2w + 1)
# copy parts from diagonal_chunk_attn into the compined matrix of attentions
# - copying the main diagonal and the upper triangle
diagonal_attn[:, :-1, :, w:] = diagonal_chunk_attn[:, :, :w, : w + 1]
diagonal_attn[:, -1, :, w:] = diagonal_chunk_attn[:, -1, w:, : w + 1]
# - copying the lower triangle
diagonal_attn[:, 1:, :, :w] = diagonal_chunk_attn[:, :, -(w + 1) : -1, w + 1 :]
diagonal_attn[:, 0, 1:w, 1:w] = diagonal_chunk_attn[:, 0, : w - 1, 1 - w :]
# separate bsz and num_heads dimensions again
diagonal_attn = diagonal_attn.view(bsz, num_heads, seqlen, 2 * w + 1)
# (batch, head, time, 2w + 1)
self.mask_invalid_locations(diagonal_attn, w)
return diagonal_attn
def sliding_chunks_matmul_pv(self, prob: torch.Tensor, v: torch.Tensor, w: int):
"""Same as sliding_chunks_matmul_qk but for prob and value tensors.
Args:
prob (torch.Tensor): (batch, head, time, size)
v (torch.Tensor): (batch, head, time, size)
w (int): Chunk overlap size
Returns:
output (torch.Tensor): (batch, time, head, size)
"""
bsz, num_heads, seqlen, head_dim = v.size()
chunks_count = seqlen // w - 1
# group bsz and num_heads dimensions into one, then chunk seqlen into chunks of size 2w
chunk_prob = prob.reshape(bsz * num_heads, seqlen // w, w, 2 * w + 1)
# (batch x head, chunks_count + 1, w, 2w + 1)
# group bsz and num_heads dimensions into one
v = v.reshape(bsz * num_heads, seqlen, head_dim)
# (batch x head, time, size)
# pad seqlen with w at the beginning of the sequence and another w at the end
padded_v = F.pad(v, (0, 0, w, w), value=-1)
# (batch x head, time + 2w, size)
# chunk padded_v into chunks of size 3w and an overlap of size w
chunk_v_size = (bsz * num_heads, chunks_count + 1, 3 * w, head_dim)
chunk_v_stride = padded_v.stride()
chunk_v_stride = chunk_v_stride[0], w * chunk_v_stride[1], chunk_v_stride[1], chunk_v_stride[2]
chunk_v = padded_v.as_strided(size=chunk_v_size, stride=chunk_v_stride)
# (batch x head, chunks_count + 1, 3w, size)
skewed_prob = self._skew2(chunk_prob, padding_value=0)
# (batch x head, chunks_count + 1, w, 3w)
context = torch.einsum('bcwd,bcdh->bcwh', (skewed_prob, chunk_v))
# (batch x head, chunks_count + 1, w, size)
return context.view(bsz, num_heads, seqlen, head_dim).transpose(1, 2)
class PositionalEncoding(torch.nn.Module):
def __init__(self, d_model, dropout_rate, max_len=5000, xscale=None, dropout_rate_emb=0.0):
"""Construct an PositionalEncoding object."""
super(PositionalEncoding, self).__init__()
self.d_model = d_model
self.xscale = xscale
self.dropout = torch.nn.Dropout(p=dropout_rate)
self.max_len = max_len
if dropout_rate_emb > 0:
self.dropout_emb = nn.Dropout(dropout_rate_emb)
else:
self.dropout_emb = None
def create_pe(self, positions):
pos_length = positions.size(0)
pe = torch.zeros(pos_length, self.d_model, device=positions.device)
div_term = torch.exp(
torch.arange(0, self.d_model, 2, dtype=torch.float32, device=positions.device)
* -(math.log(10000.0) / self.d_model)
)
pe[:, 0::2] = torch.sin(positions * div_term)
pe[:, 1::2] = torch.cos(positions * div_term)
pe = pe.unsqueeze(0)
if hasattr(self, 'pe'):
self.pe = pe
else:
self.register_buffer('pe', pe, persistent=False)
def extend_pe(self, length, device):
"""Reset and extend the positional encodings if needed."""
if hasattr(self, 'pe') and self.pe.size(1) >= length:
return
positions = torch.arange(0, length, dtype=torch.float32, device=device).unsqueeze(1)
self.create_pe(positions=positions)
def forward(self, x: torch.Tensor, cache_len=0):
"""Adds positional encoding.
Args:
x (torch.Tensor): Input. Its shape is (batch, time, feature_size)
cache_len (int): the size of the cache which is used to shift positions
Returns:
x+pos_emb (torch.Tensor): Its shape is (batch, time, feature_size)
pos_emb (torch.Tensor): Its shape is (1, time, feature_size)
"""
input_len = x.size(1) + cache_len
if self.xscale:
x = x * self.xscale
pos_emb = self.pe[:, :input_len]
if self.dropout_emb:
pos_emb = self.dropout_emb(pos_emb)
x = x + pos_emb
return self.dropout(x), pos_emb
class RelPositionalEncoding(PositionalEncoding):
def extend_pe(self, length, device):
"""Reset and extend the positional encodings if needed."""
needed_size = 2 * length - 1
if hasattr(self, 'pe') and self.pe.size(1) >= needed_size:
return
# positions would be from negative numbers to positive
# positive positions would be used for left positions and negative for right positions
positions = torch.arange(length - 1, -length, -1, dtype=torch.float32, device=device).unsqueeze(1)
self.create_pe(positions=positions)
def forward(self, x, cache_len=0):
"""Compute positional encoding.
Args:
x (torch.Tensor): Input. Its shape is (batch, time, feature_size)
cache_len (int): the size of the cache which is used to shift positions
Returns:
x (torch.Tensor): Its shape is (batch, time, feature_size)
pos_emb (torch.Tensor): Its shape is (1, time, feature_size)
"""
if self.xscale:
x = x * self.xscale
# center_pos would be the index of position 0
# negative positions would be used for right and positive for left tokens
# for input of length L, 2*L-1 positions are needed, positions from (L-1) to -(L-1)
input_len = x.size(1) + cache_len
center_pos = self.pe.size(1) // 2 + 1
start_pos = center_pos - input_len
end_pos = center_pos + input_len - 1
pos_emb = self.pe[:, start_pos:end_pos]
if self.dropout_emb:
pos_emb = self.dropout_emb(pos_emb)
return self.dropout(x), pos_emb
class CausalConv2D(nn.Conv2d):
"""
A causal version of nn.Conv2d where each location in the 2D matrix would have no access to locations on its right or down
All arguments are the same as nn.Conv2d except padding which should be set as None
"""
def __init__(
self,
in_channels: int,
out_channels: int,
kernel_size: int,
stride: int = 1,
padding: Union[str, int] = 0,
dilation: int = 1,
groups: int = 1,
bias: bool = True,
padding_mode: str = 'zeros',
device=None,
dtype=None,
) -> None:
if padding is not None:
raise ValueError("Argument padding should be set to None for CausalConv2D.")
self._left_padding = kernel_size - 1
self._right_padding = stride - 1
padding = 0
super(CausalConv2D, self).__init__(
in_channels,
out_channels,
kernel_size,
stride,
padding,
dilation,
groups,
bias,
padding_mode,
device,
dtype,
)
def forward(
self, x,
):
x = F.pad(x, pad=(self._left_padding, self._right_padding, self._left_padding, self._right_padding))
x = super().forward(x)
return x
class CausalConv1D(nn.Conv1d):
"""
A causal version of nn.Conv1d where each step would have limited access to locations on its right or left
All arguments are the same as nn.Conv1d except padding.
If padding is set None, then paddings are set automatically to make it a causal convolution where each location would not see any steps on its right.
If padding is set as a list (size of 2), then padding[0] would be used as left padding and padding[1] as right padding.
It would make it possible to control the number of steps to be accessible on the right and left.
This mode is not supported when stride > 1. padding[0]+padding[1] should be equal to (kernel_size - 1).
"""
def __init__(
self,
in_channels: int,
out_channels: int,
kernel_size: int,
stride: int = 1,
padding: Union[str, int] = 0,
dilation: int = 1,
groups: int = 1,
bias: bool = True,
padding_mode: str = 'zeros',
device=None,
dtype=None,
) -> None:
self.cache_drop_size = None
if padding is None:
self._left_padding = kernel_size - 1
self._right_padding = stride - 1
else:
if stride != 1 and padding != kernel_size - 1:
raise ValueError("No striding allowed for non-symmetric convolutions!")
if isinstance(padding, int):
self._left_padding = padding
self._right_padding = padding
elif isinstance(padding, list) and len(padding) == 2 and padding[0] + padding[1] == kernel_size - 1:
self._left_padding = padding[0]
self._right_padding = padding[1]
else:
raise ValueError(f"Invalid padding param: {padding}!")
self._max_cache_len = self._left_padding
super(CausalConv1D, self).__init__(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=kernel_size,
stride=stride,
padding=0,
dilation=dilation,
groups=groups,
bias=bias,
padding_mode=padding_mode,
device=device,
dtype=dtype,
)
def update_cache(self, x, cache=None):
if cache is None:
new_x = F.pad(x, pad=(self._left_padding, self._right_padding))
next_cache = cache
else:
new_x = F.pad(x, pad=(0, self._right_padding))
new_x = torch.cat([cache, new_x], dim=-1)
if self.cache_drop_size > 0:
next_cache = new_x[:, :, : -self.cache_drop_size]
else:
next_cache = new_x
next_cache = next_cache[:, :, -cache.size(-1) :]