exp2-means / code /labels.py
mild-rgb's picture
code and labels for the sub-mean stage
c1e59e7 verified
Raw History Blame Contribute Delete
3.69 kB
"""Build the per-run table for luckeciano's Qwen2.5-Math-7B fine-tunes from their training logs (local, no GPU).
Reads every repo's trainer_state.json (never training_args.bin: it is a pickle, so loading it could run code).
Writes meta/runs.csv and meta/logs/<repo>.json (raw logs, for later checks).
"""
import os, re, json, hashlib, csv, time
from pathlib import Path
from concurrent.futures import ThreadPoolExecutor
import requests
HERE = Path(__file__).parent
OUT = HERE / "meta"
(OUT / "logs").mkdir(parents=True, exist_ok=True)
TOK = open(os.path.expanduser("~/.cache/huggingface/token")).read().strip()
H = {"Authorization": f"Bearer {TOK}"}
S = requests.Session()
lst = S.get("https://huggingface.co/api/models", headers=H, params={
"author": "luckeciano", "filter": "base_model:finetune:Qwen/Qwen2.5-Math-7B", "limit": 1000, "full": "true"}).json()
json.dump(lst, open(OUT / "model_list.json", "w"))
print("repos", len(lst))
def get(m):
rid = m["id"]
p = OUT / "logs" / (rid.split("/")[1] + ".json")
if not p.exists():
for k in range(6):
r = S.get(f"https://huggingface.co/{rid}/resolve/{m['sha']}/trainer_state.json", headers=H)
if r.status_code == 200:
p.write_bytes(r.content)
break
time.sleep(5 * (k + 1))
else:
return rid, None
return rid, json.loads(p.read_text())
with ThreadPoolExecutor(8) as ex:
states = dict(ex.map(get, lst))
def mean(xs):
xs = [x for x in xs if x is not None]
return sum(xs) / len(xs) if xs else None
rows = []
for m in lst:
rid, st = m["id"], states[m["id"]]
name = rid.split("/")[1]
seed = re.search(r"_(\d+)$", name)
row = dict(repo=rid, sha=m["sha"], created=m["createdAt"][:10], recipe=re.sub(r"_\d+$", "", name),
seed=seed.group(1) if seed else "", ok_log=st is not None)
if st:
h = [x for x in st["log_history"] if "accuracy_reward" in x]
acc = [x["accuracy_reward"] for x in h]
ln = [x.get("completion_length") for x in h]
lmax = [x.get("completion_length/max") for x in h if x.get("completion_length/max") is not None]
roll = [mean(acc[i:i + 10]) for i in range(max(1, len(acc) - 9))]
row.update(max_steps=st.get("max_steps"), global_step=st.get("global_step"), n_steps_logged=len(h),
batch=st.get("train_batch_size"),
lr_first=h[0].get("learning_rate") if h else None,
lr_max=max((x.get("learning_rate") or 0) for x in h) if h else None,
acc_first10=mean(acc[:10]), acc_last10=mean(acc[-10:]), acc_peak10=max(roll) if acc else None,
acc_last=acc[-1] if acc else None,
len_first10=mean(ln[:10]), len_last10=mean(ln[-10:]), len_last=ln[-1] if ln else None,
len_cap_seen=max(lmax) if lmax else None,
log_hash=hashlib.sha1(json.dumps(st["log_history"], sort_keys=True).encode()).hexdigest()[:12])
rows.append(row)
keys = sorted(set(k for r in rows for k in r), key=lambda k: list(rows[0]).index(k) if k in rows[0] else 99)
keys = list(dict.fromkeys(["repo", "sha", "created", "recipe", "seed", "ok_log", "max_steps", "global_step",
"n_steps_logged", "batch", "lr_first", "lr_max", "acc_first10", "acc_last10", "acc_peak10",
"acc_last", "len_first10", "len_last10", "len_last", "len_cap_seen", "log_hash"]))
with open(OUT / "runs.csv", "w", newline="") as f:
w = csv.DictWriter(f, keys)
w.writeheader()
w.writerows(rows)
print("wrote", len(rows), "missing logs", sum(not r["ok_log"] for r in rows))