-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_glue.py
416 lines (339 loc) · 15 KB
/
train_glue.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
import pickle
import random
import argparse
import os
import sys
sys.path.append(os.getcwd())
os.environ["TOKENIZERS_PARALLELISM"] = "false"
import numpy as np
from transformers import *
from torch.utils.data import DataLoader
from model.bert import BERT_GLUE, BERT_STS, BERT_MNLI
from task.result_calculator import *
from datetime import datetime
import torch.nn as nn
from sklearn.metrics import matthews_corrcoef
from scipy.stats import pearsonr, spearmanr
import scipy.stats as stats
from torch.utils.data import Dataset
from model.__main__module import *
from tqdm import tqdm
import time
from model.bert_layers import Bert_For_Att_output, Bert_For_Att_output_MLM
from model.minilm_v2 import Bert_For_minilm_v2, Bert_For_minilm_v2_MLM
from torch.nn.parallel import DistributedDataParallel as DDP
Datapath = {
"cola" : "CoLA",
"mnli" : "MNLI",
"mrpc" : "MRPC",
"rte" : "RTE",
"sst" : "SST_2",
"sts" : "STS_B",
"qqp" : "QQP",
"qnli" : "QNLI"
}
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
def get_linear_decay_lr(args_lr, step, warmup_step, max_step):
if step <= warmup_step:
lr = args_lr * (step / warmup_step)
else:
#lr = args_lr * (1-step/max_step)
lr = args_lr / (max_step - warmup_step) * (max_step - step)
return lr
def layerwise_decay(lr, optimizer, args, config):
layer_num = config.num_hidden_layers + 2
for num, params in enumerate(optimizer.param_groups):
l = lr * args.layer_wise ** (layer_num - 1 - num)
params["lr"] = l
return optimizer
class create_dataset(Dataset):
def __init__(self, mode, tokenizer, dataset):
with open("./task/GLUE/" + Datapath[dataset.lower()] + "/dataset/"+ str(mode), 'rb') as f:
self.datas = pickle.load(f)
self.tokenizer = tokenizer
def __len__(self):
# 데이터 전체의 사이즈 반환
return len(self.datas)
def __getitem__(self, idx):
datapoint = self.datas[idx]
return datapoint
def padded_sequence_solo(samples):
encoded_dump = []
batch_label = []
LM_max = 0
for sample in samples:
#CoLA, SST
encoded_txt = tokenizer.encode(sample['pure_txt'])
# print(sample['pure_txt'].lower())
encoded_dump.append(encoded_txt)
batch_label.append(sample['label'])
if len(encoded_txt) > LM_max:
LM_max = len(encoded_txt)
if LM_max >= 128:
LM_max = 128
encoded_batch_txt = []
for i, encoded_example in enumerate(encoded_dump):
if len(encoded_example) <= LM_max:
encoded_batch_txt.append(encoded_example + [tokenizer.pad_token_id] * (LM_max-len(encoded_example)))
else:
encoded_batch_txt.append(encoded_example[:LM_max])
return encoded_batch_txt, batch_label
def padded_sequence_pair(samples):
encoded_dump = []
batch_label = []
LM_max = 0
for sample in samples:
#MRPC, RTE, STS
encoded_txt = tokenizer.encode(sample['sen1'] + tokenizer.sep_token + sample['sen2'])
encoded_dump.append(encoded_txt)
batch_label.append(sample['label'])
if len(encoded_txt) > LM_max:
LM_max = len(encoded_txt)
if LM_max >= 128:
LM_max = 128
encoded_batch_txt = []
for i, encoded_example in enumerate(encoded_dump):
if len(encoded_example) <= LM_max:
encoded_batch_txt.append(encoded_example + [tokenizer.pad_token_id] * (LM_max-len(encoded_example)))
else:
encoded_batch_txt.append(encoded_example[:LM_max])
return encoded_batch_txt, batch_label
def get_padded_sequence(dataset):
if dataset.lower() in ["cola", "sst"]:
return padded_sequence_solo
else:
return padded_sequence_pair
def get_spearman(val_dataloader, model, softmax):
model.eval()
val_pred = []
val_truth = []
spearmanr_vale = []
for batch_idx, batch in enumerate(val_dataloader):
encoded_batch_txt, batch_label = batch
val_batch = torch.LongTensor(encoded_batch_txt)
val_label = batch_label
#outputs = model(val_batch.to(device))
attention_mask = (val_batch != tokenizer.pad_token_id)
outputs = model(val_batch.to(device), attention_mask = attention_mask.to(device))
p = outputs.squeeze(-1).detach().cpu().numpy()
spearmanr_vale.append((spearmanr(p, val_label)[0]))
val_prob = softmax(outputs)
y_pred = np.argsort(val_prob.detach().cpu().numpy(), axis=1)
for i in range(y_pred.shape[0]):
val_pred.append(y_pred[i])
val_truth.append(val_label[i])
precision_score, recall_score, F1_score = pr_score(val_truth, val_pred)
accuracy = top1_acc(val_truth, val_pred)
return sum(spearmanr_vale)/len(spearmanr_vale)
#MRPC, SST, RTE
def get_accuracy(val_dataloader, model, softmax):
model.eval()
val_pred = []
val_truth = []
for batch_idx, batch in enumerate(val_dataloader):
encoded_batch_txt, batch_label = batch
val_batch = torch.LongTensor(encoded_batch_txt)
val_label = torch.LongTensor(batch_label)
attention_mask = (val_batch != tokenizer.pad_token_id)
outputs = model(val_batch.to(device), attention_mask = attention_mask.to(device))
val_prob = softmax(outputs)
y_pred = np.argsort(val_prob.detach().cpu().numpy(), axis=1)
for i in range(y_pred.shape[0]):
val_pred.append(y_pred[i])
val_truth.append(val_label[i])
precision_score, recall_score, F1_score = pr_score(val_truth, val_pred)
accuracy = top1_acc(val_truth, val_pred)
return accuracy, F1_score
#CoLA
def get_cola_score(val_dataloader, model, softmax):
model.eval()
val_pred = []
val_truth = []
mathew = []
mathew_gold = []
for batch_idx, batch in enumerate(val_dataloader):
encoded_batch_txt, batch_label = batch
val_batch = torch.LongTensor(encoded_batch_txt)
val_label = torch.LongTensor(batch_label)
attention_mask = (val_batch != tokenizer.pad_token_id)
outputs = model(val_batch.to(device), attention_mask = attention_mask.to(device))
val_prob = softmax(outputs)
y_pred = np.argsort(val_prob.detach().cpu().numpy(), axis=1)
for i in range(y_pred.shape[0]):
if int(y_pred[i][-1]) == 1:
mathew.append(1)
else:
mathew.append(-1)
if val_label[i] == 1:
mathew_gold.append(1)
else:
mathew_gold.append(-1)
val_pred.append(y_pred[i])
val_truth.append(val_label[i])
MCC = matthews_corrcoef(mathew_gold, mathew)
precision_score, recall_score, F1_score = pr_score(val_truth, val_pred)
accuracy = top1_acc(val_truth, val_pred)
return MCC
def get_opt_parameters(model, args, config):
if args.layer_wise > 0:
layer_num = config.num_hidden_layers + 2
optimizer_grouped_parameters = [{"params" : [], "lr" : args.lr * (args.layer_wise) ** (layer_num - depth), "weight_decay" : 0.0 } for depth in range(1, layer_num + 1)]
for name, params in model.named_parameters():
if "embeddings" in name or "embeddings_project" in name:
optimizer_grouped_parameters[0]["params"].append(params)
#last layer
elif "pooler" in name or "classifier" in name:
optimizer_grouped_parameters[-1]["params"].append(params)
#other layer
elif "layer" in name:
num = int(name.split(".")[3]) + 1
optimizer_grouped_parameters[num]["params"].append(params)
else:
print(name)
else:
param_optimizer = list(model.named_parameters())
no_decay = ['bias', 'gamma', 'beta', 'LayerNorm']
optimizer_grouped_parameters = [
{'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay)],
'weight_decay': args.weight_decay},
{'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
]
return optimizer_grouped_parameters
def train_glue(model, dataset, device, num, config, args):
random.seed(args.random_seed)
torch.manual_seed(args.random_seed)
if dataset == "sts":
criteria = nn.MSELoss()
args.num_labels = 1
elif dataset.lower() == "mnli":
criteria = nn.CrossEntropyLoss()
args.num_labels = 3
else:
criteria = nn.CrossEntropyLoss()
optimizer_grouped_parameters = get_opt_parameters(model, args, config)
optimizer = torch.optim.Adam(optimizer_grouped_parameters, betas=(0.9, 0.999), eps=1e-6, lr=args.lr)
scaler = torch.cuda.amp.GradScaler()
softmax = nn.Softmax(-1)
step = 1
iters = 1
padded_sequence = get_padded_sequence(dataset)
best_score = 0
train_dataset = create_dataset('train', tokenizer, dataset)
val_dataset = create_dataset('val', tokenizer, dataset)
total_step = args.epochs * (len(train_dataset) // args.step_batch_size)
warmup_step = int(0.1 * total_step)
st = time.time()
for epoch in tqdm(range(args.epochs)):
Loss = 0
Loss_len = 0
train_dataloader = DataLoader(train_dataset, batch_size = args.step_batch_size, shuffle = True, collate_fn = padded_sequence, drop_last= True, num_workers= num)
model.train()
for batch in train_dataloader:
encoded_batch_txt, batch_label = batch
train_batch = torch.LongTensor(encoded_batch_txt)
if dataset == "sts":
train_label = torch.FloatTensor(batch_label).unsqueeze(1).to(device)
attention_mask = (train_batch != tokenizer.pad_token_id)
outputs = model(train_batch.to(device), attention_mask = attention_mask.to(device))
else:
train_label = torch.LongTensor(batch_label)
with torch.cuda.amp.autocast():
attention_mask = (train_batch != tokenizer.pad_token_id)
outputs = model(train_batch.to(device), attention_mask = attention_mask.to(device))
loss = criteria(outputs, train_label.to(device))
Loss += loss.item()
scaler.scale(loss).backward()
if args.layer_wise > 0:
lr = get_linear_decay_lr(args.lr, step, warmup_step=warmup_step, max_step=total_step)
optimizer = layerwise_decay(lr, optimizer, args, config)
else:
optimizer, lr = lr_scheduler(args.lr, optimizer, step, warmup_step=warmup_step, max_step=total_step)
scaler.step(optimizer)
scaler.update()
optimizer.zero_grad()
step += 1
iters += 1
Loss_len += 1
# validation
val_dataloader = DataLoader(val_dataset, batch_size=args.step_batch_size, shuffle=True, collate_fn=padded_sequence,
drop_last=False, num_workers = num)
if dataset.lower() == "cola":
score = get_cola_score(val_dataloader, model, softmax)
elif dataset.lower() == "sts":
score = get_spearman(val_dataloader, model, softmax)
elif dataset.lower() == "mrpc":
s, f = get_accuracy(val_dataloader, model, softmax)
score = f
else:
score, _ = get_accuracy(val_dataloader, model, softmax)
if score > best_score:
best_score = score
print(f"The best score in {dataset} | lr : {args.lr} | batch : {args.step_batch_size} | seed : {args.random_seed} | warmup : {warmup_step} | total : {total_step} | time: {round(time.time() - st, 2)} ==> {best_score}")
return best_score
def get_model(args, configuration):
#if args.config.lower() == "half":
if args.from_pretrained is not None:
#base_model = Bert_For_Att_output.from_pretrained(args.from_pretrained)
base_model = BertModel.from_pretrained(args.from_pretrained)
#base_model = base_model.bert
else:
if "ext" in args.config.lower():
base_model = Bert_For_Att_output(configuration, True, configuration.hidden_size // 2)
base_model = Tutor_KD(base_model, configuration)
else:
base_model = Bert_For_Att_output(configuration, True, None)
base_model = Tutor_KD(base_model, configuration)
torch.cuda.set_device(device)
base_model.load_state_dict(torch.load(args.model_save_path))
base_model = base_model.base_model
if dataset == "sts":
model = BERT_STS(base_model, tokenizer, configuration)
elif dataset.lower() == "mnli":
model = BERT_MNLI(base_model, tokenizer, configuration)
else:
model = BERT_GLUE(base_model, tokenizer, configuration)
model.to(device)
return model
def get_config(args):
if args.from_pretrained is not None:
configuration = BertConfig.from_pretrained(args.from_pretrained)
return configuration
if args.config == "half":
configuration = Bert_6_layer
elif args.config == "ext-12":
configuration = Bert_Small_Head_12_Config
elif args.config == "ext-6":
configuration = Bert_Small_Head_6_Config
elif args.config == "ext-2":
configuration = Bert_Small_Head_2_Config
return configuration
if __name__ == "__main__":
import torch
from transformers import BertForMaskedLM, ElectraModel
import numpy as np
from config import *
from model.bert import Small_Bert
from model.tinybert import TinyBertForSequenceClassification, TinyBertForPreTraining
parser = argparse.ArgumentParser()
parser.add_argument("--gpu_num", default='0', help="choose gpu number: 0, 1, 2, 3", type=int)
parser.add_argument("--data", default = "sts", help = "choose glue data : cola, rte, mrpc, sst, sts, qnli...", type = str)
parser.add_argument("--epochs", default =5, type = int)
parser.add_argument("--from_pretrained", default= "bert-base-uncased", help="if you load the model from pre-trained use this", type=str)
parser.add_argument("--model_save_path", default="", help="choose model save path", type=str)
parser.add_argument("--config", default='bert-base-uncased', help="choose model architecture from: half, extreme-12, ext-6, ext-2 ", type=str)
parser.add_argument("--weight_decay", default=0.0, help="insert weight decay", type=float)
parser.add_argument("--layer_wise", default= 0.0, help = "layer wise dacay", type = float)
parser.add_argument("--batch_size", default=32, help="insert batch size", type=int)
parser.add_argument("--step_batch_size", default=32, help="insert step batch size", type=int)
parser.add_argument("--random_seed", default=23, help="insert step batch size", type=int)
parser.add_argument("--lr", default=5e-5, help="insert learning rate", type=float)
args = parser.parse_args()
configuration = get_config(args)
dataset = args.data
device = torch.device("cuda:%d"%args.gpu_num)
model_save_path = args.model_save_path
score = []
settings = []
model = get_model(args, configuration)
score = train_glue(model, dataset, device, 5, configuration, args)
print(score)