174 lines
7.4 KiB
Python
174 lines
7.4 KiB
Python
#!/usr/bin/env python3
|
|
"""run_training.py — 一体化: 安装依赖 + 下载模型 + 训练 + 评估
|
|
用法: python3 run_training.py
|
|
所有数据通过 gitea clone 获取 (/tmp/jd)。
|
|
"""
|
|
import subprocess, sys, os, json
|
|
|
|
def pip_install(*pkgs):
|
|
r = subprocess.run([sys.executable, "-m", "pip", "install", "-q"] + list(pkgs), capture_output=True, text=True)
|
|
if r.returncode != 0:
|
|
r = subprocess.run([sys.executable, "-m", "pip", "install", "-q", "-i",
|
|
"https://pypi.tuna.tsinghua.edu.cn/simple"] + list(pkgs), capture_output=True, text=True)
|
|
return r.returncode == 0
|
|
|
|
# Step 1: 安装依赖
|
|
print("=== Step 1: Install deps ===", flush=True)
|
|
deps = ["transformers==4.57.6", "tokenizers>=0.21", "peft", "sentencepiece",
|
|
"accelerate", "datasets", "safetensors", "huggingface_hub", "laya"]
|
|
if not pip_install(*deps):
|
|
pip_install(*deps)
|
|
r = subprocess.run([sys.executable, "-m", "pip", "install", "-q", "--no-deps", "torchaudio"], capture_output=True)
|
|
import torch
|
|
print(f" torch {torch.__version__}, cuda={torch.cuda.is_available()}", flush=True)
|
|
|
|
# 验证 laya
|
|
try:
|
|
from laya.common import build_sequence
|
|
print(" laya.common.build_sequence OK", flush=True)
|
|
except Exception as e:
|
|
print(f" laya FAIL: {e}", flush=True)
|
|
sys.exit(1)
|
|
|
|
# Step 2: 下载 mmBERT (ModelScope)
|
|
print("=== Step 2: Download mmBERT from ModelScope ===", flush=True)
|
|
import urllib.request
|
|
MS_BASE = "https://www.modelscope.cn/models/jhu-clsp/mmBERT-base/resolve/main"
|
|
os.makedirs("/tmp/mmbert", exist_ok=True)
|
|
for fn in ["config.json", "tokenizer.json", "tokenizer_config.json", "special_tokens_map.json", "pytorch_model.bin"]:
|
|
dst = os.path.join("/tmp/mmbert", fn)
|
|
if os.path.exists(dst) and os.path.getsize(dst) > 100:
|
|
continue
|
|
with urllib.request.urlopen(f"{MS_BASE}/{fn}", timeout=120) as r, open(dst + ".tmp", "wb") as f:
|
|
total = 0
|
|
while True:
|
|
b = r.read(8*1024*1024)
|
|
if not b: break
|
|
f.write(b); total += len(b)
|
|
os.replace(dst + ".tmp", dst)
|
|
print(f" {fn}: {total//1024//1024}MB", flush=True)
|
|
|
|
# Step 3: 训练
|
|
print("=== Step 3: Training ===", flush=True)
|
|
import torch.nn as nn
|
|
import torch.nn.functional as F
|
|
|
|
# 构建 DecisionModel
|
|
sys.path.insert(0, "/tmp/jd") # repo root (for any imports)
|
|
from laya.common import DecisionModel, build_sequence
|
|
|
|
class NoulDS(Dataset):
|
|
def __init__(self, rows, tok, max_len=512):
|
|
self.rows, self.tok, self.max_len = rows, tok, max_len
|
|
self.q = {"t": "noul", "ins": INSTR, "crit": None}
|
|
def __len__(self): return len(self.rows)
|
|
def __getitem__(self, i):
|
|
r = self.rows[i]
|
|
state = f"用户问题: {r['query']}\n\n文档内容:\n{r['doc_text'][:2500]}"
|
|
ids, markers = build_sequence(self.tok, state, self.q, self.max_len, 192)
|
|
pad = self.max_len - len(ids)
|
|
mm = [True]*len(markers) + [False]*(8-len(markers))
|
|
return {"input_ids": torch.tensor(ids + [0]*pad),
|
|
"attention_mask": torch.tensor([1]*len(ids) + [0]*pad),
|
|
"marker_pos": torch.tensor(markers + [0]*(8-len(markers))),
|
|
"marker_mask": torch.tensor(mm),
|
|
"label": torch.tensor(float(r["hard_label"])),
|
|
"soft": torch.tensor(float(r.get("soft_label") or r["hard_label"]))}
|
|
|
|
def collate_noul(batch):
|
|
keys = batch[0].keys()
|
|
return {k: torch.stack([b[k] for b in batch]) for k in keys}
|
|
|
|
def ece(probs, labels):
|
|
bins = [[] for _ in range(15)]
|
|
for p, y in zip(probs, labels):
|
|
bins[min(int(p*15), 14)].append((p, y))
|
|
e = 0.0
|
|
for b in bins:
|
|
if not b: continue
|
|
e += len(b)/len(probs) * abs(sum(p for p,_ in b)/len(b) - sum(y for _,y in b)/len(b))
|
|
return e
|
|
|
|
import bisect
|
|
|
|
INSTR = "Does this document contain the information needed to answer the user's question? Answer based only on the document content."
|
|
|
|
def evaluate(dm, loader, dev):
|
|
dm.eval()
|
|
probs, labels = [], []
|
|
with torch.no_grad():
|
|
for b in loader:
|
|
logits, _ = dm(input_ids=b["input_ids"].to(dev), attention_mask=b["attention_mask"].to(dev),
|
|
marker_pos=b["marker_pos"].to(dev), marker_mask=b["marker_mask"].bool().to(dev),
|
|
qtype=b["qtype"].to(dev))
|
|
p = torch.softmax(logits.float(), -1)[..., 1]
|
|
probs += p.tolist(); labels += b["label"].tolist()
|
|
dm.train()
|
|
pos = sorted(p for p, y in zip(probs, labels) if y == 1)
|
|
neg = sorted(p for p, y in zip(probs, labels) if y == 0)
|
|
auc = sum(bisect.bisect_left(neg, p) for p in pos) / (len(pos)*len(neg)) if pos and neg else 0
|
|
return {"auc": round(auc, 4), "ece": round(ece(probs, labels), 4), "n": len(labels)}
|
|
|
|
# 配置
|
|
dev = "cuda" if torch.cuda.is_available() else "cpu"
|
|
data_dir = "/tmp/jd"
|
|
model_dir = "/tmp/mmbert"
|
|
output_dir = "/tmp/laya_out"
|
|
epochs = 2
|
|
bs = 16
|
|
lr = 1e-4
|
|
os.makedirs(output_dir, exist_ok=True)
|
|
|
|
tok = AutoTokenizer.from_pretrained(model_dir)
|
|
enc_cfg = AutoConfig.from_pretrained(model_dir)
|
|
enc = AutoModel.from_config(enc_cfg)
|
|
dm = DecisionModel(enc, head_layers=2, n_act=2)
|
|
lora = LoraConfig(r=16, lora_alpha=32, lora_dropout=0.05, target_modules=["Wqkv", "Wo"], task_type=None)
|
|
dm.encoder = get_peft_model(dm.encoder, lora)
|
|
dm.to(dev); dm.train()
|
|
n_train = sum(p.numel() for p in dm.parameters() if p.requires_grad)
|
|
print(f"model on {dev}, trainable {n_train:,}", flush=True)
|
|
|
|
train = [r for r in load_jsonl(os.path.join(data_dir, "train.jsonl")) if r.get("hard_label") in (0, 1)]
|
|
calib = load_jsonl(os.path.join(data_dir, "calibration.jsonl"))
|
|
print(f"train={len(train)} calib={len(calib)}", flush=True)
|
|
|
|
dl = DataLoader(NoulDS(train, tok), batch_size=bs, shuffle=True, num_workers=2, drop_last=True, collate_fn=collate_noul)
|
|
dl_cal = DataLoader(NoulDS(calib, tok), batch_size=32, collate_fn=collate_noul)
|
|
|
|
steps = len(dl) * epochs
|
|
opt = torch.optim.AdamW([p for p in dm.parameters() if p.requires_grad], lr=lr, weight_decay=0.01)
|
|
from torch.optim.lr_scheduler import CosineAnnealingLR
|
|
sch = CosineAnnealingLR(opt, T_max=steps)
|
|
best_auc = 0.0
|
|
gstep = 0
|
|
for ep in range(epochs):
|
|
dm.train()
|
|
for b in dl:
|
|
with torch.autocast("cuda", enabled=(dev=="cuda")):
|
|
logits, act = dm(input_ids=b["input_ids"].to(dev), attention_mask=b["attention_mask"].to(dev),
|
|
marker_pos=b["marker_pos"].to(dev), marker_mask=b["marker_mask"].bool().to(dev),
|
|
qtype=b["qtype"].to(dev))
|
|
true_logit = logits[..., 1].float()
|
|
hard = b["label"].to(dev); soft = b["soft"].to(dev)
|
|
loss = F.binary_cross_entropy_with_logits(true_logit, hard) + 0.5*F.mse_loss(torch.sigmoid(true_logit), soft)
|
|
opt.zero_grad(); loss.backward()
|
|
torch.nn.utils.clip_grad_norm_([p for p in dm.parameters() if p.requires_grad], 1.0)
|
|
opt.step(); sch.step()
|
|
gstep += 1
|
|
if gstep % 100 == 0:
|
|
print(f"[jev] ep{ep} step{gstep}/{steps} loss={loss.item():.4f}", flush=True)
|
|
auc = evaluate(dm, dl_cal, dev)
|
|
print(f"[jev] ep{ep} calib {auc}", flush=True)
|
|
if auc["auc"] > best_auc:
|
|
best_auc = auc["auc"]
|
|
torch.save(dm.state_dict(), os.path.join(output_dir, "model.pt"))
|
|
print(f"[jev] TRAINING-DONE best_auc={best_auc:.4f}", flush=True)
|
|
|
|
# 回传日志
|
|
log_src = "/tmp/laya_out/training_log.json"
|
|
json.dump({"best_auc": best_auc, "train_pairs": len(train), "steps": gstep},
|
|
open(log_src, "w"), ensure_ascii=False, indent=1)
|
|
print("ALL-DONE", flush=True)
|
|
PYEOF
|
|
echo "run_training.py written" |