matlod commited on
Commit
6423ae1
Β·
verified Β·
1 Parent(s): 51f4daf

Add outcome and efficiency benchmarks to edit anatomy

Browse files
Files changed (1) hide show
  1. research/build_anatomy.py +39 -6
research/build_anatomy.py CHANGED
@@ -13,7 +13,8 @@ rows = json.load(open(os.path.join(OUT, "edit_anatomy.json")))
13
  ARMS = [("solve_rho0.1", "Gram solve ρ=0.1", 1, "solve_mu0_rho0.1_L21-41"),
14
  ("solve_rho1", "Gram solve ρ=1", 2, "solve_mu0_rho1_L21-41"),
15
  ("kijai_m1", "Kijai LoRA @ βˆ’1", 3, "kijai_m1"),
16
- ("kijai_m05", "Kijai LoRA @ βˆ’0.5", 7, "kijai_m05"),
 
17
  ("revert21", "Exact revert 21–41", 4, "solve_mu0_L21-41"),
18
  ("revert32", "Quality+ (revert 32–41)", 5, "qualityplus_lora"),
19
  ("revert37", "DeBand (revert 37–41)", 6, "deband_lora")]
@@ -26,9 +27,11 @@ CKPT = {"solve_rho0.1": "ckpt_rho0.1", "solve_rho1": "ckpt_rho1"} # saved check
26
 
27
  def edit(r, arm, centred=False):
28
  """The arm's numbers for one linear, or None where it does not edit."""
29
- if arm == "kijai_m05": # half strength: energies x 0.25, alignment x 0.5
30
- k = r.get("kijai_m1")
31
- return k and {key: v / (2 if key == "dot" else 4) for key, v in k.items()}
 
 
32
  if centred and arm in CKPT:
33
  return r.get(CKPT[arm])
34
  if arm in RANGES:
@@ -107,7 +110,7 @@ plain = rendered("swap055_L21-41")
107
 
108
  # chart 3: per-layer output-change size on solids vs general prompts
109
  sel = {}
110
- for key in ("solve_rho0.1", "solve_rho1", "kijai_m1"):
111
  sel[key] = {"solid": [], "gen": []}
112
  for L in glayers:
113
  rs = [by[(L, p)] for p in PROJ]
@@ -117,7 +120,37 @@ for key in ("solve_rho0.1", "solve_rho1", "kijai_m1"):
117
  sel[key]["solid"].append(ratio(sum(e[f] for e in es if e), sum(r["revert"][f] for r in rs)))
118
  sel[key]["gen"].append(ratio(sum(e[g] for e in es if e), sum(r["revert"][g] for r in rs)))
119
 
120
- data = {"arms": [[k, l, s] for k, l, s, _ in ARMS], "layers": layers, "size": size, "per_matrix": per_matrix,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  "sweep": sweep, "points": points, "plain": plain, "glayers": glayers, "sel": sel, "centred": centred}
122
  html = open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "anatomy_template.html")).read()
123
  open(os.path.join(OUT, "edit_anatomy.html"), "w").write(html.replace("__DATA__", json.dumps(data, ensure_ascii=False)))
 
13
  ARMS = [("solve_rho0.1", "Gram solve ρ=0.1", 1, "solve_mu0_rho0.1_L21-41"),
14
  ("solve_rho1", "Gram solve ρ=1", 2, "solve_mu0_rho1_L21-41"),
15
  ("kijai_m1", "Kijai LoRA @ βˆ’1", 3, "kijai_m1"),
16
+ ("strong", "DeBand Strong @1.0", 7, "strong_full"),
17
+ ("nynxz", "nynxz Destripe @1.0", 8, "nynxz_1.0"),
18
  ("revert21", "Exact revert 21–41", 4, "solve_mu0_L21-41"),
19
  ("revert32", "Quality+ (revert 32–41)", 5, "qualityplus_lora"),
20
  ("revert37", "DeBand (revert 37–41)", 6, "deband_lora")]
 
27
 
28
  def edit(r, arm, centred=False):
29
  """The arm's numbers for one linear, or None where it does not edit."""
30
+ scaled = {"kijai_m05": ("kijai_m1", 0.5), "nynxz_1.5": ("nynxz", 1.5)} # LoRA strength s: energies x s^2, alignment x s
31
+ if arm in scaled:
32
+ src, f = scaled[arm]
33
+ k = r.get(src)
34
+ return k and {key: v * (f if key == "dot" else f * f) for key, v in k.items()}
35
  if centred and arm in CKPT:
36
  return r.get(CKPT[arm])
37
  if arm in RANGES:
 
110
 
111
  # chart 3: per-layer output-change size on solids vs general prompts
112
  sel = {}
113
+ for key in ("strong", "solve_rho1", "kijai_m1", "nynxz"):
114
  sel[key] = {"solid": [], "gen": []}
115
  for L in glayers:
116
  rs = [by[(L, p)] for p in PROJ]
 
120
  sel[key]["solid"].append(ratio(sum(e[f] for e in es if e), sum(r["revert"][f] for r in rs)))
121
  sel[key]["gen"].append(ratio(sum(e[g] for e in es if e), sum(r["revert"][g] for r in rs)))
122
 
123
+ # outcome benchmarks: rendered results (scores.jsonl) against weight-change share of the whole Base-SFT change (all 42 layers)
124
+ total = sum(r["revert"]["w2"] for r in rows)
125
+
126
+
127
+ def share(key, scale=1.0):
128
+ if key == "base":
129
+ return 1.0
130
+ return scale * scale * sum(e["w2"] for r in rows for e in [edit(r, key)] if e) / total
131
+
132
+
133
+ def outcome(arm):
134
+ rs = [s for (a, _, _), s in scores.items() if a == arm]
135
+ hf = [s["hf_db"] for s in rs if s.get("hf_db") is not None]
136
+ af = [s["arcface"] for s in rs if s.get("arcface") is not None]
137
+ dd = [s["dino_dist"] for s in rs if s.get("dino_dist") is not None]
138
+ return {"clean": -statistics.median(hf), "clean_mean": -statistics.fmean(hf), "worst": -max(hf), "face": statistics.fmean(af),
139
+ "dino": statistics.fmean(dd), "n": len(hf)}
140
+
141
+
142
+ OUTCOMES = []
143
+ for label, slot, arm, key, scale in [("nynxz Destripe @1.0", 8, "nynxz_1.0", "nynxz", 1.0), ("nynxz Destripe @1.5", 8, "nynxz_1.5", "nynxz", 1.5),
144
+ ("DeBand Strong @1.0", 7, "strong_full", "strong", 1.0), ("DeBand Strong @1.2", 7, "gcsolve_mu0.3_rho0.05_x1.2_L21-41", "strong", 1.2),
145
+ ("Gram solve ρ=0.1", 1, "solve_mu0_rho0.1_L21-41", "solve_rho0.1", 1.0), ("Gram solve ρ=1", 2, "solve_mu0_rho1_L21-41", "solve_rho1", 1.0),
146
+ ("Kijai LoRA @ βˆ’1", 3, "kijai_m1", "kijai_m1", 1.0), ("Kijai LoRA @ βˆ’0.5", 3, "kijai_m05", "kijai_m1", 0.5),
147
+ ("Exact revert 21–41", 4, "solve_mu0_L21-41", "revert21", 1.0), ("Quality+ (revert 32–41)", 5, "qualityplus_lora", "revert32", 1.0),
148
+ ("DeBand (revert 37–41)", 6, "deband_lora", "revert37", 1.0), ("Base model", 0, "base", "base", 1.0)]:
149
+ o = outcome(arm)
150
+ o.update(label=label, slot=slot, share=share(key, scale))
151
+ OUTCOMES.append(o)
152
+
153
+ data = {"arms": [[k, l, s] for k, l, s, _ in ARMS], "outcomes": OUTCOMES, "layers": layers, "size": size, "per_matrix": per_matrix,
154
  "sweep": sweep, "points": points, "plain": plain, "glayers": glayers, "sel": sel, "centred": centred}
155
  html = open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "anatomy_template.html")).read()
156
  open(os.path.join(OUT, "edit_anatomy.html"), "w").write(html.replace("__DATA__", json.dumps(data, ensure_ascii=False)))