-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvmbench_demo_runtime.py
More file actions
333 lines (315 loc) · 11.9 KB
/
Copy pathvmbench_demo_runtime.py
File metadata and controls
333 lines (315 loc) · 11.9 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
from __future__ import annotations
from pathlib import Path
from typing import Any
import json
from branch_ranker import rank_candidates
from candidate_generator import generate_candidates
from search_runner import solve_next_2_steps_record
from vm_transition_verifier import _section_map, load_jsonl, verification_mode_verdict, verify_single_step
from vmbench_product_surface import resolve_mcp_path
def load_benchmark_record(dataset_path: str, record_index: int) -> dict[str, Any]:
rows = load_jsonl(resolve_mcp_path(dataset_path, must_exist=True))
if not (0 <= record_index < len(rows)):
raise IndexError(f"record_index {record_index} out of range for dataset with {len(rows)} rows")
record = rows[record_index]
if "S0" not in _section_map(record["prompt"]):
raise ValueError("benchmark record must contain S0/S1/S2 sections")
return record
def solve_record_payload(
record: dict[str, Any],
*,
candidate_limit: int,
candidate_mode: str,
ranker: str,
ranker_model_path: str | None,
verifier_mode: str,
node_budget: int | None,
) -> dict[str, Any]:
result = solve_next_2_steps_record(
record,
candidate_limit=candidate_limit,
candidate_mode=candidate_mode,
ranker=ranker,
ranker_model_path=ranker_model_path,
target_mode=verifier_mode,
node_budget=node_budget,
)
return {
"program_name": record["program_name"],
"solved": result.solved,
"successful_path": list(result.successful_path),
"nodes_explored": result.nodes_explored,
"budget_exhausted": result.budget_exhausted,
"attempts": [
{
"instruction_text": attempt.instruction_text,
"source": attempt.source,
"expected_instruction": attempt.result.expected_instruction,
"error_type": attempt.result.error_type,
"notes": list(attempt.result.notes),
}
for attempt in result.attempts
],
}
def choose_next_step_payload(
record: dict[str, Any],
*,
candidate_limit: int,
candidate_mode: str,
ranker: str,
ranker_model_path: str | None,
verifier_mode: str,
) -> dict[str, Any]:
sections = _section_map(record["prompt"])
target_sections = _section_map(record["target"])
before_state_text = sections["S0"]
candidates = rank_candidates(
generate_candidates(
program_name=record["program_name"],
before_state_text=before_state_text,
mode=candidate_mode,
limit=candidate_limit,
),
before_state_text=before_state_text,
program_name=record["program_name"],
remaining_steps=2,
strategy=ranker,
model_path=ranker_model_path,
)
ranked_candidates = []
winner: dict[str, Any] | None = None
for index, candidate in enumerate(candidates, start=1):
result = verify_single_step(
program_name=record["program_name"],
input_values=record.get("input_values"),
before_state_text=before_state_text,
instruction_text=candidate.instruction_text,
target_state_text=target_sections["S1"] if verifier_mode in {"intermediate_oracle", "state_diff"} else None,
)
accepted, notes = verification_mode_verdict(result, mode=verifier_mode)
payload = {
"rank": index,
"instruction_text": candidate.instruction_text,
"source": candidate.source,
"verified": accepted,
"matches_first_target_state": result.after_state_text == target_sections["S1"],
"error_type": result.error_type,
"notes": list(notes),
}
ranked_candidates.append(payload)
if winner is None and accepted:
winner = payload
return {
"program_name": record["program_name"],
"verifier_mode": verifier_mode,
"ranker": ranker,
"winner": winner,
"top_candidates": ranked_candidates,
}
def failure_category_payload(
record: dict[str, Any],
*,
candidate_limit: int,
candidate_mode: str,
ranker: str,
ranker_model_path: str | None,
verifier_mode: str,
node_budget: int,
) -> dict[str, Any]:
solve_payload = solve_record_payload(
record,
candidate_limit=candidate_limit,
candidate_mode=candidate_mode,
ranker=ranker,
ranker_model_path=ranker_model_path,
verifier_mode=verifier_mode,
node_budget=node_budget,
)
if solve_payload["solved"]:
return {"category": "solved", "details": solve_payload}
sections = _section_map(record["prompt"])
target_sections = _section_map(record["target"])
before_state_text = sections["S0"]
candidates = rank_candidates(
generate_candidates(
program_name=record["program_name"],
before_state_text=before_state_text,
mode=candidate_mode,
limit=candidate_limit,
),
before_state_text=before_state_text,
program_name=record["program_name"],
remaining_steps=2,
strategy=ranker,
model_path=ranker_model_path,
)
first_hit_rank = None
for idx, candidate in enumerate(candidates, start=1):
result = verify_single_step(
program_name=record["program_name"],
input_values=record.get("input_values"),
before_state_text=before_state_text,
instruction_text=candidate.instruction_text,
target_state_text=target_sections["S1"] if verifier_mode in {"intermediate_oracle", "state_diff"} else None,
)
accepted, _ = verification_mode_verdict(result, mode=verifier_mode)
if accepted and result.after_state_text == target_sections["S1"]:
first_hit_rank = idx
break
category = "budget_or_rank_order" if solve_payload["budget_exhausted"] or first_hit_rank is not None else "first_step_unreachable"
return {
"category": category,
"first_hit_rank": first_hit_rank,
"details": solve_payload,
}
def demo_reasoning_runtime_payload(
record: dict[str, Any],
*,
dataset_path: str,
record_index: int,
candidate_limit: int,
candidate_mode: str,
verifier_mode: str,
learned_model_path: str | None,
budgets: list[int],
) -> dict[str, Any]:
next_step = choose_next_step_payload(
record,
candidate_limit=min(candidate_limit, 8),
candidate_mode=candidate_mode,
ranker="learned" if learned_model_path else "heuristic",
ranker_model_path=learned_model_path,
verifier_mode=verifier_mode,
)
compare = {
"policies": [
{"policy": "none", **solve_record_payload(record, candidate_limit=candidate_limit, candidate_mode=candidate_mode, ranker="none", ranker_model_path=None, verifier_mode=verifier_mode, node_budget=max(budgets))},
{"policy": "heuristic", **solve_record_payload(record, candidate_limit=candidate_limit, candidate_mode=candidate_mode, ranker="heuristic", ranker_model_path=None, verifier_mode=verifier_mode, node_budget=max(budgets))},
]
}
if learned_model_path is not None:
compare["policies"].append(
{
"policy": "learned",
**solve_record_payload(
record,
candidate_limit=candidate_limit,
candidate_mode=candidate_mode,
ranker="learned",
ranker_model_path=learned_model_path,
verifier_mode=verifier_mode,
node_budget=max(budgets),
),
}
)
budget_curve = []
for budget in budgets:
row = {"node_budget": budget, "policies": []}
for policy, model_path in [("none", None), ("heuristic", None), ("learned", learned_model_path)]:
if policy == "learned" and model_path is None:
continue
solved = solve_record_payload(
record,
candidate_limit=candidate_limit,
candidate_mode=candidate_mode,
ranker=policy,
ranker_model_path=model_path,
verifier_mode=verifier_mode,
node_budget=budget,
)
row["policies"].append(
{
"policy": policy,
"solved": solved["solved"],
"nodes_explored": solved["nodes_explored"],
"budget_exhausted": solved["budget_exhausted"],
"successful_path": solved["successful_path"],
}
)
budget_curve.append(row)
failure_demo = failure_category_payload(
record,
candidate_limit=candidate_limit,
candidate_mode=candidate_mode,
ranker="none",
ranker_model_path=None,
verifier_mode=verifier_mode,
node_budget=min(budgets),
)
return {
"dataset_path": str(resolve_mcp_path(dataset_path, must_exist=True)),
"record_index": record_index,
"scenario_id": f"{record['program_name']}#{record_index}",
"scenario_label": f"{record['program_name']} #{record_index}",
"program_name": record["program_name"],
"verifier_mode": verifier_mode,
"candidate_mode": candidate_mode,
"candidate_limit": candidate_limit,
"budgets": budgets,
"choose_next_step": next_step,
"solve_with_budget_curve": budget_curve,
"failure_demo": failure_demo,
"compare_policies": compare,
}
def demo_reasoning_runtime_bundle(
*,
dataset_path: str,
record_indices: list[int],
candidate_limit: int,
candidate_mode: str,
verifier_mode: str,
learned_model_path: str | None,
budgets: list[int],
) -> dict[str, Any]:
scenarios = []
for record_index in record_indices:
record = load_benchmark_record(dataset_path, record_index)
scenarios.append(
demo_reasoning_runtime_payload(
record,
dataset_path=dataset_path,
record_index=record_index,
candidate_limit=candidate_limit,
candidate_mode=candidate_mode,
verifier_mode=verifier_mode,
learned_model_path=learned_model_path,
budgets=budgets,
)
)
default_scenario_id = scenarios[0]["scenario_id"] if scenarios else None
return {
"dataset_path": str(resolve_mcp_path(dataset_path, must_exist=True)),
"record_indices": list(record_indices),
"default_scenario_id": default_scenario_id,
"scenario_count": len(scenarios),
"scenarios": scenarios,
}
def write_demo_runtime_payload(
*,
dataset_path: str,
record_index: int | None,
output_path: str,
candidate_limit: int = 32,
candidate_mode: str = "program_global",
verifier_mode: str = "state_diff",
budgets: list[int] | None = None,
learned_model_path: str | None = None,
record_indices: list[int] | None = None,
) -> dict[str, Any]:
active_record_indices = list(record_indices or ([] if record_index is None else [record_index]))
if not active_record_indices:
raise ValueError("record_index or record_indices must be provided")
normalized_budgets = budgets or [2, 4, 8, 12]
payload = demo_reasoning_runtime_bundle(
dataset_path=dataset_path,
record_indices=active_record_indices,
candidate_limit=candidate_limit,
candidate_mode=candidate_mode,
verifier_mode=verifier_mode,
learned_model_path=learned_model_path,
budgets=normalized_budgets,
)
output = resolve_mcp_path(output_path, output=True)
output.parent.mkdir(parents=True, exist_ok=True)
output.write_text("{\n \"status\": \"success\",\n \"payload\": " + json.dumps(payload, ensure_ascii=True, indent=2).replace("\n", "\n ") + "\n}\n", encoding="utf-8")
return {"output_path": str(output), "payload": payload}