Claude Claude Opus 5.5 commited on
eval_mlflow: retro logging — skip files already logged (sha), expand globs, tag source_mtime/run_commit
Browse filesCo-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GwsdogGsnbFRJYfG8rdbzK
- test_eval_mlflow.py +17 -0
- training/eval_mlflow.py +40 -2
test_eval_mlflow.py
CHANGED
|
@@ -71,5 +71,22 @@ mp = mlflow.get_run(E.log_eval(mv)).data.params
|
|
| 71 |
check("gemini_model ว่าง → เติมจาก model_version ของเทิร์น",
|
| 72 |
mp.get("sys.gemini_model") == "gemini-3.8-flash" and mp.get("sys.gemini_model_from") == "model_version", mp)
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
print(f"\nรวม {ok}/{ok + fail}")
|
| 75 |
sys.exit(1 if fail else 0)
|
|
|
|
| 71 |
check("gemini_model ว่าง → เติมจาก model_version ของเทิร์น",
|
| 72 |
mp.get("sys.gemini_model") == "gemini-3.8-flash" and mp.get("sys.gemini_model_from") == "model_version", mp)
|
| 73 |
|
| 74 |
+
# บันทึกย้อนหลัง: วันที่ไฟล์ · commit ที่รัน · กันบันทึกซ้ำ · ขยาย * เอง (PowerShell ไม่ขยายให้)
|
| 75 |
+
import json as _json, subprocess as _sp
|
| 76 |
+
d = Path(tempfile.mkdtemp()) # นอก mlruns (file store อ่านทุกโฟลเดอร์ในนั้นเป็น experiment)
|
| 77 |
+
for n in ("qi_blind_r1.json", "qi_blind_r2.json"):
|
| 78 |
+
(d / n).write_text(_json.dumps(dict(summary, results=res)), encoding="utf-8")
|
| 79 |
+
(d / "qi_blind_r2.json").write_text(_json.dumps(dict(summary, results=res, passed=2)), encoding="utf-8")
|
| 80 |
+
check("ขยาย * เป็นไฟล์เรียงชื่อ", [Path(x).name for x in E.expand([str(d / "qi_*.json")])] == ["qi_blind_r1.json", "qi_blind_r2.json"])
|
| 81 |
+
rr = mlflow.get_run(E.log_eval(_json.loads((d / "qi_blind_r1.json").read_text(encoding="utf-8")),
|
| 82 |
+
json_path=str(d / "qi_blind_r1.json"))).data.tags
|
| 83 |
+
check("tag source_sha · source_mtime · run_commit จาก system_info",
|
| 84 |
+
rr.get("source_sha") == E.file_sha(d / "qi_blind_r1.json") and rr.get("source_mtime", "").startswith("20")
|
| 85 |
+
and rr.get("run_commit") == "e1b331f", rr)
|
| 86 |
+
check("ไฟล์ที่บันทึกแล้วถูกจับได้ · ไฟล์ใหม่ไม่", E.already_logged(d / "qi_blind_r1.json") and not E.already_logged(d / "qi_blind_r2.json"))
|
| 87 |
+
out = _sp.run([sys.executable, str(Path(E.__file__)), str(d / "*.json"), "--round", "1"], capture_output=True, text=True,
|
| 88 |
+
encoding="utf-8", env=dict(os.environ, PYTHONIOENCODING="utf-8")).stdout
|
| 89 |
+
check("main: ข้ามไฟล์ที่บันทึกแล้ว บันทึกไฟล์ใหม่", "ข้าม" in out and out.count("ผ่าน ") == 1, out)
|
| 90 |
+
|
| 91 |
print(f"\nรวม {ok}/{ok + fail}")
|
| 92 |
sys.exit(1 if fail else 0)
|
training/eval_mlflow.py
CHANGED
|
@@ -24,6 +24,7 @@ import re
|
|
| 24 |
import subprocess
|
| 25 |
import sys
|
| 26 |
from collections import Counter
|
|
|
|
| 27 |
from pathlib import Path
|
| 28 |
|
| 29 |
HERE = Path(__file__).resolve().parent
|
|
@@ -119,6 +120,33 @@ def cost_metrics(results: list) -> dict:
|
|
| 119 |
return out
|
| 120 |
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
def log_eval(summary: dict, json_path: str | None = None, code: str = "", round_no: str = "",
|
| 123 |
note: str = "", blind: bool | None = None) -> str:
|
| 124 |
os.environ.setdefault("MLFLOW_ALLOW_FILE_STORE", "true") # ข้อ 5.2 กำหนด file store
|
|
@@ -133,9 +161,15 @@ def log_eval(summary: dict, json_path: str | None = None, code: str = "", round_
|
|
| 133 |
if tc and (ROOT / tc).exists():
|
| 134 |
tc_hash = hashlib.sha256((ROOT / tc).read_bytes().replace(b"\r\n", b"\n")).hexdigest()[:12]
|
| 135 |
name = Path(json_path).stem if json_path else f"{summary.get('module')}-{round_no or 'run'}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
with mlflow.start_run(run_name=name) as run:
|
| 137 |
mlflow.set_tags({"kind": "answer_eval", "code": code or "unknown", "round": str(round_no or ""),
|
| 138 |
-
"commit_at_logging": commit, "source_file": json_path or "", "note": note
|
|
|
|
| 139 |
mlflow.log_params({"module": summary.get("module"), "testcases": tc, "testcases_sha": tc_hash})
|
| 140 |
info = system_of(summary)
|
| 141 |
prompt = info.pop("prompt_text", None)
|
|
@@ -168,8 +202,12 @@ def main() -> int:
|
|
| 168 |
ap.add_argument("--note", default="")
|
| 169 |
ap.add_argument("--no-mlflow", action="store_true", help="แค่พิมพ์ผล ไม่ log")
|
| 170 |
ap.add_argument("--round", default="", help="รอบที่เท่าไหร่ (pass^k ใช้แยกรอบ 1/2)")
|
|
|
|
| 171 |
a = ap.parse_args()
|
| 172 |
-
for f in a.files:
|
|
|
|
|
|
|
|
|
|
| 173 |
s = json.loads(Path(f).read_text(encoding="utf-8"))
|
| 174 |
lk = leak_summary(s.get("results", []))
|
| 175 |
words = " · ".join(f"{w} {n}" for w, n in lk["per_word"].items()) or "-"
|
|
|
|
| 24 |
import subprocess
|
| 25 |
import sys
|
| 26 |
from collections import Counter
|
| 27 |
+
from datetime import datetime
|
| 28 |
from pathlib import Path
|
| 29 |
|
| 30 |
HERE = Path(__file__).resolve().parent
|
|
|
|
| 120 |
return out
|
| 121 |
|
| 122 |
|
| 123 |
+
def file_sha(path) -> str:
|
| 124 |
+
return hashlib.sha256(Path(path).read_bytes()).hexdigest()[:16]
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
def already_logged(path) -> list:
|
| 128 |
+
"""run ที่บันทึกไฟล์นี้ (sha เดียวกัน) ไว้แล้ว — กันบันทึกซ้ำเมื่อย้อนบันทึกทั้งโฟลเดอร์"""
|
| 129 |
+
os.environ.setdefault("MLFLOW_ALLOW_FILE_STORE", "true")
|
| 130 |
+
import mlflow
|
| 131 |
+
mlflow.set_tracking_uri(MLRUNS.as_uri())
|
| 132 |
+
exp = mlflow.get_experiment_by_name("answer_eval")
|
| 133 |
+
if exp is None:
|
| 134 |
+
return []
|
| 135 |
+
runs = mlflow.search_runs([exp.experiment_id], filter_string=f"tags.source_sha = '{file_sha(path)}'",
|
| 136 |
+
output_format="list")
|
| 137 |
+
return [r.info.run_id for r in runs]
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
def expand(patterns: list) -> list:
|
| 141 |
+
"""PowerShell ไม่ขยาย * ให้ python → ขยายเอง · คงลำดับ · ตัดซ้ำ"""
|
| 142 |
+
import glob
|
| 143 |
+
out = []
|
| 144 |
+
for pat in patterns:
|
| 145 |
+
hits = sorted(glob.glob(pat)) if any(c in pat for c in "*?[") else [pat]
|
| 146 |
+
out += [h for h in hits if h not in out]
|
| 147 |
+
return out
|
| 148 |
+
|
| 149 |
+
|
| 150 |
def log_eval(summary: dict, json_path: str | None = None, code: str = "", round_no: str = "",
|
| 151 |
note: str = "", blind: bool | None = None) -> str:
|
| 152 |
os.environ.setdefault("MLFLOW_ALLOW_FILE_STORE", "true") # ข้อ 5.2 กำหนด file store
|
|
|
|
| 161 |
if tc and (ROOT / tc).exists():
|
| 162 |
tc_hash = hashlib.sha256((ROOT / tc).read_bytes().replace(b"\r\n", b"\n")).hexdigest()[:12]
|
| 163 |
name = Path(json_path).stem if json_path else f"{summary.get('module')}-{round_no or 'run'}"
|
| 164 |
+
src = {}
|
| 165 |
+
if json_path and Path(json_path).exists(): # บันทึกย้อนหลัง: วันที่ไฟล์ + sha ไว้กันบันทึกซ้ำ
|
| 166 |
+
fp = Path(json_path)
|
| 167 |
+
src = {"source_sha": file_sha(fp),
|
| 168 |
+
"source_mtime": datetime.fromtimestamp(fp.stat().st_mtime).isoformat(timespec="seconds")}
|
| 169 |
with mlflow.start_run(run_name=name) as run:
|
| 170 |
mlflow.set_tags({"kind": "answer_eval", "code": code or "unknown", "round": str(round_no or ""),
|
| 171 |
+
"commit_at_logging": commit, "source_file": json_path or "", "note": note,
|
| 172 |
+
"run_commit": str((summary.get("system_info") or {}).get("commit", "")), **src})
|
| 173 |
mlflow.log_params({"module": summary.get("module"), "testcases": tc, "testcases_sha": tc_hash})
|
| 174 |
info = system_of(summary)
|
| 175 |
prompt = info.pop("prompt_text", None)
|
|
|
|
| 202 |
ap.add_argument("--note", default="")
|
| 203 |
ap.add_argument("--no-mlflow", action="store_true", help="แค่พิมพ์ผล ไม่ log")
|
| 204 |
ap.add_argument("--round", default="", help="รอบที่เท่าไหร่ (pass^k ใช้แยกรอบ 1/2)")
|
| 205 |
+
ap.add_argument("--force", action="store_true", help="บันทึกซ้ำแม้ไฟล์ sha เดียวกันเคยบันทึกแล้ว")
|
| 206 |
a = ap.parse_args()
|
| 207 |
+
for f in expand(a.files):
|
| 208 |
+
if not a.no_mlflow and not a.force and (ids := already_logged(f)):
|
| 209 |
+
print(f" {f:<22} ข้าม — บันทึกแล้ว ({ids[0][:8]})")
|
| 210 |
+
continue
|
| 211 |
s = json.loads(Path(f).read_text(encoding="utf-8"))
|
| 212 |
lk = leak_summary(s.get("results", []))
|
| 213 |
words = " · ".join(f"{w} {n}" for w, n in lk["per_word"].items()) or "-"
|