Download scripts/mk_soup3.py from Akahsizrr/alea-runs: direct link, hf CLI and curl.
- Browser
- Download file 716 Bytes
-
https://huggingface.co/datasets/Akahsizrr/alea-runs/resolve/main/scripts/mk_soup3.py
- Command line
-
hf download hf://datasets/Akahsizrr/alea-runs/scripts/mk_soup3.py
-
curl -L -o mk_soup3.py https://huggingface.co/datasets/Akahsizrr/alea-runs/resolve/main/scripts/mk_soup3.py
716 Bytes
| import torch, sys, os | |
| spec=sys.argv[1] # path:weight,path:weight,... | |
| out_path=sys.argv[2] | |
| parts=[(p,float(w)) for p,w in (x.rsplit(":",1) for x in spec.split(","))] | |
| ck=[torch.load(p,map_location="cpu") for p,_ in parts] | |
| ws=[w for _,w in parts]; s=sum(ws); ws=[w/s for w in ws] | |
| def merge(vals): | |
| a=vals[0] | |
| if isinstance(a,dict): return {k:merge([v[k] for v in vals]) for k in a} | |
| if torch.is_tensor(a): | |
| return sum(w*v.float() for w,v in zip(ws,vals)) | |
| return a | |
| out={"args":ck[0].get("args")} | |
| for sec in ["backbone","heads","aux"]: | |
| if sec in ck[0]: out[sec]=merge([c[sec] for c in ck]) | |
| os.makedirs(os.path.dirname(out_path),exist_ok=True) | |
| torch.save(out,out_path); print("saved",out_path,ws) | |