File size: 21,933 Bytes
d13981b | 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 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 | #!/usr/bin/env python3
"""PCD low-score forensic summaries for existing no-visual runs."""
from __future__ import annotations
import argparse
import ast
import csv
import json
import math
import re
from collections import Counter
from pathlib import Path
from statistics import mean
from typing import Any
DEFAULT_RUNS = [
(
"pcd_bs1_aligned",
Path("outputs/test-fast/pcd-no-visual/pcd_no_visual_aligned_4epoch/deplot_no_vs_opd_pcd"),
Path("outputs/test-fast/logs/pcd_no_visual_pcd_no_visual_aligned_4epoch/deplot_no_vs_opd_pcd"),
Path("outputs/test-fast/pcd-low-score-forensics/eval_sweep_aligned/summary.csv"),
),
(
"pcd_bs8_staged",
Path("outputs/test-fast/pcd-no-visual/pcd_no_visual_staged/deplot_no_vs_opd_pcd"),
Path("outputs/test-fast/logs/pcd_no_visual_pcd_no_visual_staged/deplot_no_vs_opd_pcd"),
Path("outputs/test-fast/pcd-low-score-forensics/eval_sweep_staged/summary.csv"),
),
]
TRAIN_KEYS = [
"reward",
"rewards/accuracy/mean",
"reward_std",
"signal/grpo_zero_loss_rate",
"signal/group_all_wrong_rate",
"routing/grpo_route_rate",
"routing/opd_route_rate",
"routing/sft_route_rate",
"routing/teacher_probe_candidate_rate",
"routing/teacher_probe_correct_rate",
"routing/teacher_probe_candidate_accuracy",
"routing/teacher_probe_answer_flag_rate",
"routing/teacher_probe_parse_fail_rate",
"teacher_probe/generated_tokens_mean",
"teacher_probe/generated_tokens_p95",
"teacher_probe/clipped_rate",
"completions/degenerate_rate",
"completions/clipped_ratio",
"completions/eos_rate",
"loss/opsd",
"loss/teacher_traj_fkl",
"grad_norm",
]
def _safe_float(value: Any) -> float | None:
try:
out = float(value)
except (TypeError, ValueError):
return None
if not math.isfinite(out):
return None
return out
def _write_csv(path: Path, rows: list[dict[str, Any]], fields: list[str]) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("w", newline="", encoding="utf-8") as handle:
writer = csv.DictWriter(handle, fieldnames=fields, extrasaction="ignore")
writer.writeheader()
writer.writerows(rows)
def _checkpoint_sort_key(label: str) -> tuple[int, int | str]:
match = re.search(r"checkpoint-(\d+)", label)
if match:
return (0, int(match.group(1)))
if "final" in label:
return (1, 10**9)
return (2, label)
def parse_eval_log(path: Path) -> dict[str, str]:
text = path.read_text(encoding="utf-8", errors="replace")
acc = re.findall(r"Current Global Mean Accuracy:\s*([0-9.]+)", text)
processed = re.findall(r"Global samples processed:\s*(\d+)\s*/\s*(\d+)", text)
output_types = re.findall(r"Output type counts:\s*(\{[^\n]*\})", text)
row: dict[str, str] = {
"accuracy": acc[-1] if acc else "",
"processed": processed[-1][0] if processed else "",
"total": processed[-1][1] if processed else "",
"output_types": output_types[-1] if output_types else "",
"log_path": str(path),
}
try:
counts = ast.literal_eval(row["output_types"]) if row["output_types"] else {}
except (SyntaxError, ValueError):
counts = {}
total = int(row["processed"] or 0)
for key in ("other", "full_cot", "answer_flag"):
value = int(counts.get(key, 0) or 0)
row[f"{key}_rate"] = f"{value / total:.6f}" if total else ""
return row
def collect_eval_rows(label: str, run_dir: Path, extra_summaries: list[Path] | None = None) -> list[dict[str, Any]]:
rows: dict[str, dict[str, Any]] = {}
summaries = [
run_dir / "eval_chartqa" / "summary.csv",
run_dir / "eval_sweep" / "summary.csv",
run_dir / "eval_chartqa_checkpoints" / "summary.csv",
]
summaries.extend(extra_summaries or [])
for summary in summaries:
if not summary.exists():
continue
for row in csv.DictReader(summary.open(encoding="utf-8")):
raw_label = row.get("label") or row.get("checkpoint") or ""
checkpoint = raw_label
match = re.search(r"(checkpoint-\d+|final_checkpoint)", raw_label)
if match:
checkpoint = match.group(1)
elif raw_label.startswith("eval_"):
checkpoint = raw_label.removeprefix("eval_").split("_2026", 1)[0]
out = {
"run": label,
"checkpoint": checkpoint,
"accuracy": row.get("accuracy", ""),
"processed": row.get("processed", ""),
"total": row.get("total", ""),
"output_types": row.get("output_types", ""),
"other_rate": row.get("other_rate", ""),
"full_cot_rate": row.get("full_cot_rate", ""),
"answer_flag_rate": row.get("answer_flag_rate", ""),
"log_path": row.get("log_path", ""),
}
if out["log_path"]:
log_path = Path(out["log_path"])
if not log_path.is_absolute():
log_path = Path.cwd() / log_path
if log_path.exists() and not (out["other_rate"] and out["full_cot_rate"]):
parsed = parse_eval_log(log_path)
out.update({k: v for k, v in parsed.items() if v})
rows[checkpoint] = out
for log in sorted((run_dir / "eval_chartqa").glob("eval_*.log")) + sorted(
(run_dir / "eval_sweep" / "logs").glob("*.log")
):
parsed = parse_eval_log(log)
raw = log.stem
match = re.search(r"(checkpoint-\d+|final_checkpoint)", raw)
checkpoint = match.group(1) if match else raw
rows.setdefault(
checkpoint,
{
"run": label,
"checkpoint": checkpoint,
**parsed,
},
)
return sorted(rows.values(), key=lambda row: _checkpoint_sort_key(str(row.get("checkpoint", ""))))
def _token_len(text: str) -> int:
return len(str(text or "").split())
def collect_candidate_funnel(label: str, run_dir: Path) -> list[dict[str, Any]]:
rows = []
records = []
for path in sorted((run_dir / "teacher_probe_candidates").glob("rank*.jsonl")):
with path.open(encoding="utf-8", errors="replace") as handle:
for line in handle:
line = line.strip()
if not line:
continue
try:
record = json.loads(line)
except json.JSONDecodeError:
continue
records.append(record)
scopes = {
"all": records,
"all_wrong": [r for r in records if r.get("is_all_wrong_probe_candidate") or r.get("group_all_wrong")],
"mixed_wrong": [r for r in records if r.get("is_mixed_wrong_probe_candidate")],
}
for scope, scope_records in scopes.items():
n = len(scope_records)
if not n:
rows.append({"run": label, "scope": scope, "candidates": 0})
continue
teacher_correct = sum(1 for r in scope_records if bool(r.get("teacher_correct")))
final_opd = sum(1 for r in scope_records if str(r.get("final_route", "")).lower() == "opd")
final_sft = sum(1 for r in scope_records if str(r.get("final_route", "")).lower().startswith("sft"))
parse_fail = sum(1 for r in scope_records if bool(r.get("parse_failed")))
answer_flag = sum(1 for r in scope_records if bool(r.get("has_answer_flag")))
gold_like = sum(1 for r in scope_records if bool(r.get("teacher_probe_gold_suffix")))
token_lens = [_token_len(r.get("teacher_output", "")) for r in scope_records]
rows.append(
{
"run": label,
"scope": scope,
"candidates": n,
"teacher_correct": teacher_correct,
"teacher_correct_rate": teacher_correct / n,
"final_opd": final_opd,
"final_opd_rate": final_opd / n,
"final_sft": final_sft,
"final_sft_rate": final_sft / n,
"parse_fail": parse_fail,
"parse_fail_rate": parse_fail / n,
"answer_flag": answer_flag,
"answer_flag_rate": answer_flag / n,
"teacher_gold_suffix_like": gold_like,
"teacher_tokens_mean": mean(token_lens) if token_lens else 0.0,
"teacher_tokens_p95": sorted(token_lens)[min(len(token_lens) - 1, int(0.95 * (len(token_lens) - 1)))],
}
)
return rows
def _metric_rows(log_path: Path) -> list[dict[str, Any]]:
rows = []
for line in log_path.read_text(encoding="utf-8", errors="replace").splitlines():
match = re.search(r"\{.*\}", line)
if not match:
continue
try:
row = ast.literal_eval(match.group())
except (SyntaxError, ValueError):
continue
if isinstance(row, dict) and any(
key in row
for key in (
"reward",
"rewards/accuracy/mean",
"routing/opd_route_rate",
"signal/grpo_zero_loss_rate",
)
):
rows.append(row)
return rows
def collect_training_windows(label: str, log_dir: Path) -> list[dict[str, Any]]:
logs = sorted(log_dir.glob("train_opd_7b_dyme_probe_*.log"))
completed = []
for path in logs:
rows = _metric_rows(path)
if rows and rows[-1].get("epoch", 0) and float(rows[-1].get("epoch", 0)) >= 3.9:
completed.append((path, rows))
if not completed and logs:
completed = [(logs[-1], _metric_rows(logs[-1]))]
if not completed:
return []
log_path, rows = completed[-1]
windows = [
("first_50", rows[:50]),
("mid_50", rows[max(0, len(rows) // 2 - 25) : max(0, len(rows) // 2 - 25) + 50]),
("last_50", rows[-50:]),
("all", rows),
]
out = []
for window_name, window_rows in windows:
row: dict[str, Any] = {
"run": label,
"window": window_name,
"n": len(window_rows),
"train_log": str(log_path),
}
for key in TRAIN_KEYS:
values = [_safe_float(r.get(key)) for r in window_rows]
values = [v for v in values if v is not None]
row[key] = mean(values) if values else ""
out.append(row)
return out
def classify_failure(pred: str, gold: str) -> str:
text = (pred or "").lower()
gold_text = (gold or "").lower()
if "visual facts" in text or "deplot" in text:
return "deplot_or_visual_fact_leak"
if re.search(r"\d+\.\d{6,}", text):
return "long_decimal"
if any(word in text or word in gold_text for word in ("ratio", "average", "difference", "sum", "%")):
return "numeric_reasoning"
if text.count("answer:") != 1:
return "answer_format"
return "other_wrong"
def collect_eval_failures(label: str, run_dir: Path, limit_per_type: int = 12) -> list[dict[str, Any]]:
logs = sorted((run_dir / "eval_chartqa").glob("eval_final_checkpoint_*.log"))
if not logs:
logs = sorted((run_dir / "eval_sweep" / "logs").glob("final_checkpoint.log"))
if not logs:
return []
text = logs[-1].read_text(encoding="utf-8", errors="replace")
rows: list[dict[str, Any]] = []
counts: Counter[str] = Counter()
for line in text.splitlines():
if "######" not in line:
continue
parts = line.split("######")
if len(parts) < 3:
continue
pred = parts[0].strip()
gold = parts[1].strip()
score_text = parts[2].strip().lower()
if score_text.startswith("true"):
continue
category = classify_failure(pred, gold)
if counts[category] >= limit_per_type:
continue
counts[category] += 1
rows.append(
{
"run": label,
"category": category,
"prediction_preview": pred[:240].replace("\n", "\\n"),
"gold": gold[:120],
"eval_log": str(logs[-1]),
}
)
return rows
def load_run_config(label: str, run_dir: Path) -> dict[str, Any]:
path = run_dir / "resolved_config.json"
if not path.exists():
return {"run": label}
cfg = json.loads(path.read_text(encoding="utf-8"))
opsd = cfg.get("opsd", {})
probe = opsd.get("teacher_probe", {})
visual = opsd.get("visual_supervision", {})
loss = opsd.get("loss", {})
return {
"run": label,
"num_train_epochs": cfg.get("training", {}).get("dyme_args", {}).get("num_train_epochs"),
"probe_batch_size": probe.get("batch_size"),
"probe_all_wrong_after_step": probe.get("probe_all_wrong_after_step"),
"probe_max_per_batch": probe.get("max_per_batch"),
"loss_type": loss.get("loss_type"),
"opsd_weight": loss.get("opsd_weight"),
"variance_adaptive": loss.get("variance_adaptive"),
"visual_enabled": visual.get("enabled"),
"visual_prefetch_ic": visual.get("prefetch_ic"),
"train_dataset": cfg.get("dataset", {}).get("train_dataset"),
}
def build_report(
out_dir: Path,
config_rows: list[dict[str, Any]],
eval_rows: list[dict[str, Any]],
funnel_rows: list[dict[str, Any]],
train_rows: list[dict[str, Any]],
failure_rows: list[dict[str, Any]],
anchor_dirs: list[Path],
) -> None:
best_by_run = {}
for row in eval_rows:
acc = _safe_float(row.get("accuracy"))
if acc is None:
continue
current = best_by_run.get(row["run"])
if current is None or acc > float(current["accuracy"]):
best_by_run[row["run"]] = row
lines = ["# PCD Low-Score Forensics", ""]
lines.append("## Checkpoint Sweep")
if best_by_run:
for run, row in sorted(best_by_run.items()):
lines.append(
f"- `{run}` best `{row.get('checkpoint')}` accuracy={float(row['accuracy']):.4f}; "
f"final rows are in `checkpoint_accuracy.csv`."
)
else:
lines.append("- No completed checkpoint eval rows found yet.")
lines.extend(["", "## Key Configs"])
for row in config_rows:
lines.append(
f"- `{row['run']}`: probe_batch_size={row.get('probe_batch_size')}, "
f"all_wrong_after_step={row.get('probe_all_wrong_after_step')}, "
f"visual_prefetch_ic={row.get('visual_prefetch_ic')}, loss={row.get('loss_type')}."
)
lines.extend(["", "## Rescue Funnel"])
for row in funnel_rows:
if row.get("scope") not in ("all_wrong", "mixed_wrong"):
continue
n = int(row.get("candidates") or 0)
if not n:
continue
lines.append(
f"- `{row['run']}` `{row['scope']}`: n={n}, "
f"teacher_correct={float(row.get('teacher_correct_rate', 0)):.3f}, "
f"OPD={float(row.get('final_opd_rate', 0)):.3f}, "
f"parse_fail={float(row.get('parse_fail_rate', 0)):.3f}."
)
lines.extend(["", "## Training Signal"])
for row in train_rows:
if row.get("window") != "last_50":
continue
lines.append(
f"- `{row['run']}` last_50: acc_reward={float(row.get('rewards/accuracy/mean') or 0):.4f}, "
f"grpo_zero={float(row.get('signal/grpo_zero_loss_rate') or 0):.3f}, "
f"all_wrong={float(row.get('signal/group_all_wrong_rate') or 0):.3f}, "
f"opd_route={float(row.get('routing/opd_route_rate') or 0):.3f}."
)
lines.extend(["", "## Anchor Status"])
if anchor_dirs:
for path in anchor_dirs:
lines.append(f"- Found candidate no-PCD anchor: `{path}`")
else:
lines.append("- No same-protocol `deplot_no_vs_opd` 4epoch anchor found under `outputs/test-fast`.")
lines.append("- To fill this gap, run: `bash scripts/test/run_pcd_forensics_anchor.sh`.")
lines.extend(["", "## Failure Samples"])
by_category = Counter(row["category"] for row in failure_rows)
if by_category:
for category, count in by_category.most_common():
lines.append(f"- {category}: {count} sampled rows in `eval_failure_samples.csv`.")
else:
lines.append("- No final eval failure samples parsed.")
lines.append("")
out_dir.joinpath("pcd_low_score_forensics.md").write_text("\n".join(lines), encoding="utf-8")
def make_checkpoint_accuracy_plot(out_dir: Path, eval_rows: list[dict[str, Any]]) -> None:
try:
import matplotlib.pyplot as plt
except Exception as exc:
out_dir.joinpath("checkpoint_accuracy_plot.skipped.txt").write_text(
f"matplotlib unavailable: {exc}\n",
encoding="utf-8",
)
return
series: dict[str, list[tuple[int, float, str]]] = {}
for row in eval_rows:
acc = _safe_float(row.get("accuracy"))
if acc is None:
continue
checkpoint = str(row.get("checkpoint") or "")
match = re.search(r"checkpoint-(\d+)", checkpoint)
step = int(match.group(1)) if match else 588 if checkpoint == "final_checkpoint" else 10**9
series.setdefault(str(row.get("run")), []).append((step, acc, checkpoint))
if not series:
return
fig, ax = plt.subplots(figsize=(8, 4.5))
for run, points in sorted(series.items()):
points = sorted(points)
xs = [p[0] for p in points]
ys = [p[1] for p in points]
ax.plot(xs, ys, marker="o", label=run)
for x, y, checkpoint in points:
if checkpoint == "final_checkpoint":
ax.annotate("final", (x, y), textcoords="offset points", xytext=(5, 5), fontsize=8)
ax.set_title("PCD Checkpoint Accuracy Sweep")
ax.set_xlabel("global step")
ax.set_ylabel("ChartQA accuracy")
ax.set_ylim(0.35, max(0.62, max(y for points in series.values() for _, y, _ in points) + 0.03))
ax.grid(True, alpha=0.25)
ax.legend()
fig.tight_layout()
fig.savefig(out_dir / "checkpoint_accuracy_curve.png", dpi=180)
plt.close(fig)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--out-dir", default="outputs/test-fast/pcd-low-score-forensics")
parser.add_argument(
"--run",
action="append",
default=[],
metavar="LABEL:RUN_DIR:LOG_DIR[:EVAL_SUMMARY]",
help="Add/override a run tuple. Can be repeated.",
)
return parser.parse_args()
def main() -> int:
args = parse_args()
out_dir = Path(args.out_dir)
runs = []
if args.run:
for spec in args.run:
parts = spec.split(":")
if len(parts) not in (3, 4):
raise SystemExit(f"bad --run spec: {spec}")
extra = Path(parts[3]) if len(parts) == 4 else None
runs.append((parts[0], Path(parts[1]), Path(parts[2]), extra))
else:
runs = DEFAULT_RUNS
config_rows: list[dict[str, Any]] = []
eval_rows: list[dict[str, Any]] = []
funnel_rows: list[dict[str, Any]] = []
train_rows: list[dict[str, Any]] = []
failure_rows: list[dict[str, Any]] = []
for item in runs:
label, run_dir, log_dir = item[:3]
extra_summary = item[3] if len(item) > 3 else None
config_rows.append(load_run_config(label, run_dir))
eval_rows.extend(collect_eval_rows(label, run_dir, [extra_summary] if extra_summary else []))
funnel_rows.extend(collect_candidate_funnel(label, run_dir))
train_rows.extend(collect_training_windows(label, log_dir))
failure_rows.extend(collect_eval_failures(label, run_dir))
anchor_dirs = sorted(Path("outputs/test-fast").glob("**/deplot_no_vs_opd"))
_write_csv(
out_dir / "run_config_summary.csv",
config_rows,
[
"run",
"num_train_epochs",
"probe_batch_size",
"probe_all_wrong_after_step",
"probe_max_per_batch",
"loss_type",
"opsd_weight",
"variance_adaptive",
"visual_enabled",
"visual_prefetch_ic",
"train_dataset",
],
)
_write_csv(
out_dir / "checkpoint_accuracy.csv",
eval_rows,
[
"run",
"checkpoint",
"accuracy",
"processed",
"total",
"other_rate",
"full_cot_rate",
"answer_flag_rate",
"output_types",
"log_path",
],
)
_write_csv(
out_dir / "route_funnel.csv",
funnel_rows,
[
"run",
"scope",
"candidates",
"teacher_correct",
"teacher_correct_rate",
"final_opd",
"final_opd_rate",
"final_sft",
"final_sft_rate",
"parse_fail",
"parse_fail_rate",
"answer_flag",
"answer_flag_rate",
"teacher_gold_suffix_like",
"teacher_tokens_mean",
"teacher_tokens_p95",
],
)
_write_csv(out_dir / "training_windows.csv", train_rows, ["run", "window", "n", *TRAIN_KEYS, "train_log"])
_write_csv(
out_dir / "eval_failure_samples.csv",
failure_rows,
["run", "category", "prediction_preview", "gold", "eval_log"],
)
make_checkpoint_accuracy_plot(out_dir, eval_rows)
build_report(out_dir, config_rows, eval_rows, funnel_rows, train_rows, failure_rows, anchor_dirs)
print(f"Wrote forensic report under {out_dir}")
return 0
if __name__ == "__main__":
raise SystemExit(main())
|