Upload code/screen_sweep.py with huggingface_hub
Browse files- code/screen_sweep.py +89 -0
code/screen_sweep.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Broad ecosystem screen: how many RELEASED Llama-3.1-8B derivatives have actually left the base
|
| 2 |
+
model's coordinate frame? Streams one model at a time (download -> screen -> delete) so the disk
|
| 3 |
+
footprint stays at one checkpoint."""
|
| 4 |
+
import os, sys, json, time, gc, shutil, traceback
|
| 5 |
+
sys.path.insert(0, "/root/merge-accuracy")
|
| 6 |
+
import numpy as np, torch
|
| 7 |
+
from huggingface_hub import snapshot_download, hf_hub_download
|
| 8 |
+
import ma_common as C, gmap
|
| 9 |
+
from cheap_screen import screen
|
| 10 |
+
from mergeschool.core import alignment as AL
|
| 11 |
+
|
| 12 |
+
OUT = "/root/merge-accuracy/results/ecosystem_screen.json"
|
| 13 |
+
CACHE = "/root/hf_cache_mergeacc"
|
| 14 |
+
BASE = "meta-llama/Llama-3.1-8B"
|
| 15 |
+
CAND = [
|
| 16 |
+
("NousResearch/Hermes-3-Llama-3.1-8B", "instruct post-training", "Nous Research"),
|
| 17 |
+
("allenai/Llama-3.1-Tulu-3-8B-SFT", "instruct SFT", "AI2"),
|
| 18 |
+
("dphn/Dolphin3.0-Llama3.1-8B", "instruct post-training", "Dolphin"),
|
| 19 |
+
("meta-llama/Llama-Guard-3-8B", "safety classifier", "Meta"),
|
| 20 |
+
("fdtn-ai/Foundation-Sec-8B", "domain CPT (security)", "Foundation AI"),
|
| 21 |
+
("OpenSciLM/Llama-3.1_OpenScholar-8B", "domain CPT (science)", "OpenSciLM"),
|
| 22 |
+
("nvidia/OpenMath2-Llama3.1-8B", "domain SFT (maths)", "NVIDIA"),
|
| 23 |
+
("NCSOFT/Llama-VARCO-8B-Instruct", "language CPT (Korean)", "NCSOFT"),
|
| 24 |
+
("McGill-NLP/AfriqueLlama-8B", "language CPT (African)", "McGill NLP"),
|
| 25 |
+
("Yiddish-NLP/MameLoshnLM", "language CPT (Yiddish)", "Yiddish-NLP"),
|
| 26 |
+
("deepcogito/cogito-v1-preview-llama-8B", "instruct post-training", "Deep Cogito"),
|
| 27 |
+
("tokyotech-llm/Llama-3.1-Swallow-8B-v0.2", "language CPT (Japanese)", "TokyoTech"),
|
| 28 |
+
("aisingapore/Llama-SEA-LION-v3-8B", "language CPT (SEA)", "AI Singapore"),
|
| 29 |
+
("microsoft/UserLM-8b", "role post-training", "Microsoft"),
|
| 30 |
+
]
|
| 31 |
+
res = json.load(open(OUT)) if os.path.exists(OUT) else {}
|
| 32 |
+
|
| 33 |
+
mb = C.load_model(BASE, dev="cpu", dtype=torch.float32)
|
| 34 |
+
sd_base = C.sd_np(mb); cfg = mb.config
|
| 35 |
+
HID, NH, NKV, VOC = cfg.hidden_size, cfg.num_attention_heads, cfg.num_key_value_heads, cfg.vocab_size
|
| 36 |
+
del mb; gc.collect()
|
| 37 |
+
print(f"base ready HID={HID} NH={NH} NKV={NKV}", flush=True)
|
| 38 |
+
|
| 39 |
+
for repo, kind, group in CAND:
|
| 40 |
+
if repo in res: continue
|
| 41 |
+
d = None
|
| 42 |
+
try:
|
| 43 |
+
cfp = hf_hub_download(repo, "config.json", cache_dir=CACHE)
|
| 44 |
+
c = json.load(open(cfp))
|
| 45 |
+
if (c.get("hidden_size") != HID or c.get("num_attention_heads") != NH
|
| 46 |
+
or c.get("num_key_value_heads") != NKV or c.get("vocab_size") != VOC
|
| 47 |
+
or c.get("num_hidden_layers") != cfg.num_hidden_layers):
|
| 48 |
+
res[repo] = {"kind": kind, "group": group, "status": "shape mismatch",
|
| 49 |
+
"config": {k: c.get(k) for k in ("hidden_size", "num_attention_heads",
|
| 50 |
+
"num_key_value_heads", "vocab_size",
|
| 51 |
+
"num_hidden_layers")}}
|
| 52 |
+
print(f"SKIP {repo}: shape mismatch", flush=True)
|
| 53 |
+
json.dump(res, open(OUT, "w"), indent=1); continue
|
| 54 |
+
t0 = time.time()
|
| 55 |
+
d = snapshot_download(repo, allow_patterns=["*.safetensors", "*.json", "tokenizer*"],
|
| 56 |
+
cache_dir=CACHE, max_workers=8)
|
| 57 |
+
dl = time.time() - t0
|
| 58 |
+
m = C.load_model(repo, dev="cpu", dtype=torch.float32)
|
| 59 |
+
sd = C.sd_np(m); del m; gc.collect()
|
| 60 |
+
t = time.time(); f_id, pres = screen(sd, sd_base, HID); dt = time.time() - t
|
| 61 |
+
keys = C.shared_keys(sd_base, sd)
|
| 62 |
+
a = np.concatenate([sd_base[k].ravel() for k in keys])
|
| 63 |
+
b = np.concatenate([sd[k].ravel() for k in keys])
|
| 64 |
+
wc = float(a @ b / (np.linalg.norm(a) * np.linalg.norm(b)))
|
| 65 |
+
rd = float(np.linalg.norm(a - b) / np.linalg.norm(a))
|
| 66 |
+
del a, b, sd; gc.collect()
|
| 67 |
+
res[repo] = {"kind": kind, "group": group, "status": "screened",
|
| 68 |
+
"identity_fraction_worst_layer": f_id, "screen_seconds": dt,
|
| 69 |
+
"download_seconds": dl, "layers_screened": len(pres),
|
| 70 |
+
"weight_cosine_vs_base": wc, "rel_drift": rd,
|
| 71 |
+
"frame_has_drifted": bool(f_id < 0.95)}
|
| 72 |
+
print(f"{repo:48s} id_frac={f_id:.4f} wcos={wc:.4f} drift={rd:.4f} "
|
| 73 |
+
f"-> {'DRIFTED' if f_id < 0.95 else 'same frame'} ({dt:.0f}s)", flush=True)
|
| 74 |
+
json.dump(res, open(OUT, "w"), indent=1)
|
| 75 |
+
except Exception:
|
| 76 |
+
res[repo] = {"kind": kind, "group": group, "status": "error",
|
| 77 |
+
"error": traceback.format_exc()[-400:]}
|
| 78 |
+
print(f"ERR {repo}: {traceback.format_exc()[-250:]}", flush=True)
|
| 79 |
+
json.dump(res, open(OUT, "w"), indent=1)
|
| 80 |
+
finally:
|
| 81 |
+
# stream and delete: keep at most one candidate checkpoint on disk
|
| 82 |
+
keep = ("Llama-3.1-8B", "Llama-3.1-8B-Instruct", "Swallow-8B-v0.1",
|
| 83 |
+
"typhoon2", "sea-lionv3-base")
|
| 84 |
+
if not any(k in repo for k in keep):
|
| 85 |
+
p = f"{CACHE}/models--" + repo.replace("/", "--")
|
| 86 |
+
if os.path.isdir(p):
|
| 87 |
+
shutil.rmtree(p, ignore_errors=True)
|
| 88 |
+
gc.collect()
|
| 89 |
+
print("SWEEP_DONE", flush=True)
|