Update modeling_gplm.py
Browse files- modeling_gplm.py +1145 -1161
modeling_gplm.py
CHANGED
@@ -1,1161 +1,1145 @@
|
|
1 |
-
#!/usr/bin/env python
|
2 |
-
# encoding: utf-8
|
3 |
-
'''
|
4 |
-
@license: (C) Copyright 2021, Hey.
|
5 |
-
@author: Hey
|
6 |
-
@email: [email protected]
|
7 |
-
@tel: 137****6540
|
8 |
-
@datetime: 2023/7/24 10:01
|
9 |
-
@project: LucaOne
|
10 |
-
@file: modeling_gplm
|
11 |
-
@desc: LucaOne Model Detail
|
12 |
-
'''
|
13 |
-
import math
|
14 |
-
from typing import Dict, Optional, Sequence, Tuple, List, Union
|
15 |
-
import uuid
|
16 |
-
import torch
|
17 |
-
import torch.nn.functional as F
|
18 |
-
from torch import Tensor, nn
|
19 |
-
from torch.nn import Parameter
|
20 |
-
|
21 |
-
|
22 |
-
def gelu(x):
|
23 |
-
return x * 0.5 * (1.0 + torch.erf(x / math.sqrt(2.0)))
|
24 |
-
|
25 |
-
|
26 |
-
def symmetrize(x):
|
27 |
-
return x + x.transpose(-1, -2)
|
28 |
-
|
29 |
-
|
30 |
-
def apc(x):
|
31 |
-
a1 = x.sum(-1, keepdims=True)
|
32 |
-
a2 = x.sum(-2, keepdims=True)
|
33 |
-
a12 = x.sum((-1, -2), keepdims=True)
|
34 |
-
|
35 |
-
avg = a1 * a2
|
36 |
-
avg.div_(a12) # in-place to reduce memory
|
37 |
-
normalized = x - avg
|
38 |
-
return normalized
|
39 |
-
|
40 |
-
|
41 |
-
class LucaGPLM1LayerNorm(nn.Module):
|
42 |
-
def __init__(self, hidden_size, eps=1e-12, affine=True):
|
43 |
-
"""Construct a layernorm layer in the TF style (eps inside the sqrt)."""
|
44 |
-
super().__init__()
|
45 |
-
self.hidden_size = (hidden_size,) if isinstance(hidden_size, int) else tuple(hidden_size)
|
46 |
-
self.eps = eps
|
47 |
-
self.affine = bool(affine)
|
48 |
-
if self.affine:
|
49 |
-
self.weight = nn.Parameter(torch.ones(hidden_size))
|
50 |
-
self.bias = nn.Parameter(torch.zeros(hidden_size))
|
51 |
-
else:
|
52 |
-
self.weight, self.bias = None, None
|
53 |
-
|
54 |
-
def forward(self, x):
|
55 |
-
dims = tuple(-(i + 1) for i in range(len(self.hidden_size)))
|
56 |
-
means = x.mean(dims, keepdim=True)
|
57 |
-
x_zeromean = x - means
|
58 |
-
variances = x_zeromean.pow(2).mean(dims, keepdim=True)
|
59 |
-
x = x_zeromean / torch.sqrt(variances + self.eps)
|
60 |
-
if self.affine:
|
61 |
-
x = (self.weight * x) + self.bias
|
62 |
-
return x
|
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 |
-
self
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
x =
|
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 |
-
self.
|
261 |
-
)
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
self.padding_idx
|
269 |
-
|
270 |
-
self.
|
271 |
-
|
272 |
-
def
|
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 |
-
self.prepend_bos
|
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 |
-
self.
|
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 |
-
self.max_tokens_per_msa
|
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 |
-
x
|
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 |
-
key
|
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 |
-
self.
|
719 |
-
|
720 |
-
|
721 |
-
|
722 |
-
|
723 |
-
self.
|
724 |
-
self.
|
725 |
-
|
726 |
-
|
727 |
-
|
728 |
-
|
729 |
-
|
730 |
-
|
731 |
-
|
732 |
-
|
733 |
-
|
734 |
-
|
735 |
-
|
736 |
-
)
|
737 |
-
|
738 |
-
self.
|
739 |
-
self.
|
740 |
-
|
741 |
-
|
742 |
-
|
743 |
-
|
744 |
-
if
|
745 |
-
self.
|
746 |
-
|
747 |
-
|
748 |
-
|
749 |
-
|
750 |
-
self.
|
751 |
-
|
752 |
-
|
753 |
-
|
754 |
-
self.
|
755 |
-
self.
|
756 |
-
|
757 |
-
|
758 |
-
|
759 |
-
self.
|
760 |
-
|
761 |
-
|
762 |
-
|
763 |
-
|
764 |
-
|
765 |
-
|
766 |
-
|
767 |
-
|
768 |
-
|
769 |
-
|
770 |
-
|
771 |
-
|
772 |
-
|
773 |
-
|
774 |
-
|
775 |
-
|
776 |
-
|
777 |
-
|
778 |
-
|
779 |
-
if
|
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 |
-
self.
|
819 |
-
self.
|
820 |
-
|
821 |
-
|
822 |
-
|
823 |
-
|
824 |
-
|
825 |
-
|
826 |
-
|
827 |
-
|
828 |
-
|
829 |
-
|
830 |
-
|
831 |
-
|
832 |
-
|
833 |
-
|
834 |
-
|
835 |
-
|
836 |
-
|
837 |
-
|
838 |
-
|
839 |
-
if
|
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 |
-
k =
|
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 |
-
attn_weights
|
973 |
-
|
974 |
-
|
975 |
-
|
976 |
-
|
977 |
-
|
978 |
-
|
979 |
-
|
980 |
-
|
981 |
-
|
982 |
-
|
983 |
-
|
984 |
-
|
985 |
-
|
986 |
-
attn_weights =
|
987 |
-
|
988 |
-
attn_weights_float.
|
989 |
-
|
990 |
-
|
991 |
-
|
992 |
-
|
993 |
-
|
994 |
-
|
995 |
-
|
996 |
-
|
997 |
-
|
998 |
-
|
999 |
-
|
1000 |
-
|
1001 |
-
|
1002 |
-
|
1003 |
-
|
1004 |
-
|
1005 |
-
|
1006 |
-
|
1007 |
-
|
1008 |
-
|
1009 |
-
|
1010 |
-
|
1011 |
-
|
1012 |
-
|
1013 |
-
|
1014 |
-
|
1015 |
-
|
1016 |
-
|
1017 |
-
|
1018 |
-
|
1019 |
-
|
1020 |
-
|
1021 |
-
|
1022 |
-
|
1023 |
-
|
1024 |
-
|
1025 |
-
|
1026 |
-
|
1027 |
-
)
|
1028 |
-
|
1029 |
-
|
1030 |
-
|
1031 |
-
|
1032 |
-
|
1033 |
-
|
1034 |
-
|
1035 |
-
|
1036 |
-
|
1037 |
-
|
1038 |
-
|
1039 |
-
|
1040 |
-
|
1041 |
-
|
1042 |
-
|
1043 |
-
|
1044 |
-
|
1045 |
-
|
1046 |
-
|
1047 |
-
|
1048 |
-
|
1049 |
-
|
1050 |
-
def
|
1051 |
-
self, incremental_state: Dict[str, Dict[str, Optional[Tensor]]]
|
1052 |
-
):
|
1053 |
-
|
1054 |
-
if
|
1055 |
-
|
1056 |
-
|
1057 |
-
|
1058 |
-
|
1059 |
-
|
1060 |
-
|
1061 |
-
|
1062 |
-
|
1063 |
-
|
1064 |
-
|
1065 |
-
|
1066 |
-
|
1067 |
-
|
1068 |
-
|
1069 |
-
|
1070 |
-
|
1071 |
-
|
1072 |
-
|
1073 |
-
|
1074 |
-
|
1075 |
-
|
1076 |
-
|
1077 |
-
|
1078 |
-
|
1079 |
-
|
1080 |
-
|
1081 |
-
|
1082 |
-
|
1083 |
-
|
1084 |
-
|
1085 |
-
|
1086 |
-
|
1087 |
-
|
1088 |
-
|
1089 |
-
|
1090 |
-
|
1091 |
-
|
1092 |
-
|
1093 |
-
|
1094 |
-
|
1095 |
-
|
1096 |
-
|
1097 |
-
|
1098 |
-
|
1099 |
-
|
1100 |
-
|
1101 |
-
|
1102 |
-
|
1103 |
-
|
1104 |
-
|
1105 |
-
|
1106 |
-
|
1107 |
-
|
1108 |
-
|
1109 |
-
|
1110 |
-
|
1111 |
-
|
1112 |
-
|
1113 |
-
|
1114 |
-
|
1115 |
-
|
1116 |
-
|
1117 |
-
|
1118 |
-
|
1119 |
-
|
1120 |
-
|
1121 |
-
|
1122 |
-
|
1123 |
-
|
1124 |
-
|
1125 |
-
|
1126 |
-
|
1127 |
-
|
1128 |
-
|
1129 |
-
|
1130 |
-
|
1131 |
-
|
1132 |
-
|
1133 |
-
self.
|
1134 |
-
|
1135 |
-
|
1136 |
-
|
1137 |
-
|
1138 |
-
|
1139 |
-
|
1140 |
-
|
1141 |
-
|
1142 |
-
|
1143 |
-
|
1144 |
-
|
1145 |
-
|
1146 |
-
self._cos_cached = emb.cos()[None, :, :]
|
1147 |
-
self._sin_cached = emb.sin()[None, :, :]
|
1148 |
-
|
1149 |
-
return self._cos_cached, self._sin_cached
|
1150 |
-
|
1151 |
-
def forward(self, q: torch.Tensor, k: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
|
1152 |
-
self._cos_cached, self._sin_cached = self._update_cos_sin_tables(k, seq_dimension=-2)
|
1153 |
-
|
1154 |
-
return (
|
1155 |
-
apply_rotary_pos_emb(q, self._cos_cached, self._sin_cached),
|
1156 |
-
apply_rotary_pos_emb(k, self._cos_cached, self._sin_cached),
|
1157 |
-
)
|
1158 |
-
|
1159 |
-
|
1160 |
-
|
1161 |
-
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# encoding: utf-8
|
3 |
+
'''
|
4 |
+
@license: (C) Copyright 2021, Hey.
|
5 |
+
@author: Hey
|
6 |
+
@email: [email protected]
|
7 |
+
@tel: 137****6540
|
8 |
+
@datetime: 2023/7/24 10:01
|
9 |
+
@project: LucaOne
|
10 |
+
@file: modeling_gplm
|
11 |
+
@desc: LucaOne Model Detail
|
12 |
+
'''
|
13 |
+
import math
|
14 |
+
from typing import Dict, Optional, Sequence, Tuple, List, Union
|
15 |
+
import uuid
|
16 |
+
import torch
|
17 |
+
import torch.nn.functional as F
|
18 |
+
from torch import Tensor, nn
|
19 |
+
from torch.nn import Parameter
|
20 |
+
|
21 |
+
|
22 |
+
def gelu(x):
|
23 |
+
return x * 0.5 * (1.0 + torch.erf(x / math.sqrt(2.0)))
|
24 |
+
|
25 |
+
|
26 |
+
def symmetrize(x):
|
27 |
+
return x + x.transpose(-1, -2)
|
28 |
+
|
29 |
+
|
30 |
+
def apc(x):
|
31 |
+
a1 = x.sum(-1, keepdims=True)
|
32 |
+
a2 = x.sum(-2, keepdims=True)
|
33 |
+
a12 = x.sum((-1, -2), keepdims=True)
|
34 |
+
|
35 |
+
avg = a1 * a2
|
36 |
+
avg.div_(a12) # in-place to reduce memory
|
37 |
+
normalized = x - avg
|
38 |
+
return normalized
|
39 |
+
|
40 |
+
|
41 |
+
class LucaGPLM1LayerNorm(nn.Module):
|
42 |
+
def __init__(self, hidden_size, eps=1e-12, affine=True):
|
43 |
+
"""Construct a layernorm layer in the TF style (eps inside the sqrt)."""
|
44 |
+
super().__init__()
|
45 |
+
self.hidden_size = (hidden_size,) if isinstance(hidden_size, int) else tuple(hidden_size)
|
46 |
+
self.eps = eps
|
47 |
+
self.affine = bool(affine)
|
48 |
+
if self.affine:
|
49 |
+
self.weight = nn.Parameter(torch.ones(hidden_size))
|
50 |
+
self.bias = nn.Parameter(torch.zeros(hidden_size))
|
51 |
+
else:
|
52 |
+
self.weight, self.bias = None, None
|
53 |
+
|
54 |
+
def forward(self, x):
|
55 |
+
dims = tuple(-(i + 1) for i in range(len(self.hidden_size)))
|
56 |
+
means = x.mean(dims, keepdim=True)
|
57 |
+
x_zeromean = x - means
|
58 |
+
variances = x_zeromean.pow(2).mean(dims, keepdim=True)
|
59 |
+
x = x_zeromean / torch.sqrt(variances + self.eps)
|
60 |
+
if self.affine:
|
61 |
+
x = (self.weight * x) + self.bias
|
62 |
+
return x
|
63 |
+
|
64 |
+
from torch.nn import LayerNorm as LucaGPLM1bLayerNorm
|
65 |
+
|
66 |
+
class LucaGPLMTransformerLayer(nn.Module):
|
67 |
+
"""LucaGPLM Transformer layer block."""
|
68 |
+
|
69 |
+
def __init__(
|
70 |
+
self,
|
71 |
+
embed_dim,
|
72 |
+
ffn_embed_dim,
|
73 |
+
attention_heads,
|
74 |
+
add_bias_kv=True,
|
75 |
+
use_lucagplm1b_layer_norm=False,
|
76 |
+
use_rotary_embeddings: bool = False,
|
77 |
+
):
|
78 |
+
'''
|
79 |
+
Tramsformer-Encoder 层
|
80 |
+
:param embed_dim: token embedding dim
|
81 |
+
:param ffn_embed_dim: fully connected layer dim
|
82 |
+
:param attention_heads: heads num
|
83 |
+
:param add_bias_kv: key-value layer add bias
|
84 |
+
:param use_lucagplm1b_layer_norm: whether to use lucagplm 1b layer norm
|
85 |
+
:param use_rotary_embeddings: whether to use rotary embedding
|
86 |
+
'''
|
87 |
+
super().__init__()
|
88 |
+
self.embed_dim = embed_dim
|
89 |
+
self.ffn_embed_dim = ffn_embed_dim
|
90 |
+
self.attention_heads = attention_heads
|
91 |
+
self.use_rotary_embeddings = use_rotary_embeddings
|
92 |
+
self._init_submodules(add_bias_kv, use_lucagplm1b_layer_norm)
|
93 |
+
|
94 |
+
def _init_submodules(self, add_bias_kv, use_lucagplm1b_layer_norm):
|
95 |
+
LucaGPLMLayerNorm = LucaGPLM1bLayerNorm if use_lucagplm1b_layer_norm else LucaGPLM1LayerNorm
|
96 |
+
|
97 |
+
# pre layer norm
|
98 |
+
self.pre_layer_norm = LucaGPLMLayerNorm(self.embed_dim)
|
99 |
+
|
100 |
+
self.self_attn = LucaGPLMMultiheadAttention(
|
101 |
+
self.embed_dim,
|
102 |
+
self.attention_heads,
|
103 |
+
add_bias_kv=add_bias_kv,
|
104 |
+
add_zero_attn=False,
|
105 |
+
use_rotary_embeddings=self.use_rotary_embeddings,
|
106 |
+
)
|
107 |
+
|
108 |
+
# post layer norm
|
109 |
+
self.post_layer_norm = LucaGPLMLayerNorm(self.embed_dim)
|
110 |
+
|
111 |
+
# dimension increase by the fully connected layer
|
112 |
+
self.fc1 = nn.Linear(self.embed_dim, self.ffn_embed_dim)
|
113 |
+
|
114 |
+
# dimension reduction by the fully connected layer
|
115 |
+
self.fc2 = nn.Linear(self.ffn_embed_dim, self.embed_dim)
|
116 |
+
|
117 |
+
def forward(
|
118 |
+
self,
|
119 |
+
x,
|
120 |
+
self_attn_mask=None,
|
121 |
+
self_attn_padding_mask=None,
|
122 |
+
need_head_weights=False
|
123 |
+
):
|
124 |
+
residual = x
|
125 |
+
x = self.pre_layer_norm(x)
|
126 |
+
x, attn = self.self_attn(
|
127 |
+
query=x,
|
128 |
+
key=x,
|
129 |
+
value=x,
|
130 |
+
key_padding_mask=self_attn_padding_mask,
|
131 |
+
need_weights=True,
|
132 |
+
need_head_weights=need_head_weights,
|
133 |
+
attn_mask=self_attn_mask,
|
134 |
+
)
|
135 |
+
x = residual + x
|
136 |
+
|
137 |
+
residual = x
|
138 |
+
x = self.post_layer_norm(x)
|
139 |
+
x = gelu(self.fc1(x))
|
140 |
+
x = self.fc2(x)
|
141 |
+
x = residual + x
|
142 |
+
|
143 |
+
return x, attn
|
144 |
+
|
145 |
+
|
146 |
+
class AxialTransformerLayer(nn.Module):
|
147 |
+
def __init__(
|
148 |
+
self,
|
149 |
+
embedding_dim: int = 768,
|
150 |
+
ffn_embedding_dim: int = 3072,
|
151 |
+
num_attention_heads: int = 8,
|
152 |
+
dropout: float = 0.1,
|
153 |
+
attention_dropout: float = 0.1,
|
154 |
+
activation_dropout: float = 0.1,
|
155 |
+
max_tokens_per_msa: int = 2**14,
|
156 |
+
) -> None:
|
157 |
+
super().__init__()
|
158 |
+
|
159 |
+
# Initialize parameters
|
160 |
+
self.embedding_dim = embedding_dim
|
161 |
+
self.dropout_prob = dropout
|
162 |
+
|
163 |
+
row_self_attention = RowSelfAttention(
|
164 |
+
embedding_dim,
|
165 |
+
num_attention_heads,
|
166 |
+
dropout=dropout,
|
167 |
+
max_tokens_per_msa=max_tokens_per_msa,
|
168 |
+
)
|
169 |
+
|
170 |
+
column_self_attention = ColumnSelfAttention(
|
171 |
+
embedding_dim,
|
172 |
+
num_attention_heads,
|
173 |
+
dropout=dropout,
|
174 |
+
max_tokens_per_msa=max_tokens_per_msa,
|
175 |
+
)
|
176 |
+
|
177 |
+
feed_forward_layer = FeedForwardNetwork(
|
178 |
+
embedding_dim,
|
179 |
+
ffn_embedding_dim,
|
180 |
+
activation_dropout=activation_dropout,
|
181 |
+
max_tokens_per_msa=max_tokens_per_msa,
|
182 |
+
)
|
183 |
+
|
184 |
+
self.row_self_attention = self.build_residual(row_self_attention)
|
185 |
+
self.column_self_attention = self.build_residual(column_self_attention)
|
186 |
+
self.feed_forward_layer = self.build_residual(feed_forward_layer)
|
187 |
+
|
188 |
+
def build_residual(self, layer: nn.Module):
|
189 |
+
return NormalizedResidualBlock(
|
190 |
+
layer,
|
191 |
+
self.embedding_dim,
|
192 |
+
self.dropout_prob,
|
193 |
+
)
|
194 |
+
|
195 |
+
def forward(
|
196 |
+
self,
|
197 |
+
x: torch.Tensor,
|
198 |
+
self_attn_mask: Optional[torch.Tensor] = None,
|
199 |
+
self_attn_padding_mask: Optional[torch.Tensor] = None,
|
200 |
+
need_head_weights: bool = False,
|
201 |
+
):
|
202 |
+
x, row_attn = self.row_self_attention(
|
203 |
+
x,
|
204 |
+
self_attn_mask=self_attn_mask,
|
205 |
+
self_attn_padding_mask=self_attn_padding_mask,
|
206 |
+
)
|
207 |
+
x, column_attn = self.column_self_attention(
|
208 |
+
x,
|
209 |
+
self_attn_mask=self_attn_mask,
|
210 |
+
self_attn_padding_mask=self_attn_padding_mask,
|
211 |
+
)
|
212 |
+
x = self.feed_forward_layer(x)
|
213 |
+
if need_head_weights:
|
214 |
+
return x, column_attn, row_attn
|
215 |
+
else:
|
216 |
+
return x
|
217 |
+
|
218 |
+
|
219 |
+
class LearnedPositionalEmbedding(nn.Embedding):
|
220 |
+
def __init__(self, num_embeddings: int, embedding_dim: int, padding_idx: int):
|
221 |
+
if padding_idx is not None:
|
222 |
+
num_embeddings_ = num_embeddings + padding_idx + 1
|
223 |
+
else:
|
224 |
+
num_embeddings_ = num_embeddings
|
225 |
+
super().__init__(num_embeddings_, embedding_dim, padding_idx)
|
226 |
+
self.max_positions = num_embeddings
|
227 |
+
|
228 |
+
def forward(self, input: torch.Tensor):
|
229 |
+
"""Input is expected to be of size [bsz x seqlen]."""
|
230 |
+
if input.size(1) > self.max_positions:
|
231 |
+
raise ValueError(
|
232 |
+
f"Sequence length {input.size(1)} above maximum "
|
233 |
+
f" sequence length of {self.max_positions}"
|
234 |
+
)
|
235 |
+
mask = input.ne(self.padding_idx).int()
|
236 |
+
positions = (torch.cumsum(mask, dim=1).type_as(mask) * mask).long() + self.padding_idx
|
237 |
+
return F.embedding(
|
238 |
+
positions,
|
239 |
+
self.weight,
|
240 |
+
self.padding_idx,
|
241 |
+
self.max_norm,
|
242 |
+
self.norm_type,
|
243 |
+
self.scale_grad_by_freq,
|
244 |
+
self.sparse,
|
245 |
+
)
|
246 |
+
|
247 |
+
|
248 |
+
class SinusoidalPositionalEmbedding(nn.Module):
|
249 |
+
def __init__(self, embed_dim, padding_idx, learned=False):
|
250 |
+
super().__init__()
|
251 |
+
self.embed_dim = embed_dim
|
252 |
+
self.padding_idx = padding_idx
|
253 |
+
self.register_buffer("_float_tensor", torch.FloatTensor(1))
|
254 |
+
self.weights = None
|
255 |
+
|
256 |
+
def forward(self, x):
|
257 |
+
bsz, seq_len = x.shape
|
258 |
+
max_pos = self.padding_idx + 1 + seq_len
|
259 |
+
if self.weights is None or max_pos > self.weights.size(0):
|
260 |
+
self.weights = self.get_embedding(max_pos)
|
261 |
+
self.weights = self.weights.type_as(self._float_tensor)
|
262 |
+
|
263 |
+
positions = self.make_positions(x)
|
264 |
+
return self.weights.index_select(0, positions.view(-1)).view(bsz, seq_len, -1).detach()
|
265 |
+
|
266 |
+
def make_positions(self, x):
|
267 |
+
mask = x.ne(self.padding_idx)
|
268 |
+
range_buf = torch.arange(x.size(1), device=x.device).expand_as(x) + self.padding_idx + 1
|
269 |
+
positions = range_buf.expand_as(x)
|
270 |
+
return positions * mask.long() + self.padding_idx * (1 - mask.long())
|
271 |
+
|
272 |
+
def get_embedding(self, num_embeddings):
|
273 |
+
half_dim = self.embed_dim // 2
|
274 |
+
emb = math.log(10000) / (half_dim - 1)
|
275 |
+
emb = torch.exp(torch.arange(half_dim, dtype=torch.float) * -emb)
|
276 |
+
emb = torch.arange(num_embeddings, dtype=torch.float).unsqueeze(1) * emb.unsqueeze(0)
|
277 |
+
emb = torch.cat([torch.sin(emb), torch.cos(emb)], dim=1).view(num_embeddings, -1)
|
278 |
+
if self.embed_dim % 2 == 1:
|
279 |
+
# zero pad
|
280 |
+
emb = torch.cat([emb, torch.zeros(num_embeddings, 1)], dim=1)
|
281 |
+
if self.padding_idx is not None:
|
282 |
+
emb[self.padding_idx, :] = 0
|
283 |
+
return emb
|
284 |
+
|
285 |
+
|
286 |
+
class RobertaLMHead(nn.Module):
|
287 |
+
def __init__(self, embed_dim, output_dim, weight):
|
288 |
+
super().__init__()
|
289 |
+
self.dense = nn.Linear(embed_dim, embed_dim)
|
290 |
+
self.layer_norm = LucaGPLM1bLayerNorm(embed_dim)
|
291 |
+
self.weight = weight
|
292 |
+
self.bias = nn.Parameter(torch.zeros(output_dim))
|
293 |
+
|
294 |
+
def forward(self, features):
|
295 |
+
x = self.dense(features)
|
296 |
+
x = gelu(x)
|
297 |
+
x = self.layer_norm(x)
|
298 |
+
# project back to size of vocabulary with bias
|
299 |
+
x = F.linear(x, self.weight) + self.bias
|
300 |
+
return x
|
301 |
+
|
302 |
+
|
303 |
+
class ContactPredictionHead(nn.Module):
|
304 |
+
def __init__(
|
305 |
+
self,
|
306 |
+
in_features: int,
|
307 |
+
prepend_bos: bool,
|
308 |
+
append_eos: bool,
|
309 |
+
bias=True,
|
310 |
+
eos_idx: Optional[int] = None,
|
311 |
+
):
|
312 |
+
super().__init__()
|
313 |
+
self.in_features = in_features
|
314 |
+
self.prepend_bos = prepend_bos
|
315 |
+
self.append_eos = append_eos
|
316 |
+
if append_eos and eos_idx is None:
|
317 |
+
raise ValueError("Using an alphabet with eos token, but no eos token was passed in.")
|
318 |
+
self.eos_idx = eos_idx
|
319 |
+
self.regression = nn.Linear(in_features, 1, bias)
|
320 |
+
self.activation = nn.Sigmoid()
|
321 |
+
|
322 |
+
def forward(self, tokens, attentions):
|
323 |
+
# remove eos token attentions
|
324 |
+
if self.append_eos:
|
325 |
+
eos_mask = tokens.ne(self.eos_idx).to(attentions)
|
326 |
+
eos_mask = eos_mask.unsqueeze(1) * eos_mask.unsqueeze(2)
|
327 |
+
attentions = attentions * eos_mask[:, None, None, :, :]
|
328 |
+
attentions = attentions[..., :-1, :-1]
|
329 |
+
# remove cls token attentions
|
330 |
+
if self.prepend_bos:
|
331 |
+
attentions = attentions[..., 1:, 1:]
|
332 |
+
batch_size, layers, heads, seqlen, _ = attentions.size()
|
333 |
+
attentions = attentions.view(batch_size, layers * heads, seqlen, seqlen)
|
334 |
+
|
335 |
+
# features: B x C x T x T
|
336 |
+
attentions = attentions.to(
|
337 |
+
self.regression.weight.device
|
338 |
+
) # attentions always float32, may need to convert to float16
|
339 |
+
attentions = apc(symmetrize(attentions))
|
340 |
+
attentions = attentions.permute(0, 2, 3, 1)
|
341 |
+
return self.activation(self.regression(attentions).squeeze(3))
|
342 |
+
|
343 |
+
|
344 |
+
class NormalizedResidualBlock(nn.Module):
|
345 |
+
def __init__(
|
346 |
+
self,
|
347 |
+
layer: nn.Module,
|
348 |
+
embedding_dim: int,
|
349 |
+
dropout: float = 0.1,
|
350 |
+
):
|
351 |
+
super().__init__()
|
352 |
+
self.embedding_dim = embedding_dim
|
353 |
+
|
354 |
+
self.layer = layer
|
355 |
+
self.dropout_module = nn.Dropout(
|
356 |
+
dropout,
|
357 |
+
)
|
358 |
+
self.layer_norm = LucaGPLM1bLayerNorm(self.embedding_dim)
|
359 |
+
|
360 |
+
def forward(self, x, *args, **kwargs):
|
361 |
+
residual = x
|
362 |
+
x = self.layer_norm(x)
|
363 |
+
outputs = self.layer(x, *args, **kwargs)
|
364 |
+
if isinstance(outputs, tuple):
|
365 |
+
x, *out = outputs
|
366 |
+
else:
|
367 |
+
x = outputs
|
368 |
+
out = None
|
369 |
+
|
370 |
+
x = self.dropout_module(x)
|
371 |
+
x = residual + x
|
372 |
+
|
373 |
+
if out is not None:
|
374 |
+
return (x,) + tuple(out)
|
375 |
+
else:
|
376 |
+
return x
|
377 |
+
|
378 |
+
|
379 |
+
class FeedForwardNetwork(nn.Module):
|
380 |
+
def __init__(
|
381 |
+
self,
|
382 |
+
embedding_dim: int,
|
383 |
+
ffn_embedding_dim: int,
|
384 |
+
activation_dropout: float = 0.1,
|
385 |
+
max_tokens_per_msa: int = 2**14,
|
386 |
+
):
|
387 |
+
super().__init__()
|
388 |
+
self.embedding_dim = embedding_dim
|
389 |
+
self.ffn_embedding_dim = ffn_embedding_dim
|
390 |
+
self.max_tokens_per_msa = max_tokens_per_msa
|
391 |
+
self.activation_fn = nn.GELU()
|
392 |
+
self.activation_dropout_module = nn.Dropout(
|
393 |
+
activation_dropout,
|
394 |
+
)
|
395 |
+
self.fc1 = nn.Linear(embedding_dim, ffn_embedding_dim)
|
396 |
+
self.fc2 = nn.Linear(ffn_embedding_dim, embedding_dim)
|
397 |
+
|
398 |
+
def forward(self, x):
|
399 |
+
x = self.activation_fn(self.fc1(x))
|
400 |
+
x = self.activation_dropout_module(x)
|
401 |
+
x = self.fc2(x)
|
402 |
+
return x
|
403 |
+
|
404 |
+
|
405 |
+
class RowSelfAttention(nn.Module):
|
406 |
+
"""Compute self-attention over rows of a 2D input."""
|
407 |
+
|
408 |
+
def __init__(
|
409 |
+
self,
|
410 |
+
embed_dim,
|
411 |
+
num_heads,
|
412 |
+
dropout=0.0,
|
413 |
+
max_tokens_per_msa: int = 2 ** 16,
|
414 |
+
):
|
415 |
+
super().__init__()
|
416 |
+
self.num_heads = num_heads
|
417 |
+
self.dropout = dropout
|
418 |
+
self.head_dim = embed_dim // num_heads
|
419 |
+
self.scaling = self.head_dim ** -0.5
|
420 |
+
self.max_tokens_per_msa = max_tokens_per_msa
|
421 |
+
self.attn_shape = "hnij"
|
422 |
+
|
423 |
+
self.k_proj = nn.Linear(embed_dim, embed_dim)
|
424 |
+
self.v_proj = nn.Linear(embed_dim, embed_dim)
|
425 |
+
self.q_proj = nn.Linear(embed_dim, embed_dim)
|
426 |
+
|
427 |
+
self.out_proj = nn.Linear(embed_dim, embed_dim)
|
428 |
+
self.dropout_module = nn.Dropout(dropout)
|
429 |
+
|
430 |
+
def align_scaling(self, q):
|
431 |
+
num_rows = q.size(0)
|
432 |
+
return self.scaling / math.sqrt(num_rows)
|
433 |
+
|
434 |
+
def _batched_forward(
|
435 |
+
self,
|
436 |
+
x,
|
437 |
+
self_attn_mask=None,
|
438 |
+
self_attn_padding_mask=None,
|
439 |
+
):
|
440 |
+
num_rows, num_cols, batch_size, embed_dim = x.size()
|
441 |
+
max_rows = max(1, self.max_tokens_per_msa // num_cols)
|
442 |
+
attns = 0
|
443 |
+
scaling = self.align_scaling(x)
|
444 |
+
for start in range(0, num_rows, max_rows):
|
445 |
+
attn_weights = self.compute_attention_weights(
|
446 |
+
x[start : start + max_rows],
|
447 |
+
scaling,
|
448 |
+
self_attn_mask=self_attn_mask,
|
449 |
+
self_attn_padding_mask=self_attn_padding_mask[:, start : start + max_rows]
|
450 |
+
if self_attn_padding_mask is not None
|
451 |
+
else None,
|
452 |
+
)
|
453 |
+
attns += attn_weights
|
454 |
+
attn_probs = attns.softmax(-1)
|
455 |
+
attn_probs = self.dropout_module(attn_probs)
|
456 |
+
|
457 |
+
outputs = []
|
458 |
+
for start in range(0, num_rows, max_rows):
|
459 |
+
output = self.compute_attention_update(x[start : start + max_rows], attn_probs)
|
460 |
+
outputs.append(output)
|
461 |
+
|
462 |
+
output = torch.cat(outputs, 0)
|
463 |
+
return output, attn_probs
|
464 |
+
|
465 |
+
def compute_attention_weights(
|
466 |
+
self,
|
467 |
+
x,
|
468 |
+
scaling: float,
|
469 |
+
self_attn_mask=None,
|
470 |
+
self_attn_padding_mask=None,
|
471 |
+
):
|
472 |
+
num_rows, num_cols, batch_size, embed_dim = x.size()
|
473 |
+
q = self.q_proj(x).view(num_rows, num_cols, batch_size, self.num_heads, self.head_dim)
|
474 |
+
k = self.k_proj(x).view(num_rows, num_cols, batch_size, self.num_heads, self.head_dim)
|
475 |
+
q *= scaling
|
476 |
+
if self_attn_padding_mask is not None:
|
477 |
+
# Zero out any padded aligned positions - this is important since
|
478 |
+
# we take a sum across the alignment axis.
|
479 |
+
q *= 1 - self_attn_padding_mask.permute(1, 2, 0).unsqueeze(3).unsqueeze(4).to(q)
|
480 |
+
|
481 |
+
attn_weights = torch.einsum(f"rinhd,rjnhd->{self.attn_shape}", q, k)
|
482 |
+
|
483 |
+
if self_attn_mask is not None:
|
484 |
+
raise NotImplementedError
|
485 |
+
# Mask Size: [B x R x C], Weights Size: [H x B x C x C]
|
486 |
+
|
487 |
+
if self_attn_padding_mask is not None:
|
488 |
+
attn_weights = attn_weights.masked_fill(
|
489 |
+
self_attn_padding_mask[:, 0].unsqueeze(0).unsqueeze(2),
|
490 |
+
-10000,
|
491 |
+
)
|
492 |
+
|
493 |
+
return attn_weights
|
494 |
+
|
495 |
+
def compute_attention_update(
|
496 |
+
self,
|
497 |
+
x,
|
498 |
+
attn_probs,
|
499 |
+
):
|
500 |
+
num_rows, num_cols, batch_size, embed_dim = x.size()
|
501 |
+
v = self.v_proj(x).view(num_rows, num_cols, batch_size, self.num_heads, self.head_dim)
|
502 |
+
context = torch.einsum(f"{self.attn_shape},rjnhd->rinhd", attn_probs, v)
|
503 |
+
context = context.contiguous().view(num_rows, num_cols, batch_size, embed_dim)
|
504 |
+
output = self.out_proj(context)
|
505 |
+
return output
|
506 |
+
|
507 |
+
def forward(
|
508 |
+
self,
|
509 |
+
x,
|
510 |
+
self_attn_mask=None,
|
511 |
+
self_attn_padding_mask=None,
|
512 |
+
):
|
513 |
+
num_rows, num_cols, batch_size, embed_dim = x.size()
|
514 |
+
if (num_rows * num_cols > self.max_tokens_per_msa) and not torch.is_grad_enabled():
|
515 |
+
return self._batched_forward(x, self_attn_mask, self_attn_padding_mask)
|
516 |
+
else:
|
517 |
+
scaling = self.align_scaling(x)
|
518 |
+
attn_weights = self.compute_attention_weights(
|
519 |
+
x, scaling, self_attn_mask, self_attn_padding_mask
|
520 |
+
)
|
521 |
+
attn_probs = attn_weights.softmax(-1)
|
522 |
+
attn_probs = self.dropout_module(attn_probs)
|
523 |
+
output = self.compute_attention_update(x, attn_probs)
|
524 |
+
return output, attn_probs
|
525 |
+
|
526 |
+
|
527 |
+
class ColumnSelfAttention(nn.Module):
|
528 |
+
"""Compute self-attention over columns of a 2D input."""
|
529 |
+
|
530 |
+
def __init__(
|
531 |
+
self,
|
532 |
+
embed_dim,
|
533 |
+
num_heads,
|
534 |
+
dropout=0.0,
|
535 |
+
max_tokens_per_msa: int = 2 ** 16,
|
536 |
+
):
|
537 |
+
super().__init__()
|
538 |
+
|
539 |
+
self.num_heads = num_heads
|
540 |
+
self.dropout = dropout
|
541 |
+
self.head_dim = embed_dim // num_heads
|
542 |
+
self.scaling = self.head_dim ** -0.5
|
543 |
+
self.max_tokens_per_msa = max_tokens_per_msa
|
544 |
+
|
545 |
+
self.k_proj = nn.Linear(embed_dim, embed_dim)
|
546 |
+
self.v_proj = nn.Linear(embed_dim, embed_dim)
|
547 |
+
self.q_proj = nn.Linear(embed_dim, embed_dim)
|
548 |
+
|
549 |
+
self.out_proj = nn.Linear(embed_dim, embed_dim)
|
550 |
+
self.dropout_module = nn.Dropout(dropout)
|
551 |
+
|
552 |
+
def _batched_forward(
|
553 |
+
self,
|
554 |
+
x,
|
555 |
+
self_attn_mask=None,
|
556 |
+
self_attn_padding_mask=None,
|
557 |
+
):
|
558 |
+
num_rows, num_cols, batch_size, embed_dim = x.size()
|
559 |
+
max_cols = max(1, self.max_tokens_per_msa // num_rows)
|
560 |
+
outputs = []
|
561 |
+
attns = []
|
562 |
+
for start in range(0, num_cols, max_cols):
|
563 |
+
output, attn = self(
|
564 |
+
x[:, start : start + max_cols],
|
565 |
+
self_attn_mask=self_attn_mask,
|
566 |
+
self_attn_padding_mask=self_attn_padding_mask[:, :, start : start + max_cols]
|
567 |
+
if self_attn_padding_mask is not None
|
568 |
+
else None,
|
569 |
+
)
|
570 |
+
outputs.append(output)
|
571 |
+
attns.append(attn)
|
572 |
+
output = torch.cat(outputs, 1)
|
573 |
+
attns = torch.cat(attns, 1)
|
574 |
+
return output, attns
|
575 |
+
|
576 |
+
def compute_attention_update(
|
577 |
+
self,
|
578 |
+
x,
|
579 |
+
self_attn_mask=None,
|
580 |
+
self_attn_padding_mask=None,
|
581 |
+
):
|
582 |
+
num_rows, num_cols, batch_size, embed_dim = x.size()
|
583 |
+
if num_rows == 1:
|
584 |
+
# if there is only 1 position, this is equivalent and doesn't break with padding
|
585 |
+
attn_probs = torch.ones(
|
586 |
+
self.num_heads,
|
587 |
+
num_cols,
|
588 |
+
batch_size,
|
589 |
+
num_rows,
|
590 |
+
num_rows,
|
591 |
+
device=x.device,
|
592 |
+
dtype=x.dtype,
|
593 |
+
)
|
594 |
+
output = self.out_proj(self.v_proj(x))
|
595 |
+
else:
|
596 |
+
q = self.q_proj(x).view(num_rows, num_cols, batch_size, self.num_heads, self.head_dim)
|
597 |
+
k = self.k_proj(x).view(num_rows, num_cols, batch_size, self.num_heads, self.head_dim)
|
598 |
+
v = self.v_proj(x).view(num_rows, num_cols, batch_size, self.num_heads, self.head_dim)
|
599 |
+
q *= self.scaling
|
600 |
+
|
601 |
+
attn_weights = torch.einsum("icnhd,jcnhd->hcnij", q, k)
|
602 |
+
|
603 |
+
if self_attn_mask is not None:
|
604 |
+
raise NotImplementedError
|
605 |
+
if self_attn_padding_mask is not None:
|
606 |
+
attn_weights = attn_weights.masked_fill(
|
607 |
+
self_attn_padding_mask.permute(2, 0, 1).unsqueeze(0).unsqueeze(3),
|
608 |
+
-10000,
|
609 |
+
)
|
610 |
+
|
611 |
+
attn_probs = attn_weights.softmax(-1)
|
612 |
+
attn_probs = self.dropout_module(attn_probs)
|
613 |
+
context = torch.einsum("hcnij,jcnhd->icnhd", attn_probs, v)
|
614 |
+
context = context.contiguous().view(num_rows, num_cols, batch_size, embed_dim)
|
615 |
+
output = self.out_proj(context)
|
616 |
+
return output, attn_probs
|
617 |
+
|
618 |
+
def forward(
|
619 |
+
self,
|
620 |
+
x,
|
621 |
+
self_attn_mask=None,
|
622 |
+
self_attn_padding_mask=None,
|
623 |
+
):
|
624 |
+
num_rows, num_cols, batch_size, embed_dim = x.size()
|
625 |
+
# if False and num_rows * num_cols > 2 ** 14 and not torch.is_grad_enabled():
|
626 |
+
if (num_rows * num_cols) > self.max_tokens_per_msa and not torch.is_grad_enabled():
|
627 |
+
return self._batched_forward(
|
628 |
+
x,
|
629 |
+
self_attn_mask,
|
630 |
+
self_attn_padding_mask,
|
631 |
+
)
|
632 |
+
else:
|
633 |
+
return self.compute_attention_update(x, self_attn_mask, self_attn_padding_mask)
|
634 |
+
|
635 |
+
|
636 |
+
def utils_softmax(x, dim: int, onnx_trace: bool = False):
|
637 |
+
if onnx_trace:
|
638 |
+
return F.softmax(x.float(), dim=dim)
|
639 |
+
else:
|
640 |
+
return F.softmax(x, dim=dim, dtype=torch.float32)
|
641 |
+
|
642 |
+
|
643 |
+
class FairseqIncrementalState(object):
|
644 |
+
def __init__(self, *args, **kwargs):
|
645 |
+
super().__init__(*args, **kwargs)
|
646 |
+
self.init_incremental_state()
|
647 |
+
|
648 |
+
def init_incremental_state(self):
|
649 |
+
self._incremental_state_id = str(uuid.uuid4())
|
650 |
+
|
651 |
+
def _get_full_incremental_state_key(self, key: str) -> str:
|
652 |
+
return "{}.{}".format(self._incremental_state_id, key)
|
653 |
+
|
654 |
+
def get_incremental_state(
|
655 |
+
self,
|
656 |
+
incremental_state: Optional[Dict[str, Dict[str, Optional[Tensor]]]],
|
657 |
+
key: str,
|
658 |
+
) -> Optional[Dict[str, Optional[Tensor]]]:
|
659 |
+
"""Helper for getting incremental state for an nn.Module."""
|
660 |
+
full_key = self._get_full_incremental_state_key(key)
|
661 |
+
if incremental_state is None or full_key not in incremental_state:
|
662 |
+
return None
|
663 |
+
return incremental_state[full_key]
|
664 |
+
|
665 |
+
def set_incremental_state(
|
666 |
+
self,
|
667 |
+
incremental_state: Optional[Dict[str, Dict[str, Optional[Tensor]]]],
|
668 |
+
key: str,
|
669 |
+
value: Dict[str, Optional[Tensor]],
|
670 |
+
) -> Optional[Dict[str, Dict[str, Optional[Tensor]]]]:
|
671 |
+
"""Helper for setting incremental state for an nn.Module."""
|
672 |
+
if incremental_state is not None:
|
673 |
+
full_key = self._get_full_incremental_state_key(key)
|
674 |
+
incremental_state[full_key] = value
|
675 |
+
return incremental_state
|
676 |
+
|
677 |
+
|
678 |
+
def with_incremental_state(cls):
|
679 |
+
cls.__bases__ = (FairseqIncrementalState,) + tuple(
|
680 |
+
b for b in cls.__bases__ if b != FairseqIncrementalState
|
681 |
+
)
|
682 |
+
return cls
|
683 |
+
|
684 |
+
|
685 |
+
@with_incremental_state
|
686 |
+
class LucaGPLMMultiheadAttention(nn.Module):
|
687 |
+
def __init__(
|
688 |
+
self,
|
689 |
+
embed_dim,
|
690 |
+
num_heads,
|
691 |
+
kdim=None,
|
692 |
+
vdim=None,
|
693 |
+
dropout=0.0,
|
694 |
+
bias=True,
|
695 |
+
add_bias_kv: bool = False,
|
696 |
+
add_zero_attn: bool = False,
|
697 |
+
self_attention: bool = False,
|
698 |
+
encoder_decoder_attention: bool = False,
|
699 |
+
use_rotary_embeddings: bool = False,
|
700 |
+
):
|
701 |
+
super().__init__()
|
702 |
+
self.embed_dim = embed_dim
|
703 |
+
self.kdim = kdim if kdim is not None else embed_dim
|
704 |
+
self.vdim = vdim if vdim is not None else embed_dim
|
705 |
+
self.qkv_same_dim = self.kdim == embed_dim and self.vdim == embed_dim
|
706 |
+
|
707 |
+
self.num_heads = num_heads
|
708 |
+
self.dropout = dropout
|
709 |
+
self.head_dim = embed_dim // num_heads
|
710 |
+
assert (
|
711 |
+
self.head_dim * num_heads == self.embed_dim
|
712 |
+
), "embed_dim must be divisible by num_heads"
|
713 |
+
self.scaling = self.head_dim**-0.5
|
714 |
+
|
715 |
+
self.self_attention = self_attention
|
716 |
+
self.encoder_decoder_attention = encoder_decoder_attention
|
717 |
+
|
718 |
+
assert not self.self_attention or self.qkv_same_dim, (
|
719 |
+
"Self-attention requires query, key and " "value to be of the same size"
|
720 |
+
)
|
721 |
+
|
722 |
+
self.k_proj = nn.Linear(self.kdim, embed_dim, bias=bias)
|
723 |
+
self.v_proj = nn.Linear(self.vdim, embed_dim, bias=bias)
|
724 |
+
self.q_proj = nn.Linear(embed_dim, embed_dim, bias=bias)
|
725 |
+
|
726 |
+
self.out_proj = nn.Linear(embed_dim, embed_dim, bias=bias)
|
727 |
+
|
728 |
+
if add_bias_kv:
|
729 |
+
self.bias_k = Parameter(torch.Tensor(1, 1, embed_dim))
|
730 |
+
self.bias_v = Parameter(torch.Tensor(1, 1, embed_dim))
|
731 |
+
else:
|
732 |
+
self.bias_k = self.bias_v = None
|
733 |
+
|
734 |
+
self.add_zero_attn = add_zero_attn
|
735 |
+
|
736 |
+
self.reset_parameters()
|
737 |
+
|
738 |
+
self.onnx_trace = False
|
739 |
+
self.rot_emb = None
|
740 |
+
if use_rotary_embeddings:
|
741 |
+
self.rot_emb = RotaryEmbedding(dim=self.head_dim)
|
742 |
+
|
743 |
+
self.enable_torch_version = False
|
744 |
+
if hasattr(F, "multi_head_attention_forward"):
|
745 |
+
self.enable_torch_version = True
|
746 |
+
else:
|
747 |
+
self.enable_torch_version = False
|
748 |
+
|
749 |
+
def prepare_for_onnx_export_(self):
|
750 |
+
self.onnx_trace = True
|
751 |
+
|
752 |
+
def reset_parameters(self):
|
753 |
+
nn.init.xavier_uniform_(self.k_proj.weight, gain=nn.init.calculate_gain("relu"))
|
754 |
+
nn.init.xavier_uniform_(self.v_proj.weight, gain=nn.init.calculate_gain("relu"))
|
755 |
+
nn.init.xavier_uniform_(self.q_proj.weight, gain=nn.init.calculate_gain("relu"))
|
756 |
+
|
757 |
+
nn.init.xavier_uniform_(self.out_proj.weight, gain=nn.init.calculate_gain("relu"))
|
758 |
+
# nn.init.xavier_uniform_(self.out_proj.weight)
|
759 |
+
if self.out_proj.bias is not None:
|
760 |
+
nn.init.constant_(self.out_proj.bias, 0.0)
|
761 |
+
if self.bias_k is not None:
|
762 |
+
nn.init.xavier_normal_(self.bias_k)
|
763 |
+
if self.bias_v is not None:
|
764 |
+
nn.init.xavier_normal_(self.bias_v)
|
765 |
+
|
766 |
+
def forward(
|
767 |
+
self,
|
768 |
+
query,
|
769 |
+
key: Optional[Tensor],
|
770 |
+
value: Optional[Tensor],
|
771 |
+
key_padding_mask: Optional[Tensor] = None,
|
772 |
+
incremental_state: Optional[Dict[str, Dict[str, Optional[Tensor]]]] = None,
|
773 |
+
need_weights: bool = True,
|
774 |
+
static_kv: bool = False,
|
775 |
+
attn_mask: Optional[Tensor] = None,
|
776 |
+
before_softmax: bool = False,
|
777 |
+
need_head_weights: bool = False,
|
778 |
+
) -> Tuple[Tensor, Optional[Tensor]]:
|
779 |
+
if need_head_weights:
|
780 |
+
need_weights = True
|
781 |
+
|
782 |
+
tgt_len, bsz, embed_dim = query.size()
|
783 |
+
assert embed_dim == self.embed_dim
|
784 |
+
assert list(query.size()) == [tgt_len, bsz, embed_dim]
|
785 |
+
|
786 |
+
if (
|
787 |
+
not self.rot_emb
|
788 |
+
and self.enable_torch_version
|
789 |
+
and not self.onnx_trace
|
790 |
+
and incremental_state is None
|
791 |
+
and not static_kv
|
792 |
+
# A workaround for quantization to work. Otherwise JIT compilation
|
793 |
+
# treats bias in linear module as method.
|
794 |
+
and not torch.jit.is_scripting()
|
795 |
+
and not need_head_weights
|
796 |
+
):
|
797 |
+
assert key is not None and value is not None
|
798 |
+
return F.multi_head_attention_forward(
|
799 |
+
query,
|
800 |
+
key,
|
801 |
+
value,
|
802 |
+
self.embed_dim,
|
803 |
+
self.num_heads,
|
804 |
+
torch.empty([0]),
|
805 |
+
torch.cat((self.q_proj.bias, self.k_proj.bias, self.v_proj.bias)),
|
806 |
+
self.bias_k,
|
807 |
+
self.bias_v,
|
808 |
+
self.add_zero_attn,
|
809 |
+
self.dropout,
|
810 |
+
self.out_proj.weight,
|
811 |
+
self.out_proj.bias,
|
812 |
+
self.training,
|
813 |
+
key_padding_mask,
|
814 |
+
need_weights,
|
815 |
+
attn_mask,
|
816 |
+
use_separate_proj_weight=True,
|
817 |
+
q_proj_weight=self.q_proj.weight,
|
818 |
+
k_proj_weight=self.k_proj.weight,
|
819 |
+
v_proj_weight=self.v_proj.weight,
|
820 |
+
)
|
821 |
+
if incremental_state is not None:
|
822 |
+
saved_state = self._get_input_buffer(incremental_state)
|
823 |
+
if saved_state is not None and "prev_key" in saved_state:
|
824 |
+
# previous time steps are cached - no need to recompute
|
825 |
+
# key and value if they are static
|
826 |
+
if static_kv:
|
827 |
+
assert self.encoder_decoder_attention and not self.self_attention
|
828 |
+
key = value = None
|
829 |
+
else:
|
830 |
+
saved_state = None
|
831 |
+
|
832 |
+
if self.self_attention:
|
833 |
+
q = self.q_proj(query)
|
834 |
+
k = self.k_proj(query)
|
835 |
+
v = self.v_proj(query)
|
836 |
+
elif self.encoder_decoder_attention:
|
837 |
+
# encoder-decoder attention
|
838 |
+
q = self.q_proj(query)
|
839 |
+
if key is None:
|
840 |
+
assert value is None
|
841 |
+
k = v = None
|
842 |
+
else:
|
843 |
+
k = self.k_proj(key)
|
844 |
+
v = self.v_proj(key)
|
845 |
+
|
846 |
+
else:
|
847 |
+
assert key is not None and value is not None
|
848 |
+
q = self.q_proj(query)
|
849 |
+
k = self.k_proj(key)
|
850 |
+
v = self.v_proj(value)
|
851 |
+
q *= self.scaling
|
852 |
+
|
853 |
+
if self.bias_k is not None:
|
854 |
+
assert self.bias_v is not None
|
855 |
+
k = torch.cat([k, self.bias_k.repeat(1, bsz, 1)])
|
856 |
+
v = torch.cat([v, self.bias_v.repeat(1, bsz, 1)])
|
857 |
+
if attn_mask is not None:
|
858 |
+
attn_mask = torch.cat(
|
859 |
+
[attn_mask, attn_mask.new_zeros(attn_mask.size(0), 1)], dim=1
|
860 |
+
)
|
861 |
+
if key_padding_mask is not None:
|
862 |
+
key_padding_mask = torch.cat(
|
863 |
+
[
|
864 |
+
key_padding_mask,
|
865 |
+
key_padding_mask.new_zeros(key_padding_mask.size(0), 1),
|
866 |
+
],
|
867 |
+
dim=1,
|
868 |
+
)
|
869 |
+
|
870 |
+
q = q.contiguous().view(tgt_len, bsz * self.num_heads, self.head_dim).transpose(0, 1)
|
871 |
+
if k is not None:
|
872 |
+
k = k.contiguous().view(-1, bsz * self.num_heads, self.head_dim).transpose(0, 1)
|
873 |
+
if v is not None:
|
874 |
+
v = v.contiguous().view(-1, bsz * self.num_heads, self.head_dim).transpose(0, 1)
|
875 |
+
|
876 |
+
if saved_state is not None:
|
877 |
+
# saved states are stored with shape (bsz, num_heads, seq_len, head_dim)
|
878 |
+
if "prev_key" in saved_state:
|
879 |
+
_prev_key = saved_state["prev_key"]
|
880 |
+
assert _prev_key is not None
|
881 |
+
prev_key = _prev_key.view(bsz * self.num_heads, -1, self.head_dim)
|
882 |
+
if static_kv:
|
883 |
+
k = prev_key
|
884 |
+
else:
|
885 |
+
assert k is not None
|
886 |
+
k = torch.cat([prev_key, k], dim=1)
|
887 |
+
if "prev_value" in saved_state:
|
888 |
+
_prev_value = saved_state["prev_value"]
|
889 |
+
assert _prev_value is not None
|
890 |
+
prev_value = _prev_value.view(bsz * self.num_heads, -1, self.head_dim)
|
891 |
+
if static_kv:
|
892 |
+
v = prev_value
|
893 |
+
else:
|
894 |
+
assert v is not None
|
895 |
+
v = torch.cat([prev_value, v], dim=1)
|
896 |
+
prev_key_padding_mask: Optional[Tensor] = None
|
897 |
+
if "prev_key_padding_mask" in saved_state:
|
898 |
+
prev_key_padding_mask = saved_state["prev_key_padding_mask"]
|
899 |
+
assert k is not None and v is not None
|
900 |
+
key_padding_mask = LucaGPLMMultiheadAttention._append_prev_key_padding_mask(
|
901 |
+
key_padding_mask=key_padding_mask,
|
902 |
+
prev_key_padding_mask=prev_key_padding_mask,
|
903 |
+
batch_size=bsz,
|
904 |
+
src_len=k.size(1),
|
905 |
+
static_kv=static_kv,
|
906 |
+
)
|
907 |
+
|
908 |
+
saved_state["prev_key"] = k.view(bsz, self.num_heads, -1, self.head_dim)
|
909 |
+
saved_state["prev_value"] = v.view(bsz, self.num_heads, -1, self.head_dim)
|
910 |
+
saved_state["prev_key_padding_mask"] = key_padding_mask
|
911 |
+
# In this branch incremental_state is never None
|
912 |
+
assert incremental_state is not None
|
913 |
+
incremental_state = self._set_input_buffer(incremental_state, saved_state)
|
914 |
+
assert k is not None
|
915 |
+
src_len = k.size(1)
|
916 |
+
|
917 |
+
# This is part of a workaround to get around fork/join parallelism
|
918 |
+
# not supporting Optional types.
|
919 |
+
if key_padding_mask is not None and key_padding_mask.dim() == 0:
|
920 |
+
key_padding_mask = None
|
921 |
+
|
922 |
+
if key_padding_mask is not None:
|
923 |
+
assert key_padding_mask.size(0) == bsz
|
924 |
+
assert key_padding_mask.size(1) == src_len
|
925 |
+
|
926 |
+
if self.add_zero_attn:
|
927 |
+
assert v is not None
|
928 |
+
src_len += 1
|
929 |
+
k = torch.cat([k, k.new_zeros((k.size(0), 1) + k.size()[2:])], dim=1)
|
930 |
+
v = torch.cat([v, v.new_zeros((v.size(0), 1) + v.size()[2:])], dim=1)
|
931 |
+
if attn_mask is not None:
|
932 |
+
attn_mask = torch.cat(
|
933 |
+
[attn_mask, attn_mask.new_zeros(attn_mask.size(0), 1)], dim=1
|
934 |
+
)
|
935 |
+
if key_padding_mask is not None:
|
936 |
+
key_padding_mask = torch.cat(
|
937 |
+
[
|
938 |
+
key_padding_mask,
|
939 |
+
torch.zeros(key_padding_mask.size(0), 1).type_as(key_padding_mask),
|
940 |
+
],
|
941 |
+
dim=1,
|
942 |
+
)
|
943 |
+
|
944 |
+
if self.rot_emb:
|
945 |
+
q, k = self.rot_emb(q, k)
|
946 |
+
|
947 |
+
attn_weights = torch.bmm(q, k.transpose(1, 2))
|
948 |
+
attn_weights = LucaGPLMMultiheadAttention.apply_sparse_mask(attn_weights, tgt_len, src_len, bsz)
|
949 |
+
|
950 |
+
assert list(attn_weights.size()) == [bsz * self.num_heads, tgt_len, src_len]
|
951 |
+
|
952 |
+
if attn_mask is not None:
|
953 |
+
attn_mask = attn_mask.unsqueeze(0)
|
954 |
+
if self.onnx_trace:
|
955 |
+
attn_mask = attn_mask.repeat(attn_weights.size(0), 1, 1)
|
956 |
+
attn_weights += attn_mask
|
957 |
+
|
958 |
+
if key_padding_mask is not None:
|
959 |
+
# don't attend to padding symbols
|
960 |
+
attn_weights = attn_weights.view(bsz, self.num_heads, tgt_len, src_len)
|
961 |
+
attn_weights = attn_weights.masked_fill(
|
962 |
+
key_padding_mask.unsqueeze(1).unsqueeze(2).to(torch.bool), float("-inf")
|
963 |
+
)
|
964 |
+
attn_weights = attn_weights.view(bsz * self.num_heads, tgt_len, src_len)
|
965 |
+
|
966 |
+
if before_softmax:
|
967 |
+
return attn_weights, v
|
968 |
+
|
969 |
+
attn_weights_float = utils_softmax(attn_weights, dim=-1, onnx_trace=self.onnx_trace)
|
970 |
+
attn_weights = attn_weights_float.type_as(attn_weights)
|
971 |
+
attn_probs = F.dropout(
|
972 |
+
attn_weights_float.type_as(attn_weights),
|
973 |
+
p=self.dropout,
|
974 |
+
training=self.training,
|
975 |
+
)
|
976 |
+
assert v is not None
|
977 |
+
attn = torch.bmm(attn_probs, v)
|
978 |
+
assert list(attn.size()) == [bsz * self.num_heads, tgt_len, self.head_dim]
|
979 |
+
if self.onnx_trace and attn.size(1) == 1:
|
980 |
+
# when ONNX tracing a single decoder step (sequence length == 1)
|
981 |
+
# the transpose is a no-op copy before view, thus unnecessary
|
982 |
+
attn = attn.contiguous().view(tgt_len, bsz, embed_dim)
|
983 |
+
else:
|
984 |
+
attn = attn.transpose(0, 1).contiguous().view(tgt_len, bsz, embed_dim)
|
985 |
+
attn = self.out_proj(attn)
|
986 |
+
attn_weights: Optional[Tensor] = None
|
987 |
+
if need_weights:
|
988 |
+
attn_weights = attn_weights_float.view(
|
989 |
+
bsz, self.num_heads, tgt_len, src_len
|
990 |
+
).type_as(attn).transpose(1, 0)
|
991 |
+
if not need_head_weights:
|
992 |
+
# average attention weights over heads
|
993 |
+
attn_weights = attn_weights.mean(dim=0)
|
994 |
+
|
995 |
+
return attn, attn_weights
|
996 |
+
|
997 |
+
@staticmethod
|
998 |
+
def _append_prev_key_padding_mask(
|
999 |
+
key_padding_mask: Optional[Tensor],
|
1000 |
+
prev_key_padding_mask: Optional[Tensor],
|
1001 |
+
batch_size: int,
|
1002 |
+
src_len: int,
|
1003 |
+
static_kv: bool,
|
1004 |
+
) -> Optional[Tensor]:
|
1005 |
+
# saved key padding masks have shape (bsz, seq_len)
|
1006 |
+
if prev_key_padding_mask is not None and static_kv:
|
1007 |
+
new_key_padding_mask = prev_key_padding_mask
|
1008 |
+
elif prev_key_padding_mask is not None and key_padding_mask is not None:
|
1009 |
+
new_key_padding_mask = torch.cat(
|
1010 |
+
[prev_key_padding_mask.float(), key_padding_mask.float()], dim=1
|
1011 |
+
)
|
1012 |
+
# During incremental decoding, as the padding token enters and
|
1013 |
+
# leaves the frame, there will be a time when prev or current
|
1014 |
+
# is None
|
1015 |
+
elif prev_key_padding_mask is not None:
|
1016 |
+
filler = torch.zeros(
|
1017 |
+
(batch_size, src_len - prev_key_padding_mask.size(1)),
|
1018 |
+
device=prev_key_padding_mask.device,
|
1019 |
+
)
|
1020 |
+
new_key_padding_mask = torch.cat(
|
1021 |
+
[prev_key_padding_mask.float(), filler.float()], dim=1
|
1022 |
+
)
|
1023 |
+
elif key_padding_mask is not None:
|
1024 |
+
filler = torch.zeros(
|
1025 |
+
(batch_size, src_len - key_padding_mask.size(1)),
|
1026 |
+
device=key_padding_mask.device,
|
1027 |
+
)
|
1028 |
+
new_key_padding_mask = torch.cat([filler.float(), key_padding_mask.float()], dim=1)
|
1029 |
+
else:
|
1030 |
+
new_key_padding_mask = prev_key_padding_mask
|
1031 |
+
return new_key_padding_mask
|
1032 |
+
|
1033 |
+
@torch.jit.export
|
1034 |
+
def reorder_incremental_state(
|
1035 |
+
self, incremental_state: Dict[str, Dict[str, Optional[Tensor]]], new_order: Tensor
|
1036 |
+
):
|
1037 |
+
input_buffer = self._get_input_buffer(incremental_state)
|
1038 |
+
if input_buffer is not None:
|
1039 |
+
for k in input_buffer.keys():
|
1040 |
+
input_buffer_k = input_buffer[k]
|
1041 |
+
if input_buffer_k is not None:
|
1042 |
+
if self.encoder_decoder_attention and input_buffer_k.size(0) == new_order.size(
|
1043 |
+
0
|
1044 |
+
):
|
1045 |
+
break
|
1046 |
+
input_buffer[k] = input_buffer_k.index_select(0, new_order)
|
1047 |
+
incremental_state = self._set_input_buffer(incremental_state, input_buffer)
|
1048 |
+
return incremental_state
|
1049 |
+
|
1050 |
+
def _get_input_buffer(
|
1051 |
+
self, incremental_state: Optional[Dict[str, Dict[str, Optional[Tensor]]]]
|
1052 |
+
) -> Dict[str, Optional[Tensor]]:
|
1053 |
+
result = self.get_incremental_state(incremental_state, "attn_state")
|
1054 |
+
if result is not None:
|
1055 |
+
return result
|
1056 |
+
else:
|
1057 |
+
empty_result: Dict[str, Optional[Tensor]] = {}
|
1058 |
+
return empty_result
|
1059 |
+
|
1060 |
+
def _set_input_buffer(
|
1061 |
+
self,
|
1062 |
+
incremental_state: Dict[str, Dict[str, Optional[Tensor]]],
|
1063 |
+
buffer: Dict[str, Optional[Tensor]],
|
1064 |
+
):
|
1065 |
+
return self.set_incremental_state(incremental_state, "attn_state", buffer)
|
1066 |
+
|
1067 |
+
def apply_sparse_mask(attn_weights, tgt_len: int, src_len: int, bsz: int):
|
1068 |
+
return attn_weights
|
1069 |
+
|
1070 |
+
def upgrade_state_dict_named(self, state_dict, name):
|
1071 |
+
prefix = name + "." if name != "" else ""
|
1072 |
+
items_to_add = {}
|
1073 |
+
keys_to_remove = []
|
1074 |
+
for k in state_dict.keys():
|
1075 |
+
if k.endswith(prefix + "in_proj_weight"):
|
1076 |
+
dim = int(state_dict[k].shape[0] / 3)
|
1077 |
+
items_to_add[prefix + "q_proj.weight"] = state_dict[k][:dim]
|
1078 |
+
items_to_add[prefix + "k_proj.weight"] = state_dict[k][dim : 2 * dim]
|
1079 |
+
items_to_add[prefix + "v_proj.weight"] = state_dict[k][2 * dim :]
|
1080 |
+
|
1081 |
+
keys_to_remove.append(k)
|
1082 |
+
|
1083 |
+
k_bias = prefix + "in_proj_bias"
|
1084 |
+
if k_bias in state_dict.keys():
|
1085 |
+
dim = int(state_dict[k].shape[0] / 3)
|
1086 |
+
items_to_add[prefix + "q_proj.bias"] = state_dict[k_bias][:dim]
|
1087 |
+
items_to_add[prefix + "k_proj.bias"] = state_dict[k_bias][dim : 2 * dim]
|
1088 |
+
items_to_add[prefix + "v_proj.bias"] = state_dict[k_bias][2 * dim :]
|
1089 |
+
|
1090 |
+
keys_to_remove.append(prefix + "in_proj_bias")
|
1091 |
+
|
1092 |
+
for k in keys_to_remove:
|
1093 |
+
del state_dict[k]
|
1094 |
+
|
1095 |
+
for key, value in items_to_add.items():
|
1096 |
+
state_dict[key] = value
|
1097 |
+
|
1098 |
+
|
1099 |
+
def rotate_half(x):
|
1100 |
+
x1, x2 = x.chunk(2, dim=-1)
|
1101 |
+
return torch.cat((-x2, x1), dim=-1)
|
1102 |
+
|
1103 |
+
|
1104 |
+
def apply_rotary_pos_emb(x, cos, sin):
|
1105 |
+
cos = cos[:, : x.shape[-2], :]
|
1106 |
+
sin = sin[:, : x.shape[-2], :]
|
1107 |
+
|
1108 |
+
return (x * cos) + (rotate_half(x) * sin)
|
1109 |
+
|
1110 |
+
|
1111 |
+
class RotaryEmbedding(torch.nn.Module):
|
1112 |
+
def __init__(self, dim: int, *_, **__):
|
1113 |
+
super().__init__()
|
1114 |
+
inv_freq = 1.0 / (10000 ** (torch.arange(0, dim, 2).float() / dim))
|
1115 |
+
self.register_buffer("inv_freq", inv_freq)
|
1116 |
+
|
1117 |
+
self._seq_len_cached = None
|
1118 |
+
self._cos_cached = None
|
1119 |
+
self._sin_cached = None
|
1120 |
+
|
1121 |
+
def _update_cos_sin_tables(self, x, seq_dimension=1):
|
1122 |
+
seq_len = x.shape[seq_dimension]
|
1123 |
+
|
1124 |
+
if seq_len != self._seq_len_cached or self._cos_cached.device != x.device:
|
1125 |
+
self._seq_len_cached = seq_len
|
1126 |
+
t = torch.arange(x.shape[seq_dimension], device=x.device).type_as(self.inv_freq)
|
1127 |
+
freqs = torch.einsum("i,j->ij", t, self.inv_freq)
|
1128 |
+
emb = torch.cat((freqs, freqs), dim=-1).to(x.device)
|
1129 |
+
|
1130 |
+
self._cos_cached = emb.cos()[None, :, :]
|
1131 |
+
self._sin_cached = emb.sin()[None, :, :]
|
1132 |
+
|
1133 |
+
return self._cos_cached, self._sin_cached
|
1134 |
+
|
1135 |
+
def forward(self, q: torch.Tensor, k: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
|
1136 |
+
self._cos_cached, self._sin_cached = self._update_cos_sin_tables(k, seq_dimension=-2)
|
1137 |
+
|
1138 |
+
return (
|
1139 |
+
apply_rotary_pos_emb(q, self._cos_cached, self._sin_cached),
|
1140 |
+
apply_rotary_pos_emb(k, self._cos_cached, self._sin_cached),
|
1141 |
+
)
|
1142 |
+
|
1143 |
+
|
1144 |
+
|
1145 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|