-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgenvariants_parallel.py
More file actions
381 lines (336 loc) · 13.7 KB
/
genvariants_parallel.py
File metadata and controls
381 lines (336 loc) · 13.7 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
#!/usr/bin/env python3
import json
import random
import os
from typing import List, Optional, Dict
from argparse import ArgumentParser
import requests
from concurrent.futures import ThreadPoolExecutor, as_completed
def get_endpoints() -> Dict[str, str]:
result = dict()
endpoint_list = os.getenv('ENDPOINTS').split(' ') # type: ignore
for endpoint_pair in endpoint_list:
(model, endpoint) = endpoint_pair.split(':', 1)
result[model] = endpoint
return result
def model_info():
"""Get information about the model."""
return requests.get(f'{ENDPOINT}/info').json()
def generate_completion(
prompt,
temperature=0.2,
max_new_tokens=1200,
repetition_penalty=1.1,
stop=None,
):
"""Generate a completion of the prompt."""
data = {
'inputs': prompt,
'parameters': {
'temperature': temperature,
'max_new_tokens': max_new_tokens,
'do_sample': True,
'repetition_penalty': repetition_penalty,
'details': True, # So we get the finish_reason
},
}
if stop is not None:
data['parameters']['stop'] = stop
return requests.post(f'{ENDPOINT}/generate', json=data).json()
def infilling_prompt_llama(
pre: str,
suf: str,
) -> str:
"""
Format an infilling problem for Code Llama.
If `suffix_first` is set, format in suffix-prefix-middle format.
"""
return f'<PRE> {pre} <SUF>{suf} <MID>'
def infilling_prompt_qwen(
pre: str,
suf: str,
) -> str:
"""
Format an infilling problem for Qwen.
"""
return f'<|fim_prefix|>{pre}<|fim_suffix|>{suf}<|fim_middle|>'
def infilling_prompt_starcoder(
pre: str,
suf: str,
) -> str:
"""
Format an infilling problem for StarCoder
If `suffix_first` is set, format in suffix-prefix-middle format.
"""
return f'<fim_prefix>{pre}<fim_suffix>{suf}<fim_middle>'
infilling_prompt = None
def continue_completion(text: str) -> tuple[str, str]:
text_lines = text.split('\n')
# Pick a random line number to cut at
cut_line = len(text_lines)
prompt_text = '\n'.join(text_lines[:cut_line])
real_completion = ''
return prompt_text, real_completion
def random_completion(text: str, start_line: int = 1) -> tuple[str,str]:
"""Generate a completion of the text starting from a random line.
Always include at least 1 line to avoid an empty prompt."""
text_lines = text.split('\n')
# Pick a random line number to cut at
cut_line = len(text_lines) - 2 if start_line + 1 >= len(text_lines) - 1 else random.randint(start_line + 1, len(text_lines) - 1)
prompt_text = '\n'.join(text_lines[:cut_line])
real_completion = '\n'.join(text_lines[cut_line:])
return prompt_text, real_completion
def random_fim(text: str, start_line: int = 1) -> tuple[str,str,str]:
"""Fill in the middle of the text with a random completion."""
text_lines = text.split('\n')
# Random start and end lines. Make sure we always have at least
# one line in each section.
fim_start_line = len(text_lines) - 3 if start_line + 1 >= len(text_lines) - 2 else random.randint(start_line + 1, len(text_lines) - 2)
fim_end_line = random.randint(fim_start_line + 1, len(text_lines) - 1)
prefix_text = '\n'.join(text_lines[:fim_start_line]) + '\n'
suffix_text = '\n'.join(text_lines[fim_end_line:])
real_middle = '\n'.join(text_lines[fim_start_line:fim_end_line])
return prefix_text, suffix_text, real_middle
def random_crossover(text1: str, text2: str, start_line: int = 1) -> tuple[str,str]:
"""Generate a splice of two texts."""
text_lines1 = text1.split('\n')
text_lines2 = text2.split('\n')
common_prefix = 0
for i in range(min(len(text_lines1), len(text_lines2))):
if text_lines1[i] != text_lines2[i]:
common_prefix = i - 1
break
cut_line1 = len(text_lines1) - 2 if start_line + 1 >= len(text_lines1) -1 else random.randint(start_line + 1, len(text_lines1) - 1)
may_overlap = min(cut_line1 - 1, common_prefix)
cut_line2_start = max(may_overlap, start_line)
cut_line2 = len(text_lines2) - 2 if cut_line2_start + 1 >= len(text_lines2) - 1 else random.randint(cut_line2_start + 1, len(text_lines2) - 1)
prefix = '\n'.join(text_lines1[:cut_line1])
suffix = '\n'.join(text_lines2[cut_line2:])
return prefix, suffix
# SRCS = [
# '/home/moyix/git/gifdec/gifdec.c',
# ]
# def random_snippet(text: str, start_line: int = 1) -> [str,str]:
# """Include commented out code from the parser code."""
# parser_chunks = open(random.choice(SRCS)).read().split('\n\n')
# "# NOTE: the corresponding parser code in C is:\n#\n"
def new_base(filename: str) -> tuple[str, str]:
# filename and extension
base = os.path.basename(filename)
base, ext = os.path.splitext(base)
# Get the first occurrence (if any) of ".base_"
first = base.find('.base_')
if first == -1:
return base, ext
else:
base = base[:first]
return base, ext
def generate_variant(i, generators, model, filename, args):
# Pick a random generator
generator = random.choice(generators)
if generator == 'infilled':
prefix, suffix, orig = random_fim(open(filename).read(), args.start_line)
prompt = infilling_prompt(prefix, suffix) # type: ignore
stop = []
elif generator == 'lmsplice':
other_files = [f for f in args.files if f != filename]
if other_files:
filename2 = random.choice(other_files)
else:
filename2 = filename
prefix, suffix = random_crossover(open(filename).read(), open(filename2).read(), args.start_line)
orig = ''
prompt = infilling_prompt(prefix, suffix) # type: ignore
stop = []
elif generator == 'continue':
assert False, 'Continue not supported'
prefix, orig = continue_completion(open(filename).read())
suffix = ''
prompt = prefix
stop = ['\nif', '\nclass', '\nfor', '\nwhile']
else:
assert generator == 'complete'
prefix, orig = random_completion(open(filename).read(), args.start_line)
suffix = ''
prompt = prefix
stop = ['\nif', '\nclass', '\nfor', '\nwhile']
# Prepare metadata up front in case we fail to generate
# filename and extension
base, ext = new_base(filename)
if generator == 'lmsplice':
base2, _ = new_base(filename2)
else:
base2 = base
# Count lines
plines = prefix.count('\n')
slines = suffix.count('\n')
olines = orig.count('\n')
# Output filenames
out_file = f'var_{i:04}.{generator}{ext}'
out_path = os.path.join(args.output_dir,out_file)
meta_file = os.path.join(args.log_dir, out_file + '.json')
res = generate_completion(
prompt,
stop=stop,
**vars(args.gen),
)
if 'generated_text' not in res:
meta = {
'model': model,
'prompt': prompt,
'generator': generator,
'prompt_lines': plines,
'orig_lines': olines,
'gen_lines': 0,
'suffix_lines': slines,
'finish_reason': 'err',
'base': [base] + ([base2] if generator == 'lmsplice' else []),
'response': res,
}
# Write (error) metadata to logdir
with open(meta_file, 'w') as f:
f.write(json.dumps(meta))
return None
# Fix up the generated text
text = res['generated_text']
if 'codellama' in model:
# CodeLlama tokenizer decoding seems slightly broken in TGI,
# so we need to remove the ' <EOT>' token manually, and trim the
# stop sequences.
text = text.replace(' <EOT>', '')
for stop_seq in stop:
if text.endswith(stop_seq):
text = text[:-len(stop_seq)]
gen_lines = text.count('\n')
# one of [length, eos_token, stop_sequence]
finish_reason = res['details']['finish_reason']
finish_reason = {
'length': 'len',
'eos_token': 'eos',
'stop_sequence': 'stp',
}[finish_reason]
meta = {
'model': model,
'prompt': prompt,
'generator': generator,
'prompt_lines': plines,
'orig_lines': olines,
'gen_lines': gen_lines,
'suffix_lines': slines,
'finish_reason': finish_reason,
'base': [base] + ([base2] if generator == 'lmsplice' else []),
'response': res,
}
# Write output to file
with open(out_path, 'w') as f:
f.write(prefix)
f.write(text)
f.write(suffix)
# Write metadata to logdir
with open(meta_file, 'w') as f:
f.write(json.dumps(meta))
return out_path
def make_parser():
parser = ArgumentParser(
description='Use a code model to generate variants of a file.'
)
parser.add_argument('files', type=str, nargs='+')
parser.add_argument('-M', '--model_name', type=str, default='codellama/CodeLlama-13b-hf',
help='Model to use for generation')
parser.add_argument('--no-completion', action='store_true',
help='Disable the completion mutator')
parser.add_argument('--no-fim', action='store_true',
help='Disable the FIM (infilling) mutator')
parser.add_argument('--no-splice', action='store_true',
help='Disable the splice mutator')
parser.add_argument('-n', '--num_variants', type=int, default=1,
help='Number of variants to generate for each seed')
parser.add_argument('-O', '--output_dir', type=str, default='.',
help='Directory to write variants to')
parser.add_argument('-L', '--log_dir', type=str, default='logs',
help='Directory to write generation metadata to')
parser.add_argument('-s', '--start_line', type=int, default=0,
help='When making random cuts, always start at this line. ' + \
'Allows specifying an immutable region not subject to mutation.')
parser.add_argument('-j', '--jobs', type=int, default=16,
help='Number of inference jobs to run in parallel')
# Generation params
parser.add_argument('-t', '--gen.temperature', type=float, default=0.2, help='Generation temperature')
parser.add_argument('-m', '--gen.max-new-tokens', type=int, default=2048, help='Maximum number of tokens to generate')
parser.add_argument('-r', '--gen.repetition-penalty', type=float, default=1.1, help='Repetition penalty')
return parser
def init_parser(elm):
# Add a bit of help text to the generation options
elm.subgroup_help['gen'] = 'Generation parameters'
def main():
global ENDPOINT
global infilling_prompt
import sys
from elmconfig import ELMFuzzConfig
config = ELMFuzzConfig(parents={'genvariants_parallel': make_parser()})
init_parser(config)
args = config.parse_args()
try:
access_info = on_nsf_access()
ENDPOINT = args.model.endpoints[args.model_name] if access_info is None else access_info['endpoint']
except KeyError:
print(f'WARNING: no endpoint for model {args.model_name}, using default: {ENDPOINT}', file=sys.stderr)
info = model_info()
model = info['model_id']
if model != args.model_name:
print(f'WARNING: Expected model {args.model_name}, but {ENDPOINT} is actually {model}', file=sys.stderr)
if model == 'bigcode/starcoder':
infilling_prompt = infilling_prompt_starcoder
elif model in ('codellama/CodeLlama-13b-hf',
'codellama/CodeLlama-7b-hf'):
infilling_prompt = infilling_prompt_llama
elif model.startswith('Qwen/Qwen2.5-Coder'):
infilling_prompt = infilling_prompt_qwen
if infilling_prompt is None and not args.no_fim:
config.parser.error(f'Model {model} does not support FIM')
if args.no_completion and args.no_fim and args.no_splice:
config.parser.error(f'Nothing to do')
os.makedirs(args.output_dir, exist_ok=True)
os.makedirs(args.log_dir, exist_ok=True)
forbidden = os.environ.get('ELFUZZ_FORBIDDEN_MUTATORS', '').split(',')
forbidden = [f.strip() for f in forbidden if f.strip()]
generators = []
if not args.no_completion or 'complete' not in forbidden:
generators += ['complete']
if not args.no_fim or 'infilled' not in forbidden:
generators += ['infilled']
if not args.no_splice or 'lmsplice' not in forbidden:
generators += ['lmsplice']
# generators += ['continue']
# Print the number of variants we'll generate so that the next
# stage (genoutputs) knows how many to expect.
print(len(args.files) * args.num_variants, flush=True)
worklist = []
i = 0
for _ in range(args.num_variants):
for filename in args.files:
worklist.append((i, filename))
i += 1
# pbar = tqdm(total=len(worklist), desc='Generating', unit='variant')
with ThreadPoolExecutor(max_workers=args.jobs) as executor:
futures = []
for i, filename in worklist:
future = executor.submit(generate_variant, i, generators, model, filename, args)
# future.add_done_callback(lambda _: pbar.update())
futures.append(future)
for future in as_completed(futures):
res = future.result()
if res is not None:
print(res, flush=True)
# pbar.close()
def on_nsf_access() -> dict[str, str] | None:
if not 'ACCESS_INFO' in os.environ:
return None
endpoint = os.environ['ACCESS_INFO']
return {
'endpoint': endpoint
}
if __name__ == '__main__':
access_info = on_nsf_access()
ENDPOINT = get_endpoints()['codellama/CodeLlama-13b-hf'] if access_info is None else access_info['endpoint']
main()