-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvbert.py
1310 lines (1045 loc) · 48.5 KB
/
convbert.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
# author: Jeiyoon
# This code is based on awesome works (See README.md)
from setproctitle import *
setproctitle('k4ke-convbert-test')
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "4,5,6,7"
import json
import argparse
import logging
import inspect
import random
from random import choice, shuffle, randrange
from typing import Callable, List, Optional, Set, Tuple, Union
import math
import torch
from torch import Tensor
import torch.nn as nn
import torch.nn.functional as F
import sentencepiece as spm
import numpy as np
from tqdm import tqdm
from sklearn.metrics import accuracy_score
from transformers import Trainer, TrainingArguments, EarlyStoppingCallback
logger = logging.getLogger()
def get_args(description="ConvBERT"):
parser = argparse.ArgumentParser(description=description)
parser.add_argument("--task", default=None, type=str, required=True,
help="task name: pretrain or finetune")
parser.add_argument("--vocab_model_path", default="web-crawler/kowiki/kowiki.model", type=str, required=False,
help="vocabulary model path (model)")
parser.add_argument("--vocab_input_path", default="web-crawler/kowiki/kowiki.txt", type=str, required=False,
help="vocabulary input path (txt)")
parser.add_argument("--vocab_output_path", default="vocab_bert/vocab_bert_{}.json", type=str, required=False,
help="vocabulary output path (json)")
parser.add_argument("--finetune_data_path", default="web-crawler", type=str, required=False,
help="data path for finetuning")
parser.add_argument("--pretrained_model_path", default="pretrain/save_convbert_pretrain.pth", type=str, required=False,
help="prtrained model path")
args = parser.parse_args()
return args
def generate_pretrain_data(vocab, count, n_seq, mask_prob):
args = get_args()
in_file = args.vocab_input_path # pretrain 전처리에 사용할 corpus 경로
out_file = args.vocab_output_path # 전처리후 저장되는 경로
# generate pretrain dataset
make_pretrain_data(vocab, in_file, out_file, count, n_seq, mask_prob)
def apply_chunking_to_forward(
forward_fn: Callable[..., torch.Tensor], chunk_size: int, chunk_dim: int, *input_tensors
) -> torch.Tensor:
"""
This function chunks the `input_tensors` into smaller input tensor parts of size `chunk_size` over the dimension
`chunk_dim`. It then applies a layer `forward_fn` to each chunk independently to save memory.
If the `forward_fn` is independent across the `chunk_dim` this function will yield the same result as directly
applying `forward_fn` to `input_tensors`.
Args:
forward_fn (`Callable[..., torch.Tensor]`):
The forward function of the model.
chunk_size (`int`):
The chunk size of a chunked tensor: `num_chunks = len(input_tensors[0]) / chunk_size`.
chunk_dim (`int`):
The dimension over which the `input_tensors` should be chunked.
input_tensors (`Tuple[torch.Tensor]`):
The input tensors of `forward_fn` which will be chunked
Returns:
`torch.Tensor`: A tensor with the same shape as the `forward_fn` would have given if applied`.
Examples:
```python
# rename the usual forward() fn to forward_chunk()
def forward_chunk(self, hidden_states):
hidden_states = self.decoder(hidden_states)
return hidden_states
# implement a chunked forward function
def forward(self, hidden_states):
return apply_chunking_to_forward(self.forward_chunk, self.chunk_size_lm_head, self.seq_len_dim, hidden_states)
```"""
assert len(input_tensors) > 0, f"{input_tensors} has to be a tuple/list of tensors"
input_tensors_clone = input_tensors[0]
if input_tensors_clone.size()[1] % 2 != 0:
input_tensors_clone = input_tensors_clone[:,:-1,:]
input_tensors = list(input_tensors)
input_tensors[0] = input_tensors_clone
input_tensors = tuple(input_tensors)
# inspect.signature exist since python 3.5 and is a python method -> no problem with backward compatibility
num_args_in_forward_chunk_fn = len(inspect.signature(forward_fn).parameters)
if num_args_in_forward_chunk_fn != len(input_tensors):
raise ValueError(
f"forward_chunk_fn expects {num_args_in_forward_chunk_fn} arguments, but only {len(input_tensors)} input "
"tensors are given"
)
if chunk_size > 0:
tensor_shape = input_tensors[0].shape[chunk_dim]
for input_tensor in input_tensors:
if input_tensor.shape[chunk_dim] != tensor_shape:
raise ValueError(
f"All input tenors have to be of the same shape: {tensor_shape}, "
f"found shape {input_tensor.shape[chunk_dim]}"
)
if input_tensors[0].shape[chunk_dim] % chunk_size != 0:
raise ValueError(
f"The dimension to be chunked {input_tensors[0].shape[chunk_dim]} has to be a multiple of the chunk "
f"size {chunk_size}"
)
num_chunks = input_tensors[0].shape[chunk_dim] // chunk_size
# chunk input tensor into tuples
input_tensors_chunks = tuple(input_tensor.chunk(num_chunks, dim=chunk_dim) for input_tensor in input_tensors)
# apply forward fn to every tuple
output_chunks = tuple(forward_fn(*input_tensors_chunk) for input_tensors_chunk in zip(*input_tensors_chunks))
# concatenate output at same dimension
return torch.cat(output_chunks, dim=chunk_dim)
return forward_fn(*input_tensors)
def get_attn_pad_mask(seq_q, seq_k, i_pad):
batch_size, len_q = seq_q.size()
batch_size, len_k = seq_k.size()
pad_attn_mask = seq_k.data.eq(i_pad)
pad_attn_mask = pad_attn_mask.unsqueeze(1).expand(batch_size, len_q, len_k)
return pad_attn_mask
def create_pretrain_mask(tokens, mask_cnt, vocab_list):
cand_idx = []
for (i, token) in enumerate(tokens):
if token == "[CLS]" or token == "[SEP]":
continue
"""
1. # u2581: 단어의 시작 (# cls -> u2581)
- 이 값으로 시작하지 않으면 이전 token과 연결된 subword임
"""
if 0 < len(cand_idx) and not token.startswith(u"\u2581"):
cand_idx[-1].append(i)
else:
cand_idx.append([i])
"""
2. 단어 랜덤 선택을 위해 단어 index를 섞음
"""
shuffle(cand_idx)
mask_lms = []
rand = random.Random()
for index_set in cand_idx:
"""
3. mask_cnt는 전체 tokens 개수의 15%에 해당함
"""
if len(mask_lms) >= mask_cnt:
break
if len(mask_lms) + len(index_set) > mask_cnt:
continue
for index in index_set:
masked_token = None
"""
4. index에 대해 80% 확률로 [MASK]로 치환
"""
# if random() < 0.8: # 80% replace with [MASK]
if rand.random() < 0.8: # 80% replace with [MASK]
masked_token = "[MASK]"
else:
"""
5. 남은 20%에 대해 10%는 현재 값을 유지, 10%는 vocab_list (토큰 모음)에서 임의의 값을 선택함
"""
# if random() < 0.5: # 10% keep original
if rand.random() < 0.5: # 10% keep original
masked_token = tokens[index]
else: # 10% random word
masked_token = choice(vocab_list)
"""
6. masked token의 index와 정답값을 저장
"""
mask_lms.append({"index": index, "label": tokens[index]})
tokens[index] = masked_token
mask_lms = sorted(mask_lms, key=lambda x: x["index"])
mask_idx = [p["index"] for p in mask_lms]
mask_label = [p["label"] for p in mask_lms]
return tokens, mask_idx, mask_label
def trim_tokens(tokens_a, tokens_b, max_seq):
while True:
total_length = len(tokens_a) + len(tokens_b)
if total_length <= max_seq:
break
"""
1. Tokens A의 길이가 길 경우 앞에서 부터 토큰을 제거
2. Tokens B의 길이가 길 경우 뒤에서 부터 토큰을 제거
"""
if len(tokens_a) > len(tokens_b):
del tokens_a[0]
else:
tokens_b.pop()
def create_pretrain_instances(docs, doc_idx, doc, n_seq, mask_prob, vocab_list):
# tokens = ["[CLS]"] + tokens_a + ["[SEP]"] + tokens_b + ["[SEP]"]
max_seq = n_seq - 3 # [CLS], [SEP], [SEP]
tgt_seq = max_seq
instances = []
current_chunk = []
current_length = 0
for i in range(len(doc)):
current_chunk.append(doc[i]) # line 추가
current_length += len(doc[i]) # line의 token 수 추가
"""
1. 마지막 줄이거나 target token 수를 넘을경우 학습 데이터 생성
"""
if i == len(doc) - 1 or current_length >= tgt_seq:
if 0 < len(current_chunk):
a_end = 1
if 1 < len(current_chunk):
# 랜덤하게 길이를 선택해서 tokens_a를 만들음
a_end = randrange(1, len(current_chunk))
tokens_a = []
for j in range(a_end):
tokens_a.extend(current_chunk[j])
tokens_b = []
rand = random.Random()
if len(current_chunk) == 1 or rand.random() < 0.5:
is_next = 0 # False
tokens_b_len = tgt_seq - len(tokens_a)
random_doc_idx = doc_idx # 현재 doc idx
while doc_idx == random_doc_idx:
random_doc_idx = randrange(0, len(docs))
random_doc = docs[random_doc_idx] # 다른 doc index
random_start = randrange(0, len(random_doc))
for j in range(random_start, len(random_doc)):
tokens_b.extend(random_doc[j])
else: # 0.5
is_next = 1 # True
for j in range(a_end, len(current_chunk)): # tokens_a에 이어서 tokens_b 생성
tokens_b.extend(current_chunk[j])
"""
2. 토큰들의 길이가 최대길이를 넘지않게 줄이기
"""
trim_tokens(tokens_a, tokens_b, max_seq)
assert 0 < len(tokens_a)
assert 0 < len(tokens_b)
tokens = ["[CLS]"] + tokens_a + ["[SEP]"] + tokens_b + ["[SEP]"]
segment = [0] * (len(tokens_a) + 2) + [1] * (len(tokens_b) + 1) # bert 학습을 위한 segment 나누기
"""
3. Pretrain을 위한 [MASK] 생성
"""
tokens, mask_idx, mask_label = create_pretrain_mask(tokens, int((len(tokens) - 3) * mask_prob),
vocab_list)
instance = {
"tokens": tokens,
"segment": segment,
"is_next": is_next, # A에 이어서 B를 생성했는지 (bool)
"mask_idx": mask_idx,
"mask_label": mask_label
}
instances.append(instance)
current_chunk = []
current_length = 0
return instances
def make_pretrain_data(vocab, in_file, out_file, count, n_seq, mask_prob):
# len(vocab_list): 8006
"""
1. unknown 제거
"""
vocab_list = []
for id in range(vocab.get_piece_size()):
if not vocab.is_unknown(id):
vocab_list.append(vocab.id_to_piece(id))
"""
2. 학습에 사용할 말뭉치가 총 몇 라인인지 확인
"""
# line_cnt: 4875507
line_cnt = 0
with open(in_file, "r") as in_f:
for _ in in_f:
line_cnt += 1
"""
3. 각 라인마다 vocab을 이용해 tokenize한 뒤 docs 리스트에 추가
"""
# list(623167)
docs = [] # 단락 배열
with open(in_file, "r") as f:
doc = [] # 단락
with tqdm(total=line_cnt, desc=f"Loading") as pbar:
for i, line in enumerate(f):
line = line.strip()
if line == "":
if 0 < len(doc): # 빈 줄이면 단락의 끝이므로 doc을 docs에 추가하고 doc을 새로 만들음
docs.append(doc)
doc = []
else:
pieces = vocab.encode_as_pieces(line)
if 0 < len(pieces):
doc.append(pieces)
pbar.update(1)
if doc:
docs.append(doc)
"""
4. count 횟수 만큼 돌면서 pretrain data를 생성함
- 왜냐하면 bert는 Masking을 15%만 하기 때문에 한번에 전체적인 단어를 학습할 수 가 없음
- e.g., count = 10
"""
for index in range(count):
output = out_file.format(index)
if os.path.isfile(output): continue
with open(output, "w") as out_f:
with tqdm(total=len(docs), desc=f"Making") as pbar:
for i, doc in enumerate(docs):
"""
5. 단락(doc)별 pretrain 데이터 생성
"""
instances = create_pretrain_instances(docs, i, doc, n_seq, mask_prob, vocab_list)
for instance in instances:
out_f.write(json.dumps(instance))
out_f.write("\n")
pbar.update(1)
def pretrin_collate_fn(inputs):
labels_cls, labels_lm, inputs, segments = list(zip(*inputs))
"""
배치단위로 데이터를 처리하기 위해 패딩을 추가하여 길이를 맞춤
"""
labels_lm = torch.nn.utils.rnn.pad_sequence(labels_lm, batch_first=True, padding_value=-1)
inputs = torch.nn.utils.rnn.pad_sequence(inputs, batch_first=True, padding_value=0)
segments = torch.nn.utils.rnn.pad_sequence(segments, batch_first=True, padding_value=0)
batch = [
torch.stack(labels_cls, dim=0),
labels_lm,
inputs,
segments
]
return batch
def finetune_collate_fn(inputs):
labels, inputs, segments = list(zip(*inputs))
"""
pretrain이 아니라서 labels_lm이 없음
"""
inputs = torch.nn.utils.rnn.pad_sequence(inputs, batch_first=True, padding_value=0)
segments = torch.nn.utils.rnn.pad_sequence(segments, batch_first=True, padding_value=0)
batch = [
torch.stack(labels, dim=0),
inputs,
segments,
]
return batch
def train_epoch(config, epoch, model, criterion_lm, criterion_cls, optimizer, train_loader):
# pretrain
losses = []
model.train()
with tqdm(total=len(train_loader), desc=f"Train({epoch})") as pbar:
for i, value in enumerate(train_loader):
labels_cls, labels_lm, inputs, segments = map(lambda v: v.to(config.device), value)
optimizer.zero_grad()
outputs = model(inputs, segments)
logits_cls, logits_lm = outputs[0], outputs[1]
loss_cls = criterion_cls(logits_cls, labels_cls)
loss_lm = criterion_lm(logits_lm.view(-1, logits_lm.size(2)), labels_lm.view(-1))
loss = loss_cls + loss_lm
loss_val = loss_lm.item()
losses.append(loss_val)
loss.backward()
optimizer.step()
pbar.update(1)
pbar.set_postfix_str(f"Loss: {loss_val:.3f} ({np.mean(losses):.3f})")
return np.mean(losses)
def finetune_epoch(config, epoch, model, criterion_cls, optimizer, train_loader):
losses = []
model.train()
with tqdm(total=len(train_loader), desc=f"Train({epoch})") as pbar:
for i, value in enumerate(train_loader):
labels, inputs, segments = map(lambda v: v.to(config.device), value)
optimizer.zero_grad()
outputs = model(inputs, segments)
# logits_cls = outputs[0]
# logits_cls = outputs[:, 0]
"""
두 값 중 큰값의 idx를 label과 비교함
"""
logits_cls = torch.argmax(outputs, dim=1)
# logits_cls: Tensor(2,) -> Tensor(32, )
# labels: Tensor(32, )
# criterion_cls = torch.nn.CrossEntropyLoss()
loss_cls = criterion_cls(logits_cls.float(), labels.float())
loss = loss_cls
loss_val = loss_cls.item()
losses.append(loss_val)
loss.requires_grad_(True)
loss.backward()
optimizer.step()
pbar.update(1)
pbar.set_postfix_str(f"Loss: {loss_val:.3f} ({np.mean(losses):.3f})")
return np.mean(losses)
def finetune(model, config, learning_rate, n_epoch, train_loader, test_loader):
"""
Fine tuning과 evaluation을 진행하는 부분
Multi-gpu setting
"""
config.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
NGPU = torch.cuda.device_count()
if NGPU > 1:
model = torch.nn.DataParallel(model, device_ids=list(range(NGPU)))
model.to(config.device)
criterion_cls = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
best_epoch, best_loss, best_score = 0, 0, 0
losses, scores = [], []
for epoch in range(n_epoch):
loss = finetune_epoch(config, epoch, model, criterion_cls, optimizer, train_loader)
score = eval_epoch(config, model, test_loader)
losses.append(loss)
scores.append(score)
if best_score < score:
best_epoch, best_loss, best_score = epoch, loss, score
print(f">>>> epoch={best_epoch}, loss={best_loss:.5f}, socre={best_score:.5f}")
return losses, scores
def eval_epoch(config, model, data_loader):
matchs = []
model.eval()
with tqdm(total=len(data_loader), desc=f"Valid") as pbar:
for i, value in enumerate(data_loader):
labels, inputs, segments = map(lambda v: v.to(config.device), value)
# outputs: Tensor(128, 2)
outputs = model(inputs, segments)
indices = torch.argmax(outputs, dim=1)
# logits_cls = outputs[0]
# logits_cls: Tensor(2, )
# _, indices = logits_cls.max(1)
# labels: Tensor(128, )
match = torch.eq(indices, labels).detach()
matchs.extend(match.cpu())
"""
평가 지표: Accuracy
"""
accuracy = np.sum(matchs) / len(matchs) if 0 < len(matchs) else 0
pbar.update(1)
pbar.set_postfix_str(f"Acc: {accuracy:.3f}")
return np.sum(matchs) / len(matchs) if 0 < len(matchs) else 0
class Config(dict):
__getattr__ = dict.__getitem__
__setattr__ = dict.__setitem__
@classmethod
def load(cls, file):
with open(file, 'r') as f:
config = json.loads(f.read())
return Config(config)
class SeparableConv1D(nn.Module):
def __init__(self, config, input_filters, output_filters, kernel_size, **kwargs):
super().__init__()
# https://pytorch.org/docs/stable/generated/torch.nn.Conv1d.html
self.depthwise = nn.Conv1d(
input_filters,
input_filters,
kernel_size=kernel_size,
groups=input_filters,
padding=kernel_size // 2,
bias=False,
)
self.pointwise = nn.Conv1d(input_filters, output_filters, kernel_size=1, bias=False)
self.bias = nn.Parameter(torch.zeros(output_filters, 1))
self.depthwise.weight.data.normal_(mean=0.0, std=config.initializer_range)
self.pointwise.weight.data.normal_(mean=0.0, std=config.initializer_range)
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
x = self.depthwise(hidden_states)
x = self.pointwise(x)
x += self.bias
return x
class GELUActivation(nn.Module):
"""
Original Implementation of the GELU activation function in Google BERT repo when initially created. For
information: OpenAI GPT's GELU is slightly different (and gives slightly different results): 0.5 * x * (1 +
torch.tanh(math.sqrt(2 / math.pi) * (x + 0.044715 * torch.pow(x, 3)))) This is now written in C in nn.functional
Also see the Gaussian Error Linear Units paper: https://arxiv.org/abs/1606.08415
"""
def __init__(self, use_gelu_python: bool = False):
super().__init__()
if use_gelu_python:
self.act = self._gelu_python
else:
self.act = nn.functional.gelu
def _gelu_python(self, input: Tensor) -> Tensor:
return input * 0.5 * (1.0 + torch.erf(input / math.sqrt(2.0)))
def forward(self, input: Tensor) -> Tensor:
return self.act(input)
class SpanBasedDynamicConvolution(nn.Module):
def __init__(self, config):
super().__init__()
"""
Bottleneck design for self-attention
"""
# e.g., 12 // 2 = 6
new_num_attention_heads = config.n_head // config.head_ratio
if new_num_attention_heads < 1: # head ratio exception
self.head_ratio = config.n_head
self.num_attention_heads = 1
else:
self.num_attention_heads = new_num_attention_heads # new size
self.head_ratio = config.head_ratio
self.conv_kernel_size = config.conv_kernel_size # e.g., 9 (best performance)
if config.d_hidn % self.num_attention_heads != 0:
raise ValueError("hidden_size should be divisible by num_attention_heads")
self.attention_head_size = config.d_hidn // config.n_head # e.g., 768 // 12 = 64
self.all_head_size = self.num_attention_heads * self.attention_head_size # e.g., 6 * 64 = 384
# self.W_Q = nn.Linear(self.config.d_hidn, self.config.n_head * self.config.d_head) # 12 * 64
self.W_Q = nn.Linear(config.d_hidn, self.all_head_size) # e.g., 6 * 64 = 384
self.W_K = nn.Linear(config.d_hidn, self.all_head_size)
self.W_V = nn.Linear(config.d_hidn, self.all_head_size)
# def __init__(self, config, input_filters, output_filters, kernel_size, **kwargs):
self.key_conv_attn_layer = SeparableConv1D(
config, config.d_hidn, self.all_head_size, self.conv_kernel_size
)
self.conv_kernel_layer = nn.Linear(self.all_head_size,
self.num_attention_heads * self.conv_kernel_size) # Linear(384, 6 * 9)
self.conv_out_layer = nn.Linear(config.d_hidn, self.all_head_size)
# https://pytorch.org/docs/stable/generated/torch.nn.Unfold.html
# https://npclinic3.tistory.com/6
self.unfold = nn.Unfold(
kernel_size=[self.conv_kernel_size, 1],
padding=[int((self.conv_kernel_size - 1) / 2), 0]
)
self.dropout = nn.Dropout(config.dropout)
# self.scaled_dot_attn = ScaledDotProductAttention(self.config) # self-attn
def transpose_for_scores(self, x):
new_x_shape = x.size()[:-1] + (self.num_attention_heads, self.attention_head_size)
x = x.view(*new_x_shape)
return x.permute(0, 2, 1, 3)
def forward(self,
hidden_states: torch.Tensor,
attention_mask: Optional[torch.FloatTensor] = None,
head_mask: Optional[torch.FloatTensor] = None,
encoder_hidden_states: Optional[torch.Tensor] = None,
output_attentions: Optional[bool] = False,
) -> Tuple[torch.Tensor, Optional[torch.Tensor]]:
mixed_query_layer = self.W_Q(hidden_states)
batch_size = hidden_states.size(0)
if encoder_hidden_states is not None:
mixed_key_layer = self.W_K(encoder_hidden_states)
mixed_value_layer = self.W_V(encoder_hidden_states)
else:
mixed_key_layer = self.W_K(hidden_states)
mixed_value_layer = self.W_V(hidden_states)
mixed_key_conv_attn_layer = self.key_conv_attn_layer(hidden_states.transpose(1, 2))
mixed_key_conv_attn_layer = mixed_key_conv_attn_layer.transpose(1, 2)
query_layer = self.transpose_for_scores(mixed_query_layer)
value_layer = self.transpose_for_scores(mixed_value_layer)
key_layer = self.transpose_for_scores(mixed_key_layer)
"""
[Multiply]
"""
conv_attn_layer = torch.multiply(mixed_key_conv_attn_layer, mixed_query_layer)
"""
[Linear]
"""
conv_kernel_layer = self.conv_kernel_layer(conv_attn_layer)
conv_kernel_layer = torch.reshape(conv_kernel_layer, [-1, self.conv_kernel_size, 1])
"""
[Softmax]
"""
conv_kernel_layer = torch.softmax(conv_kernel_layer, dim=1)
"""
[LConv & Value multiplication]
"""
conv_out_layer = self.conv_out_layer(hidden_states)
conv_out_layer = torch.reshape(conv_out_layer, [batch_size, -1, self.all_head_size])
conv_out_layer = conv_out_layer.transpose(1, 2).contiguous().unsqueeze(-1)
conv_out_layer = nn.functional.unfold(
conv_out_layer,
kernel_size=[self.conv_kernel_size, 1],
dilation=1,
padding=[(self.conv_kernel_size - 1) // 2, 0],
stride=1,
)
conv_out_layer = conv_out_layer.transpose(1, 2).reshape(
batch_size, -1, self.all_head_size, self.conv_kernel_size
)
conv_out_layer = torch.reshape(conv_out_layer, [-1, self.attention_head_size, self.conv_kernel_size])
conv_out_layer = torch.matmul(conv_out_layer, conv_kernel_layer)
conv_out_layer = torch.reshape(conv_out_layer, [-1, self.all_head_size])
"""
Self-attention
"""
attention_scores = torch.matmul(query_layer, key_layer.transpose(-1, -2))
attention_scores = attention_scores / math.sqrt(self.attention_head_size) # scale
# attention_scores: Tensor(128, 6, 256, 256)
# attention_mask: Tensor(128, 256, 256) -> Tensor(128, 6, 256, 256)
attention_mask = attention_mask.unsqueeze(1).repeat(1, self.num_attention_heads, 1, 1)
if attention_mask is not None:
if attention_scores.size()[3] > attention_mask.size()[3]:
attention_scores = attention_scores[:,:,:-1,:-1]
elif attention_scores.size()[3] < attention_mask.size()[3]:
attention_mask = attention_mask[:,:,:-1,:-1]
else:
pass
attention_scores = attention_scores + attention_mask
# softmax
attention_probs = nn.functional.softmax(attention_scores, dim=-1)
attention_probs = self.dropout(attention_probs)
if head_mask is not None:
attention_probs = attention_probs * head_mask
# value
context_layer = torch.matmul(attention_probs, value_layer)
context_layer = context_layer.permute(0, 2, 1, 3).contiguous()
# context_layer: Self-Attn
# conv_out: SDConv
conv_out = torch.reshape(conv_out_layer, [batch_size, -1, self.num_attention_heads, self.attention_head_size])
"""
Mixed Attention ([Concat])
"""
context_layer = torch.cat([context_layer, conv_out], 2)
# self.head_ratio: 2
# self.all_head_size: 384
# new_context_layer_shape: torch.Size([128, 256, 768])
# context_layer: Tensor(128, 256, 12, 64)
# context_layer.size()[:-2]: torch.Size([128, 256])
# attention_probs: Tensor(32, 6, 86, 86)
# context_layer: Tensor(32, 86, 768)
new_context_layer_shape = context_layer.size()[:-2] + (self.head_ratio * self.all_head_size,)
context_layer = context_layer.view(*new_context_layer_shape)
outputs = (context_layer, attention_probs) if output_attentions else (context_layer,)
return outputs
class ConvBertSelfOutput(nn.Module):
def __init__(self, config):
"""
BERT output을 만들기 위한 layer들 정의
"""
super().__init__()
self.dense = nn.Linear(config.d_hidn, config.d_hidn)
self.LayerNorm = nn.LayerNorm(config.d_hidn, eps=config.layer_norm_epsilon)
self.dropout = nn.Dropout(config.dropout)
def forward(self,
hidden_states: torch.Tensor,
input_tensor: torch.Tensor) -> torch.Tensor:
hidden_states = self.dense(hidden_states)
hidden_states = self.dropout(hidden_states)
hidden_states = self.LayerNorm(hidden_states + input_tensor)
# Tensor(32, 94, 768)
return hidden_states
class ConvBertAttention(nn.Module):
def __init__(self, config):
super().__init__()
self.self = SpanBasedDynamicConvolution(config)
self.output = ConvBertSelfOutput(config)
def forward(self,
hidden_states: torch.Tensor,
attention_mask: Optional[torch.FloatTensor] = None,
head_mask: Optional[torch.FloatTensor] = None,
encoder_hidden_states: Optional[torch.Tensor] = None,
output_attentions: Optional[bool] = False) -> Tuple[torch.Tensor, Optional[torch.FloatTensor]]:
self_outputs = self.self(
hidden_states,
attention_mask,
head_mask,
encoder_hidden_states,
output_attentions,
)
self_outputs_clone = self_outputs[0].clone()
# attention_output = self.output(self_outputs[0], hidden_states)
attention_output = self.output(self_outputs_clone, hidden_states)
outputs = (attention_output,) + self_outputs[1:]
return outputs
class GroupedLinearLayer(nn.Module):
def __init__(self, input_size, output_size, num_groups):
super().__init__()
self.input_size = input_size
self.output_size = output_size
self.num_groups = num_groups
self.group_in_dim = self.input_size // self.num_groups
self.group_out_dim = self.output_size // self.num_groups
# https://easy-going-programming.tistory.com/11
# https://tutorials.pytorch.kr/prototype/skip_param_init.html
# self.weight = nn.Parameter(torch.empty(self.num_groups, self.group_in_dim, self.group_out_dim))
# self.bias = nn.Parameter(torch.empty(output_size))
self.register_parameter('weight', nn.Parameter(torch.empty(self.num_groups, self.group_in_dim, self.group_out_dim)))
self.register_parameter('bias', nn.Parameter(torch.empty(output_size)))
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
batch_size = list(hidden_states.size())[0]
# Tensor(32, 2, 768) -> Tensor(64, 2, 384)
x = torch.reshape(hidden_states, [-1, self.num_groups, self.group_in_dim])
x = x.permute(1, 0, 2)
x = torch.matmul(x, self.weight)
x = x.permute(1, 0, 2)
x = torch.reshape(x, [batch_size, -1, self.output_size])
x = x + self.bias
return x
class ConvBertIntermediate(nn.Module):
def __init__(self, config):
super().__init__()
if config.num_groups == 1:
self.dense = nn.Linear(config.d_hidn, config.intermediate_size)
else:
self.dense = GroupedLinearLayer(
input_size=config.d_hidn,
output_size=config.intermediate_size,
num_groups=config.num_groups
)
self.intermediate_act_fn = GELUActivation()
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
hidden_states = self.dense(hidden_states)
hidden_states = self.intermediate_act_fn(hidden_states)
return hidden_states
class ConvBertOutput(nn.Module):
def __init__(self, config):
super().__init__()
if config.num_groups == 1:
self.dense = nn.Linear(config.intermediate_size, config.d_hidn)
else: # num_groups == 2 라서 여기로 들어옴
self.dense = GroupedLinearLayer(
input_size=config.intermediate_size,
output_size=config.d_hidn,
num_groups=config.num_groups
)
self.LayerNorm = nn.LayerNorm(config.d_hidn, eps=config.layer_norm_epsilon)
self.dropout = nn.Dropout(config.dropout)
def forward(self, hidden_states: torch.Tensor, input_tensor: torch.Tensor) -> torch.Tensor:
hidden_states = self.dense(hidden_states)
hidden_states = self.dropout(hidden_states)
hidden_states = self.LayerNorm(hidden_states + input_tensor)
return hidden_states
class EncoderLayer(nn.Module):
def __init__(self, config):
super().__init__()
self.config = config
# https://huggingface.co/docs/transformers/main_classes/configuration#configuration
self.chunk_size_feed_forward = config.num_groups # best performance is 2 / config.chunk_size_feed_forward
self.seq_len_dim = 1
self.attention = ConvBertAttention(config)
self.is_decoder = False # config.is_decoder
self.add_cross_attention = False # config.add_cross_attention
self.intermediate = ConvBertIntermediate(config)
self.output = ConvBertOutput(config)
def forward(
self,
hidden_states: torch.Tensor,
attention_mask: Optional[torch.FloatTensor] = None,
head_mask: Optional[torch.FloatTensor] = None,
encoder_hidden_states: Optional[torch.Tensor] = None,
encoder_attention_mask: Optional[torch.Tensor] = None,
output_attentions: Optional[bool] = False,
) -> Tuple[torch.Tensor, Optional[torch.FloatTensor]]:
self_attention_outputs = self.attention(
hidden_states,
attention_mask,
head_mask,
output_attentions=output_attentions,
)
attention_output = self_attention_outputs[0]
# outputs = self_attention_outputs[1:] # no cross attention
"""
GL 적용
"""
layer_output = apply_chunking_to_forward(
self.feed_forward_chunk, self.chunk_size_feed_forward, self.seq_len_dim, attention_output
)
outputs = (layer_output,) # + outputs
# Tensor(batch_size, 256, 768)
return outputs[0] # attention_output
def feed_forward_chunk(self, attention_output):
intermediate_output = self.intermediate(attention_output)
layer_output = self.output(intermediate_output, attention_output)
return layer_output
class Encoder(nn.Module):
def __init__(self, config):
super().__init__()
self.config = config
# 12 (based-size model)
self.layers = nn.ModuleList([EncoderLayer(self.config) for _ in range(self.config.n_layer)])
self.gradient_checkpointing = False
self.enc_emb = nn.Embedding(self.config.n_enc_vocab, self.config.d_hidn)
self.pos_emb = nn.Embedding(self.config.n_enc_seq + 1, self.config.d_hidn)
self.seg_emb = nn.Embedding(self.config.n_seg_type, self.config.d_hidn) # to distinguish different sentences
def forward(self, inputs, segments):
positions = torch.arange(inputs.size(1),
device=inputs.device,
dtype=inputs.dtype).expand(inputs.size(0), inputs.size(1)).contiguous() + 1
pos_mask = inputs.eq(self.config.i_pad)
positions.masked_fill_(pos_mask, 0)
# (bs, n_enc_seq, d_hidn)
outputs = self.enc_emb(inputs) + self.pos_emb(positions) + self.seg_emb(segments)
# (bs, n_enc_seq, n_enc_seq)
attn_mask = get_attn_pad_mask(inputs, inputs, self.config.i_pad)
# self.layers = nn.ModuleList([EncoderLayer(self.config) for _ in range(self.config.n_layer)])
for layer in self.layers:
# (bs, n_enc_seq, d_hidn), (bs, n_head, n_enc_seq, n_enc_seq)
outputs = layer(outputs, attn_mask)
# (bs, n_enc_seq, d_hidn), [(bs, n_head, n_enc_seq, n_enc_seq)]
return outputs
class ConvBERT(nn.Module):
def __init__(self, config):
super().__init__()
self.config = config
"""
ConvBERT Encoder 선언 부분
"""
self.encoder = Encoder(self.config)
self.linear = nn.Linear(config.d_hidn, config.d_hidn)
self.activation = torch.tanh
def forward(self, inputs, segments):
# (bs, n_seq, d_hidn), [(bs, n_head, n_enc_seq, n_enc_seq)]
# outputs, self_attn_probs = self.encoder(inputs, segments)
outputs = self.encoder(inputs, segments)
# (bs, d_hidn)
outputs_cls = outputs[:, 0].contiguous()
outputs_cls = self.linear(outputs_cls)
outputs_cls = self.activation(outputs_cls)
# (bs, n_enc_seq, n_enc_vocab), (bs, d_hidn), [(bs, n_head, n_enc_seq, n_enc_seq)]
return outputs, outputs_cls # , self_attn_probs