150 lines
7.1 KiB
Python
150 lines
7.1 KiB
Python
#!/usr/bin/env python3
|
||
"""sync_status —— 把 registry/models.csv 的 status 列刷新成实机事实。
|
||
|
||
PYTHONPATH=. python3 scripts/sync_status.py # dry-run:只打印将发生的变化
|
||
PYTHONPATH=. python3 scripts/sync_status.py --apply # 真正写 models.csv
|
||
|
||
models.csv 的 status 是 Excel 导入时(excel_to_models.py)按来源启发式打的:
|
||
torchvision/cls→ready、ultralytics/det→ready、其余 needs_adapter/needs_eval/skip。
|
||
跑了一阵子之后它和实机进度脱节:适配器、probe、t1/calibration 都陆续补齐了,
|
||
但 status 没人回头改,于是 `bench expand` 默认只展开 status=ready,看不到这些
|
||
实际已具备准入条件的模型。
|
||
|
||
判定不重新发明:直接复用 harness/coverage.py 的 model_blockers()(与
|
||
master_inventory 同一调用方式:models() → model_spec() → model_blockers(row, cache))。
|
||
规则:
|
||
blockers 为空 且 status ∈ {needs_adapter, needs_eval} → 改 ready
|
||
blockers 非空 → 保持原 status
|
||
status = skip → 一律不动(非神经网络,不量化)
|
||
status = ready → 不动(只升不降;是否有 blocker
|
||
与 label 无关,不做降级)
|
||
写入用 csv 模块 round-trip:列结构、行序、其他列字节不变。写入前先验证未修改
|
||
内容能被同一套 writer 逐字节还原(防格式漂移),再只改 status 单元格,
|
||
临时文件 + os.replace 原子落盘。
|
||
|
||
默认 dry-run 的原因:models.csv 参与 registry_fingerprint(),跑着的 collector /
|
||
scheduler 对指纹变化敏感——当前 run 有 rollout 在采时改 registry 可能扰动它们。
|
||
何时可以 --apply:当前 run 收尾(rollout 全部终态、调度进程退出)之后再跑;
|
||
改完后用 `PYTHONPATH=. python3 -c "from harness.registry import status_counts; print(status_counts())"`
|
||
复核,再正常 scripts/start_run.sh --resume。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import argparse
|
||
import csv
|
||
import io
|
||
import os
|
||
import sys
|
||
import tempfile
|
||
import time
|
||
from collections import Counter
|
||
from pathlib import Path
|
||
|
||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||
from harness import paths # noqa: E402
|
||
from harness.adapters import model_spec # noqa: E402
|
||
from harness.coverage import model_blockers # noqa: E402
|
||
from harness.registry import models, status_counts # noqa: E402
|
||
|
||
PROMOTE = ("needs_adapter", "needs_eval")
|
||
|
||
|
||
def roundtrip(rows: list[list[str]], terminator: str) -> bytes:
|
||
"""csv 模块序列化;与读入同一 dialect,只有 terminator 按文件实测。"""
|
||
buf = io.StringIO(newline="")
|
||
csv.writer(buf, lineterminator=terminator).writerows(rows)
|
||
return buf.getvalue().encode("utf-8")
|
||
|
||
|
||
def main() -> int:
|
||
ap = argparse.ArgumentParser(description="按 model_blockers() 实况刷新 models.csv 的 status")
|
||
ap.add_argument("--csv", default=str(paths.REGISTRY / "models.csv"),
|
||
help="models.csv 路径(默认 registry/models.csv)")
|
||
ap.add_argument("--apply", action="store_true",
|
||
help="真正写入(默认 dry-run 只打印)")
|
||
a = ap.parse_args()
|
||
csv_path = Path(a.csv)
|
||
|
||
raw = csv_path.read_bytes()
|
||
terminator = "\r\n" if b"\r\n" in raw else "\n"
|
||
text = raw.decode("utf-8")
|
||
rows = list(csv.reader(io.StringIO(text, newline="")))
|
||
pristine = [list(r) for r in rows] # 改动前的逐字节快照
|
||
header = rows[0]
|
||
col = {name: i for i, name in enumerate(header)}
|
||
for need in ("model_id", "task", "status"):
|
||
if need not in col:
|
||
raise SystemExit(f"{csv_path} 缺列 {need!r},现有:{header}")
|
||
|
||
# ---- 判定:与 master_inventory() 相同的调用链,共享一个 prerequisite cache
|
||
cache: dict = {}
|
||
t0 = time.time()
|
||
admission: dict[str, list[str]] = {}
|
||
for row in models():
|
||
spec = model_spec(row)
|
||
admission[spec["model_id"]] = model_blockers(spec, cache)
|
||
elapsed = time.time() - t0
|
||
|
||
# ---- 规则应用(改的是原始 CSV 行,不是 models() 的 strip 副本)
|
||
st_before = Counter((r[col["status"]].strip() or "ready")
|
||
for r in pristine[1:] if r[col["model_id"]].strip())
|
||
changes: list[tuple[str, str, str, str]] = [] # (model, task, old, new)
|
||
for r in rows[1:]:
|
||
mid = r[col["model_id"]].strip()
|
||
if not mid or mid not in admission:
|
||
continue
|
||
old = r[col["status"]].strip()
|
||
if old in ("skip", "ready"):
|
||
continue # skip 不动;ready 只升不降
|
||
if not admission[mid] and old in PROMOTE:
|
||
r[col["status"]] = "ready"
|
||
changes.append((mid, r[col["task"]].strip(), old, "ready"))
|
||
|
||
# ---- 报告
|
||
print(f"models.csv: {csv_path} · {len(rows) - 1} 行 · model_blockers() 实测 {len(admission)} 个模型,耗时 {elapsed:.1f}s")
|
||
print(f"status 现状: {dict(st_before.most_common())}")
|
||
print()
|
||
if not changes:
|
||
print("没有需要变更的模型(blockers 为空的都已 ready,其余按规则保持)")
|
||
return 0
|
||
for mid, task, old, new in changes:
|
||
print(f" {mid:<42} {task:<6} {old:<14} → {new} 依据: model_blockers()=0(适配器/probe/t1/calibration 就绪)")
|
||
st_after = dict(st_before)
|
||
for _, _, old, new in changes:
|
||
st_after[old] -= 1
|
||
st_after[new] += 1
|
||
print()
|
||
print(f"将变更 {len(changes)} 个模型 · 按 task: {dict(Counter(t for _, t, _, _ in changes))}")
|
||
print(f"变更后 status: {dict(Counter({k: v for k, v in st_after.items() if v}).most_common())}")
|
||
|
||
if not a.apply:
|
||
print("\ndry-run,未写盘。确认后加 --apply(等当前 run 收尾、rollout 全部终态之后再动 registry)。")
|
||
return 0
|
||
|
||
# ---- 写入:先证明这套 writer 配置能逐字节还原原文件,再落盘
|
||
if roundtrip(pristine, terminator) != raw:
|
||
raise SystemExit("round-trip 校验失败:csv 序列化无法逐字节还原原文件,拒绝写入(防格式漂移)")
|
||
fd, tmp = tempfile.mkstemp(dir=csv_path.parent, prefix=csv_path.name + ".")
|
||
try:
|
||
with os.fdopen(fd, "w", newline="", encoding="utf-8") as stream:
|
||
csv.writer(stream, lineterminator=terminator).writerows(rows)
|
||
os.replace(tmp, csv_path)
|
||
finally:
|
||
if os.path.exists(tmp):
|
||
os.unlink(tmp)
|
||
print(f"\nwrote {len(changes)} 处 status 变更 → {csv_path}")
|
||
with csv_path.open(encoding="utf-8", newline="") as stream:
|
||
written = list(csv.reader(stream))
|
||
if written != rows:
|
||
raise SystemExit("写入后回读与预期行不一致,请立即检查文件")
|
||
if csv_path == paths.REGISTRY / "models.csv":
|
||
models.cache_clear()
|
||
print(f"复核 registry.models() 重新加载: {status_counts()}") # 只认 REGISTRY 下的真身
|
||
else:
|
||
print(f"复核回读: {dict(Counter((r[col['status']].strip() or 'ready') for r in written[1:] if r[col['model_id']].strip()).most_common())}")
|
||
return 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main())
|