-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
282 lines (251 loc) · 9.2 KB
/
run.py
File metadata and controls
282 lines (251 loc) · 9.2 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
"""Command-line interface for running the HumanEvalFix bug-fixing harness."""
from __future__ import annotations
import argparse
import json
import logging
import os
import subprocess
import sys
from pathlib import Path
from typing import Any
from src.eval.humaneval_eval import EvalConfig, run_pass_at_1
LOG = logging.getLogger(__name__)
def _setup_logging(verbose: bool = False, debug: bool = False) -> None:
"""Configure the root logger with optional ANSI coloring."""
root = logging.getLogger()
root.setLevel(logging.DEBUG if debug else logging.INFO)
while root.handlers:
root.handlers.pop()
class ColorFormatter(logging.Formatter):
RESET = "\x1b[0m"
BOLD = "\x1b[1m"
COLORS = {
logging.DEBUG: "\x1b[34m",
logging.INFO: "\x1b[32m",
logging.WARNING: "\x1b[33m",
logging.ERROR: "\x1b[31m",
logging.CRITICAL: "\x1b[35m",
}
def __init__(self, fmt: str, use_color: bool = True) -> None:
super().__init__(fmt)
self.use_color = use_color
def format(self, record: logging.LogRecord) -> str:
message = super().format(record)
if not self.use_color:
return message
color = self.COLORS.get(record.levelno, "")
return f"{color}{message}{self.RESET}"
def _supports_color(stream: object) -> bool:
return (
hasattr(stream, "isatty")
and getattr(stream, "isatty")()
and os.getenv("NO_COLOR") is None
)
handler = logging.StreamHandler(stream=sys.stderr)
fmt = "%(asctime)s %(levelname)s %(message)s"
color = _supports_color(handler.stream)
handler.setFormatter(ColorFormatter(fmt, use_color=color))
root.addHandler(handler)
if not verbose and not debug:
logging.getLogger("datasets").setLevel(logging.WARNING)
logging.getLogger("transformers").setLevel(logging.WARNING)
def _log_cli_settings(
args: argparse.Namespace, parser: argparse.ArgumentParser
) -> None:
"""Log a table summarizing active CLI settings compared to defaults."""
rows: list[tuple[str, str, str]] = []
for action in parser._actions:
if not action.option_strings or action.dest == "help":
continue
flag = next(
(opt for opt in action.option_strings if opt.startswith("--")),
action.option_strings[-1],
)
value = getattr(args, action.dest, action.default)
default = action.default
rows.append((flag, repr(value), repr(default)))
rows.sort(key=lambda row: row[0])
header = ("Flag", "Value", "Default")
widths = [
max(len(column), *(len(row[idx]) for row in rows)) if rows else len(column)
for idx, column in enumerate(header)
]
def _fmt_row(columns: tuple[str, str, str]) -> str:
return " | ".join(
column.ljust(widths[idx]) for idx, column in enumerate(columns)
)
lines = [
_fmt_row(header),
"-+-".join("-" * width for width in widths),
*(_fmt_row(row) for row in rows),
]
LOG.info("CLI settings\n%s", "\n".join(lines))
def main() -> None:
parser_preview = argparse.ArgumentParser(add_help=False)
parser_preview.add_argument("--verbose", action="store_true")
parser_preview.add_argument("--debug", action="store_true")
preview_args, _ = parser_preview.parse_known_args()
_setup_logging(verbose=preview_args.verbose, debug=preview_args.debug)
os.environ.setdefault("TRANSFORMERS_VERBOSITY", "info")
parser = argparse.ArgumentParser(
parents=[parser_preview],
description="Run the ReAct bug-fixing agent on HumanEvalFix tasks (pass@1)",
)
parser.add_argument(
"--model",
type=str,
default="Qwen/Qwen2.5-0.5B-Instruct",
help=(
"HF model id (e.g. 'Qwen/Qwen2.5-0.5B-Instruct'). Common aliases like 'qwen3-0.6b' also work."
),
)
parser.add_argument(
"--max",
dest="max_problems",
type=int,
default=None,
help="Max problems to evaluate",
)
parser.add_argument(
"--temperature",
type=float,
default=0.0,
help="Sampling temperature passed to the chat model and agent.",
)
parser.add_argument(
"--iters",
dest="iters",
type=int,
default=0,
help="Optional number of repair loops after first execute (0 = strict pass@1)",
)
parser.add_argument(
"--dataset-path",
type=str,
default=None,
help=(
"Path to a local HumanEvalFix export (e.g., ./dataset/humanevalfix_python.jsonl). "
"If provided, bypasses Hugging Face Hub downloads."
),
)
parser.add_argument(
"--out", type=str, default=None, help="Path to JSONL results output"
)
parser.add_argument(
"--sandbox",
type=lambda value: value.lower(),
choices=["process", "docker"],
default="process",
help=(
"Sandbox backend for executing tests. 'process' uses the built-in isolated subprocess,"
" while 'docker' launches a Python container."
),
)
parser.add_argument(
"--visualize",
action="store_true",
help="Render a PNG summary using src.visualize_results once results are saved.",
)
parser.add_argument(
"--resume",
action="store_true",
help="Resume from an existing results JSONL file instead of rerunning completed tasks.",
)
args = parser.parse_args()
args.verbose = getattr(args, "verbose", False) or preview_args.verbose
args.debug = getattr(args, "debug", False) or preview_args.debug
_setup_logging(verbose=args.verbose, debug=args.debug)
_log_cli_settings(args, parser)
os.environ.setdefault("TOKENIZERS_PARALLELISM", "false")
output_path: Path | None = Path(args.out) if args.out else None
if args.resume and output_path is None:
output_path = Path("results") / "results.jsonl"
args.out = str(output_path)
LOG.info("Resume requested without --out; defaulting to %s", output_path)
if args.visualize and output_path is None:
output_path = Path("results") / "results.jsonl"
args.out = str(output_path)
if output_path is not None:
output_path.parent.mkdir(parents=True, exist_ok=True)
existing_results: list[dict[str, Any]] = []
if args.resume:
if output_path is None:
LOG.error("Resume requested but no output path is available.")
sys.exit(1)
if output_path.exists():
try:
with open(output_path, "r", encoding="utf-8") as handle:
for line in handle:
line = line.strip()
if not line:
continue
existing_results.append(json.loads(line))
except json.JSONDecodeError as exc:
LOG.error(
"Failed to parse existing results at %s: %s", output_path, exc
)
sys.exit(1)
LOG.info(
"Loaded %s completed task(s) from %s for resume.",
len(existing_results),
output_path,
)
else:
LOG.info(
"Resume requested but %s does not exist; starting a fresh run.",
output_path,
)
cfg = EvalConfig(
model=args.model,
max_problems=args.max_problems,
temperature=float(args.temperature),
max_new_tokens=512,
dataset_path=args.dataset_path or None,
iters=max(0, int(args.iters)),
sandbox=args.sandbox,
)
result = run_pass_at_1(
cfg,
out_path=args.out,
verbose=args.verbose,
existing_results=existing_results if args.resume else None,
)
summary = {key: value for key, value in result.items() if key != "results"}
LOG.info("Summary: %s", json.dumps(summary))
if args.visualize:
if output_path is None:
LOG.error("Visualization requested but no results path was determined.")
return
if not output_path.exists():
LOG.error(
"Visualization requested but results file %s was not created.",
output_path,
)
return
png_path = (
output_path.with_suffix(".png")
if output_path.suffix
else Path(f"{output_path}.png")
)
try:
model_for_plot = result.get("model") or cfg.model
subprocess.run(
[
sys.executable,
"-m",
"src.visualize_results",
"--input",
str(output_path),
"--output",
str(png_path),
"--model-name",
model_for_plot,
],
check=True,
)
except subprocess.CalledProcessError as exc:
LOG.error("Visualization command failed with %s", exc.returncode)
else:
LOG.info("Visualization saved to %s", png_path)
if __name__ == "__main__":
main()