Upload scripts/temp_cal.py with huggingface_hub
Browse files- scripts/temp_cal.py +56 -0
scripts/temp_cal.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json, math, random, torch, sys
|
| 2 |
+
sys.path.insert(0,"/root/alea")
|
| 3 |
+
from alea.eval import load_model, predict_all
|
| 4 |
+
from transformers import AutoTokenizer
|
| 5 |
+
ckpt=sys.argv[1]; data=sys.argv[2]; out=sys.argv[3]; n=int(sys.argv[4]) if len(sys.argv)>4 else 4000
|
| 6 |
+
dev=torch.device("cuda")
|
| 7 |
+
tok=AutoTokenizer.from_pretrained("Qwen/Qwen3-0.6B")
|
| 8 |
+
model=load_model(ckpt,"Qwen/Qwen3-0.6B",dev)
|
| 9 |
+
rows=[json.loads(l) for l in open(data)]
|
| 10 |
+
random.seed(0); random.shuffle(rows); rows=rows[:n]
|
| 11 |
+
preds=predict_all(model,tok,rows,dev,max_len=4096)
|
| 12 |
+
# collect (probs, target) per type
|
| 13 |
+
ch=[]; sc=[]; nu=[]
|
| 14 |
+
for r in rows:
|
| 15 |
+
pr=preds.get(r["id"],{})
|
| 16 |
+
for qid,spec in r["questions"].items():
|
| 17 |
+
t=r["targets"].get(qid); a=pr.get(qid)
|
| 18 |
+
if t is None or a is None: continue
|
| 19 |
+
if spec["type"]=="choice" and a.get("type")=="choice":
|
| 20 |
+
ch.append((torch.tensor(a["probs"],dtype=torch.float64),torch.tensor(t,dtype=torch.float64)))
|
| 21 |
+
elif spec["type"]=="score" and a.get("type")=="choice" :
|
| 22 |
+
sc.append((torch.tensor(a["probs"],dtype=torch.float64),torch.tensor(t,dtype=torch.float64)))
|
| 23 |
+
elif spec["type"]=="noul" and a.get("type")=="noul":
|
| 24 |
+
nu.append((a["p"],float(t)))
|
| 25 |
+
def scale_probs(p,tau):
|
| 26 |
+
q=torch.log(p.clamp_min(1e-12))/tau
|
| 27 |
+
return torch.softmax(q,-1)
|
| 28 |
+
def brier_list(items,tau):
|
| 29 |
+
s=0
|
| 30 |
+
for p,y in items:
|
| 31 |
+
q=scale_probs(p,tau); s+=((q-y)**2).sum().item()
|
| 32 |
+
return s/len(items)
|
| 33 |
+
def noul_brier(items,tau):
|
| 34 |
+
s=0
|
| 35 |
+
for p,y in items:
|
| 36 |
+
lp=math.log(max(p,1e-9)/(1-max(p,1e-9)))/tau
|
| 37 |
+
q=1/(1+math.exp(-lp)); s+=(q-y)**2
|
| 38 |
+
return s/len(items)
|
| 39 |
+
best={}
|
| 40 |
+
for name,items,fn in [("choice",ch,brier_list),("score",sc,brier_list),("noul",nu,noul_brier)]:
|
| 41 |
+
if not items: best[name]=(1.0,0.0); continue
|
| 42 |
+
cands=[x/20 for x in range(10,81)]
|
| 43 |
+
scores=[(t,fn(items,t)) for t in cands]
|
| 44 |
+
best[name]=min(scores,key=lambda z:z[1])
|
| 45 |
+
base=fn(items,1.0)
|
| 46 |
+
print(f"{name}: n={len(items)} base_brier={base:.4f} tau={best[name][0]:.2f} brier={best[name][1]:.4f}")
|
| 47 |
+
# bake
|
| 48 |
+
c=torch.load(ckpt,map_location="cpu")
|
| 49 |
+
tc=best["choice"][0]; ts=best["score"][0]; tn=best["noul"][0]
|
| 50 |
+
for k in ["out.weight","out.bias"]:
|
| 51 |
+
c["heads"]["choice"][k]=c["heads"]["choice"][k]/tc
|
| 52 |
+
c["heads"]["score"][k]=c["heads"]["score"][k]/ts
|
| 53 |
+
for k in ["net.3.weight","net.3.bias"]:
|
| 54 |
+
c["heads"]["noul"][k]=c["heads"]["noul"][k]/tn
|
| 55 |
+
c["temp_cal"]={"choice":tc,"score":ts,"noul":tn,"fit_data":data,"n":n}
|
| 56 |
+
torch.save(c,out); print("saved",out)
|