forked from princeton-vl/CoqGym
-
Notifications
You must be signed in to change notification settings - Fork 1
/
agent.py
357 lines (302 loc) · 14.1 KB
/
agent.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
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
import torch
import torch.nn.functional as F
import os
from gallina import GallinaTermParser
from utils import SexpCache, log
from eval_env import FileEnv
import re
import pickle
from progressbar import ProgressBar
from glob import glob
import json
from random import random
import pdb
from hashlib import sha1
import gc
from copy import deepcopy
from time import time
def action_seq_loss(logits_batch, actions_batch, opts):
assert len(logits_batch) == len(actions_batch)
loss = 0
for logits, actions in zip(logits_batch, actions_batch):
length = min(logits.shape[0], actions.shape[0])
loss += F.cross_entropy(logits[:length], actions[:length].to(opts.device))
loss /= len(logits_batch)
return loss
# merge this with extract_proof_steps.py
term_parser = GallinaTermParser(caching=True)
sexp_cache = SexpCache('../sexp_cache', readonly=True)
def filter_env(env):
filtered_env = []
for const in [const for const in env['constants'] if const['qualid'].startswith('SerTop')][-10:]:
ast = sexp_cache[const['sexp']]
filtered_env.append({'qualid': const['qualid'], 'ast': term_parser.parse(ast)})
return filtered_env
def parse_goal(g):
goal = {'id': g['id'], 'text': g['type'], 'ast': term_parser.parse(g['sexp'])}
local_context = []
for i, h in enumerate(g['hypotheses']):
for ident in h['idents']:
local_context.append({'ident': ident, 'text': h['type'], 'ast': term_parser.parse(h['sexp'])})
return local_context, goal['ast']
def print_single_goal(g):
for h in g['hypotheses']:
for ident in h['idents']:
print('\t%s: %s' % (ident, h['type']))
print('---------------')
print('\t%s' % g['type'])
print('##########')
def print_goals(obs):
if 'fg_goals' not in obs:
print('##########')
return
print('########## fg_goals ##########')
for g in obs['fg_goals']:
print_single_goal(g)
print('########## bg_goals ##########')
for g in obs['bg_goals']:
print_single_goal(g)
print('########## shelved_goals ##########')
for g in obs['shelved_goals']:
print_single_goal(g)
print('########## given_up_goals ##########')
for g in obs['given_up_goals']:
print_single_goal(g)
def get_goal_signature(goal):
sexp = goal['sexp'] + ''.join([h['sexp'] for h in goal['hypotheses']])
return sha1(sexp.encode('utf-8')).hexdigest()
class Agent:
def __init__(self, model, optimizer, dataloader, opts):
self.model = model
self.optimizer = optimizer
self.dataloader = dataloader
self.opts = opts
self.projs_split = json.load(open(opts.projs_split))
def train(self, n_epoch):
self.model.train()
log('training with teacher forcing %f..' % self.opts.teacher_forcing)
bar = ProgressBar(max_value=len(self.dataloader['train']))
for i, data_batch in enumerate(self.dataloader['train']):
use_teacher_forcing = random() < self.opts.teacher_forcing
asts, loss = self.model(data_batch['env'], data_batch['local_context'],
data_batch['goal'], data_batch['tactic_actions'], use_teacher_forcing)
log('\nteacher forcing = %s, loss = %f' % (str(use_teacher_forcing), loss.item()))
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
gc.collect()
bar.update(i)
if self.opts.smoke and i == 11:
break
log('\ntraining losses: %f' % loss)
def valid(self, n_epoch):
self.model.eval()
log('validating..')
loss_avg = 0
predictions = []
num_correct = 0
bar = ProgressBar(max_value=len(self.dataloader['valid']))
for i, data_batch in enumerate(self.dataloader['valid']):
asts, loss = self.model(data_batch['env'], data_batch['local_context'],
data_batch['goal'], data_batch['tactic_actions'], False)
loss_avg += loss.item()
for n in range(len(data_batch['file'])):
tac_gt = data_batch['tactic_str'][n]
tac_pred = asts[n].to_tokens()
if tac_gt.replace(' ', '') == tac_pred.replace(' ', ''):
num_correct += 1
predictions.append({'file_name': data_batch['file'][n],
'proof_name': data_batch['proof_name'][n],
'n_step': data_batch['n_step'][n],
'tac_gt': tac_gt,
'tac_pred': tac_pred})
gc.collect()
bar.update(i)
if self.opts.smoke and i == 11:
break
pickle.dump(predictions, open(os.path.join(self.opts.log_dir, 'predictions/pred_%03d.pickle' % n_epoch), 'wb'))
loss_avg /= len(self.dataloader['valid'])
log('\nvalidation losses: %f' % loss_avg)
acc = num_correct / len(predictions)
log('validation accuracy: %f' % acc)
return loss_avg
def evaluate(self, filename, proof_name=None):
if self.model is not None:
self.model.eval()
if 'hammer' in self.opts.method:
for atp in ['Vampire', 'Z3', 'CVC4', 'Eprover']:
if ('hammer_' + atp) in self.opts.method:
with_hammer = atp
self.opts.method = self.opts.method.replace('hammer_' + atp, 'hammer')
break
else:
with_hammer = 'All'
else:
with_hammer = None
assert 'hammer_' not in self.opts.method
hammer_timeout = self.opts.hammer_timeout if 'ours' in self.opts.method else self.opts.timeout
with FileEnv(filename, self.opts.max_num_tactics, self.opts.timeout, with_hammer=with_hammer, hammer_timeout=hammer_timeout) as file_env:
results = []
for proof_env in file_env: # start a proof
if proof_name is not None and proof_env.proof['name'] != proof_name:
continue
print('proof: ', proof_env.proof['name'])
#print('cuda memory allocated before proof: ', torch.cuda.memory_allocated(self.opts.device), file=sys.stderr)
success, proof_pred, time, num_tactics = self.prove(proof_env)
results.append({
'filename': filename, 'proof_name': proof_env.proof['name'], 'success': success,
'proof_gt': [step['command'][0] for step in proof_env.proof['steps'] if step['command'][1] != 'VernacEndProof'],
'proof_pred': proof_pred,
'time': time,
'num_tactics': num_tactics,})
if proof_name is not None:
break
return results
def prove_one_tactic(self, proof_env, tac):
obs = proof_env.init()
print_goals(obs)
obs = proof_env.step(tac + '.')
print(obs['result'])
print_goals(obs)
time = self.opts.timeout - obs['time_left']
if obs['result'] == 'SUCCESS':
return True, [tac], time, 1
else:
return False, [tac], time, 1
def prove(self, proof_env):
'prove a theorem interactively'
if 'ours' not in self.opts.method: # auto, hammer, etc.
return self.prove_one_tactic(proof_env, self.opts.method)
m = re.fullmatch(r'ours\+(?P<auto_tac>\w+)', self.opts.method) # ours+auto/hammer/etc.
if m is not None:
tac_template = m['auto_tac'] + '; %s.'
else:
tac_template = '%s.'
return self.prove_DFS(proof_env, tac_template)
def prove_DFS(self, proof_env, tac_template):
obs = proof_env.init()
env = filter_env(obs['env'])
first_goal_signatures = {get_goal_signature(obs['fg_goals'][0])}
# initialize the stack
local_context, goal = parse_goal(obs['fg_goals'][0])
tactics = self.model.beam_search(env, local_context, goal)
stack = [[tac_template % tac.to_tokens() for tac in tactics[::-1]]]
script = []
# depth-first search starting from the trace
while stack != [[]]:
#print('stack: ', stack)
# pick a tactic
if stack[-1] == []: # all candidate have been tried, backtrack
stack.pop()
script.pop()
proof_env.step('Undo.')
continue
else:
tac = stack[-1].pop()
obs = proof_env.step(tac)
print(obs['result'])
print_goals(obs)
if obs['result'] == 'SUCCESS':
script.append(tac)
time = self.opts.timeout - obs['time_left']
num_tactics = self.opts.max_num_tactics - obs['num_tactics_left']
return True, script, time, num_tactics
elif obs['result'] in ['MAX_NUM_TACTICS_REACHED', 'MAX_TIME_REACHED']:
time = self.opts.timeout - obs['time_left']
num_tactics = self.opts.max_num_tactics - obs['num_tactics_left']
return False, script, time, num_tactics
elif obs['result'] in ['ERROR']:
continue
else:
assert obs['result'] == 'PROVING'
script.append(tac)
sig = get_goal_signature(obs['fg_goals'][0])
if sig in first_goal_signatures or len(script) >= self.opts.depth_limit:
proof_env.step('Undo.')
script.pop()
continue
first_goal_signatures.add(sig)
local_context, goal = parse_goal(obs['fg_goals'][0])
tactics = self.model.beam_search(env, local_context, goal)
stack.append([tac_template % tac.to_tokens() for tac in tactics[::-1]])
obs = proof_env.step('Admitted.')
print(obs['result'])
time = self.opts.timeout - obs['time_left']
num_tactics = self.opts.max_num_tactics - obs['num_tactics_left']
return False, script, time, num_tactics
def prove_IDDFS(self, proof_env, tac_template):
obs = proof_env.init()
env = filter_env(obs['env'])
first_goal_signatures = {get_goal_signature(obs['fg_goals'][0])}
depth_limit = self.opts.depth_limit
traces = [[]]
# iterative deepening depth-first search
while traces != []:
# depth-first search with depth_limit
new_traces = [] # the newly-discovered truncated proofs
for script in traces:
# execute the tactics in the trace
for tac in script:
obs = proof_env.step(tac)
print(obs['result'])
print_goals(obs)
if obs['result'] != 'PROVING':
assert obs['result'] in ['MAX_NUM_TACTICS_REACHED', 'MAX_TIME_REACHED']
time = self.opts.timeout - obs['time_left']
num_tactics = self.opts.max_num_tactics - obs['num_tactics_left']
return False, script, time, num_tactics
# initialize the stack
local_context, goal = parse_goal(obs['fg_goals'][0])
tactics = self.model.beam_search(env, local_context, goal)
stack = [[tac_template % tac.to_tokens() for tac in tactics[::-1]]]
# depth-first search starting from the trace
while stack != [[]]:
print('stack: ', stack)
# pick a tactic
if stack[-1] == []: # all candidate have been tried, backtrack
stack.pop()
script.pop()
proof_env.step('Undo.')
continue
else:
tac = stack[-1].pop()
obs = proof_env.step(tac)
print(obs['result'])
print_goals(obs)
if obs['result'] == 'SUCCESS':
script.append(tac)
time = self.opts.timeout - obs['time_left']
num_tactics = self.opts.max_num_tactics - obs['num_tactics_left']
return True, script, time, num_tactics
elif obs['result'] in ['MAX_NUM_TACTICS_REACHED', 'MAX_TIME_REACHED']:
time = self.opts.timeout - obs['time_left']
num_tactics = self.opts.max_num_tactics - obs['num_tactics_left']
return False, script, time, num_tactics
elif obs['result'] in ['ERROR']:
continue
else:
assert obs['result'] == 'PROVING'
script.append(tac)
sig = get_goal_signature(obs['fg_goals'][0])
if sig in first_goal_signatures or len(script) >= depth_limit:
if len(script) >= depth_limit and sig not in first_goal_signatures:
new_traces.append(deepcopy(script))
proof_env.step('Undo.')
script.pop()
continue
first_goal_signatures.add(sig)
local_context, goal = parse_goal(obs['fg_goals'][0])
tactics = self.model.beam_search(env, local_context, goal)
stack.append([tac_template % tac.to_tokens() for tac in tactics[::-1]])
proof_env.step('Restart.')
gc.collect()
depth_limit *= 2
traces = new_traces
obs = proof_env.step('Admitted.')
print(obs['result'])
time = self.opts.timeout - obs['time_left']
num_tactics = self.opts.max_num_tactics - obs['num_tactics_left']
return False, script, time, num_tactics
def save(self, n_epoch, dirname):
torch.save({'state_dict': self.model.state_dict(), 'n_epoch': n_epoch,
'optimizer': self.optimizer.state_dict()}, os.path.join(dirname, 'model_%03d.pth' % n_epoch))