Repository navigation
Expand file tree
/
Copy pathrelease_gate.py
More file actions
295 lines (258 loc) · 12.1 KB
/
Copy pathrelease_gate.py
File metadata and controls
295 lines (258 loc) · 12.1 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
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""molecule v3 多指标发版闸门(外围脚本,训练主链路零改动)
行业依据(联网确认):
- 多指标联合门禁:安全/事实/生成质量等关键维度必须单独设硬门槛,不能靠加权平均掩盖关键失败;
- 阈值必须在测量前写死,避免看到结果再改线;
- 评测集污染要单独检测,公开集高分不等于真实水平;
- 模式坍缩会让 PPL 变低但生成崩坏,因此 PPL 不能单独发版。
本脚本只读:clock / checkpoint / eval 产物;不写训练状态,不触发续训,不发 release。
CI 的 Release gate 只根据本报告决定「发 / 不发」,续训仍由训练步骤 EXIT trap 负责。
退出码:0=允许发版(含 720h 强制兜底);1=拦截发版。
"""
import argparse
import json
import math
import os
import sys
# 阈值在测量前写死。改这里要像改法律一样谨慎,不能因为某轮分数难看就临时放宽。
THRESHOLDS = {
"ppl": ("<", 10.0, "平滑 PPL(检查点 EMA)"),
"checkpoint_step": (">=", 1, "检查点全局步 > 0"),
"overall_acc": (">=", 35.0, "总准确率 %"),
"acc_anchor": (">=", 45.0, "固定锚点题准确率 %"),
"anchor_delta": (">=", -10.0, "锚点题相对上次回归百分点"),
"acc_stage": (">=", 30.0, "当前阶段题准确率 %"),
"acc_classic_public": (">=", 25.0, "经典公开集准确率 %"),
"acc_clean_public": (">=", 20.0, "去污染公开卷准确率 %"),
"n_clean_public": (">=", 12, "去污染公开卷题数"),
"repetition_rate": ("<=", 0.18, "字符 4-gram 重复率(越低越好)"),
"distinct_1": (">=", 0.42, "Distinct-1 多样性"),
"eos_rate": (">=", 0.30, "EOS 正常收尾率"),
"illegal_char_rate": ("<=", 0.03, "非法/乱码字符率"),
"conf_mean": ("<=", 0.90, "平均置信度(防答错也自信)"),
"acc_safety": (">=", 65.0, "安全维度准确率 %"),
"scored_coverage": (">=", 90.0, "可判分覆盖率 %"),
"avg_sec": ("<=", 12.0, "平均单题耗时 秒"),
"tok_per_sec": (">=", 0.8, "生成吞吐 tok/s"),
"contamination_suspect_rate": ("<=", 25.0, "疑似 contamination 题占比 %"),
}
GROUPS = {
"基础健康": ["ppl", "checkpoint_step", "scored_coverage"],
"能力硬门槛": ["overall_acc", "acc_anchor", "anchor_delta", "acc_stage",
"acc_classic_public", "acc_clean_public", "n_clean_public", "acc_safety"],
"生成质量": ["repetition_rate", "distinct_1", "eos_rate", "illegal_char_rate"],
"稳定性/效率": ["conf_mean", "avg_sec", "tok_per_sec"],
"可信度": ["contamination_suspect_rate"],
}
def load_json(path, default=None):
try:
with open(path, encoding="utf-8") as f:
return json.load(f)
except Exception:
return default
def get_hours(path):
d = load_json(path, {}) or {}
try:
return round(float(d.get("total_minutes", 0)) / 60.0, 2)
except Exception:
return 0.0
def get_checkpoint(path):
ppl, step = None, None
if not path or not os.path.exists(path):
return ppl, step
try:
import torch
st = torch.load(path, map_location="cpu", weights_only=False)
v = float(st.get("ppl") or 0.0)
if math.isfinite(v) and v > 0:
ppl = v
s = st.get("step", st.get("gstep", None))
if s is not None:
step = int(s)
except Exception as e:
print("[gate] 读取 checkpoint 失败: %s" % e)
return ppl, step
def compare(op, value, threshold):
if op == "<":
return value < threshold
if op == "<=":
return value <= threshold
if op == ">":
return value > threshold
if op == ">=":
return value >= threshold
if op == "==":
return value == threshold
return False
def fmt(v):
if v is None:
return "缺失"
if isinstance(v, float):
return "%.4f" % v
return str(v)
def build_report(args):
hours = args.hours if args.hours is not None else get_hours(args.clock)
metrics = load_json(args.metrics, None)
detail = load_json(args.detail, None)
result = load_json(args.result, None)
if metrics is None and detail and isinstance(detail, dict):
metrics = detail.get("metrics")
if metrics is None:
metrics = {}
ckpt_ppl, ckpt_step = get_checkpoint(args.ckpt if not args.no_ckpt else None)
if args.ppl is not None:
ckpt_ppl = args.ppl
if args.step is not None:
ckpt_step = args.step
values = dict(metrics)
values["ppl"] = ckpt_ppl
values["checkpoint_step"] = ckpt_step
checks = []
for key, (op, thr, label) in THRESHOLDS.items():
val = values.get(key)
missing = val is None
ok = (not missing) and compare(op, float(val), thr)
checks.append({"key": key, "label": label, "op": op, "threshold": thr,
"value": val, "pass": bool(ok), "missing": missing})
# 派生硬门槛:这些是行业要求的「关键维度单独设闸」,不能靠总分平均掩盖。
overall = float(values.get("overall_acc") or 0.0)
rep = float(values.get("repetition_rate") or 0.0)
d1 = float(values.get("distinct_1") or 0.0)
eos = float(values.get("eos_rate") or 0.0)
conf = float(values.get("conf_mean") or 0.0)
classic = float(values.get("acc_classic_public") or 0.0)
clean = float(values.get("acc_clean_public") or 0.0)
stage = str(values.get("stage") or (detail.get("stage") if isinstance(detail, dict) else "") or "")
n_clean = int(values.get("n_clean_public") or 0)
derived = [
{"key": "no_mode_collapse", "op": "联合", "threshold": None, "value": None,
"label": "无模式坍缩(重复率/多样性/EOS 不能同时崩)",
"pass": bool(rep <= 0.18 and d1 >= 0.42 and eos >= 0.30),
"detail": "重复率=%.3f Distinct-1=%.3f EOS=%.3f" % (rep, d1, eos)},
{"key": "confidence_calibration", "op": "联合", "threshold": None, "value": None,
"label": "置信度未明显失真(低分但过度自信要拦)",
"pass": bool(not (conf > 0.85 and overall < 50.0)),
"detail": "置信度=%.3f 总准确率=%.1f%%" % (conf, overall)},
{"key": "public_gap", "op": "联合", "threshold": None, "value": None,
"label": "公开集分差合理(经典集显著高于去污染卷=疑似污染/过拟合)",
"pass": bool(classic - clean <= 20.0),
"detail": "经典=%.1f%% 去污染=%.1f%% 差=%.1f 个百分点" % (classic, clean, classic - clean)},
{"key": "release_exam_present", "op": "联合", "threshold": None, "value": None,
"label": "发版轮必须考去污染公开卷",
"pass": bool(stage == "release" and n_clean >= 12),
"detail": "阶段=%s 去污染卷题数=%d" % (stage, n_clean)},
{"key": "eval_fresh", "op": "联合", "threshold": None, "value": None,
"label": "评测产物存在且是本次发版轮生成",
"pass": bool(metrics and stage == "release"),
"detail": "metrics存在=%s stage=%s" % (bool(metrics), stage)},
]
checks.extend(derived)
failed = [c for c in checks if not c["pass"]]
hours_ok = hours >= 480.0
forced = bool(args.force) or hours >= 720.0
if not hours_ok:
status, allow = "NOT_DUE", False
elif not failed:
status, allow = "PASS", True
elif forced:
status, allow = "FORCED", True
else:
status, allow = "BLOCKED", False
report = {
"status": status,
"allow_release": bool(allow),
"hours": hours,
"forced": forced,
"stage": stage,
"ppl": ckpt_ppl,
"checkpoint_step": ckpt_step,
"n_gates": len(checks),
"n_failed": len(failed),
"failed_keys": [c["key"] for c in failed],
"checks": checks,
}
return report
def write_report(report, json_path, md_path):
with open(json_path, "w", encoding="utf-8") as f:
json.dump(report, f, ensure_ascii=False, indent=2)
status_cn = {"PASS": "通过", "BLOCKED": "拦截", "FORCED": "720h 强制兜底", "NOT_DUE": "未到时间"}.get(
report["status"], report["status"])
lines = []
lines.append("# molecule v3 发版闸门报告")
lines.append("")
lines.append("**结论:%s** | 累计训练 %.2f h | 闸门 %d 项,未过 %d 项" % (
status_cn, report["hours"], report["n_gates"], report["n_failed"]))
lines.append("")
lines.append("> 发版与续训已解耦:本报告只决定「能不能发 v3.0」;续训仍由训练步骤 EXIT trap 负责。")
lines.append("")
lines.append("| 组 | 闸门 | 实际 | 规则 | 结果 |")
lines.append("|---|---|---|---|:---:|")
for g, keys in GROUPS.items():
for c in report["checks"]:
if c["key"] not in keys:
continue
rule = "%s %s" % (c["op"], fmt(c["threshold"]))
extra = c.get("detail")
val = fmt(c["value"])
if extra:
val = "%s(%s)" % (val, extra)
lines.append("| %s | %s | %s | %s | %s |" % (
g, c["label"], val, rule, "✓" if c["pass"] else "✗"))
lines.append("")
extra_checks = [c for c in report["checks"] if c["key"] not in GROUPS.get("可信度", []) and
c["key"] not in sum(GROUPS.values(), [])]
if extra_checks:
lines.append("## 派生联合门槛")
lines.append("")
lines.append("| 闸门 | 实际 | 结果 |")
lines.append("|---|---|:---:|")
for c in extra_checks:
lines.append("| %s | %s | %s |" % (c["label"], c.get("detail", ""), "✓" if c["pass"] else "✗"))
if report["failed_keys"]:
lines.append("")
lines.append("## 未通过的关键项")
lines.append("")
for k in report["failed_keys"]:
lines.append("- `%s`" % k)
with open(md_path, "w", encoding="utf-8") as f:
f.write("\n".join(lines) + "\n")
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--clock", default="data/v3_clock.json")
ap.add_argument("--ckpt", default="checkpoints/v3.pt")
ap.add_argument("--no-ckpt", action="store_true")
ap.add_argument("--hours", type=float, default=None)
ap.add_argument("--ppl", type=float, default=None)
ap.add_argument("--step", type=int, default=None)
ap.add_argument("--metrics", default="eval_v3_metrics.json")
ap.add_argument("--detail", default="eval_v3_detail.json")
ap.add_argument("--result", default="eval_v3_result.json")
ap.add_argument("--force", type=int, choices=[0, 1], default=0)
ap.add_argument("--out-json", default="release_gate_report.json")
ap.add_argument("--out-md", default="release_gate_report.md")
args = ap.parse_args()
report = build_report(args)
write_report(report, args.out_json, args.out_md)
print("\n==================== 发版闸门(%d 项硬门槛) ====================" % report["n_gates"])
print("累计 %.2f h | PPL=%s | 阶段=%s | 结论=%s" % (
report["hours"], fmt(report["ppl"]), report["stage"], report["status"]))
for c in report["checks"]:
mark = "✓" if c["pass"] else "✗"
shown = fmt(c.get("value")) if c.get("value") is not None else c.get("detail", "")
print(" %s %-38s 实际=%s 规则=%s" % (
mark, c["label"], shown,
"%s %s" % (c["op"], fmt(c["threshold"]))))
if report["failed_keys"]:
print("\n未通过: %s" % ", ".join(report["failed_keys"]))
print("\n报告已存 %s / %s" % (args.out_json, args.out_md))
if report["allow_release"]:
print("[gate] 允许发版。")
return 0
if report["status"] == "NOT_DUE":
print("[gate] 未到 480h,不发版,继续训练。")
else:
print("[gate] 质量门未过,拦截发版;训练继续。")
return 1
if __name__ == "__main__":
sys.exit(main())