train_all r41: crosspair long negs (len×label balance) + agg-level T_agg calibration
This commit is contained in:
parent
27fa0f5c06
commit
276e20bb32
118
train_all.py
118
train_all.py
@ -3,7 +3,7 @@
|
||||
在 fat 容器内运行。数据和 laya_repo 模块通过 gitea clone 获取 (/tmp/jd)。
|
||||
用法: python3 train_all.py [--epochs N] [--lr LR] [--bs N]
|
||||
"""
|
||||
import json, os, sys, bisect
|
||||
import json, os, sys, bisect, random
|
||||
|
||||
INSTR = "Does this document contain the information needed to answer the user's question? Answer based only on the document content."
|
||||
DATA_DIR = "/tmp/jd"
|
||||
@ -47,6 +47,34 @@ def bisect_auc(probs, labels):
|
||||
return 0.0
|
||||
return sum(bisect.bisect_left(neg, p) for p in pos) / (len(pos) * len(neg))
|
||||
|
||||
def _lang(s):
|
||||
return "zh" if any("一" <= c <= "鿿" for c in s) else "en"
|
||||
|
||||
def augment_crosspair_negs(rows, win_tokens, max_new=2500):
|
||||
"""长文档 × 随机同语言其他 query → 长负例。
|
||||
训练负例几乎都是短文档而 eval_v2 负例全是长文档——模型没学过
|
||||
『长而无关 → 每窗都低分』。跨配对直接补齐 长度×标签 的失衡象限。"""
|
||||
rng = random.Random(20261106)
|
||||
nch_thr = {"zh": win_tokens, "en": win_tokens * 3} # 字符≈token 粗估
|
||||
cand = [i for i, r in enumerate(rows)
|
||||
if len(r.get("doc_text") or "") > nch_thr[_lang(r.get("doc_text") or "")]]
|
||||
rng.shuffle(cand)
|
||||
pools = {}
|
||||
for i, r in enumerate(rows):
|
||||
pools.setdefault(_lang(r["query"]), []).append(i)
|
||||
added = []
|
||||
for i in cand[:max_new]:
|
||||
L = _lang(rows[i].get("doc_text") or "")
|
||||
pool = pools[L]
|
||||
j = rng.choice(pool)
|
||||
if j == i:
|
||||
continue
|
||||
added.append({"pair_id": f"xp-{rows[i].get('pair_id', i)}",
|
||||
"query": rows[j]["query"],
|
||||
"doc_text": rows[i]["doc_text"],
|
||||
"hard_label": 0, "soft_label": 0.0})
|
||||
return added
|
||||
|
||||
def phase_data():
|
||||
"""v05n tar.gz 分片重组 → DATA_DIR/train.jsonl"""
|
||||
import glob, shutil, tarfile
|
||||
@ -274,6 +302,10 @@ def run(args):
|
||||
if args.tiny:
|
||||
train, calib = train[:64], calib[:16]
|
||||
log(" TINY mode: train[:64] calib[:16]")
|
||||
else:
|
||||
xp = augment_crosspair_negs(train, args.win_tokens, max_new=2500)
|
||||
train = train + xp
|
||||
log(f"crosspair long negs: +{len(xp)} (total {len(train)})")
|
||||
log(f"train={len(train)} calib={len(calib)}")
|
||||
|
||||
os.makedirs(OUT_DIR, exist_ok=True)
|
||||
@ -364,6 +396,90 @@ def run(args):
|
||||
}
|
||||
log(f"CALIBRATION T={best_T:.2f} raw(ece={calib_info['ece_raw']}) "
|
||||
f"calibrated(ece={calib_info['ece_calibrated']}) auc={calib_info['auc_calibrated']}")
|
||||
|
||||
# ===== 聚合后温度 T_agg: 合成 4-doc 长文档跑滑窗 max (生产聚合口径) =====
|
||||
def agg_calib():
|
||||
rng = random.Random(7)
|
||||
pos_rows = [r for r in calib if r.get("hard_label") == 1][:150]
|
||||
neg_rows = [r for r in calib if r.get("hard_label") == 0][:150]
|
||||
neg_pool = [r for r in calib if r.get("hard_label") == 0]
|
||||
if not pos_rows or not neg_rows:
|
||||
return None
|
||||
def windows_of(row):
|
||||
docs = [row["doc_text"]] + [o["doc_text"] for o in rng.sample(neg_pool, 3)]
|
||||
rng.shuffle(docs)
|
||||
return [d[:4000] for d in docs]
|
||||
zs, ys = [], []
|
||||
B = 8
|
||||
buf_ids, buf_lab, buf_idx = [], [], []
|
||||
def flush_buf():
|
||||
if not buf_ids:
|
||||
return
|
||||
b = {
|
||||
"input_ids": torch.tensor([x[0] for x in buf_ids]).to(dev),
|
||||
"attention_mask": torch.tensor(
|
||||
[[1] * x[2] + [0] * (args.max_len - x[2]) for x in buf_ids]).to(dev),
|
||||
"marker_pos": torch.tensor([x[1] for x in buf_ids]).to(dev),
|
||||
"marker_mask": torch.tensor(
|
||||
[[True] * x[3] + [False] * (8 - x[3]) for x in buf_ids]).bool().to(dev),
|
||||
"qtype": torch.tensor([2] * len(buf_ids)).to(dev),
|
||||
}
|
||||
with torch.no_grad():
|
||||
with torch.autocast("cuda", enabled=(dev == "cuda")):
|
||||
lg, _ = dm(input_ids=b["input_ids"],
|
||||
attention_mask=b["attention_mask"],
|
||||
marker_pos=b["marker_pos"],
|
||||
marker_mask=b["marker_mask"],
|
||||
qtype=b["qtype"])
|
||||
p1 = torch.softmax(lg.float(), -1)[..., 1].tolist()
|
||||
for k in range(len(buf_ids)):
|
||||
zs[buf_idx[k]][1].append(p1[k])
|
||||
buf_ids.clear(); buf_lab.clear(); buf_idx.clear()
|
||||
# zs: list of [label, [p_win...]]
|
||||
zs = []
|
||||
for r in pos_rows + neg_rows:
|
||||
y = float(r["hard_label"])
|
||||
row_pos = len(zs)
|
||||
zs.append([y, []])
|
||||
for d in windows_of(r):
|
||||
state = f"用户问题: {r['query']}\n\n文档内容:\n{d}"
|
||||
ids, markers = build_sequence(tok, state, {"t": "noul", "ins": INSTR, "crit": None},
|
||||
args.max_len, args.head_len)
|
||||
ids = ids + [0] * (args.max_len - len(ids))
|
||||
buf_ids.append((ids, (markers + [0] * (8 - len(markers)))[:8],
|
||||
len(ids), len(markers)))
|
||||
buf_lab.append(y)
|
||||
buf_idx.append(row_pos)
|
||||
if len(buf_ids) >= B:
|
||||
flush_buf()
|
||||
flush_buf()
|
||||
max_p = [max(ps) if ps else 0.5 for _, ps in zs]
|
||||
labels = [y for y, _ in zs]
|
||||
z_agg = [math.log(max(p, 1e-6) / max(1 - p, 1e-6)) for p in max_p]
|
||||
def nll_z(zlist, T):
|
||||
tot = 0.0
|
||||
for z, y in zip(zlist, labels):
|
||||
p = 1 / (1 + math.exp(-z / T))
|
||||
p = min(max(p, 1e-6), 1 - 1e-6)
|
||||
tot -= math.log(p) if y == 1 else math.log(1 - p)
|
||||
return tot / len(zlist)
|
||||
t_best, n_best = 1.0, nll_z(z_agg, 1.0)
|
||||
T = 0.30
|
||||
while T <= 6.001:
|
||||
n = nll_z(z_agg, T)
|
||||
if n < n_best:
|
||||
t_best, n_best = T, n
|
||||
T += 0.05
|
||||
p_cal = [1 / (1 + math.exp(-z / t_best)) for z in z_agg]
|
||||
return {"n": len(labels),
|
||||
"auc_max_raw": round(bisect_auc(max_p, labels), 4),
|
||||
"ece_max_raw": round(ece(max_p, labels), 4),
|
||||
"T_agg": round(t_best, 2),
|
||||
"ece_max_calibrated": round(ece(p_cal, labels), 4)}
|
||||
agg = agg_calib()
|
||||
if agg:
|
||||
calib_info["agg"] = agg
|
||||
log(f"AGG-CALIB {agg}")
|
||||
json.dump({"best_auc": best_auc, "train_pairs": len(train),
|
||||
"steps": gstep, "epochs": args.epochs,
|
||||
"pretrain_calib": m0, "final_calib": m, "calibration": calib_info},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user