Datasets:
Download scripts/small_merges.py from Cross-Mergeability/crossarch-accuracy: direct link, hf CLI and curl.
- Browser
- Download file 4.57 kB
-
https://huggingface.co/datasets/Cross-Mergeability/crossarch-accuracy/resolve/main/scripts/small_merges.py
- Command line
-
hf download hf://datasets/Cross-Mergeability/crossarch-accuracy/scripts/small_merges.py
-
curl -L -o small_merges.py https://huggingface.co/datasets/Cross-Mergeability/crossarch-accuracy/resolve/main/scripts/small_merges.py
4.57 kB
| """Table 2 — the small merged models, scored on the same suite with chance levels attached. | |
| Each repo holds `{operator}__{rung}` subfolders (rungs: naive / aligned / transport). We score | |
| the `average` operator on every rung present, plus the parents where the pair is unambiguous. | |
| At 14M-350M these are expected to sit at chance; the point of the table is to say so with | |
| numbers next to the chance level rather than to leave the manuscript's "no small merged model | |
| is yet usable" sentence without accuracy evidence. | |
| """ | |
| import os, sys, json, gc, time, fcntl | |
| sys.path.insert(0, "/root/crossarch-accuracy/scripts") | |
| import torch, bench | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| from huggingface_hub import HfApi | |
| ROOT = "/root/crossarch-accuracy" | |
| LEDGER = f"{ROOT}/results/small_merges.jsonl" | |
| TASKS = ["arc_easy", "sciq", "piqa", "lambada"] | |
| REPOS = ["Mergeability/goldfish-en-nld_latn", "Mergeability/goldfish-en-deu_latn", | |
| "Mergeability/goldfish-en-fra_latn", "Mergeability/goldfish-en-spa_latn", | |
| "Mergeability/goldfish-en-pol_latn", "Mergeability/pythia-en-zh-14m", | |
| "Mergeability/polypythia-14m-s1s2", "Mergeability/codegen-mono-multi"] | |
| # parents that are unambiguous from the pair name; left empty where they are not | |
| PARENTS = { | |
| "Mergeability/polypythia-14m-s1s2": ["EleutherAI/pythia-14m-seed1", "EleutherAI/pythia-14m-seed2"], | |
| "Mergeability/codegen-mono-multi": ["Salesforce/codegen-350M-mono", "Salesforce/codegen-350M-multi"], | |
| "Mergeability/pythia-en-zh-14m": ["EleutherAI/pythia-14m"], | |
| } | |
| def done(): | |
| if not os.path.exists(LEDGER): return set() | |
| return {json.loads(l)["key"] for l in open(LEDGER) if l.strip()} | |
| def append(row): | |
| with open(LEDGER, "a") as f: | |
| fcntl.flock(f, fcntl.LOCK_EX); f.write(json.dumps(row) + "\n"); f.flush() | |
| fcntl.flock(f, fcntl.LOCK_UN) | |
| def score(repo, sub, dev, key, meta): | |
| kw = dict(subfolder=sub) if sub else {} | |
| tok = AutoTokenizer.from_pretrained(repo, **kw) | |
| if tok.pad_token is None: tok.pad_token = tok.eos_token | |
| m = AutoModelForCausalLM.from_pretrained(repo, dtype=torch.float16, | |
| low_cpu_mem_usage=True, **kw).to(dev).eval() | |
| # several of these small checkpoints have max_position_embeddings well below 1024; | |
| # exceeding it is a device-side assert that poisons the CUDA context for every later job | |
| mp = getattr(m.config, "max_position_embeddings", None) or getattr(m.config, "n_positions", 1024) | |
| ml = max(64, min(1024, int(mp) - 1)) | |
| res = {} | |
| for t in TASKS: | |
| res[t] = bench.score_task(m, tok, t, dev, batch_size=64, max_len=ml, token_budget=4096) | |
| res["_max_len"] = ml | |
| n_par = sum(p.numel() for p in m.parameters()) | |
| del m; gc.collect(); torch.cuda.empty_cache() | |
| row = dict(key=key, repo=repo, variant=sub or "(root)", n_params=n_par, results=res, **meta) | |
| row["max_len"] = res.pop("_max_len", None) | |
| row["acc_macro"] = sum(res[t]["acc"] for t in TASKS) / len(TASKS) | |
| row["chance_macro"] = sum(bench.TASKS[t]["chance"] for t in TASKS) / len(TASKS) | |
| append(row); return row | |
| def main(): | |
| dev = "cuda"; api = HfApi(); have = done() | |
| jobs = [] | |
| for r in REPOS: | |
| try: fs = api.list_repo_files(r) | |
| except Exception as e: print("ERR list", r, repr(e)[:120], flush=True); continue | |
| subs = sorted({f.split("/")[0] for f in fs if "/" in f and f.startswith("average__")}) | |
| for s in subs: | |
| jobs.append((r, s, dict(kind="merge", operator="average", rung=s.split("__")[1]))) | |
| for p in PARENTS.get(r, []): | |
| jobs.append((p, "", dict(kind="parent", of=r))) | |
| for repo, sub, meta in jobs: | |
| key = f"{repo}|{sub or '(root)'}" | |
| if key in have: continue | |
| t0 = time.time() | |
| try: | |
| import multiprocessing as mp_ | |
| q = mp_.get_context("spawn") | |
| pr = q.Process(target=score, args=(repo, sub, dev, key, meta)) | |
| pr.start(); pr.join(1200) | |
| if pr.exitcode != 0: raise RuntimeError(f"child exit {pr.exitcode}") | |
| row = [json.loads(l) for l in open(LEDGER) if json.loads(l)["key"] == key][-1] | |
| print("OK", key, round(row["acc_macro"], 4), "chance", row["chance_macro"], | |
| f"{time.time()-t0:.0f}s", flush=True) | |
| except Exception as e: | |
| print("FAIL", key, repr(e)[:200], flush=True) | |
| print("SMALLDONE", flush=True) | |
| if __name__ == "__main__": | |
| import multiprocessing as _mp | |
| _mp.set_start_method("spawn", force=True) | |
| main() | |