H1W0XXX commited on
Commit ·
ec20773
1
Parent(s): 13c9dd5
Add task-level concurrency
Browse files- run_eval.py +220 -176
- tools/concurrency_selftest.py +65 -0
run_eval.py
CHANGED
|
@@ -5,6 +5,7 @@ import base64
|
|
| 5 |
import argparse
|
| 6 |
import mimetypes
|
| 7 |
import copy
|
|
|
|
| 8 |
from tqdm import tqdm
|
| 9 |
from openai import OpenAI
|
| 10 |
import traceback
|
|
@@ -236,6 +237,214 @@ def diagnose_failure(solver, ai_json, gt_json):
|
|
| 236 |
return 0.50, "Geometry and supports are correct, but the member connection types (hinge/rigid) are incorrect."
|
| 237 |
|
| 238 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
def main():
|
| 240 |
parser = argparse.ArgumentParser(description="Structural AI Benchmark Evaluator")
|
| 241 |
parser.add_argument("--model", type=str, default="debug-mode", help="Model name")
|
|
@@ -247,8 +456,11 @@ def main():
|
|
| 247 |
parser.add_argument("--prompt-type", type=str, default="standard", choices=PROMPT_REGISTRY.keys())
|
| 248 |
parser.add_argument("--filter", type=str, default=None, help="Filter tasks")
|
| 249 |
parser.add_argument("--verbose-response", action="store_true", help="Print full streaming model responses to console")
|
|
|
|
| 250 |
|
| 251 |
args = parser.parse_args()
|
|
|
|
|
|
|
| 252 |
|
| 253 |
# 1. System Prompt
|
| 254 |
current_system_prompt = PROMPT_REGISTRY.get(args.prompt_type)
|
|
@@ -256,8 +468,6 @@ def main():
|
|
| 256 |
|
| 257 |
# 2. Components
|
| 258 |
loader = BenchmarkDataLoader()
|
| 259 |
-
solver = TrussSolver("bin/framecalc.wasm")
|
| 260 |
-
client = OpenAI(api_key=args.api_key, base_url=args.api_base) if not args.debug else None
|
| 261 |
|
| 262 |
# 3. Tasks
|
| 263 |
tasks = loader.load_tasks_for_eval()
|
|
@@ -268,181 +478,15 @@ def main():
|
|
| 268 |
if args.limit > 0:
|
| 269 |
tasks = tasks[:args.limit]
|
| 270 |
|
| 271 |
-
print(f"Starting evaluation on {len(tasks)} tasks.")
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
for task in tqdm(tasks, desc="Evaluating", ascii=True):
|
| 275 |
-
task_id = task['id']
|
| 276 |
-
gt_solution = task['gt_solution']
|
| 277 |
-
if isinstance(gt_solution, list) and len(gt_solution) > 0: gt_solution = gt_solution[0]
|
| 278 |
-
|
| 279 |
-
# Load Raw GT Model for diagnosis
|
| 280 |
-
gt_raw_json = loader.load_raw_model_by_id(task_id)
|
| 281 |
-
|
| 282 |
-
best_score = 0
|
| 283 |
-
final_details = {}
|
| 284 |
-
fail_reason = "Unknown"
|
| 285 |
-
attempts_used = 0
|
| 286 |
-
best_attempt = 0
|
| 287 |
-
attempt_logs = []
|
| 288 |
-
|
| 289 |
-
# --- Debug Mode ---
|
| 290 |
-
if args.debug:
|
| 291 |
-
attempts_used = 1
|
| 292 |
-
ai_json = gt_raw_json
|
| 293 |
-
if not ai_json:
|
| 294 |
-
fail_reason = "GT JSON Missing"
|
| 295 |
-
else:
|
| 296 |
-
ai_solution, solver_error = solver.solve(ai_json)
|
| 297 |
-
if solver_error:
|
| 298 |
-
fail_reason = f"Physics Solver Crashed: {solver_error}"
|
| 299 |
-
else:
|
| 300 |
-
score, details = compute_score(ai_solution, gt_solution)
|
| 301 |
-
best_score = score
|
| 302 |
-
best_attempt = 1
|
| 303 |
-
final_details = details
|
| 304 |
-
fail_reason = "Success" if score == 1.0 else "Wrong Answer"
|
| 305 |
-
|
| 306 |
-
# --- AI Mode ---
|
| 307 |
-
else:
|
| 308 |
-
base64_image = encode_image(task['image_path'])
|
| 309 |
-
# 基础对话历史 (System + User/Image)
|
| 310 |
-
base_messages = [
|
| 311 |
-
{"role": "system", "content": current_system_prompt},
|
| 312 |
-
{"role": "user", "content": [
|
| 313 |
-
{"type": "text", "text": "Analyze the structure in this image and output the JSON definition."},
|
| 314 |
-
{"type": "image_url", "image_url": {"url": base64_image}}
|
| 315 |
-
]}
|
| 316 |
-
]
|
| 317 |
-
|
| 318 |
-
# 用于重试的上下文 (Last Assistant Response + Error)
|
| 319 |
-
retry_context = []
|
| 320 |
-
|
| 321 |
-
for attempt in range(args.max_retries + 1):
|
| 322 |
-
attempts_used = attempt + 1
|
| 323 |
-
current_temp = 0.6 if attempt == 0 else 0.7
|
| 324 |
-
|
| 325 |
-
# 构造本次请求的消息列表
|
| 326 |
-
messages = base_messages + retry_context
|
| 327 |
-
|
| 328 |
-
tqdm.write(f"[{task_id}] attempt {attempts_used}/{args.max_retries + 1}: requesting API")
|
| 329 |
-
response_text = run_chat_completion(
|
| 330 |
-
client,
|
| 331 |
-
args.model,
|
| 332 |
-
messages,
|
| 333 |
-
temperature=current_temp,
|
| 334 |
-
stream_output=args.verbose_response
|
| 335 |
-
)
|
| 336 |
-
attempt_log = {
|
| 337 |
-
"attempt": attempts_used,
|
| 338 |
-
"temperature": current_temp,
|
| 339 |
-
"response_text": response_text,
|
| 340 |
-
"extracted_json": None,
|
| 341 |
-
"feedback": "",
|
| 342 |
-
"score": None,
|
| 343 |
-
"details": {},
|
| 344 |
-
"failure": None
|
| 345 |
-
}
|
| 346 |
-
|
| 347 |
-
if not response_text:
|
| 348 |
-
fail_reason = "API Failure"
|
| 349 |
-
attempt_log["failure"] = fail_reason
|
| 350 |
-
attempt_logs.append(attempt_log)
|
| 351 |
-
tqdm.write(f"[{task_id}] attempt {attempts_used}: API failure")
|
| 352 |
-
break
|
| 353 |
-
|
| 354 |
-
json_str = extract_json(response_text)
|
| 355 |
-
attempt_log["extracted_json"] = json_str
|
| 356 |
-
error_feedback = ""
|
| 357 |
-
|
| 358 |
-
if not json_str:
|
| 359 |
-
error_feedback = "I cannot find valid JSON. Please output standard JSON inside <json> tags."
|
| 360 |
-
fail_reason = "Parse Error"
|
| 361 |
-
attempt_log["failure"] = fail_reason
|
| 362 |
-
else:
|
| 363 |
-
try:
|
| 364 |
-
ai_json = JSON_LIB.loads(json_str)
|
| 365 |
-
ai_solution, solver_error = solver.solve(ai_json)
|
| 366 |
-
|
| 367 |
-
if solver_error:
|
| 368 |
-
error_feedback = f"Solver Error: {solver_error}. Check connectivity."
|
| 369 |
-
fail_reason = "Solver Crashed"
|
| 370 |
-
attempt_log["failure"] = fail_reason
|
| 371 |
-
elif not ai_solution:
|
| 372 |
-
error_feedback = "Unstable structure (empty result)."
|
| 373 |
-
fail_reason = "Unstable"
|
| 374 |
-
attempt_log["failure"] = fail_reason
|
| 375 |
-
else:
|
| 376 |
-
score, details = compute_score(ai_solution, gt_solution)
|
| 377 |
-
attempt_log["score"] = score
|
| 378 |
-
attempt_log["details"] = details
|
| 379 |
-
|
| 380 |
-
if score == 1.0:
|
| 381 |
-
best_score = 1.0
|
| 382 |
-
best_attempt = attempts_used
|
| 383 |
-
final_details = details
|
| 384 |
-
fail_reason = "Success"
|
| 385 |
-
attempt_log["failure"] = None
|
| 386 |
-
attempt_logs.append(attempt_log)
|
| 387 |
-
tqdm.write(f"[{task_id}] attempt {attempts_used}: success")
|
| 388 |
-
break # Perfect!
|
| 389 |
-
else:
|
| 390 |
-
# ❌ 计算结果不对,启动诊断
|
| 391 |
-
fail_reason = "Wrong Answer"
|
| 392 |
-
final_details = details
|
| 393 |
-
attempt_log["failure"] = fail_reason
|
| 394 |
-
|
| 395 |
-
# 只有当存在 GT Raw Model 时才能诊断
|
| 396 |
-
if gt_raw_json:
|
| 397 |
-
partial_score, diag_feedback = diagnose_failure(solver, ai_json, gt_raw_json)
|
| 398 |
-
error_feedback = f"Result incorrect. Diagnostic: {diag_feedback}"
|
| 399 |
-
attempt_log["diagnostic_score"] = partial_score
|
| 400 |
-
|
| 401 |
-
best_score, best_attempt, final_details, fail_reason = keep_best_retry_score(
|
| 402 |
-
best_score,
|
| 403 |
-
best_attempt,
|
| 404 |
-
final_details,
|
| 405 |
-
fail_reason,
|
| 406 |
-
partial_score,
|
| 407 |
-
attempts_used,
|
| 408 |
-
details,
|
| 409 |
-
f"Partial: {diag_feedback}",
|
| 410 |
-
)
|
| 411 |
-
else:
|
| 412 |
-
error_feedback = "Result incorrect (Reaction forces mismatch)."
|
| 413 |
-
|
| 414 |
-
except Exception as e:
|
| 415 |
-
error_feedback = f"JSON Syntax Error: {e}"
|
| 416 |
-
fail_reason = "Syntax Error"
|
| 417 |
-
attempt_log["failure"] = fail_reason
|
| 418 |
-
|
| 419 |
-
attempt_log["feedback"] = error_feedback
|
| 420 |
-
attempt_logs.append(attempt_log)
|
| 421 |
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
{"role": "assistant", "content": response_text},
|
| 428 |
-
{"role": "user", "content": f"Error: {error_feedback} Fix the JSON."}
|
| 429 |
-
]
|
| 430 |
-
|
| 431 |
-
# Final Score Calculation: Difficulty * Ratio
|
| 432 |
-
final_score = best_score * task.get("difficulty", 1)
|
| 433 |
-
|
| 434 |
-
results.append({
|
| 435 |
-
"id": task_id,
|
| 436 |
-
"score": final_score, # Now this is weighted
|
| 437 |
-
"ratio": best_score, # Store the raw ratio (0.0 - 1.0)
|
| 438 |
-
"difficulty": task.get("difficulty", 1),
|
| 439 |
-
"reason": fail_reason,
|
| 440 |
-
"attempts_used": attempts_used,
|
| 441 |
-
"best_attempt": best_attempt,
|
| 442 |
-
"details": final_details,
|
| 443 |
-
"attempt_logs": attempt_logs
|
| 444 |
-
})
|
| 445 |
-
tqdm.write(f"[{task_id}] done: ratio={best_score:.2f}, reason={fail_reason}, attempts={attempts_used}")
|
| 446 |
|
| 447 |
# Summary
|
| 448 |
total_score = sum(r['score'] for r in results)
|
|
|
|
| 5 |
import argparse
|
| 6 |
import mimetypes
|
| 7 |
import copy
|
| 8 |
+
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 9 |
from tqdm import tqdm
|
| 10 |
from openai import OpenAI
|
| 11 |
import traceback
|
|
|
|
| 237 |
return 0.50, "Geometry and supports are correct, but the member connection types (hinge/rigid) are incorrect."
|
| 238 |
|
| 239 |
|
| 240 |
+
def evaluate_task(task, args, current_system_prompt):
|
| 241 |
+
task_id = task['id']
|
| 242 |
+
gt_solution = task['gt_solution']
|
| 243 |
+
if isinstance(gt_solution, list) and len(gt_solution) > 0: gt_solution = gt_solution[0]
|
| 244 |
+
|
| 245 |
+
loader = BenchmarkDataLoader()
|
| 246 |
+
solver = TrussSolver("bin/framecalc.wasm")
|
| 247 |
+
client = OpenAI(api_key=args.api_key, base_url=args.api_base) if not args.debug else None
|
| 248 |
+
|
| 249 |
+
# Load Raw GT Model for diagnosis
|
| 250 |
+
gt_raw_json = loader.load_raw_model_by_id(task_id)
|
| 251 |
+
|
| 252 |
+
best_score = 0
|
| 253 |
+
final_details = {}
|
| 254 |
+
fail_reason = "Unknown"
|
| 255 |
+
attempts_used = 0
|
| 256 |
+
best_attempt = 0
|
| 257 |
+
attempt_logs = []
|
| 258 |
+
|
| 259 |
+
# --- Debug Mode ---
|
| 260 |
+
if args.debug:
|
| 261 |
+
attempts_used = 1
|
| 262 |
+
ai_json = gt_raw_json
|
| 263 |
+
if not ai_json:
|
| 264 |
+
fail_reason = "GT JSON Missing"
|
| 265 |
+
else:
|
| 266 |
+
ai_solution, solver_error = solver.solve(ai_json)
|
| 267 |
+
if solver_error:
|
| 268 |
+
fail_reason = f"Physics Solver Crashed: {solver_error}"
|
| 269 |
+
else:
|
| 270 |
+
score, details = compute_score(ai_solution, gt_solution)
|
| 271 |
+
best_score = score
|
| 272 |
+
best_attempt = 1
|
| 273 |
+
final_details = details
|
| 274 |
+
fail_reason = "Success" if score == 1.0 else "Wrong Answer"
|
| 275 |
+
|
| 276 |
+
# --- AI Mode ---
|
| 277 |
+
else:
|
| 278 |
+
base64_image = encode_image(task['image_path'])
|
| 279 |
+
# 基础对话历史 (System + User/Image)
|
| 280 |
+
base_messages = [
|
| 281 |
+
{"role": "system", "content": current_system_prompt},
|
| 282 |
+
{"role": "user", "content": [
|
| 283 |
+
{"type": "text", "text": "Analyze the structure in this image and output the JSON definition."},
|
| 284 |
+
{"type": "image_url", "image_url": {"url": base64_image}}
|
| 285 |
+
]}
|
| 286 |
+
]
|
| 287 |
+
|
| 288 |
+
# 用于重试的上下文 (Last Assistant Response + Error)
|
| 289 |
+
retry_context = []
|
| 290 |
+
|
| 291 |
+
for attempt in range(args.max_retries + 1):
|
| 292 |
+
attempts_used = attempt + 1
|
| 293 |
+
current_temp = 0.6 if attempt == 0 else 0.7
|
| 294 |
+
|
| 295 |
+
# 构造本次请求的消息列表
|
| 296 |
+
messages = base_messages + retry_context
|
| 297 |
+
|
| 298 |
+
tqdm.write(f"[{task_id}] attempt {attempts_used}/{args.max_retries + 1}: requesting API")
|
| 299 |
+
response_text = run_chat_completion(
|
| 300 |
+
client,
|
| 301 |
+
args.model,
|
| 302 |
+
messages,
|
| 303 |
+
temperature=current_temp,
|
| 304 |
+
stream_output=args.verbose_response
|
| 305 |
+
)
|
| 306 |
+
attempt_log = {
|
| 307 |
+
"attempt": attempts_used,
|
| 308 |
+
"temperature": current_temp,
|
| 309 |
+
"response_text": response_text,
|
| 310 |
+
"extracted_json": None,
|
| 311 |
+
"feedback": "",
|
| 312 |
+
"score": None,
|
| 313 |
+
"details": {},
|
| 314 |
+
"failure": None
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
+
if not response_text:
|
| 318 |
+
fail_reason = "API Failure"
|
| 319 |
+
attempt_log["failure"] = fail_reason
|
| 320 |
+
attempt_logs.append(attempt_log)
|
| 321 |
+
tqdm.write(f"[{task_id}] attempt {attempts_used}: API failure")
|
| 322 |
+
break
|
| 323 |
+
|
| 324 |
+
json_str = extract_json(response_text)
|
| 325 |
+
attempt_log["extracted_json"] = json_str
|
| 326 |
+
error_feedback = ""
|
| 327 |
+
|
| 328 |
+
if not json_str:
|
| 329 |
+
error_feedback = "I cannot find valid JSON. Please output standard JSON inside <json> tags."
|
| 330 |
+
fail_reason = "Parse Error"
|
| 331 |
+
attempt_log["failure"] = fail_reason
|
| 332 |
+
else:
|
| 333 |
+
try:
|
| 334 |
+
ai_json = JSON_LIB.loads(json_str)
|
| 335 |
+
ai_solution, solver_error = solver.solve(ai_json)
|
| 336 |
+
|
| 337 |
+
if solver_error:
|
| 338 |
+
error_feedback = f"Solver Error: {solver_error}. Check connectivity."
|
| 339 |
+
fail_reason = "Solver Crashed"
|
| 340 |
+
attempt_log["failure"] = fail_reason
|
| 341 |
+
elif not ai_solution:
|
| 342 |
+
error_feedback = "Unstable structure (empty result)."
|
| 343 |
+
fail_reason = "Unstable"
|
| 344 |
+
attempt_log["failure"] = fail_reason
|
| 345 |
+
else:
|
| 346 |
+
score, details = compute_score(ai_solution, gt_solution)
|
| 347 |
+
attempt_log["score"] = score
|
| 348 |
+
attempt_log["details"] = details
|
| 349 |
+
|
| 350 |
+
if score == 1.0:
|
| 351 |
+
best_score = 1.0
|
| 352 |
+
best_attempt = attempts_used
|
| 353 |
+
final_details = details
|
| 354 |
+
fail_reason = "Success"
|
| 355 |
+
attempt_log["failure"] = None
|
| 356 |
+
attempt_logs.append(attempt_log)
|
| 357 |
+
tqdm.write(f"[{task_id}] attempt {attempts_used}: success")
|
| 358 |
+
break # Perfect!
|
| 359 |
+
else:
|
| 360 |
+
# ❌ 计算结果不对,启动诊断
|
| 361 |
+
fail_reason = "Wrong Answer"
|
| 362 |
+
final_details = details
|
| 363 |
+
attempt_log["failure"] = fail_reason
|
| 364 |
+
|
| 365 |
+
# 只有当存在 GT Raw Model 时才能诊断
|
| 366 |
+
if gt_raw_json:
|
| 367 |
+
partial_score, diag_feedback = diagnose_failure(solver, ai_json, gt_raw_json)
|
| 368 |
+
error_feedback = f"Result incorrect. Diagnostic: {diag_feedback}"
|
| 369 |
+
attempt_log["diagnostic_score"] = partial_score
|
| 370 |
+
|
| 371 |
+
best_score, best_attempt, final_details, fail_reason = keep_best_retry_score(
|
| 372 |
+
best_score,
|
| 373 |
+
best_attempt,
|
| 374 |
+
final_details,
|
| 375 |
+
fail_reason,
|
| 376 |
+
partial_score,
|
| 377 |
+
attempts_used,
|
| 378 |
+
details,
|
| 379 |
+
f"Partial: {diag_feedback}",
|
| 380 |
+
)
|
| 381 |
+
else:
|
| 382 |
+
error_feedback = "Result incorrect (Reaction forces mismatch)."
|
| 383 |
+
|
| 384 |
+
except Exception as e:
|
| 385 |
+
error_feedback = f"JSON Syntax Error: {e}"
|
| 386 |
+
fail_reason = "Syntax Error"
|
| 387 |
+
attempt_log["failure"] = fail_reason
|
| 388 |
+
|
| 389 |
+
attempt_log["feedback"] = error_feedback
|
| 390 |
+
attempt_logs.append(attempt_log)
|
| 391 |
+
|
| 392 |
+
# Retry Logic: 只保留最近一次的错误
|
| 393 |
+
if attempt < args.max_retries and error_feedback:
|
| 394 |
+
tqdm.write(f"[{task_id}] attempt {attempts_used}: {short_text(error_feedback)}")
|
| 395 |
+
# 更新 retry_context,覆盖掉旧的错误历史
|
| 396 |
+
retry_context = [
|
| 397 |
+
{"role": "assistant", "content": response_text},
|
| 398 |
+
{"role": "user", "content": f"Error: {error_feedback} Fix the JSON."}
|
| 399 |
+
]
|
| 400 |
+
|
| 401 |
+
# Final Score Calculation: Difficulty * Ratio
|
| 402 |
+
final_score = best_score * task.get("difficulty", 1)
|
| 403 |
+
|
| 404 |
+
result = {
|
| 405 |
+
"id": task_id,
|
| 406 |
+
"score": final_score, # Now this is weighted
|
| 407 |
+
"ratio": best_score, # Store the raw ratio (0.0 - 1.0)
|
| 408 |
+
"difficulty": task.get("difficulty", 1),
|
| 409 |
+
"reason": fail_reason,
|
| 410 |
+
"attempts_used": attempts_used,
|
| 411 |
+
"best_attempt": best_attempt,
|
| 412 |
+
"details": final_details,
|
| 413 |
+
"attempt_logs": attempt_logs
|
| 414 |
+
}
|
| 415 |
+
tqdm.write(f"[{task_id}] done: ratio={best_score:.2f}, reason={fail_reason}, attempts={attempts_used}")
|
| 416 |
+
return result
|
| 417 |
+
|
| 418 |
+
|
| 419 |
+
def run_task_batch(tasks, concurrency, task_runner, show_progress=True):
|
| 420 |
+
"""
|
| 421 |
+
题目级调度。并发完成顺序可能不同,但返回结果始终保持输入任务顺序。
|
| 422 |
+
"""
|
| 423 |
+
if concurrency < 1:
|
| 424 |
+
raise ValueError("concurrency must be >= 1")
|
| 425 |
+
|
| 426 |
+
if concurrency == 1:
|
| 427 |
+
iterator = enumerate(tasks)
|
| 428 |
+
if show_progress:
|
| 429 |
+
iterator = tqdm(iterator, total=len(tasks), desc="Evaluating", ascii=True)
|
| 430 |
+
return [task_runner(index, task) for index, task in iterator]
|
| 431 |
+
|
| 432 |
+
results = [None] * len(tasks)
|
| 433 |
+
with ThreadPoolExecutor(max_workers=concurrency) as executor:
|
| 434 |
+
future_to_index = {
|
| 435 |
+
executor.submit(task_runner, index, task): index
|
| 436 |
+
for index, task in enumerate(tasks)
|
| 437 |
+
}
|
| 438 |
+
iterator = as_completed(future_to_index)
|
| 439 |
+
if show_progress:
|
| 440 |
+
iterator = tqdm(iterator, total=len(tasks), desc="Evaluating", ascii=True)
|
| 441 |
+
for future in iterator:
|
| 442 |
+
index = future_to_index[future]
|
| 443 |
+
results[index] = future.result()
|
| 444 |
+
|
| 445 |
+
return results
|
| 446 |
+
|
| 447 |
+
|
| 448 |
def main():
|
| 449 |
parser = argparse.ArgumentParser(description="Structural AI Benchmark Evaluator")
|
| 450 |
parser.add_argument("--model", type=str, default="debug-mode", help="Model name")
|
|
|
|
| 456 |
parser.add_argument("--prompt-type", type=str, default="standard", choices=PROMPT_REGISTRY.keys())
|
| 457 |
parser.add_argument("--filter", type=str, default=None, help="Filter tasks")
|
| 458 |
parser.add_argument("--verbose-response", action="store_true", help="Print full streaming model responses to console")
|
| 459 |
+
parser.add_argument("--concurrency", type=int, default=1, help="Number of tasks to evaluate concurrently")
|
| 460 |
|
| 461 |
args = parser.parse_args()
|
| 462 |
+
if args.concurrency < 1:
|
| 463 |
+
parser.error("--concurrency must be >= 1")
|
| 464 |
|
| 465 |
# 1. System Prompt
|
| 466 |
current_system_prompt = PROMPT_REGISTRY.get(args.prompt_type)
|
|
|
|
| 468 |
|
| 469 |
# 2. Components
|
| 470 |
loader = BenchmarkDataLoader()
|
|
|
|
|
|
|
| 471 |
|
| 472 |
# 3. Tasks
|
| 473 |
tasks = loader.load_tasks_for_eval()
|
|
|
|
| 478 |
if args.limit > 0:
|
| 479 |
tasks = tasks[:args.limit]
|
| 480 |
|
| 481 |
+
print(f"Starting evaluation on {len(tasks)} tasks. Concurrency: {args.concurrency}")
|
| 482 |
+
if args.concurrency > 1 and args.verbose_response:
|
| 483 |
+
print("[Warning] --verbose-response output may interleave when --concurrency > 1.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 484 |
|
| 485 |
+
results = run_task_batch(
|
| 486 |
+
tasks,
|
| 487 |
+
args.concurrency,
|
| 488 |
+
lambda index, task: evaluate_task(task, args, current_system_prompt),
|
| 489 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 490 |
|
| 491 |
# Summary
|
| 492 |
total_score = sum(r['score'] for r in results)
|
tools/concurrency_selftest.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import threading
|
| 4 |
+
import time
|
| 5 |
+
import unittest
|
| 6 |
+
|
| 7 |
+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
| 8 |
+
|
| 9 |
+
from run_eval import run_task_batch
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class ConcurrencySelfTest(unittest.TestCase):
|
| 13 |
+
def test_concurrent_results_keep_input_order(self):
|
| 14 |
+
tasks = ["task-0", "task-1", "task-2", "task-3"]
|
| 15 |
+
|
| 16 |
+
def runner(index, task):
|
| 17 |
+
time.sleep((len(tasks) - index) * 0.01)
|
| 18 |
+
return {"id": task, "index": index}
|
| 19 |
+
|
| 20 |
+
results = run_task_batch(tasks, concurrency=4, task_runner=runner, show_progress=False)
|
| 21 |
+
|
| 22 |
+
self.assertEqual([r["id"] for r in results], tasks)
|
| 23 |
+
self.assertEqual([r["index"] for r in results], list(range(len(tasks))))
|
| 24 |
+
|
| 25 |
+
def test_concurrency_runs_multiple_tasks_at_once(self):
|
| 26 |
+
tasks = list(range(6))
|
| 27 |
+
lock = threading.Lock()
|
| 28 |
+
active = 0
|
| 29 |
+
max_active = 0
|
| 30 |
+
|
| 31 |
+
def runner(index, task):
|
| 32 |
+
nonlocal active, max_active
|
| 33 |
+
with lock:
|
| 34 |
+
active += 1
|
| 35 |
+
max_active = max(max_active, active)
|
| 36 |
+
time.sleep(0.03)
|
| 37 |
+
with lock:
|
| 38 |
+
active -= 1
|
| 39 |
+
return task
|
| 40 |
+
|
| 41 |
+
results = run_task_batch(tasks, concurrency=3, task_runner=runner, show_progress=False)
|
| 42 |
+
|
| 43 |
+
self.assertEqual(results, tasks)
|
| 44 |
+
self.assertGreater(max_active, 1)
|
| 45 |
+
|
| 46 |
+
def test_sequential_mode_still_works(self):
|
| 47 |
+
tasks = ["a", "b", "c"]
|
| 48 |
+
seen = []
|
| 49 |
+
|
| 50 |
+
def runner(index, task):
|
| 51 |
+
seen.append((index, task))
|
| 52 |
+
return task.upper()
|
| 53 |
+
|
| 54 |
+
results = run_task_batch(tasks, concurrency=1, task_runner=runner, show_progress=False)
|
| 55 |
+
|
| 56 |
+
self.assertEqual(results, ["A", "B", "C"])
|
| 57 |
+
self.assertEqual(seen, [(0, "a"), (1, "b"), (2, "c")])
|
| 58 |
+
|
| 59 |
+
def test_invalid_concurrency_is_rejected(self):
|
| 60 |
+
with self.assertRaises(ValueError):
|
| 61 |
+
run_task_batch([], concurrency=0, task_runner=lambda index, task: task, show_progress=False)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
unittest.main(verbosity=2)
|