-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGenerate_Iterative_Sample.py
196 lines (147 loc) · 7.48 KB
/
Generate_Iterative_Sample.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
import numpy as np
import pandas as pd
import torch,sys
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader, RandomSampler, SequentialSampler
import warnings
from torch import cuda
from transformers import T5Tokenizer, T5ForConditionalGeneration
import loader
import torch.autograd as autograd
import csv
import os, gc
import sys, subprocess,fnmatch, shutil, csv,re, datetime
def inRevertList(save_to_revert,patch_revert):
flag=False
if not os.path.exists(patch_revert):
with open(patch_revert, 'w') as revertlist:
revertlist.write(save_to_revert+'\n')
return flag
else:
with open(patch_revert, 'r') as revertlist:
lines = revertlist.readlines()
for line in lines:
line=line.replace('\n','')
if save_to_revert in line and line in save_to_revert:
flag = True
return flag
if not flag:
with open(patch_revert, 'a') as revertlist:
revertlist.write(save_to_revert+'\n')
return flag
def getBugInfo(bugid):
bugid=str(bugid).replace(' ','')
with open(TEST_PATH,'r') as testfile:
lines = testfile.readlines()
for l in lines:
bid=l.split('\t')[0]
bid=bid.replace(' ','')
if bid in bugid and bugid in bid:
patch = l.split('\t')[1]
patch = patch.replace('[PATCH]','')
bug_represent = l.split('\t')[2]
file_path = l.split('\t')[3]
if '[FE]' in bug_represent:
buggy = bug_represent.split('[FE]')[0]
buggy = buggy.split('[BUGGY]')[1]
bug_represent = ' [FE]' + bug_represent.split('[FE]')[1]
elif '[CE]' in bug_represent:
buggy = bug_represent.split('[CE]')[0]
buggy = buggy.split('[BUGGY]')[1]
bug_represent = ' [CE]' + bug_represent.split('[CE]')[1]
elif '[CONTEXT]' in bug_represent:
buggy = bug_represent.split('[CONTEXT]')[0]
buggy = buggy.split('[BUGGY]')[1]
bug_represent=' [CONTEXT] ' + bug_represent.split('[CONTEXT]')[1]
return patch, buggy, bug_represent,file_path
def getNewId(result_filename):
if not os.path.exists(result_filename):
return 0
with open(result_filename,'r') as results:
lines = results.readlines()
return len(lines)
def test( model, tokenizer, device, loader,model_index,index):
return_sequences = 100
model.eval()
identicalset=[]
with torch.no_grad():
for _,data in enumerate(loader, 0):
if _>-1:
gc.collect()
torch.cuda.empty_cache()
y = data['target_ids'].to(device, dtype = torch.long)
ids = data['source_ids'].to(device, dtype = torch.long)
mask = data['source_mask'].to(device, dtype = torch.long)
bugid = data['bugid'].to(device, dtype = torch.long)
print("====bugid===",bugid.item())
generated_ids = model.generate(
input_ids = ids,
attention_mask = mask,
max_length=64,
num_beams=return_sequences,
repetition_penalty=3.0,
# length_penalty=0.5,
early_stopping = False,
num_return_sequences=return_sequences,
num_beam_groups = 1
)
preds = [tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=True) for g in generated_ids]
target = [tokenizer.decode(t, skip_special_tokens=True, clean_up_tokenization_spaces=True)for t in y]
target = target[0]
patch, buggy, bug_represent,file_path = getBugInfo(bugid.item())
for i in range(0,return_sequences):
predition = preds[i]
predition_no_space=predition.replace(' ','')
patch_no_space = patch.replace(' ','')
buggy_no_space = buggy.replace(' ','')
# not identical to patch
if predition_no_space in patch_no_space and patch_no_space in predition_no_space:
print('identical to patch')
with open('./D4J1_TEST_IDENTICAL_Iterative_'+index+'.csv', 'a') as csvfile:
filewriter = csv.writer(csvfile, delimiter='\t',escapechar=' ',quoting=csv.QUOTE_NONE)
position=(model_index-5)*return_sequences+i+1
filewriter.writerow([bugid.item(), position, patch, predition, bug_represent,file_path])
# not identical to buggy
elif predition_no_space in buggy_no_space and buggy_no_space in predition_no_space:
print('identical to original buggy')
else:
# repeatFlag = inRevertList(str(bugid.item())+'#'+predition_no_space,'revertlist.csv')
# print('repeatFlag: '+str(repeatFlag))
# if not repeatFlag:
new_bug_represent= '[BUG] [BUGGY] '+predition+' '+bug_represent
result_filename='./D4J1_TEST_Iterative'+index+'.csv'
newid = getNewId(result_filename)
with open(result_filename, 'a') as csvfile:
filewriter = csv.writer(csvfile, delimiter='\t',escapechar=' ',quoting=csv.QUOTE_NONE)
filewriter.writerow([str(newid), '[PATCH] '+patch, new_bug_represent, file_path, bugid.item()])
def getGeneratorDataLoader(filepatch,tokenizer,batchsize):
df = pd.read_csv(filepatch,encoding='latin-1',delimiter='\t')
print(df.head(1))
df = df[['bugid','patch','buggy']]
params = {
'batch_size': batchsize,
'shuffle': True,
'num_workers': 0
}
dataset=df.sample(frac=1.0, random_state = SEED).reset_index(drop=True)
target_set = loader.GeneratorDataset(dataset, tokenizer, MAX_LEN, PATCH_LEN)
target_loader = DataLoader(target_set, **params)
return target_loader
def run_test(epoch,index):
for i in range(5,11):
gen = T5ForConditionalGeneration.from_pretrained('./model_IterativeRepair/IteRepair'+str(i),output_hidden_states=True)
gen_tokenizer = T5Tokenizer.from_pretrained('./model_IterativeRepair/IteRepair'+str(i),truncation=True)
gen_tokenizer.add_tokens(['[PATCH]','[BUG]','{', '}','<','^','<=','>=','==','!=','<<','>>','[CE]','[FE]','[CONTEXT]','[BUGGY]','[CLASS]','[METHOD]','[RETURN_TYPE]','[VARIABLES]','[Delete]'])
gen = gen.to(device)
test_loader=getGeneratorDataLoader(TEST_PATH,gen_tokenizer,1)
test(gen, gen_tokenizer, device, test_loader, i ,index)
if __name__ == '__main__':
warnings.filterwarnings('ignore')
SEED=42
LEARNING_RATE = 1e-4
VALID_BATCH_SIZE = 1
MAX_LEN = 384
PATCH_LEN = 76
device = 'cuda' if cuda.is_available() else 'cpu'
TEST_PATH='/home/D4J1_TEST_Iterative1.csv'
run_test(0,'2')