| """CHEAP PRE-SCREEN, and the honest accounting for the selection experiment. |
| |
| The coordinate share reported in the main study is obtained BY fitting g, so "diagnose, then align" |
| cannot claim to save the fit -- the diagnostic and the alignment are the same computation. That |
| would make the selection experiment vacuous, so we measure the thing that actually decides it: |
| |
| can we tell that a fork is still in the base model's frame WITHOUT doing the full fit? |
| |
| Yes. The per-layer weight-matching gain only has to be evaluated on a random SUBSET of its |
| contracted dimension to see whether its row-wise argmax is the identity: if the fork never permuted |
| anything, a few hundred columns already pin every row to itself. We screen a few layers on a few |
| hundred columns, which is seconds rather than the ~35 minutes the full 8B fit takes. |
| |
| usage: cheap_screen.py <out.json> |
| """ |
| import os, sys, json, time, gc |
| sys.path.insert(0, "/root/merge-accuracy") |
| import numpy as np, torch |
| import ma_common as C, gmap |
| from mergeschool.core import alignment as AL |
|
|
| OUT = sys.argv[1] if len(sys.argv) > 1 else "/root/merge-accuracy/results/cheap_screen.json" |
| BASE = "meta-llama/Llama-3.1-8B" |
| FORKS = json.load(open("/root/merge-accuracy/forks.json")) |
|
|
| def screen(sd_ref, sd_src, hidden_dim, n_cols=192, n_layers=None, seed=0): |
| """Fraction of sampled rows whose best match is itself, over a few sampled layers.""" |
| rng = np.random.default_rng(seed) |
| axes = AL.free_hidden_axes(sd_ref, hidden_dim) |
| |
| |
| |
| |
| pres = sorted(axes) if n_layers is None else sorted(axes)[:: max(1, len(axes) // n_layers)][:n_layers] |
| fracs = [] |
| for pre in pres: |
| ax = axes[pre] |
| cols = rng.choice(hidden_dim, size=min(n_cols, hidden_dim), replace=False) |
| gain = np.zeros((ax["f"], ax["f"]), np.float32) |
| for n in ax["in"]: |
| A = np.asarray(sd_ref[n], np.float32)[:, cols] |
| B = np.asarray(sd_src[n], np.float32)[:, cols] |
| gain += A @ B.T |
| for n in ax["out"]: |
| A = np.asarray(sd_ref[n], np.float32)[cols, :] |
| B = np.asarray(sd_src[n], np.float32)[cols, :] |
| gain += A.T @ B |
| am = np.argmax(gain, axis=1) |
| fracs.append(float(np.mean(am == np.arange(len(am))))) |
| del gain |
| |
| return float(np.min(fracs)), pres |
|
|
| mb = C.load_model(BASE, dev="cpu", dtype=torch.float32) |
| sd_base = C.sd_np(mb); HID = mb.config.hidden_size |
| NH, NKV = mb.config.num_attention_heads, mb.config.num_key_value_heads |
| del mb; gc.collect() |
| res = {} |
| for F in FORKS: |
| m = C.load_model(F["repo"], dev="cpu", dtype=torch.float32) |
| sd0 = C.sd_np(m); del m; gc.collect() |
| for tag, frac in [("real", 0.0), ("PERM0.25", 0.25), ("PERM0.5", 0.5), ("PERM1.0", 1.0)]: |
| if frac == 0.0: |
| sd = sd0 |
| name = F["name"] |
| else: |
| if F["name"] != FORKS[0]["name"]: |
| continue |
| rng = np.random.default_rng(int(frac * 1000)) |
| pres = sorted({p for p in (AL._layer_prefix(n) for n in sd0) if p}) |
| axes = AL.free_hidden_axes(sd0, HID) |
| picked = set(rng.choice(pres, size=max(1, int(round(frac * len(pres)))), |
| replace=False).tolist()) |
| hp = {p: rng.permutation(a["f"]) for p, a in axes.items() if p in picked} |
| sd = AL.apply_hidden_perms(sd0, hp, HID) |
| ap = gmap.random_gqa_head_perms(sd0, HID, NH, NKV, rng, only=picked) |
| sd = gmap.apply_gqa_head_perms(sd, ap, HID, NH, NKV) |
| name = f'{F["name"]}_{tag}' |
| t = time.time() |
| f_id, pres_used = screen(sd, sd_base, HID) |
| dt = time.time() - t |
| res[name] = {"identity_fraction_worst_layer": f_id, "screen_seconds": dt, |
| "screen_says_aligned_needed": bool(f_id < 0.95), |
| "layers_screened": len(pres_used), "cols_sampled": 256} |
| print(f"{name:28s} identity_frac={f_id:.4f} {dt:.1f}s " |
| f"-> {'ALIGN' if f_id < 0.95 else 'skip'}", flush=True) |
| if frac != 0.0: del sd; gc.collect() |
| del sd0; gc.collect() |
| json.dump(res, open(OUT, "w"), indent=1) |
| print("SCREEN_DONE", flush=True) |
|
|