File size: 21,150 Bytes
df55b07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74cea47
df55b07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74cea47
 
 
 
 
 
df55b07
74cea47
df55b07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
# from tree_sitter import Language, Parser
# # import pandas as pd
# import openpyxl
import json
import time
import csv
import pathlib
import difflib
import re
from bleu import _bleu
from fuzzywuzzy import fuzz
import random
import numpy as np
from transformers import RobertaTokenizer
#tokens = nltk.word_tokenize(sentence)

folder = str(pathlib.Path(__file__).parent.resolve())
isa_type_dir = folder+"/../../../Dataset"
src_dir = folder+"/../../../Dataset/Code_Generation"
dst_dir = folder

train_lis = []
valid_lis = []
test_lis = []

target_clf = {}
def get_target_clf_list():
    global target_clf
    with open(isa_type_dir+"/comback_isa_type.csv","r",encoding="utf-8") as f:
        reader = csv.reader(f)
        for idx, l in enumerate(reader):
            if l[1].lower() == "arc" or l[1].lower() == "riscv" or l[1].lower() == "nvptx":
                continue
            if l[0] + " " + l[2] not in target_clf.keys():
                target_clf[l[0] + " " + l[2]] = [l[1]]
            else:
                target_clf[l[0] + " " + l[2]] += [l[1]]


def Calculate_Statements_Ratio(Src_List, Fork_Lis, src_name, fork_name):
    src_code = ""
    Fork_code = ""
    idx = 0
    cnt_stmt = 0.0
    while idx < len(Src_List):
        src_code += Src_List[idx].replace(src_name, "").replace(src_name.upper(), "")
        if Src_List[idx] in [";", ":", "{", "}"]:
            src_code += "\n"
            cnt_stmt += 1
        idx += 1
    while idx < len(Fork_Lis):
        Fork_code += Fork_Lis[idx].replace(fork_name, "").replace(fork_name.upper(), "")
        if Fork_Lis[idx] in [";", ":", "{", "}"]:
            Fork_code += "\n"
        idx += 1
        
    code_same = 0
    code_modi = 0
    code_add = 0
    diff_code = list(difflib.Differ().compare(src_code.splitlines(), Fork_code.splitlines()))
    for idx, dv in enumerate(diff_code):
        if dv[0] == '-':
            if idx < len(diff_code) - 1 and diff_code[idx+1][0] == '?':
                code_modi += 1
            else:
                code_add += 1
        elif dv[0] == '+':
            continue
        elif dv[0] == '?':
            continue
                #vega_add -= 1
        elif dv.strip().replace("\n", "") == '':
            continue
        else:
            code_same += 1
    return round(float(code_same) / cnt_stmt, 2)



def Calculate_Gen():
    get_target_clf_list()
    print("############## Exp 2: Calculate Code-LLaMA Gen ################\n")
    
    test_lis = ["nvptx","arc","riscv"]

    avg_accuracy = {}
    codellama_gcc_code = {}
    codellama_llvm_code = {}
    dst_file = dst_dir+"/Input/codellama_gen_output_cleaned.csv"
    with open(dst_file,encoding="utf-8") as f:
        reader = csv.reader(f)
        for idx, row in enumerate(reader):
            if row[0] == "GCC":
                codellama_gcc_code[row[1] + " " + str(row[2])] = row[3]
            else:
                codellama_llvm_code[row[1] + " " + str(row[2])] = row[3]

    for comp_type in ["GCC", "LLVM"]:
        for isa_type in ["GPU", "MPU", "CPU"]:
            target_lis = target_clf[comp_type + " " + isa_type]
            test_target_dic = {}
            cnt_idx = 0
            if comp_type == "GCC":
                if isa_type == "CPU":
                    cnt_idx = 0
                    for line in open(src_dir + "/GCC/riscv.jsonl", 'r'):
                        dic = json.loads(line)
                        test_target_dic["riscv" + " " + str(cnt_idx)] = dic["ground_truth"]
                        cnt_idx += 1
                    total_EM = 0.0
                    total_ED = 0.0
                    total_PoVS = 0.0
                    total_BLEU4 = 0.0
                    for k in test_target_dic.keys():
                        edit_dis = 0.0
                        EM = 0.0
                        bleu4 = 0.0
                        stmt_mod = 0.0
                        src_code = " ".join(test_target_dic[k]).replace("riscv", "")
                        if k in codellama_gcc_code.keys():
                            chat_code = " ".join(codellama_gcc_code[k]).replace("riscv", "").replace("RISCV", "")
                            stmt_mod = Calculate_Statements_Ratio(test_target_dic[k], codellama_gcc_code[k], "riscv", "riscv")
                            with open(dst_dir+"/test.output",'w') as f, open(dst_dir+"/test.gold",'w') as f1:
                                f.write(chat_code+'\n')
                                f1.write(src_code+'\n')     
                                if chat_code==src_code:
                                    EM = 1
                                edit_dis = fuzz.ratio(chat_code, src_code)
                            if chat_code.strip() == "":
                                bleu4 = 0
                            else:
                                bleu4 = _bleu(dst_dir+"/test.gold", dst_dir+"/test.output")
                            total_BLEU4 += bleu4
                            total_ED += edit_dis
                            total_PoVS += stmt_mod
                            total_EM += EM
                            with open(dst_dir + '/result.csv', 'a', newline='') as file:
                                writer = csv.writer(file)
                                writer.writerow([comp_type, "riscv", k.split(" ")[1], str(round(float(bleu4),2)), str(round(EM*100,2)), str(round(float(edit_dis),2)), str(round(float(stmt_mod)*100,2))])
                        else:
                            print(k)
                    with open(dst_dir + '/result.csv', 'a', newline='') as file:
                        writer = csv.writer(file)
                        writer.writerow([comp_type, "riscv", "average", str(round(float(total_BLEU4 / cnt_idx),2)), str(round((total_EM / cnt_idx)*100,2)), str(round(float(total_ED / cnt_idx),2)), str(round(float(total_PoVS / cnt_idx)*100,2))])
                        avg_accuracy[comp_type + " " + "riscv"] = [str(round(float(total_BLEU4 / cnt_idx),2)), str(round((total_EM / cnt_idx)*100,2)), str(round(float(total_ED / cnt_idx),2)), str(round(float(total_PoVS / cnt_idx)*100,2))]
                if isa_type == "GPU":
                    cnt_idx = 0
                    for line in open(src_dir + "/GCC/nvptx.jsonl", 'r'):
                        dic = json.loads(line)
                        test_target_dic["nvptx" + " " + str(cnt_idx)] = dic["ground_truth"]
                        cnt_idx += 1
                    total_EM = 0.0
                    total_ED = 0.0
                    total_PoVS = 0.0
                    total_BLEU4 = 0.0
                    for k in test_target_dic.keys():
                        edit_dis = 0.0
                        EM = 0.0
                        bleu4 = 0.0
                        stmt_mod = 0.0
                        src_code = " ".join(test_target_dic[k]).replace("nvptx", "")
                        if k in codellama_gcc_code.keys():
                            chat_code = " ".join(codellama_gcc_code[k]).replace("nvptx", "").replace("NVPTX", "")
                            stmt_mod = Calculate_Statements_Ratio(test_target_dic[k], codellama_gcc_code[k], "nvptx", "nvptx")
                            with open(dst_dir+"/test.output",'w') as f, open(dst_dir+"/test.gold",'w') as f1:
                                f.write(chat_code+'\n')
                                f1.write(src_code+'\n')     
                                if chat_code==src_code:
                                    EM = 1
                                edit_dis = fuzz.ratio(chat_code, src_code)
                            if chat_code.strip() == "":
                                bleu4 = 0
                            else:
                                bleu4 = _bleu(dst_dir+"/test.gold", dst_dir+"/test.output")
                            total_BLEU4 += bleu4
                            total_ED += edit_dis
                            total_PoVS += stmt_mod
                            total_EM += EM
                            with open(dst_dir + '/result.csv', 'a', newline='') as file:
                                writer = csv.writer(file)
                                writer.writerow([comp_type, "nvptx", k.split(" ")[1], str(round(float(bleu4),2)), str(round(EM*100,2)), str(round(float(edit_dis),2)), str(round(float(stmt_mod)*100,2))])
                        else:
                            print(k)
                    with open(dst_dir + '/result.csv', 'a', newline='') as file:
                        writer = csv.writer(file)
                        writer.writerow([comp_type, "nvptx", "average", str(round(float(total_BLEU4 / cnt_idx),2)), str(round((total_EM / cnt_idx)*100,2)), str(round(float(total_ED / cnt_idx),2)), str(round(float(total_PoVS / cnt_idx)*100,2))])
                        avg_accuracy[comp_type + " " + "nvptx"] = [str(round(float(total_BLEU4 / cnt_idx),2)), str(round((total_EM / cnt_idx)*100,2)), str(round(float(total_ED / cnt_idx),2)), str(round(float(total_PoVS / cnt_idx)*100,2))]

                if isa_type == "MPU":
                    cnt_idx = 0
                    for line in open(src_dir + "/GCC/arc.jsonl", 'r'):
                        dic = json.loads(line)
                        test_target_dic["arc" + " " + str(cnt_idx)] = dic["ground_truth"]
                        cnt_idx += 1
                    total_EM = 0.0
                    total_ED = 0.0
                    total_PoVS = 0.0
                    total_BLEU4 = 0.0
                    for k in test_target_dic.keys():
                        edit_dis = 0.0
                        EM = 0.0
                        bleu4 = 0.0
                        stmt_mod = 0.0
                        src_code = " ".join(test_target_dic[k]).replace("arc", "")
                        if k in codellama_gcc_code.keys():
                            chat_code = " ".join(codellama_gcc_code[k]).replace("arc", "").replace("ARC", "")
                            stmt_mod = Calculate_Statements_Ratio(test_target_dic[k], codellama_gcc_code[k], "arc", "arc")
                            with open(dst_dir+"/test.output",'w') as f, open(dst_dir+"/test.gold",'w') as f1:
                                f.write(chat_code+'\n')
                                f1.write(src_code+'\n')     
                                if chat_code==src_code:
                                    EM = 1
                                edit_dis = fuzz.ratio(chat_code, src_code)
                            if chat_code.strip() == "":
                                bleu4 = 0
                            else:
                                bleu4 = _bleu(dst_dir+"/test.gold", dst_dir+"/test.output")
                            total_BLEU4 += bleu4
                            total_ED += edit_dis
                            total_PoVS += stmt_mod
                            total_EM += EM
                            with open(dst_dir + '/result.csv', 'a', newline='') as file:
                                writer = csv.writer(file)
                                writer.writerow([comp_type, "arc", k.split(" ")[1], str(round(float(bleu4),2)), str(round(EM*100,2)), str(round(float(edit_dis),2)), str(round(float(stmt_mod)*100,2))])
                        else:
                            print(k)
                    with open(dst_dir + '/result.csv', 'a', newline='') as file:
                        writer = csv.writer(file)
                        writer.writerow([comp_type, "arc", "average", str(round(float(total_BLEU4 / cnt_idx),2)), str(round((total_EM / cnt_idx)*100,2)), str(round(float(total_ED / cnt_idx),2)), str(round(float(total_PoVS / cnt_idx)*100,2))])
                        avg_accuracy[comp_type + " " + "arc"] = [str(round(float(total_BLEU4 / cnt_idx),2)), str(round((total_EM / cnt_idx)*100,2)), str(round(float(total_ED / cnt_idx),2)), str(round(float(total_PoVS / cnt_idx)*100,2))]

            if comp_type == "LLVM":
                if isa_type == "CPU":
                    cnt_idx = 0
                    for line in open(src_dir + "/LLVM/RISCV.jsonl", 'r'):
                        dic = json.loads(line)
                        test_target_dic["RISCV" + " " + str(cnt_idx)] = dic["ground_truth"]
                        cnt_idx += 1
                    total_EM = 0.0
                    total_ED = 0.0
                    total_PoVS = 0.0
                    total_BLEU4 = 0.0
                    for k in test_target_dic.keys():
                        edit_dis = 0.0
                        EM = 0.0
                        bleu4 = 0.0
                        stmt_mod = 0.0
                        src_code = " ".join(test_target_dic[k]).replace("RISCV", "")
                        if k in codellama_llvm_code.keys():
                            chat_code = " ".join(codellama_llvm_code[k]).replace("riscv", "").replace("RISCV", "")
                            stmt_mod = Calculate_Statements_Ratio(test_target_dic[k], codellama_llvm_code[k], "riscv", "riscv")
                            with open(dst_dir+"/test.output",'w') as f, open(dst_dir+"/test.gold",'w') as f1:
                                f.write(chat_code+'\n')
                                f1.write(src_code+'\n')     
                                if chat_code==src_code:
                                    EM = 1
                                edit_dis = fuzz.ratio(chat_code, src_code)
                            if chat_code.strip() == "":
                                bleu4 = 0
                            else:
                                bleu4 = _bleu(dst_dir+"/test.gold", dst_dir+"/test.output")
                            total_BLEU4 += bleu4
                            total_ED += edit_dis
                            total_PoVS += stmt_mod
                            total_EM += EM
                            with open(dst_dir + '/result.csv', 'a', newline='') as file:
                                writer = csv.writer(file)
                                writer.writerow([comp_type, "RISCV", k.split(" ")[1], str(round(float(bleu4),2)), str(round(EM*100,2)), str(round(float(edit_dis),2)), str(round(float(stmt_mod)*100,2))])
                        else:
                            print(k)
                    with open(dst_dir + '/result.csv', 'a', newline='') as file:
                        writer = csv.writer(file)
                        writer.writerow([comp_type, "RISCV", "average", str(round(float(total_BLEU4 / cnt_idx),2)), str(round((total_EM / cnt_idx)*100,2)), str(round(float(total_ED / cnt_idx),2)), str(round(float(total_PoVS / cnt_idx)*100,2))])
                        avg_accuracy[comp_type + " " + "RISCV"] = [str(round(float(total_BLEU4 / cnt_idx),2)), str(round((total_EM / cnt_idx)*100,2)), str(round(float(total_ED / cnt_idx),2)), str(round(float(total_PoVS / cnt_idx)*100,2))]
                if isa_type == "GPU":
                    cnt_idx = 0
                    for line in open(src_dir + "/LLVM/NVPTX.jsonl", 'r'):
                        dic = json.loads(line)
                        test_target_dic["NVPTX" + " " + str(cnt_idx)] = dic["ground_truth"]
                        cnt_idx += 1
                    
                    total_EM = 0.0
                    total_ED = 0.0
                    total_PoVS = 0.0
                    total_BLEU4 = 0.0
                    for k in test_target_dic.keys():
                        edit_dis = 0.0
                        EM = 0.0
                        bleu4 = 0.0
                        stmt_mod = 0.0
                        src_code = " ".join(test_target_dic[k]).replace("NVPTX", "")
                        if k in codellama_llvm_code.keys():
                            chat_code = " ".join(codellama_llvm_code[k]).replace("nvptx", "").replace("NVPTX", "")
                            stmt_mod = Calculate_Statements_Ratio(test_target_dic[k], codellama_llvm_code[k], "nvptx", "nvptx")
                            with open(dst_dir+"/test.output",'w') as f, open(dst_dir+"/test.gold",'w') as f1:
                                f.write(chat_code+'\n')
                                f1.write(src_code+'\n')     
                                if chat_code==src_code:
                                    EM = 1
                                edit_dis = fuzz.ratio(chat_code, src_code)
                            if chat_code.strip() == "":
                                bleu4 = 0
                            else:
                                bleu4 = _bleu(dst_dir+"/test.gold", dst_dir+"/test.output")
                            total_BLEU4 += bleu4
                            total_ED += edit_dis
                            total_PoVS += stmt_mod
                            total_EM += EM
                            with open(dst_dir + '/result.csv', 'a', newline='') as file:
                                writer = csv.writer(file)
                                writer.writerow([comp_type, "NVPTX", k.split(" ")[1], str(round(float(bleu4),2)), str(round(EM*100,2)), str(round(float(edit_dis),2)), str(round(float(stmt_mod)*100,2))])
                        else:
                            print(k)
                    with open(dst_dir + '/result.csv', 'a', newline='') as file:
                        writer = csv.writer(file)
                        writer.writerow([comp_type, "NVPTX", "average", str(round(float(total_BLEU4 / cnt_idx),2)), str(round((total_EM / cnt_idx)*100,2)), str(round(float(total_ED / cnt_idx),2)), str(round(float(total_PoVS / cnt_idx)*100,2))])
                        avg_accuracy[comp_type + " " + "NVPTX"] = [str(round(float(total_BLEU4 / cnt_idx),2)), str(round((total_EM / cnt_idx)*100,2)), str(round(float(total_ED / cnt_idx),2)), str(round(float(total_PoVS / cnt_idx)*100,2))]
                
                if isa_type == "MPU":
                    cnt_idx = 0
                    for line in open(src_dir + "/LLVM/ARC.jsonl", 'r'):
                        dic = json.loads(line)
                        test_target_dic["ARC" + " " + str(cnt_idx)] = dic["ground_truth"]
                        cnt_idx += 1
                    total_EM = 0.0
                    total_ED = 0.0
                    total_PoVS = 0.0
                    total_BLEU4 = 0.0
                    for k in test_target_dic.keys():
                        edit_dis = 0.0
                        EM = 0.0
                        bleu4 = 0.0
                        stmt_mod = 0.0
                        src_code = " ".join(test_target_dic[k]).replace("ARC", "")
                        if k in codellama_llvm_code.keys():
                            chat_code = " ".join(codellama_llvm_code[k]).replace("arc", "").replace("ARC", "")
                            stmt_mod = Calculate_Statements_Ratio(test_target_dic[k], codellama_llvm_code[k], "arc", "arc")
                            with open(dst_dir+"/test.output",'w') as f, open(dst_dir+"/test.gold",'w') as f1:
                                f.write(chat_code+'\n')
                                f1.write(src_code+'\n')     
                                if chat_code==src_code:
                                    EM = 1
                                edit_dis = fuzz.ratio(chat_code, src_code)
                            if chat_code.strip() == "":
                                bleu4 = 0
                            else:
                                bleu4 = _bleu(dst_dir+"/test.gold", dst_dir+"/test.output")
                            total_BLEU4 += bleu4
                            total_ED += edit_dis
                            total_PoVS += stmt_mod
                            total_EM += EM
                            with open(dst_dir + '/result.csv', 'a', newline='') as file:
                                writer = csv.writer(file)
                                writer.writerow([comp_type, "ARC", k.split(" ")[1], str(round(float(bleu4),2)), str(round(EM*100,2)), str(round(float(edit_dis),2)), str(round(float(stmt_mod)*100,2))])
                        else:
                            print(k)
                    with open(dst_dir + '/result.csv', 'a', newline='') as file:
                        writer = csv.writer(file)
                        writer.writerow([comp_type, "ARC", "average", str(round(float(total_BLEU4 / cnt_idx),2)), str(round((total_EM / cnt_idx)*100,2)), str(round(float(total_ED / cnt_idx),2)), str(round(float(total_PoVS / cnt_idx)*100,2))])
                        avg_accuracy[comp_type + " " + "ARC"] = [str(round(float(total_BLEU4 / cnt_idx),2)), str(round((total_EM / cnt_idx)*100,2)), str(round(float(total_ED / cnt_idx),2)), str(round(float(total_PoVS / cnt_idx)*100,2))]
    return avg_accuracy





if __name__ == "__main__":
    with open(dst_dir + '/result.csv', 'w', newline='') as file:
        writer = csv.writer(file)
        writer.writerow(["Compiler Type", "Target", "Idx", "BLEU4", "Exact Match", "Edit Didtance", "Stmt_Ratio"])

    avg_dic = Calculate_Gen()

    for k in avg_dic:
        print("########################")
        
        print(k)
        print(" ".join(["BLEU4", "Exact Match", "Edit Didtance", "Stmt_Ratio"]))
        print(" ".join(avg_dic[k]))