forked from RecklessRonan/CodeAttention
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.py
More file actions
781 lines (694 loc) · 28 KB
/
utils.py
File metadata and controls
781 lines (694 loc) · 28 KB
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
import json
import pdb
from torch.nn.init import xavier_uniform_
from torch.utils.data import TensorDataset
import numpy as np
import logging
import os
import random
import torch
import time
from tqdm import tqdm
import networkx as nx
import re
from io import StringIO
import tokenize
logger = logging.getLogger(__name__)
def add_lang_by_task(target_str, task, sub_task):
if task == 'summarize':
target_str = '<en> ' + target_str
elif task == 'refine':
target_str = '<java> ' + target_str
elif task == 'translate':
if sub_task == 'java-cs':
target_str = '<c_sharp> ' + target_str
else:
target_str = '<java> ' + target_str
elif task == 'concode':
target_str = '<java> ' + target_str
elif task == 'defect':
target_str = target_str
return target_str
def convert_examples_to_features(item):
example, example_index, tokenizer, args, stage = item
if args.model_name in ['t5', 'codet5'] and args.add_task_prefix:
if args.sub_task != 'none':
source_str = "{} {}: {}".format(
args.task, args.sub_task, example.source)
else:
source_str = "{}: {}".format(args.task, example.source)
else:
source_str = example.source
source_str = source_str.replace('</s>', '<unk>')
source_ids = tokenizer.encode(
source_str, max_length=args.max_source_length, padding='max_length', truncation=True)
assert source_ids.count(tokenizer.eos_token_id) == 1
if stage == 'test':
target_ids = []
else:
target_str = example.target
if args.add_lang_ids:
target_str = add_lang_by_task(
example.target, args.task, args.sub_task)
if args.task in ['defect', 'clone']:
if target_str == 0:
target_str = 'false'
elif target_str == 1:
target_str = 'true'
else:
raise NameError
target_str = target_str.replace('</s>', '<unk>')
target_ids = tokenizer.encode(target_str, max_length=args.max_target_length, padding='max_length',
truncation=True)
assert target_ids.count(tokenizer.eos_token_id) == 1
return InputFeatures(
example_index,
source_ids,
target_ids,
url=example.url
)
def convert_clone_examples_to_features(item):
example, example_index, tokenizer, args = item
if args.model_name in ['t5', 'codet5'] and args.add_task_prefix:
source_str = "{}: {}".format(args.task, example.source)
target_str = "{}: {}".format(args.task, example.target)
else:
source_str = example.source
target_str = example.target
code1 = tokenizer.encode(
source_str, max_length=args.block_size, padding='max_length', truncation=True)
code2 = tokenizer.encode(
target_str, max_length=args.block_size, padding='max_length', truncation=True)
source_ids = code1 + code2
return CloneInputFeatures(example_index, source_ids, example.label, example.url1, example.url2)
class CloneInputFeatures(object):
"""A single training/test features for a example."""
def __init__(self,
example_id,
source_ids,
label,
url1,
url2
):
self.example_id = example_id
self.source_ids = source_ids
self.label = label
self.url1 = url1
self.url2 = url2
class InputFeatures(object):
"""A single training/test features for a example."""
def __init__(self,
example_id,
source_ids,
target_ids,
url=None
):
self.example_id = example_id
self.source_ids = source_ids
self.target_ids = target_ids
self.url = url
class Example(object):
"""A single training/test example."""
def __init__(self,
idx,
source,
target,
url=None,
task='',
sub_task='',
ast=None,
raw_code=None
):
self.idx = idx
self.source = source
self.target = target
self.url = url
self.task = task
self.sub_task = sub_task
self.ast = ast
self.raw_code = raw_code
class CloneExample(object):
"""A single training/test example."""
def __init__(self,
code1,
code2,
label,
url1,
url2
):
self.source = code1
self.target = code2
self.label = label
self.url1 = url1
self.url2 = url2
def read_translate_examples(filename, data_num):
"""Read examples from filename."""
examples = []
assert len(filename.split(',')) == 2
src_filename = filename.split(',')[0]
trg_filename = filename.split(',')[1]
idx = 0
with open(src_filename) as f1, open(trg_filename) as f2:
for line1, line2 in zip(f1, f2):
src = line1.strip()
trg = line2.strip()
examples.append(
Example(
idx=idx,
source=src,
target=trg,
)
)
idx += 1
if idx == data_num:
break
return examples
def read_refine_examples(filename, data_num):
"""Read examples from filename."""
examples = []
assert len(filename.split(',')) == 2
src_filename = filename.split(',')[0]
trg_filename = filename.split(',')[1]
idx = 0
with open(src_filename) as f1, open(trg_filename) as f2:
for line1, line2 in zip(f1, f2):
examples.append(
Example(
idx=idx,
source=line1.strip(),
target=line2.strip(),
)
)
idx += 1
if idx == data_num:
break
return examples
def read_concode_examples(filename, data_num):
"""Read examples from filename."""
examples = []
with open(filename) as f:
for idx, line in enumerate(f):
x = json.loads(line)
examples.append(
Example(
idx=idx,
source=x["nl"].strip(),
target=x["code"].strip()
)
)
idx += 1
if idx == data_num:
break
return examples
def read_summarize_examples(filename, data_num):
"""Read examples from filename."""
examples = []
with open(filename, encoding="utf-8") as f:
for idx, line in enumerate(f):
line = line.strip()
js = json.loads(line)
if 'idx' not in js:
js['idx'] = idx
code = ' '.join(js['code_tokens']).replace('\n', ' ')
code = ' '.join(code.strip().split())
nl = ' '.join(js['docstring_tokens']).replace('\n', '')
nl = ' '.join(nl.strip().split())
examples.append(
Example(
idx=idx,
source=code,
target=nl,
raw_code=js['code']
)
)
if idx + 1 == data_num:
break
return examples
def read_defect_examples(filename, data_num):
"""Read examples from filename."""
examples = []
with open(filename, encoding="utf-8") as f:
for idx, line in enumerate(f):
line = line.strip()
js = json.loads(line)
code = ' '.join(js['func'].split())
examples.append(
Example(
idx=js['idx'],
source=code,
target=js['target']
)
)
if idx + 1 == data_num:
break
return examples
def read_clone_examples(filename, data_num):
"""Read examples from filename."""
index_filename = filename
url_to_code = {}
with open('/'.join(index_filename.split('/')[:-1]) + '/data.jsonl') as f:
for line in f:
line = line.strip()
js = json.loads(line)
code = ' '.join(js['func'].split())
# code_tokens, dfg = extract_dataflow(js['func'], parsers['java'], 'java')
# code = ' '.join(code_tokens)
# pdb.set_trace()
url_to_code[js['idx']] = code
data = []
with open(index_filename) as f:
idx = 0
for line in f:
line = line.strip()
url1, url2, label = line.split('\t')
if url1 not in url_to_code or url2 not in url_to_code:
continue
if label == '0':
label = 0
else:
label = 1
data.append(CloneExample(
url_to_code[url1], url_to_code[url2], label, url1, url2))
idx += 1
if idx == data_num:
break
return data
def load_and_cache_gen_data(args, filename, pool, tokenizer, split_tag, only_src=False, is_sample=False, is_attention=False):
# cache the data into args.cache_path except it is sampled
# only_src: control whether to return only source ids for bleu evaluating (dev/test)
# return: examples (Example object), data (TensorDataset)
data_tag = '_all' if args.data_num == -1 else '_%d' % args.data_num
cache_fn = '{}/{}.pt'.format(args.cache_path,
split_tag + ('_src' if only_src else '') + data_tag)
examples = read_examples(filename, args.data_num, args.task)
if is_sample and is_attention:
examples = random.sample(examples, min(3000, len(examples)))
if is_sample:
examples = random.sample(examples, min(5000, len(examples)))
if split_tag == 'train':
calc_stats(examples, tokenizer, is_tokenize=True)
else:
calc_stats(examples)
if os.path.exists(cache_fn) and not is_sample:
logger.info("Load cache data from %s", cache_fn)
data = torch.load(cache_fn)
else:
if is_sample:
logger.info(
"Sample 5k data for computing bleu/attention from %s", filename)
else:
logger.info("Create cache data into %s", cache_fn)
tuple_examples = [(example, idx, tokenizer, args, split_tag)
for idx, example in enumerate(examples)]
features = pool.map(convert_examples_to_features, tqdm(
tuple_examples, total=len(tuple_examples)))
all_source_ids = torch.tensor(
[f.source_ids for f in features], dtype=torch.long)
if split_tag == 'test' or only_src:
data = TensorDataset(all_source_ids)
else:
all_target_ids = torch.tensor(
[f.target_ids for f in features], dtype=torch.long)
data = TensorDataset(all_source_ids, all_target_ids)
if args.local_rank in [-1, 0] and not is_sample:
torch.save(data, cache_fn)
return examples, data
def load_and_cache_multi_gen_data(args, split_tag, pool, tokenizer, encode_target=True, is_sample=False):
cache_fn = os.path.join(args.cache_path, split_tag)
if os.path.exists(cache_fn) and not is_sample:
logger.info("Load cache data from %s", cache_fn)
examples_data_dict = torch.load(cache_fn)
else:
examples_data_dict = {}
task_list = ['summarize', 'translate', 'refine', 'concode', 'defect']
for task in task_list:
if task == 'summarize':
sub_tasks = ['ruby', 'javascript',
'go', 'python', 'java', 'php']
elif task == 'translate':
sub_tasks = ['java-cs', 'cs-java']
elif task == 'refine':
sub_tasks = ['small', 'medium']
else:
sub_tasks = ['none']
args.task = task
for sub_task in sub_tasks:
args.sub_task = sub_task
if task == 'summarize':
args.max_source_length = 256
args.max_target_length = 128
elif task == 'translate':
args.max_source_length = 320
args.max_target_length = 256
elif task == 'refine':
if sub_task == 'small':
args.max_source_length = 130
args.max_target_length = 120
else:
args.max_source_length = 240
args.max_target_length = 240
elif task == 'concode':
args.max_source_length = 320
args.max_target_length = 150
elif task == 'defect':
args.max_source_length = 512
args.max_target_length = 3 # as do not need to add lang ids
filename = get_filenames(
args.data_dir, args.task, args.sub_task, split_tag)
examples = read_examples(filename, args.data_num, args.task)
if is_sample:
examples = random.sample(
examples, min(5000, len(examples)))
if split_tag == 'train':
calc_stats(examples, tokenizer, is_tokenize=True)
else:
calc_stats(examples)
tuple_examples = [(example, idx, tokenizer, args, split_tag)
for idx, example in enumerate(examples)]
if args.data_num == -1:
features = pool.map(convert_examples_to_features, tqdm(
tuple_examples, total=len(tuple_examples)))
else:
features = [convert_examples_to_features(
x) for x in tuple_examples]
all_source_ids = torch.tensor(
[f.source_ids for f in features], dtype=torch.long)
if encode_target:
all_target_ids = torch.tensor(
[f.target_ids for f in features], dtype=torch.long)
data = TensorDataset(all_source_ids, all_target_ids)
else:
data = TensorDataset(all_source_ids)
examples_data_dict['{}_{}'.format(
task, sub_task) if sub_task != 'none' else task] = (examples, data)
if args.local_rank in [-1, 0] and not is_sample:
torch.save(examples_data_dict, cache_fn)
logger.info("Save data into %s", cache_fn)
return examples_data_dict
def load_and_cache_clone_data(args, filename, pool, tokenizer, split_tag, is_sample=False):
cache_fn = '{}/{}.pt'.format(args.cache_path, split_tag +
'_all' if args.data_num == -1 else '_%d' % args.data_num)
examples = read_examples(filename, args.data_num, args.task)
if is_sample:
examples = random.sample(examples, int(len(examples) * 0.1))
calc_stats(examples, tokenizer, is_tokenize=True)
if os.path.exists(cache_fn):
logger.info("Load cache data from %s", cache_fn)
data = torch.load(cache_fn)
else:
if is_sample:
logger.info("Sample 10 percent of data from %s", filename)
elif args.data_num == -1:
logger.info("Create cache data into %s", cache_fn)
tuple_examples = [(example, idx, tokenizer, args)
for idx, example in enumerate(examples)]
features = pool.map(convert_clone_examples_to_features, tqdm(
tuple_examples, total=len(tuple_examples)))
# features = [convert_clone_examples_to_features(x) for x in tuple_examples]
all_source_ids = torch.tensor(
[f.source_ids for f in features], dtype=torch.long)
all_labels = torch.tensor(
[f.label for f in features], dtype=torch.long)
data = TensorDataset(all_source_ids, all_labels)
if args.local_rank in [-1, 0] and args.data_num == -1:
torch.save(data, cache_fn)
return examples, data
def get_filenames(data_root, task, sub_task, split=''):
if task == 'concode':
data_dir = '{}/{}'.format(data_root, task)
train_fn = '{}/train.json'.format(data_dir)
dev_fn = '{}/dev.json'.format(data_dir)
test_fn = '{}/test.json'.format(data_dir)
elif task == 'summarize':
data_dir = '{}/{}/{}'.format(data_root, task, sub_task)
train_fn = '{}/train.jsonl'.format(data_dir)
dev_fn = '{}/valid.jsonl'.format(data_dir)
test_fn = '{}/test.jsonl'.format(data_dir)
elif task == 'refine':
data_dir = '{}/{}/{}'.format(data_root, task, sub_task)
train_fn = '{}/train.buggy-fixed.buggy,{}/train.buggy-fixed.fixed'.format(
data_dir, data_dir)
dev_fn = '{}/valid.buggy-fixed.buggy,{}/valid.buggy-fixed.fixed'.format(
data_dir, data_dir)
test_fn = '{}/test.buggy-fixed.buggy,{}/test.buggy-fixed.fixed'.format(
data_dir, data_dir)
elif task == 'translate':
data_dir = '{}/{}'.format(data_root, task)
if sub_task == 'cs-java':
train_fn = '{}/train.java-cs.txt.cs,{}/train.java-cs.txt.java'.format(
data_dir, data_dir)
dev_fn = '{}/valid.java-cs.txt.cs,{}/valid.java-cs.txt.java'.format(
data_dir, data_dir)
test_fn = '{}/test.java-cs.txt.cs,{}/test.java-cs.txt.java'.format(
data_dir, data_dir)
else:
train_fn = '{}/train.java-cs.txt.java,{}/train.java-cs.txt.cs'.format(
data_dir, data_dir)
dev_fn = '{}/valid.java-cs.txt.java,{}/valid.java-cs.txt.cs'.format(
data_dir, data_dir)
test_fn = '{}/test.java-cs.txt.java,{}/test.java-cs.txt.cs'.format(
data_dir, data_dir)
elif task == 'clone':
data_dir = '{}/{}'.format(data_root, task)
train_fn = '{}/train.txt'.format(data_dir)
dev_fn = '{}/valid.txt'.format(data_dir)
test_fn = '{}/test.txt'.format(data_dir)
elif task == 'defect':
data_dir = '{}/{}'.format(data_root, task)
train_fn = '{}/train.jsonl'.format(data_dir)
dev_fn = '{}/valid.jsonl'.format(data_dir)
test_fn = '{}/test.jsonl'.format(data_dir)
if split == 'train':
return train_fn
elif split == 'dev':
return dev_fn
elif split == 'test':
return test_fn
else:
return train_fn, dev_fn, test_fn
def read_examples(filename, data_num, task):
read_example_dict = {
# read_summarize_examples, read_summarize_indent_examples
'summarize': read_summarize_examples,
'refine': read_refine_examples,
'translate': read_translate_examples,
'concode': read_concode_examples,
'clone': read_clone_examples,
'defect': read_defect_examples,
}
return read_example_dict[task](filename, data_num)
def calc_stats(examples, tokenizer=None, is_tokenize=False):
avg_src_len = []
avg_trg_len = []
avg_src_len_tokenize = []
avg_trg_len_tokenize = []
for ex in examples:
if is_tokenize:
avg_src_len.append(len(ex.source.split()))
avg_trg_len.append(len(str(ex.target).split()))
avg_src_len_tokenize.append(len(tokenizer.tokenize(ex.source)))
avg_trg_len_tokenize.append(
len(tokenizer.tokenize(str(ex.target))))
else:
avg_src_len.append(len(ex.source.split()))
avg_trg_len.append(len(str(ex.target).split()))
if is_tokenize:
logger.info("Read %d examples, avg src len: %d, avg trg len: %d, max src len: %d, max trg len: %d",
len(examples), np.mean(avg_src_len), np.mean(avg_trg_len), max(avg_src_len), max(avg_trg_len))
logger.info("[TOKENIZE] avg src len: %d, avg trg len: %d, max src len: %d, max trg len: %d",
np.mean(avg_src_len_tokenize), np.mean(
avg_trg_len_tokenize), max(avg_src_len_tokenize),
max(avg_trg_len_tokenize))
else:
logger.info("Read %d examples, avg src len: %d, avg trg len: %d, max src len: %d, max trg len: %d",
len(examples), np.mean(avg_src_len), np.mean(avg_trg_len), max(avg_src_len), max(avg_trg_len))
def get_elapse_time(t0):
elapse_time = time.time() - t0
if elapse_time > 3600:
hour = int(elapse_time // 3600)
minute = int((elapse_time % 3600) // 60)
return "{}h{}m".format(hour, minute)
else:
minute = int((elapse_time % 3600) // 60)
return "{}m".format(minute)
def remove_comments_and_docstrings(source, lang):
if lang in ['python']:
"""
Returns 'source' minus comments and docstrings.
"""
io_obj = StringIO(source)
out = ""
prev_toktype = tokenize.INDENT
last_lineno = -1
last_col = 0
for tok in tokenize.generate_tokens(io_obj.readline):
token_type = tok[0]
token_string = tok[1]
start_line, start_col = tok[2]
end_line, end_col = tok[3]
ltext = tok[4]
if start_line > last_lineno:
last_col = 0
if start_col > last_col:
out += (" " * (start_col - last_col))
# Remove comments:
if token_type == tokenize.COMMENT:
pass
# This series of conditionals removes docstrings:
elif token_type == tokenize.STRING:
if prev_toktype != tokenize.INDENT:
# This is likely a docstring; double-check we're not inside an operator:
if prev_toktype != tokenize.NEWLINE:
if start_col > 0:
out += token_string
else:
out += token_string
prev_toktype = token_type
last_col = end_col
last_lineno = end_line
temp = []
for x in out.split('\n'):
if x.strip() != "":
temp.append(x)
return '\n'.join(temp)
elif lang in ['ruby']:
return source
else:
def replacer(match):
s = match.group(0)
if s.startswith('/'):
return " " # note: a space and not an empty string
else:
return s
pattern = re.compile(
r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
re.DOTALL | re.MULTILINE
)
temp = []
for x in re.sub(pattern, replacer, source).split('\n'):
if x.strip() != "":
temp.append(x)
return '\n'.join(temp)
# depth-first traverse
def traverse(cursor, G, came_up, node_tag, node_sum, parent_dict):
'''
cursor: the pointer of tree-sitter. An AST cursor is an object that is used to traverse an AST one node at a time
G: the graph stored in the format of networkx
came_up: used to denote whether the node is the first glance
node_tag: the tag of this node
node_sum: the number of distinct nodes
parent_dict: used to store the parent nodes of all traversed nodes
'''
if not came_up:
G.add_node(node_sum, features=cursor.node, label=node_tag)
if node_tag in parent_dict.keys():
G.add_edge(parent_dict[node_tag], node_tag)
if cursor.goto_first_child():
node_sum += 1
parent_dict[node_sum] = node_tag
traverse(cursor, G, came_up=False, node_tag=node_sum,
node_sum=node_sum, parent_dict=parent_dict)
elif cursor.goto_next_sibling():
node_sum += 1
parent_dict[node_sum] = parent_dict[node_tag]
traverse(cursor, G, came_up=False, node_tag=node_sum,
node_sum=node_sum, parent_dict=parent_dict)
elif cursor.goto_parent():
node_tag = parent_dict[node_tag]
traverse(cursor, G, came_up=True, node_tag=node_tag,
node_sum=node_sum, parent_dict=parent_dict)
else:
if cursor.goto_next_sibling():
node_sum += 1
parent_dict[node_sum] = parent_dict[node_tag]
traverse(cursor, G, came_up=False, node_tag=node_sum,
node_sum=node_sum, parent_dict=parent_dict)
elif cursor.goto_parent():
node_tag = parent_dict[node_tag]
traverse(cursor, G, came_up=True, node_tag=node_tag,
node_sum=node_sum, parent_dict=parent_dict)
def get_ast_nx(example, parser, lang):
new_code = example.raw_code#remove_comments_and_docstrings(example.raw_code, lang)
tree = parser.parse(bytes(new_code, 'utf-8'))
G = nx.Graph()
cursor = tree.walk()
traverse(cursor, G, came_up=False, node_tag=0, node_sum=0, parent_dict={})
return Example(
idx=example.idx,
source=new_code,
target=example.target,#code comment like: disconnect all sources and cancel all throttled functions
ast=G
)
def format_attention(attention, layers=None, heads=None):
"""[format attention whose batch size > 1]
Args:
attention ([type]): [description]
layers ([type], optional): [description]. Defaults to None.
heads ([type], optional): [description]. Defaults to None.
Raises:
ValueError: [description]
Returns:
[type]: [description]
"""
if type(layers) == int:
layers = [layers]
if layers:
attention = [attention[layer_index] for layer_index in layers]
squeezed = []
for layer_attention in attention:
# batch_size x num_heads x seq_len x seq_len
# print('layer_attention', layer_attention.shape)
if len(layer_attention.shape) != 4:
raise ValueError("The attention tensor does not have the correct number of dimensions. Make sure you set "
"output_attentions=True when initializing your model.")
# num_heads x batch_size x seq_len x seq_len
layer_attention = layer_attention.permute((1, 0, 2, 3))
if heads:
layer_attention = layer_attention[heads]
squeezed.append(layer_attention)
# num_layers x num_heads x batch_size x seq_len x seq_len
return torch.stack(squeezed).permute((2, 0, 1, 3, 4))
# batch_size x num_layers x num_heads x seq_len x seq_len
def num_layers(attention):
return len(attention)
def num_heads(attention):
return attention[0][0].size(0)
def format_special_chars(tokens):
return [t.replace('Ġ', '') for t in tokens]#.replace(u"\u2581", u" ")
def index_to_code_token(index, code):
code = code.split('\n')
start_point = index[0]
end_point = index[1]
if start_point[0] == end_point[0]:
s = code[start_point[0]][start_point[1]:end_point[1]]
else:
s = ""
s += code[start_point[0]][start_point[1]:]
for i in range(start_point[0] + 1, end_point[0]):
s += code[i]
s += code[end_point[0]][:end_point[1]]
return s
def is_frequent_type(token, lang):
#get frequent type from model_free_frequent_type.ipynb
frequent_type = {}
frequent_type['javascript'] = ['function',
')', 'string_fragment', 'identifier', '(', ';', '{', '}']
frequent_type['go'] = ['package_identifier',
'type_identifier', 'field_identifier', 'if', 'return', '=']
frequent_type['java'] = [')', 'public', 'string_literal',
'identifier', '}', 'return', 'type_identifier', 'if']
frequent_type['python'] = [')', 'def', 'return',
'identifier', 'if', 'for', ':', ']']
if lang in frequent_type:
return token in frequent_type[lang]
else:
return True # if lang is not provided by frequent_type, assume all token types are frequent
def top_n_scores(n, score_dict):
''' returns keys which match the top n scores of values from a name:score dict'''
lot = [(k, v)
for k, v in score_dict.items()] # make list of tuple from scores dict
nl = []
while len(lot) > 0:
nl.append(max(lot, key=lambda x: x[1]))
lot.remove(nl[-1])
return [i[0] for i in nl[0:n]]