-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtraining.py
303 lines (236 loc) · 10 KB
/
training.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
#!/usr/bin/env python3
import argparse
import json
import os
from pathlib import Path
from transformers import AutoModelForCausalLM, AutoTokenizer
from tqdm import tqdm
import openai
from peft import PeftConfig, PeftModel, LoraConfig
from transformers import AutoModelForCausalLM, AutoTokenizer
from trl import SFTConfig, SFTTrainer, DataCollatorForCompletionOnlyLM
from datasets import Dataset
import torch
from annotator import load_nontrivial_benchmarks, load_benchmarks
from program import VerificationOutcome
import completion
def print_nontrivial(args):
programs = load_nontrivial_benchmarks('DafnyBench/programs')
print(len(programs), 'non-trivial programs')
def trim_program(program: str, max_length: int = 1000):
if len(program) > max_length:
return '...' + program[-max_length:]
return program
def rationalize(program, annotation):
prompt = [
{"role": "system",
"content": "You are a Dafny expert. The user will give you a sequence of Dafny program that is missing an annotation (assertion or invariant). They will also give you the missing assertion or invariant. Your job is to provide a very short, concise rationale that would be a useful hint for someone coming up with that assertion or invariant."},
{
"role": "user",
"content": f"""Program: method intersperse(numbers: seq<int>, delimiter: int) returns (interspersed: seq<int>)
ensures |interspersed| == if |numbers| > 0 then 2 * |numbers| - 1 else 0
ensures forall i :: 0 <= i < |interspersed| ==> i % 2 == 0 ==>
interspersed[i] == numbers[i / 2]
ensures forall i :: 0 <= i < |interspersed| ==> i % 2 == 1 ==>
interspersed[i] == delimiter
{{
interspersed := [];
for i := 0 to |numbers|
{{
if i > 0 {{
interspersed := interspersed + [delimiter];
}}
interspersed := interspersed + [numbers[i]];
}}
}}
Missing annotation: invariant |interspersed| == if i > 0 then 2 * i - 1 else 0"""
},
{
"role": "assistant",
"content": "Rationale: Adapt the first ensures clause as an invariant"
},
{
"role": "user",
"content": f"Program: {program}\nMissing annotation: {annotation}"
}
]
OPENAI = openai.Client()
completion = OPENAI.chat.completions.create(
model="gpt-4o",
messages=prompt,
max_tokens=100
)
return completion.choices[0].message.content
def make_rationalization_examples(args):
programs = load_benchmarks('DafnyBench/programs')[args.skip:]
examples = []
for p in tqdm(programs):
examples.extend(p.extract_examples())
finetuning_examples = []
for p, a in examples:
# For now skip long programs to avoid OOMs.
if len(str(p)) > 2048:
continue
r = rationalize(p, a)
finetuning_examples.append({
'program': str(p),
'output': a,
'rationale': r
})
print('Extracted', len(finetuning_examples), 'training examples')
with open('rationalized_finetuning_examples.json', 'w') as f:
json.dump(finetuning_examples, f, indent=2)
def make_direct_examples(
programs_path: str,
output_path: str,
skip: int = 0,
max_length: int = 2048,
unique: bool = False):
seen_annotations = set()
programs = load_benchmarks(programs_path or 'DafnyBench/programs')[skip:]
examples = []
for p in tqdm(programs):
examples.extend(p.extract_examples())
finetuning_examples = []
for p, a in examples:
a = a.strip()
if unique and a in seen_annotations:
continue
seen_annotations.add(a)
finetuning_examples.append({
'program': trim_program(str(p), max_length),
'output': a,
'rationale': None,
})
output_path = output_path or 'direct_finetuning_examples.json'
with open(output_path, 'w') as f:
json.dump(finetuning_examples, f, indent=2)
print('Extracted', len(finetuning_examples), 'training examples, saved to', output_path)
def extract_programs_from_graph(json_path: str,
output_dir: str,
include_unproven: bool = True):
out_path = Path(output_dir)
out_path.mkdir(parents=True, exist_ok=True)
processed_count = 0
json_file_name = os.path.splitext(os.path.basename(json_path))[0]
try:
with open(json_path, 'r') as f:
data = json.load(f)
if not isinstance(data, dict) or not data.get('nodes'):
print(f"Skipping {json_path} (not a serialized edit graph)")
return
data = data['nodes']
for index, obj in enumerate(data):
if obj['type'] == 'program':
output_file = f"{json_file_name}-{obj['id']}.dfy"
output_path = os.path.join(output_dir, output_file)
with open(output_path, 'w') as f:
f.write(obj['content'])
processed_count += 1
except Exception as e:
print(f"Error processing {json_path}: {str(e)}")
print(processed_count, 'Dafny files written to', output_dir)
def extract_examples_from_graph(graph_path: str, output: str):
output_dir = os.path.dirname(output)
print('Output:', output)
extract_path = output_dir + '-programs'
os.makedirs(extract_path, exist_ok=True)
extract_programs_from_graph(graph_path, extract_path)
make_direct_examples(extract_path, output, unique=True)
peft_config = LoraConfig(
r=128,
use_rslora=True,
lora_alpha=128,
lora_dropout=0.05,
bias="none",
task_type="CAUSAL_LM",
)
def finetune(args):
dataset = []
for file_path in args.training_set:
with open(file_path) as d_in:
dataset.extend(json.load(d_in))
print('Training set size:', len(dataset))
for r in dataset:
if args.with_rationales:
rationale = r['rationale']
# FIXME: this should be done during data generation.
if rationale.startswith('Rationale: '):
rationale = rationale[len('Rationale: '):]
r['text'] = completion.make_prompt(r['program'], with_rationale=True) + ' [' + rationale + '] ' + r['output'] + '\n' + completion.END
else:
r['text'] = completion.make_prompt(r['program']) + ' ' + r['output'] + '\n' + completion.END
dataset = Dataset.from_list(dataset)
response_template = "\nAction:" # completion.RESPONSE_PREFIX
tokenizer = AutoTokenizer.from_pretrained(args.model)
tokenizer.pad_token = tokenizer.eos_token
collator = DataCollatorForCompletionOnlyLM(
response_template=tokenizer.encode(response_template)[2:],
tokenizer=tokenizer)
output_dir = args.output
sft_config = SFTConfig(
dataset_text_field="text",
max_seq_length=1024,
output_dir=output_dir + '-peft',
num_train_epochs=3,
)
model = AutoModelForCausalLM.from_pretrained(
args.model,
device_map="auto",
attn_implementation="flash_attention_2",
torch_dtype=torch.bfloat16,
)
trainer = SFTTrainer(
model,
train_dataset=dataset,
args=sft_config,
peft_config=peft_config,
data_collator=collator,
)
trainer.train()
trainer.model.save_pretrained(output_dir + '-peft')
merged_model = trainer.model.merge_and_unload()
merged_model.save_pretrained(output_dir)
tokenizer.save_pretrained(output_dir)
def merge(input_files, output_file):
combined_data = []
for file_path in input_files:
with open(file_path, 'r') as f:
data = json.load(f)
assert isinstance(data, list)
combined_data.extend(data)
with open(output_file, 'w') as f:
json.dump(combined_data, f, indent=4)
print('Wrote', output_file, 'with', len(combined_data), 'examples.')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--model', type=str, help='LLM name or path', default='meta-llama/Meta-Llama-3.1-8B')
parser.add_argument('--merge-data', action='store_true', help='Merge training sets')
parser.add_argument('--rationalize', action='store_true', help='Generate rationalization examples')
parser.add_argument('--extract-direct', action='store_true', help='Generate direct prediction examples')
parser.add_argument('--extract-direct-from-graph', action='store_true', help='Generate direct prediction examples from a synthetic graph')
parser.add_argument('--nontrivial', action='store_true', help='Filter and save non-trivial benchmarks')
parser.add_argument('--skip', type=int, default=200,
help='How many programs to skip (e.g. avoid test set)')
parser.add_argument('--graph', type=str, help='Path to input synthetic graph')
parser.add_argument('--output', type=str, help='Output path')
parser.add_argument('--programs', type=str,
help='Path to root directory containing Dafny programs to use for training.',
default='DafnyBench/programs')
parser.add_argument('--finetune', action='store_true', help='Fine tune model on dataset of existing examples')
parser.add_argument('--with-rationales', action='store_true', help='Fine tune model on dataset with rationales')
parser.add_argument('--benchmark-dir', type=str, default='./DafnyBench/programs')
parser.add_argument('--training-set', nargs='*', type=str)
args = parser.parse_args()
if args.rationalize:
make_rationalization_examples(args)
elif args.extract_direct:
make_direct_examples(args.programs, args.output, args.skip)
elif args.extract_direct_from_graph:
extract_examples_from_graph(args.graph, args.output)
elif args.nontrivial:
print_nontrivial(args)
elif args.finetune:
finetune(args)
elif args.merge_data:
merge(args.training_set, args.output)