-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrun_ablation_matrix.py
More file actions
191 lines (164 loc) · 7.31 KB
/
run_ablation_matrix.py
File metadata and controls
191 lines (164 loc) · 7.31 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
from __future__ import annotations
import argparse
import csv
import json
import os
from copy import deepcopy
from pathlib import Path
from lockr.runners.benchmark import BenchmarkRunner
from lockr.schemas import BenchmarkConfig
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description="Run the extended LOCK-R ablation matrix.")
parser.add_argument("--config", type=Path, default=Path("configs/pilot_20.json"))
parser.add_argument("--output-root", type=Path, default=Path("outputs/ablation_matrix"))
parser.add_argument("--fast", action="store_true", help="Use a reduced episode set for faster diagnostics.")
return parser
def load_config(path: Path) -> BenchmarkConfig:
return BenchmarkConfig.model_validate(json.loads(path.read_text(encoding="utf-8")))
def clone_config(config: BenchmarkConfig) -> BenchmarkConfig:
return BenchmarkConfig.model_validate(config.model_dump(mode="json"))
def maybe_reduce_episodes(config: BenchmarkConfig, *, fast: bool) -> None:
if fast:
config.episodes = config.episodes[:2]
def prepare_lm_config(
base: BenchmarkConfig,
*,
suite_name: str,
regimes: list[str],
proposal_mode: str,
verifier_mode: str,
fast: bool,
) -> BenchmarkConfig:
config = clone_config(base)
config.suite_name = suite_name
maybe_reduce_episodes(config, fast=fast)
config.agent.kind = "openai_compatible_json"
config.agent.proposal_generation_mode = proposal_mode # type: ignore[assignment]
config.agent.verifier_generation_mode = verifier_mode # type: ignore[assignment]
config.agent.repair_generation_mode = "qwen_nonthinking_eval"
config.parallel_workers = 4
config.regimes = regimes # type: ignore[assignment]
return config
def prepare_baseline_config(base: BenchmarkConfig, *, suite_name: str, fast: bool) -> BenchmarkConfig:
config = clone_config(base)
config.suite_name = suite_name
maybe_reduce_episodes(config, fast=fast)
config.agent.kind = "heuristic_confirmation"
config.parallel_workers = 4
config.regimes = [ # type: ignore[assignment]
"heuristic_lexical_agent",
"bayesian_scripted_agent",
"oracle_query_exact_update",
]
return config
def run_suite(config: BenchmarkConfig, output_root: Path) -> tuple[str, Path, dict[str, object]]:
output_dir = output_root / config.suite_name
summary = BenchmarkRunner(config=config, output_dir=output_dir).run()
return config.suite_name, output_dir, summary.model_dump(mode="json")
def write_matrix_csv(rows: list[dict[str, object]], path: Path) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("w", encoding="utf-8", newline="") as handle:
writer = csv.DictWriter(
handle,
fieldnames=[
"suite_name",
"regime",
"episode_count",
"mean_r_mean",
"r_mean_ci_low",
"r_mean_ci_high",
"mean_k_c",
"mean_cear",
"mean_delta_cal",
"mean_num_negative_steps_seen",
"mean_num_posterior_rewrites",
"mean_num_unique_tools_used",
"mean_tool_loop_repetition_rate",
"accuracy",
],
)
writer.writeheader()
for row in rows:
writer.writerow(row)
def write_matrix_markdown(rows: list[dict[str, object]], path: Path) -> None:
lines = [
"| Suite | Regime | Episodes | R_mean | 95% CI | K_c | CEAR | delta_cal | neg_steps | rewrites | unique_tools | loop_rate | Accuracy |",
"| --- | --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: |",
]
for row in rows:
lines.append(
f"| {row['suite_name']} | {row['regime']} | {row['episode_count']} | "
f"{float(row['mean_r_mean']):.4f} | "
f"[{float(row['r_mean_ci_low']):.4f}, {float(row['r_mean_ci_high']):.4f}] | "
f"{float(row['mean_k_c']):.4f} | {float(row['mean_cear']):.4f} | "
f"{float(row['mean_delta_cal']):.4f} | "
f"{float(row['mean_num_negative_steps_seen']):.2f} | "
f"{float(row['mean_num_posterior_rewrites']):.2f} | "
f"{float(row['mean_num_unique_tools_used']):.2f} | "
f"{float(row['mean_tool_loop_repetition_rate']):.4f} | "
f"{float(row['accuracy']):.3f} |"
)
path.write_text("\n".join(lines) + "\n", encoding="utf-8")
def main() -> None:
args = build_parser().parse_args()
os.environ.setdefault("OPENAI_API_KEY", "lm-studio")
base = load_config(args.config)
suites = [
prepare_lm_config(
base,
suite_name="lm_nonthinking_ablation",
regimes=[
"same_model_locked_agent",
"blind_checker",
"prompt_debias_baseline",
"oracle_query_control",
],
proposal_mode="qwen_nonthinking_eval",
verifier_mode="qwen_nonthinking_eval",
fast=args.fast,
),
prepare_lm_config(
base,
suite_name="thinking_compare_same_blind_nonthinking",
regimes=["same_model_locked_agent", "blind_checker"],
proposal_mode="qwen_nonthinking_eval",
verifier_mode="qwen_nonthinking_eval",
fast=args.fast,
),
prepare_lm_config(
base,
suite_name="thinking_compare_same_blind_thinking",
regimes=["same_model_locked_agent", "blind_checker"],
proposal_mode="qwen_precise_coding_thinking",
verifier_mode="qwen_nonthinking_eval",
fast=args.fast,
),
prepare_baseline_config(base, suite_name="sanity_baselines", fast=args.fast),
]
matrix_rows: list[dict[str, object]] = []
for suite in suites:
suite_name, output_dir, summary = run_suite(suite, args.output_root)
print(f"completed {suite_name}: {output_dir}")
for regime_summary in summary["regime_summaries"]:
matrix_rows.append(
{
"suite_name": suite_name,
"regime": regime_summary["regime"],
"episode_count": regime_summary["episode_count"],
"mean_r_mean": regime_summary["mean_r_mean"],
"r_mean_ci_low": regime_summary["r_mean_ci_low"],
"r_mean_ci_high": regime_summary["r_mean_ci_high"],
"mean_k_c": regime_summary["mean_k_c"],
"mean_cear": regime_summary["mean_cear"],
"mean_delta_cal": regime_summary["mean_delta_cal"],
"mean_num_negative_steps_seen": regime_summary["mean_num_negative_steps_seen"],
"mean_num_posterior_rewrites": regime_summary["mean_num_posterior_rewrites"],
"mean_num_unique_tools_used": regime_summary["mean_num_unique_tools_used"],
"mean_tool_loop_repetition_rate": regime_summary["mean_tool_loop_repetition_rate"],
"accuracy": regime_summary["accuracy"],
}
)
write_matrix_csv(matrix_rows, args.output_root / "matrix_summary.csv")
write_matrix_markdown(matrix_rows, args.output_root / "matrix_summary.md")
if __name__ == "__main__":
main()