-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvariant_1_finetune.py
361 lines (287 loc) · 12.9 KB
/
variant_1_finetune.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
from transformers import RobertaTokenizer, RobertaModel
import torch
from torch import nn as nn
import os
from torch.utils.data import Dataset, DataLoader
from torch.nn import functional as F
from torch import cuda
from sklearn import metrics
import numpy as np
from transformers import AdamW
from transformers import get_scheduler
from entities import VariantOneFinetuneDataset
from model import VariantOneFinetuneClassifier
from pytorchtools import EarlyStopping
from tqdm import tqdm
import pandas as pd
import preprocess_variant_1
# dataset_name = 'huawei_sub_dataset.csv'
dataset_name = 'ase_dataset_sept_19_2021.csv'
BEST_MODEL_PATH = 'model/patch_variant_1_finetune_best_model.sav'
FINE_TUNED_MODEL_PATH = 'model/patch_variant_1_finetuned_model.sav'
directory = os.path.dirname(os.path.abspath(__file__))
FINETUNE_EPOCH = 1
commit_code_folder_path = os.path.join(directory, 'commit_code')
model_folder_path = os.path.join(directory, 'model')
NUMBER_OF_EPOCHS = 15
TRAIN_BATCH_SIZE = 16
VALIDATION_BATCH_SIZE = 128
TEST_BATCH_SIZE = 128
EARLY_STOPPING_ROUND = 5
TRAIN_PARAMS = {'batch_size': TRAIN_BATCH_SIZE, 'shuffle': True, 'num_workers': 8}
VALIDATION_PARAMS = {'batch_size': VALIDATION_BATCH_SIZE, 'shuffle': True, 'num_workers': 8}
TEST_PARAMS = {'batch_size': TEST_BATCH_SIZE, 'shuffle': True, 'num_workers': 8}
LEARNING_RATE = 1e-5
use_cuda = cuda.is_available()
device = torch.device("cuda:0" if use_cuda else "cpu")
random_seed = 109
torch.manual_seed(random_seed)
torch.cuda.manual_seed(random_seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
CODE_LENGTH = 512
def get_input_and_mask(tokenizer, code):
inputs = tokenizer(code, padding='max_length', max_length=CODE_LENGTH, truncation=True, return_tensors="pt")
return inputs.data['input_ids'][0], inputs.data['attention_mask'][0]
def predict_test_data(model, testing_generator, device, need_prob=False):
print("Testing...")
y_pred = []
y_test = []
urls = []
probs = []
model.eval()
with torch.no_grad():
for id_batch, url_batch, input_batch, mask_batch, label_batch in tqdm(testing_generator):
input_batch, mask_batch, label_batch \
= input_batch.to(device), mask_batch.to(device), label_batch.to(device)
outs = model(input_batch, mask_batch)
outs = F.softmax(outs, dim=1)
y_pred.extend(torch.argmax(outs, dim=1).tolist())
y_test.extend(label_batch.tolist())
probs.extend(outs[:, 1].tolist())
urls.extend(list(url_batch))
precision = metrics.precision_score(y_pred=y_pred, y_true=y_test)
recall = metrics.recall_score(y_pred=y_pred, y_true=y_test)
f1 = metrics.f1_score(y_pred=y_pred, y_true=y_test)
try:
auc = metrics.roc_auc_score(y_true=y_test, y_score=probs)
except Exception:
auc = 0
print("Finish testing")
if not need_prob:
return precision, recall, f1, auc
else:
return precision, recall, f1, auc, urls, probs
def get_avg_validation_loss(model, validation_generator, loss_function):
validation_loss = 0
with torch.no_grad():
for id_batch, url_batch, input_batch, mask_batch, label_batch in validation_generator:
input_batch, mask_batch, label_batch \
= input_batch.to(device), mask_batch.to(device), label_batch.to(device)
outs = model(input_batch, mask_batch)
outs = F.log_softmax(outs, dim=1)
loss = loss_function(outs, label_batch)
validation_loss += loss
avg_val_los = validation_loss / len(validation_generator)
return avg_val_los
def train(model, learning_rate, number_of_epochs, training_generator, val_generator,
test_java_generator, test_python_generator):
loss_function = nn.NLLLoss()
optimizer = AdamW(model.parameters(), lr=learning_rate)
num_training_steps = number_of_epochs * len(training_generator)
lr_scheduler = get_scheduler(
"linear",
optimizer=optimizer,
num_warmup_steps=0,
num_training_steps=num_training_steps
)
train_losses = []
early_stopping = EarlyStopping(patience=EARLY_STOPPING_ROUND,
verbose=True, path=BEST_MODEL_PATH)
for epoch in range(number_of_epochs):
model.train()
total_loss = 0
current_batch = 0
for id_batch, url_batch, input_batch, mask_batch, label_batch in training_generator:
input_batch, mask_batch, label_batch \
= input_batch.to(device), mask_batch.to(device), label_batch.to(device)
outs = model(input_batch, mask_batch)
outs = F.log_softmax(outs, dim=1)
loss = loss_function(outs, label_batch)
train_losses.append(loss.item())
model.zero_grad()
loss.backward()
optimizer.step()
lr_scheduler.step()
total_loss += loss.detach().item()
current_batch += 1
if current_batch % 50 == 0:
print("Train commit iter {}, total loss {}, average loss {}".format(current_batch, np.sum(train_losses),
np.average(train_losses)))
print("epoch {}, training commit loss {}".format(epoch, np.sum(train_losses)))
train_losses = []
model.eval()
print("Calculating validation loss...")
val_loss = get_avg_validation_loss(model, val_generator, loss_function)
print("Average validation loss of this iteration: {}".format(val_loss))
print("-" * 32)
early_stopping(val_loss, model)
print("Result on Java testing dataset...")
precision, recall, f1, auc = predict_test_data(model=model,
testing_generator=test_java_generator,
device=device)
print("Precision: {}".format(precision))
print("Recall: {}".format(recall))
print("F1: {}".format(f1))
print("AUC: {}".format(auc))
print("-" * 32)
print("Result on Python testing dataset...")
precision, recall, f1, auc = predict_test_data(model=model,
testing_generator=test_python_generator,
device=device)
print("Precision: {}".format(precision))
print("Recall: {}".format(recall))
print("F1: {}".format(f1))
print("AUC: {}".format(auc))
print("-" * 32)
if early_stopping.early_stop:
print("Early stopping")
break
if epoch + 1 == FINETUNE_EPOCH:
torch.save(model.state_dict(), FINE_TUNED_MODEL_PATH)
if not isinstance(model, nn.DataParallel):
model.freeze_codebert()
else:
model.module.freeze_codebert()
return model
def retrieve_patch_data(all_data, all_label, all_url):
tokenizer = RobertaTokenizer.from_pretrained("microsoft/codebert-base")
print("Preparing tokenizer data...")
count = 0
id_to_label = {}
id_to_url = {}
id_to_input = {}
id_to_mask = {}
for i in tqdm(range(len(all_data))):
added_code = preprocess_variant_1.get_code_version(diff=all_data[i], added_version=True)
deleted_code = preprocess_variant_1.get_code_version(diff=all_data[i], added_version=False)
# TODO: need to balance code between added_code and deleted_code due to data truncation?
code = added_code + tokenizer.sep_token + deleted_code
input_ids, mask = get_input_and_mask(tokenizer, code)
id_to_input[i] = input_ids
id_to_mask[i] = mask
id_to_label[i] = all_label[i]
id_to_url[i] = all_url[i]
# count += 1
# if count % 1000 == 0:
# print("Number of records tokenized: {}/{}".format(count, len(all_data)))
return id_to_input, id_to_mask, id_to_label, id_to_url
def get_data():
print("Reading dataset...")
df = pd.read_csv(dataset_name)
df = df[['commit_id', 'repo', 'partition', 'diff', 'label', 'PL']]
items = df.to_numpy().tolist()
url_to_diff = {}
url_to_partition = {}
url_to_label = {}
url_to_pl = {}
for item in items:
commit_id = item[0]
repo = item[1]
url = repo + '/commit/' + commit_id
partition = item[2]
diff = item[3]
label = item[4]
pl = item[5]
if url not in url_to_diff:
url_to_diff[url] = ''
url_to_diff[url] = url_to_diff[url] + '\n' + diff
url_to_partition[url] = partition
url_to_label[url] = label
url_to_pl[url] = pl
patch_train, patch_val, patch_test_java, patch_test_python = [], [], [], []
label_train, label_val, label_test_java, label_test_python = [], [], [], []
url_train, url_val, url_test_java, url_test_python = [], [], [], []
print(len(url_to_diff.keys()))
for key in url_to_diff.keys():
url = key
diff = url_to_diff[key]
label = url_to_label[key]
partition = url_to_partition[key]
pl = url_to_pl[key]
if partition == 'train':
patch_train.append(diff)
label_train.append(label)
url_train.append(url)
elif partition == 'test':
if pl == 'java':
patch_test_java.append(diff)
label_test_java.append(label)
url_test_java.append(url)
elif pl == 'python':
patch_test_python.append(diff)
label_test_python.append(label)
url_test_python.append(url)
else:
raise Exception("Invalid programming language: {}".format(partition))
elif partition == 'val':
patch_val.append(diff)
label_val.append(label)
url_val.append(url)
else:
raise Exception("Invalid partition: {}".format(partition))
print("Finish reading dataset")
patch_data = {'train': patch_train, 'val': patch_val,
'test_java': patch_test_java, 'test_python': patch_test_python}
label_data = {'train': label_train, 'val': label_val,
'test_java': label_test_java, 'test_python': label_test_python}
url_data = {'train': url_train, 'val': url_val,
'test_java': url_test_java, 'test_python': url_test_python}
return patch_data, label_data, url_data
def do_train():
print("Dataset name: {}".format(dataset_name))
print("Saving model to: {}".format(BEST_MODEL_PATH))
patch_data, label_data, url_data = get_data()
train_ids, val_ids, test_java_ids, test_python_ids = [], [], [], []
index = 0
for i in range(len(patch_data['train'])):
train_ids.append(index)
index += 1
for i in range(len(patch_data['val'])):
val_ids.append(index)
index += 1
for i in range(len(patch_data['test_java'])):
test_java_ids.append(index)
index += 1
for i in range(len(patch_data['test_python'])):
test_python_ids.append(index)
index += 1
all_data = patch_data['train'] + patch_data['val'] + patch_data['test_java'] + patch_data['test_python']
all_label = label_data['train'] + label_data['val'] + label_data['test_java'] + label_data['test_python']
all_url = url_data['train'] + url_data['val'] + url_data['test_java'] + url_data['test_python']
print("Preparing commit patch data...")
id_to_input, id_to_mask, id_to_label, id_to_url = retrieve_patch_data(all_data, all_label, all_url)
print("Finish preparing commit patch data")
training_set = VariantOneFinetuneDataset(train_ids, id_to_label, id_to_url, id_to_input, id_to_mask)
val_set = VariantOneFinetuneDataset(val_ids, id_to_label, id_to_url, id_to_input, id_to_mask)
test_java_set = VariantOneFinetuneDataset(test_java_ids, id_to_label, id_to_url, id_to_input, id_to_mask)
test_python_set = VariantOneFinetuneDataset(test_python_ids, id_to_label, id_to_url, id_to_input, id_to_mask)
training_generator = DataLoader(training_set, **TRAIN_PARAMS)
val_generator = DataLoader(val_set, **VALIDATION_PARAMS)
test_java_generator = DataLoader(test_java_set, **TEST_PARAMS)
test_python_generator = DataLoader(test_python_set, **TEST_PARAMS)
model = VariantOneFinetuneClassifier()
if torch.cuda.device_count() > 1:
print("Let's use", torch.cuda.device_count(), "GPUs!")
# dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUs
model = nn.DataParallel(model)
model.to(device)
train(model=model,
learning_rate=LEARNING_RATE,
number_of_epochs=NUMBER_OF_EPOCHS,
training_generator=training_generator,
val_generator=val_generator,
test_java_generator=test_java_generator,
test_python_generator=test_python_generator)
if __name__ == '__main__':
do_train()