-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch.py
471 lines (370 loc) · 15.3 KB
/
search.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
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
#!/usr/bin/env python3
"""
LLM-guided search to annotate Dafny programs.
Uses a proposer (e.g., an LLM) to generate candidate annotations, and a
simple search algorithm to verify the program by adding these annotations.
This is the entry-point for the main experiments on DafnyBench.
"""
import os
import json
import argparse
from typing import Any, Optional
from vllm import LLM, SamplingParams
import torch
from tqdm import tqdm
from program import DafnyProgram, VerificationOutcome, parallel_verify_batch
from completion import END, make_prompt
from annotator import load_benchmarks
class SearchNode:
"""
Represents a node in the search tree for program annotations.
Attributes:
_program (DafnyProgram): The Dafny program at this node.
_parent_node (Optional[SearchNode]): Parent node in the search tree.
_parent_action (Optional[Any]): Action taken in the parent to get here.
verification_outcome (Optional[VerificationOutcome]): Feedback from Dafny.
"""
def __init__(self, program: DafnyProgram, parent_node=None,
parent_action=None):
# noqa
self._program = program
self._parent_node = parent_node
self._parent_action = parent_action
self.verification_outcome = None # Initialize as None
def program(self) -> DafnyProgram:
"""
Get the Dafny program at this node.
Returns:
DafnyProgram: The Dafny program.
"""
return self._program
def enumerate_successors_with_annotations(
self,
annotations: list[str]
) -> list['SearchNode']:
"""
Generate successor nodes by inserting the given annotations at valid positions.
Args:
annotations (list[str]): The annotations to insert.
Returns:
list[SearchNode]: A list of successor nodes.
"""
successors = []
if None in (self._program.first_line(), self._program.last_line()):
return []
for annotation in annotations:
for i in range(self._program.first_line(), self._program.last_line()):
line = self._program.lines[i]
# Ignore invariants added outside of loops
if 'invariant' in annotation and \
('for' not in line and
'while' not in line and
'invariant' not in line): # noqa
continue
new_program = self._program.insert(i, annotation)
successor = SearchNode(new_program, self, (i, annotation))
successors.append(successor)
return successors
class Proposer:
"""Base class for proposers that generate candidate annotations."""
def propose(self, nodes: list[SearchNode]) -> list[list[str]]:
"""
Make annotation proposals for a batch of search nodes.
Args:
nodes (list[SearchNode]): Batch of search nodes to annotate.
Returns:
list[list[str]]: One list of proposed annotations for each node.
"""
raise NotImplementedError
class VLLMProposer(Proposer):
"""
Proposer that uses a vLLM model to generate annotation proposals.
Attributes:
_llm (LLM): The language model.
_sampling_params (SamplingParams): vLLM sampling parameters.
_with_rationale (bool): Whether LLM predictions will include a
rationale first, before the annotation.
"""
def __init__(
self,
model_name: str,
tokenizer=None,
num_proposals: int = 2,
temperature: float = 1.0,
with_rationale: bool = False
):
# noqa
self._llm = LLM(model=model_name,
tokenizer=tokenizer,
max_model_len=4096,
tensor_parallel_size=torch.cuda.device_count())
self._sampling_params = SamplingParams(
n=num_proposals,
temperature=temperature,
stop=END,
max_tokens=300,
)
self._with_rationale = with_rationale
def propose(self, nodes: list[SearchNode]) -> list[list[str]]:
"""
Make proposals for a batch of search nodes.
Args:
nodes (list[SearchNode]): The search nodes to propose annotations for.
Returns:
list[list[str]]: A list of lists of proposed annotations for each node.
"""
prompts = [make_prompt(str(node.program()), with_rationale=self._with_rationale)
for node in nodes]
# NOTE: In the future, we can plug in Synchromesh into vLLM by
# using a logit_processor sampling param.
# For now, we just generate unconstrained.
responses = self._llm.generate(prompts, self._sampling_params)
proposals = []
for r in responses:
raw_proposals = [o.text for o in r.outputs]
lines = [line.strip()
for p in raw_proposals
for line in p.split('\n')]
lines = [line for line in lines if line and line != END]
# Rewrite rationales as comments.
# NOTE: should refactor this to something like a
# prompt_utils module.
for i in range(len(lines)):
if lines[i].startswith('[') and ']' in lines[i]:
comment, annotation = lines[i][1:].split(']', 1)
lines[i] = f'{annotation} // {comment}'.strip()
proposals.append(lines)
return proposals
class SearchAlgorithm:
"""Abstract base class for batch search algorithms to annotate methods."""
def initial_state(self, programs: list[DafnyProgram]) -> Any:
"""
Get an initial search state for annotating the given program.
Args:
program (DafnyProgram): The program to annotate.
Returns:
Any: The initial state.
"""
raise NotImplementedError
def step(self, state: Any, proposals: list[str]) -> Any:
"""
Generate successor states given a list of proposals for this node.
Args:
state (Any): Current state.
proposals (list[str]): Proposed annotations.
Returns:
list[SearchNode]: Successor states.
"""
raise NotImplementedError
def is_done(self, state: Any) -> (bool, Optional[DafnyProgram]):
"""
Return whether the search is done, and if so, the verified program.
Args:
state (Any): The current state.
Returns:
(bool, Optional[DafnyProgram]): A pair indicating if the search is
done, and the verified program.
"""
raise NotImplementedError
class GreedySearch(SearchAlgorithm):
"""
Greedy search algorithm that selects the first successful annotation.
In this search algorithm, the state is a single program node.
"""
def initial_state(self, program: DafnyProgram) -> SearchNode:
"""
Initialize the search state with the given program.
Args:
program (DafnyProgram): The program to start searching from.
Returns:
SearchNode: The initial search node.
"""
# The state in greedy search is just a single program.
return SearchNode(program)
def step(self, state: SearchNode, proposals: list[str]) -> SearchNode:
"""
Return the next state in the search.
Args:
state (SearchNode): The current search node.
proposals (list[str]): The proposed annotations.
Returns:
SearchNode: The next search node.
"""
successors = state.enumerate_successors_with_annotations(proposals)
if len(successors) > 100:
successors = successors[:100]
outcomes = parallel_verify_batch([s.program() for s in successors], timeout=10)
# Search for a successful successor first.
for s, outcome in zip(successors, outcomes):
s.verification_outcome = outcome
if outcome == VerificationOutcome.SUCCESS:
return s
# If not, return the first non-failure.
for s, outcome in zip(successors, outcomes):
if outcome != VerificationOutcome.FAIL:
return s
# If we couldn't make progress at all, return the original state.
return state
def is_done(self, state: SearchNode) -> (bool, Optional[DafnyProgram]):
"""
Check if the search is done.
Args:
state (SearchNode): The current search node.
Returns:
(bool, Optional[DafnyProgram]): A tuple indicating if the search is done,
and the verified program if it is.
"""
outcome = state.verification_outcome
if outcome == VerificationOutcome.SUCCESS:
return True, state.program()
return False, None
def select_programs_to_verify(
programs: list[DafnyProgram],
num_programs: int,
cache_path: str,
max_program_length: int = 2048
) -> list[DafnyProgram]:
"""
Select programs from the benchmark that do not already verify.
This will first take all the first `num_programs` from the benchmark
(the ordering is consistent across runs, so this is deterministic),
then filter out programs that Dafny already verifies without annotations.
Since this is called every time an experiment start, and calling Dafny
on all the benchmark programs is expensive, we cache Dafny's feedback
to disk.
Args:
programs (list[DafnyProgram]): list of Dafny programs.
num_programs (int): Number of programs to select.
cache_path (str): Path to the verification outcome cache.
max_program_length (int): Limit to program size (in characters).
Returns:
list[DafnyProgram]: A list of programs to verify.
"""
print('Selecting programs to verify...')
if os.path.exists(cache_path):
with open(cache_path) as c_in:
outcome_cache = json.load(c_in)
else:
outcome_cache = {}
benchmarks = []
for p in tqdm(programs[:num_programs]):
if len(str(p)) > max_program_length:
continue
if p.name in outcome_cache:
# NOTE: we assume the benchmark is a valid program.
# (i.e. Dafny would not return VerificationOutcome.FAIL).
outcome = (VerificationOutcome.SUCCESS
if outcome_cache[p.name] ==
'VerificationOutcome.SUCCESS'
else VerificationOutcome.GOAL_UNPROVEN)
else:
outcome = p.verify()
outcome_cache[p.name] = str(outcome)
with open(cache_path, 'w') as c_out:
json.dump(outcome_cache, c_out)
if outcome != VerificationOutcome.SUCCESS:
benchmarks.append(p)
return benchmarks
def batch_greedy_search(programs: list[DafnyProgram],
proposer: Proposer,
max_iterations: int = 5,
save_results_path: Optional[str] = None
) -> list[Optional[DafnyProgram]]:
"""
Perform batch greedy search to annotate programs for verification.
Args:
programs (list[DafnyProgram]): List of programs to search.
proposer (Proposer): The proposer to generate annotation proposals.
max_iterations (int): Maximum number of iterations to perform.
save_results_path (Optional[str]): Path to save results as JSON.
Returns:
list[Optional[DafnyProgram]]: List of verified programs (None if not verified).
"""
nodes = [SearchNode(p) for p in programs]
result = [None] * len(nodes)
is_done = [False] * len(nodes)
method = GreedySearch()
# Initialize the state for each program
states = nodes
# For saving results
all_iterations = []
for it in range(max_iterations):
print(f'Iteration {it+1}/{max_iterations}')
unfinished_indices = [i for i, d in enumerate(is_done) if not d]
if not unfinished_indices:
break
unfinished_nodes = [states[i] for i in unfinished_indices]
# Batch proposals.
proposals = proposer.propose(unfinished_nodes)
progress = 0
# Do step in each unfinished program.
for idx, node_idx in enumerate(unfinished_indices):
new_state = method.step(states[node_idx], proposals[idx])
if new_state is not states[node_idx]:
progress += 1
states[node_idx] = new_state
is_done[node_idx], p = method.is_done(states[node_idx])
if is_done[node_idx]:
print(p.name, 'verified!')
print(p)
result[node_idx] = p
# Save the state after this iteration
iteration_data = {}
for i, state in enumerate(states):
program_id = programs[i].name
iteration_data[program_id] = {
'state': str(state.program()),
'success': is_done[i],
}
all_iterations.append(iteration_data)
if save_results_path:
with open(save_results_path, 'w') as f:
json.dump(all_iterations, f, indent=4)
print('Made progress in', progress, 'programs.')
return result
def main():
# noqa
parser = argparse.ArgumentParser(
description='Evaluate dafny-annotator.') # noqa
parser.add_argument('--num-programs', type=int, default=50,
help='Number of programs to select for verification.')
parser.add_argument('--benchmark-path', type=str,
default='DafnyBench/programs',
help='Path to the benchmark programs.')
parser.add_argument('--max-iterations', type=int, default=5,
help='Maximum number of iterations for the search.')
parser.add_argument('--model', type=str, required=True,
help='Name of the LLM model to use.')
parser.add_argument('--output', type=str, default=None,
help='Path to save results as a JSON file.')
parser.add_argument('--cache-path', type=str,
default='DafnyBench/.outcome_cache.json',
help='Path to the verification outcome cache.')
parser.add_argument('--max-program-length', type=int, default=2048,
help='Filter out benchmark programs larger than this.')
args = parser.parse_args()
programs = load_benchmarks(args.benchmark_path)
programs = [p.strip_annotations() for p in programs]
proposer = VLLMProposer(model_name=args.model, with_rationale=False)
# Select programs to verify
benchmarks = select_programs_to_verify(
programs,
num_programs=args.num_programs,
cache_path=args.cache_path,
max_program_length=args.max_program_length)
results = batch_greedy_search(
benchmarks,
proposer,
args.max_iterations,
args.output)
for p, r in zip(benchmarks, results):
print('####', p.name)
if r is None:
print('Failed to verify')
else:
print('Verified program:')
print(str(r))
success_count = sum(r is not None for r in results)
print('Success rate:', success_count / len(results))
if __name__ == '__main__':
main()