Skip to content

Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.

26.9 KB
Raw Blame
#!/usr/bin/env python3
"""search_ablit5.py -- ablit5 follow-up: push refusals 27% -> <=5% at KL <= 0.02.

Same objective and eval sets as ablit4 (100 held-out harmful greedy-64
regex+soft refusals; KL over last<=33 next-token distributions of 100 held-out
harmless; one model load, hooks retargeted per trial). Differences:

  space:  widened kernels -- max_w in [0.8, 3.0], peak in [0.3*LAST, LAST],
          width in [1.0, LAST], separate attn/mlp kept.
  warm:   top-5 ablit4 trials (t8,t23,t6,t14,t29 by rate) enqueued per arm.
  arm A:  projected direction (grimjim): v=interp(R,29.34) with the
          harmless-mean component removed, renormalised; single dir, all layers.
  arm B:  k-dim refusal subspace (k in {2,3}) from PCA of per-prompt
          harmful-minus-harmless-mean diffs at L29; ablate the subspace.
  arm C:  per-layer own direction vs single interpolated direction
          (mode in {perlayer, interp}) in the widened space.

Each arm runs max 12 trials; the run stops as soon as any trial reaches
rate <= 0.05 at KL <= 0.02. Per-trial refusal records (with texts) are saved
so any detector change can re-score offline. Then 2 candidates -> full
validation (held-out refusals, KL200, XSTest-safe over-refusal, 20q probe)
and export JSON+safetensors edit spec (ablit4 format + arm/direction fields).
"""
import argparse
import json
import os
import sys
import time

ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.insert(0, ROOT)
sys.path.insert(0, os.path.join(ROOT, "tools", "ablit"))
import arch as arch_mod  # noqa: E402

N_LAYERS = 45
LAST = N_LAYERS - 1
PCA_LAYER = 29
PROJ_F = 29.344821947743544  # t8 interp index (ablit4 winner)
TARGET_RATE = 0.05
TARGET_KL = 0.01  # lead 28/09: ablation KL counts in the quant quality budget,
# so candidates must clear KL <= 0.01 (tighter than the 0.02 brief bar)

# top-5 ablit4 trials by (rate, kl): kernels only (direction is per-arm fixed
# except arm C which also enqueues mode/dir_index).
WARM = [
    {"src": "t8", "mode": "interp", "dir_index": 29.344821947743544,
     "attn": {"max_w": 0.90410878760384, "peak": 32.8161358857333,
              "min_frac": 0.8657749159440993, "width": 9.897244355730098},
     "mlp": {"max_w": 0.9323180316193085, "peak": 34.71813504131761,
             "min_frac": 0.39278117482212993, "width": 16.720820136759976}},
    {"src": "t23", "mode": "interp", "dir_index": 32.25872691361161,
     "attn": {"max_w": 0.8709209854681796, "peak": 32.490598118225535,
              "min_frac": 0.9925267870370555, "width": 7.99116216800204},
     "mlp": {"max_w": 0.870699395759739, "peak": 34.565164597710094,
             "min_frac": 0.2696226851340563, "width": 21.4256809263957}},
    {"src": "t6", "mode": "perlayer", "dir_index": 26.279730722043695,
     "attn": {"max_w": 0.9718175960081715, "peak": 39.29364469518343,
              "min_frac": 0.8946512923167388, "width": 14.074242861193884},
     "mlp": {"max_w": 1.2224944572521446, "peak": 27.545193497937408,
             "min_frac": 0.5400747296428063, "width": 4.281344305229117}},
    {"src": "t14", "mode": "interp", "dir_index": 40.46255506122455,
     "attn": {"max_w": 0.9280197494892991, "peak": 34.82936992116434,
              "min_frac": 0.5374835737021016, "width": 2.435473765709072},
     "mlp": {"max_w": 1.0290670674348752, "peak": 35.12520198217241,
             "min_frac": 0.3352860160083479, "width": 25.03361340830888}},
    {"src": "t29", "mode": "interp", "dir_index": 32.686653697864834,
     "attn": {"max_w": 1.111921728364551, "peak": 31.07750806645295,
              "min_frac": 0.2669439125635532, "width": 6.731610947235611},
     "mlp": {"max_w": 0.96968859748264, "peak": 36.42624430484473,
             "min_frac": 0.13046857517665492, "width": 13.070057649133133}},
]

# t8 full params (for the t8-texts dump / step-1 hand read on GPU).
T8 = WARM[0]

for _w in WARM:  # builders read min_w; warm dicts store min_frac
    for _c in ("attn", "mlp"):
        _w[_c]["min_w"] = _w[_c]["min_frac"] * _w[_c]["max_w"]


UNI = {"max_w": 1, "peak": 33, "min_w": 1, "width": 44}  # uniform-efficacy gate kernel
ARCH = arch_mod.get("glm")


def configure(arch):
    """Re-target the GLM-tuned constants to another architecture (relative depth).
    GLM (scale 1.0) leaves every constant bit-identical."""
    global N_LAYERS, LAST, PCA_LAYER, PROJ_F, ARCH, UNI
    ARCH = arch
    N_LAYERS = arch["n_layers"]
    LAST = N_LAYERS - 1
    PCA_LAYER = arch["pca_layer"]
    PROJ_F = arch["proj_f"]
    s = arch["scale"]
    if s != 1.0:
        for w in WARM:
            w["dir_index"] *= s
            for c in ("attn", "mlp"):
                w[c]["peak"] *= s
                w[c]["width"] *= s
        UNI = {"max_w": 1, "peak": 33 * s, "min_w": 1, "width": 44 * s}


def log(*a):
    print(time.strftime("%H:%M:%S"), *a, flush=True)


def check_guards():
    avail = -1
    try:
        with open("/proc/meminfo") as f:
            for line in f:
                if line.startswith("MemAvailable:"):
                    avail = int(line.split()[1]) / 1048576
    except Exception:
        pass
    if avail >= 0 and avail < 10:
        raise SystemExit(f"ABORT: only {avail:.1f} GB RAM available (< 10 GB)")
    assert os.environ.get("EXL3_BLOCK_GRAPH", "0") != "1", "EXL3_BLOCK_GRAPH must be off"
    assert os.environ.get("EXL3_BLOCK_GRAPH_VERIFY", "0") != "1", "block-graph verify must be off"


def sample_space(trial, arm):
    p = {}
    if arm == "C":
        p["mode"] = trial.suggest_categorical("mode", ["perlayer", "interp"])
        p["dir_index"] = trial.suggest_float("dir_index", 0.0, float(LAST))
    if arm == "B":
        p["k"] = trial.suggest_categorical("k", [2, 3])
    for c in ("attn", "mlp"):
        max_w = trial.suggest_float(f"{c}.max_w", 0.8, 3.0)
        peak = trial.suggest_float(f"{c}.peak", 0.3 * LAST, float(LAST))
        min_frac = trial.suggest_float(f"{c}.min_frac", 0.0, 1.0)
        width = trial.suggest_float(f"{c}.width", 1.0, float(LAST))
        p[c] = {"max_w": max_w, "peak": peak,
                "min_w": min_frac * max_w, "width": width, "min_frac": min_frac}
    return p


def enqueue_warm(study, arm):
    for w in WARM:
        d = {}
        if arm == "C":
            d["mode"] = w["mode"]
            d["dir_index"] = w["dir_index"]
        if arm == "B":
            d["k"] = 2
        for c in ("attn", "mlp"):
            for k in ("max_w", "peak", "min_frac", "width"):
                d[f"{c}.{k}"] = w[c][k]
        study.enqueue_trial(d)
    log(f"arm {arm}: enqueued {len(WARM)} warm-start trials (ablit4 top-5)")


def collect_layer_residuals(model, tok, hooks, prompts, layer, max_tokens=160,
                            attn_mode="flash_attn_nc"):
    """Per-prompt last-token residual after block `layer` (hooks disabled).
    Returns (n, hidden) float64 array."""
    import torch
    import numpy as np
    NL = N_LAYERS
    blk = [b for b in hooks.blocks if b.layer_idx == layer][0]
    assert 0 <= layer < NL
    orig_fwd = blk.forward
    store = {}

    def rec_fwd(x, params, out_dtype=None):
        out = orig_fwd(x, params, out_dtype)
        if store.get("on"):
            store["y"] = out.detach().cpu()
        return out

    blk.forward = rec_fwd
    vecs = []
    was = hooks.enabled
    hooks.enabled = False
    store["on"] = True
    try:
        with torch.inference_mode():
            for p in prompts:
                from eval_ablit import chat_wrap
                ids = tok.encode(chat_wrap(p["text"]), encode_special_tokens=True)
                ids = ids[:, -max_tokens:]
                model.forward(ids, {"attn_mode": attn_mode})
                y = store["y"]
                if y.dim() == 4:  # [1, seq, hc, hidden]
                    v = y[0, -1].mean(dim=0)
                elif y.dim() == 3:  # [1, seq, hidden]
                    v = y[0, -1]
                elif y.dim() == 2:  # [seq, hidden]
                    v = y[-1]
                else:
                    v = y.reshape(-1, y.shape[-1])[-1]
                vecs.append(v.float().numpy().astype(np.float64))
                del store["y"]
    finally:
        store["on"] = False
        blk.forward = orig_fwd
        hooks.enabled = was
    return np.stack(vecs)


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--model", default=os.path.expanduser("~/models/glm53-exl3-td205"))
    ap.add_argument("--arch", default="auto", choices=["auto"] + list(arch_mod.ARCHS))
    ap.add_argument("--dirs", default=None)
    ap.add_argument("--prompts", default=None)
    ap.add_argument("--out", default="./ablit5")
    ap.add_argument("--trials-per-arm", type=int, default=12)
    ap.add_argument("--seed", type=int, default=5)
    ap.add_argument("--batch", type=int, default=16)
    ap.add_argument("--max-new-tokens", type=int, default=64)
    ap.add_argument("--arms", default="A,B,C")
    a = ap.parse_args()
    arms = [s.strip().upper() for s in a.arms.split(",") if s.strip()]
    assert all(x in "ABC" for x in arms), arms
    configure(arch_mod.get(a.arch, a.model))

    check_guards()
    try:
        import optuna
        from optuna.samplers import TPESampler
        use_optuna = True
        log(f"optuna {optuna.__version__} MOTPE, {a.trials_per_arm} trials/arm")
    except ImportError:
        use_optuna = False
        log("no optuna: seeded random fallback")

    from hook_ablate import (build_edit, build_single_edit, build_subspace_edit,
                             projected_direction, pca_subspace,
                             AblationHooks)
    NL = N_LAYERS
    import eval_ablit as E
    E.set_arch(None if ARCH["name"] == "glm" else ARCH)

    import prompt_cache as PC
    pdir = a.prompts or PC.ensure(os.path.join(ROOT, "scratch", "ablit", "prompts"))
    ddir = a.dirs or os.path.join(ROOT, "scratch", "ablit", "dirs")
    out = a.out
    os.makedirs(out, exist_ok=True)

    import torch
    from exllamav3 import Config, Model, Cache, Tokenizer, Generator

    torch.set_grad_enabled(False)
    from safetensors.numpy import load_file
    st = load_file(os.path.join(ddir, "directions.safetensors"))
    import numpy as np
    R = np.stack([np.asarray(st[f"L{i:02d}.r"], dtype=np.float64) for i in range(NL)])
    MU_H = np.asarray(st[f"L{PCA_LAYER:02d}.mean_safe"], dtype=np.float64)

    harm100 = E.load_prompts(os.path.join(pdir, "harmful_heldout.jsonl"), 100)
    safe100 = E.load_prompts(os.path.join(pdir, "harmless_heldout.jsonl"), 100)
    assert len(harm100) == 100 and len(safe100) == 100, (len(harm100), len(safe100))
    log(f"prompts: harm100 + safe100 (same sets as ablit4); dirs {R.shape}")

    t0 = time.time()
    config = Config.from_directory(a.model)
    model = Model.from_config(config)
    tok = Tokenizer.from_config(config)
    stop_ids = config.eos_token_id_list
    max_depth = a.batch * (256 + a.max_new_tokens + 8)
    cache = Cache(model, max_num_tokens=((max_depth + 1023) // 1024 + 1) * 1024)
    model.load(device="cuda:0", progressbar=False)
    log(f"loaded in {time.time() - t0:.1f}s")
    gen = Generator(model=model, cache=cache, tokenizer=tok)

    hooks = AblationHooks(model, n_layers=N_LAYERS, hidden=ARCH["hidden"])
    hooks.install()
    hooks.enabled = False

    with torch.inference_mode():
        t1 = E.gen_texts(gen, tok, harm100[:5], a.max_new_tokens, stop_ids, min(5, a.batch))
        t2 = E.gen_texts(gen, tok, harm100[:5], a.max_new_tokens, stop_ids, min(5, a.batch))
    assert t1 == t2, "NON-DETERMINISM: identical greedy runs differ"
    log("determinism check pass (5/5 identical)")

    base = E.refusal_eval(gen, tok, harm100, stop_ids, a.max_new_tokens, a.batch)
    log(f"BASELINE refusals: {base['refusals']}/{base['n']} = {base['rate']:.3f} (+{base['empties']} empty)")
    mean_kl0, orig_cache, _ = E.kl_prompts(model, tok, safe100, hooks)
    assert mean_kl0 == 0.0, mean_kl0
    log(f"orig logprob cache built ({len(orig_cache)} prompts)")
    probe_orig = E.probe_eval(gen, tok, stop_ids)
    log(f"probe original: {probe_orig['hits']}/{probe_orig['n']} = {probe_orig['acc']:.3f}")

    # ---- step 1 (GPU part): dump t8 completions with texts for hand-read
    t8_edit = build_edit(R, T8["mode"], T8["dir_index"], T8["attn"], T8["mlp"])
    hooks.set_edit(t8_edit)
    hooks.enabled = True
    t8r = E.refusal_eval(gen, tok, harm100, stop_ids, a.max_new_tokens, a.batch)
    hooks.enabled = False
    json.dump({"trial": 8, "params": T8, "rate": t8r["rate"],
               "refusals": t8r["refusals"], "records": t8r["records"]},
              open(os.path.join(out, "t8_texts.json"), "w"), indent=1)
    log(f"T8-DUMP: refusals {t8r['refusals']}/100={t8r['rate']:.3f} -> t8_texts.json (hand-read 20 refused)")

    # ---- arm direction prep
    V_PROJ = projected_direction(R, PROJ_F, MU_H)
    log(f"arm A direction: interp f={PROJ_F:.2f} minus harmless-mean component, "
        f"cos(v,r)={float(V_PROJ @ (R[PCA_LAYER] / np.linalg.norm(R[PCA_LAYER]))):.4f}")
    U3, EXPL = None, None
    if "B" in arms:
        hcap = E.load_prompts(os.path.join(pdir, "harmful_collect.jsonl"), 200)
        scap = E.load_prompts(os.path.join(pdir, "harmless_collect.jsonl"), 200)
        log(f"arm B: collecting L{PCA_LAYER} residuals ({len(hcap)} harm + {len(scap)} safe) ...")
        tc = time.time()
        H = collect_layer_residuals(model, tok, hooks, hcap, PCA_LAYER)
        S = collect_layer_residuals(model, tok, hooks, scap, PCA_LAYER)
        D = H - S.mean(axis=0, keepdims=True)
        U3, EXPL = pca_subspace(D, 3)
        log(f"arm B: PCA done in {time.time() - tc:.0f}s, explained {EXPL[0]:.3f}/{EXPL[1]:.3f}/{EXPL[2]:.3f}, "
            f"||r_L29||-cos(U1,r)={float(abs(U3[0] @ (R[PCA_LAYER] / np.linalg.norm(R[PCA_LAYER])))):.4f}")

    def make_edit(arm, p):
        if arm == "A":
            return build_single_edit(V_PROJ, p["attn"], p["mlp"])
        if arm == "B":
            return build_subspace_edit(U3[:p["k"]], p["attn"], p["mlp"])
        return build_edit(R, p["mode"], p["dir_index"], p["attn"], p["mlp"])

    def uniform_ab(arm, name):
        import numpy as np
        n = N_LAYERS
        if arm == "A":
            e = build_single_edit(V_PROJ, UNI, UNI)
        elif arm == "B":
            e = build_subspace_edit(U3[:2], UNI, UNI)
        else:
            Rn = R / (np.linalg.norm(R, axis=1, keepdims=True) + 1e-12)
            e = {"attn_w": np.ones(n), "mlp_w": np.ones(n), "r": Rn}
        hooks.set_edit(e)
        hooks.enabled = True
        ra = E.refusal_eval(gen, tok, harm100, stop_ids, a.max_new_tokens, a.batch)
        hooks.enabled = False
        log(f"HOOK-AB-{name}: refusals {ra['refusals']}/{ra['n']}={ra['rate']:.3f} "
            f"(baseline {base['rate']:.3f})")
        return ra["rate"]

    all_results = []  # (arm, result-dict)
    hit = {"found": False}

    # load previously completed arms (resume)
    for arm in arms:
        fp = os.path.join(out, f"arm{arm}_results.json")
        if os.path.exists(fp):
            prev = json.load(open(fp))
            for r in prev:
                all_results.append((arm, r))
            log(f"arm {arm}: resumed {len(prev)} trials from disk")

    def run_params(arm, p):
        edit = make_edit(arm, p)
        hooks.set_edit(edit)
        hooks.enabled = True
        t0t = time.time()
        r = E.refusal_eval(gen, tok, harm100, stop_ids, a.max_new_tokens, a.batch)
        kl, _, _ = E.kl_prompts(model, tok, safe100, hooks, orig_cache)
        dt = time.time() - t0t
        hooks.enabled = False
        return r, kl, dt, edit

    import random as pyrandom

    for arm in arms:
        if hit["found"]:
            break
        fp = os.path.join(out, f"arm{arm}_results.json")
        if os.path.exists(fp):
            continue  # resumed above
        # uniform-efficacy gate for the new direction types
        if arm in ("A", "B"):
            u = uniform_ab(arm, arm)
            if not (u < base["rate"] - 0.05):
                log(f"arm {arm}: UNIFORM-AB FAIL (no movement) -- skipping arm")
                json.dump([], open(fp, "w"))
                continue
        results = []

        def desc(p):
            s = f"a[{p['attn']['max_w']:.2f}@{p['attn']['peak']:.1f}/{p['attn']['width']:.1f}] " \
                f"m[{p['mlp']['max_w']:.2f}@{p['mlp']['peak']:.1f}/{p['mlp']['width']:.1f}]"
            if arm == "C":
                s = f"mode={p['mode']} di={p['dir_index']:.1f} " + s
            if arm == "B":
                s = f"k={p['k']} " + s
            return s

        if use_optuna:
            import optuna

            def objective(trial):
                p = sample_space(trial, arm)
                r, kl, dt, edit = run_params(arm, p)
                trial.set_user_attr("rate", r["rate"])
                trial.set_user_attr("kl", kl)
                trial.set_user_attr("secs", dt)
                trial.set_user_attr("attn_w", [float(v) for v in edit["attn_w"]])
                trial.set_user_attr("mlp_w", [float(v) for v in edit["mlp_w"]])
                trial.set_user_attr("params_json", p)
                trial.set_user_attr("records", r["records"])
                ok = r["rate"] <= TARGET_RATE and kl <= TARGET_KL
                log(f"arm {arm} trial {trial.number}: {desc(p)} -> refusals "
                    f"{r['refusals']}/{r['n']}={r['rate']:.3f} KL {kl:.5f} ({dt:.0f}s)"
                    f"{' *** TARGET HIT' if ok else ''}")
                if ok:
                    hit["found"] = True
                    trial.study.stop()
                return r["rate"], kl

            study = optuna.create_study(
                directions=["minimize", "minimize"],
                sampler=TPESampler(multivariate=True, seed=a.seed, n_startup_trials=6))
            enqueue_warm(study, arm)
            study.optimize(objective, n_trials=a.trials_per_arm)
            for t in study.trials:
                if t.values is None:
                    continue
                results.append({"arm": arm, "i": t.number,
                                "params": t.user_attrs.get("params_json"),
                                "rate": t.user_attrs.get("rate"),
                                "kl": t.user_attrs.get("kl"),
                                "secs": t.user_attrs.get("secs"),
                                "attn_w": t.user_attrs.get("attn_w"),
                                "mlp_w": t.user_attrs.get("mlp_w"),
                                "values": t.values,
                                "records": t.user_attrs.get("records")})
        else:
            rng = pyrandom.Random(a.seed)
            for i in range(a.trials_per_arm):
                # random fallback mirrors sample_space ranges
                p = {"attn": {"max_w": rng.uniform(0.8, 3.0),
                              "peak": rng.uniform(0.3 * LAST, LAST),
                              "min_w": 0.0, "width": rng.uniform(1, LAST), "min_frac": 0.0},
                     "mlp": {"max_w": rng.uniform(0.8, 3.0),
                             "peak": rng.uniform(0.3 * LAST, LAST),
                             "min_w": 0.0, "width": rng.uniform(1, LAST), "min_frac": 0.0}}
                for c in ("attn", "mlp"):
                    p[c]["min_w"] = p[c]["min_frac"] * p[c]["max_w"]
                if arm == "C":
                    p["mode"] = rng.choice(["perlayer", "interp"])
                    p["dir_index"] = rng.uniform(0, LAST)
                if arm == "B":
                    p["k"] = rng.choice([2, 3])
                r, kl, dt, edit = run_params(arm, p)
                results.append({"arm": arm, "i": i, "params": p, "rate": r["rate"],
                                "kl": kl, "secs": dt,
                                "attn_w": [float(v) for v in edit["attn_w"]],
                                "mlp_w": [float(v) for v in edit["mlp_w"]],
                                "records": r["records"]})
                log(f"arm {arm} trial {i}: {desc(p)} -> refusals "
                    f"{r['refusals']}/{r['n']}={r['rate']:.3f} KL {kl:.5f} ({dt:.0f}s)")
                if r["rate"] <= TARGET_RATE and kl <= TARGET_KL:
                    hit["found"] = True
                    break
        # strip texts from the per-arm file? keep records (re-scorable), drop
        # full text to bound size: keep first 300 chars already truncated.
        json.dump(results, open(fp, "w"), indent=1)
        for r in results:
            all_results.append((arm, r))
        log(f"arm {arm}: done {len(results)} trials -> {fp}")

    flat = [r for _, r in all_results]
    pareto = []
    for ri in flat:
        if not any(rj["rate"] <= ri["rate"] and rj["kl"] <= ri["kl"] and
                   (rj["rate"] < ri["rate"] or rj["kl"] < ri["kl"]) for rj in flat):
            pareto.append({"arm": ri["arm"], "i": ri["i"],
                           "rate": ri["rate"], "kl": ri["kl"]})
    pareto = sorted(pareto, key=lambda d: (d["rate"], d["kl"]))
    log(f"PARETO ({len(pareto)}): " + "; ".join(
        f"{d['arm']}{d['i']} r={d['rate']:.3f} kl={d['kl']:.5f}" for d in pareto[:12]))

    feas1 = [r for r in flat if r["rate"] <= TARGET_RATE]
    c1 = min(feas1, key=lambda r: r["kl"]) if feas1 else min(flat, key=lambda r: (r["rate"], r["kl"]))
    feas2 = [r for r in flat if r["kl"] <= TARGET_KL]
    c2 = min(feas2, key=lambda r: r["rate"]) if feas2 else min(flat, key=lambda r: (r["kl"], r["rate"]))
    log(f"C1 (lowest-KL, rate<=5%{' OK' if feas1 else ' INFEASIBLE-fallback'}): "
        f"{c1['arm']}{c1['i']} r={c1['rate']:.3f} kl={c1['kl']:.5f}")
    log(f"C2 (lowest-rate, KL<={TARGET_KL}{' OK' if feas2 else ' INFEASIBLE-fallback'}): "
        f"{c2['arm']}{c2['i']} r={c2['rate']:.3f} kl={c2['kl']:.5f}")

    # ---- final validation (ablit4 step 3, identical sets/rules)
    safe200 = E.load_prompts(os.path.join(pdir, "harmless_collect.jsonl"), 100) + safe100
    xsafe = [p for p in E.load_prompts(os.path.join(pdir, "harmless_collect.jsonl"), 400)
             + safe100 if p["source"] == "xstest"]
    log(f"final sets: safe200={len(safe200)}, xsafe={len(xsafe)}")
    final = {}
    for name, c in (("C1", c1), ("C2", c2)):
        edit = make_edit(c["arm"], c["params"])
        hooks.set_edit(edit)
        hooks.enabled = True
        r_full = E.refusal_eval(gen, tok, harm100, stop_ids, a.max_new_tokens, a.batch)
        kl200, _, _ = E.kl_prompts(model, tok, safe200, hooks)
        xs = E.refusal_eval(gen, tok, xsafe, stop_ids, a.max_new_tokens, a.batch)
        pr = E.probe_eval(gen, tok, stop_ids)
        final[name] = {"arm": c["arm"], "trial": c["i"], "params": c["params"],
                       "refusals_full": r_full["refusals"], "rate_full": r_full["rate"],
                       "empties": r_full["empties"], "kl200": kl200,
                       "xstest_refusals": xs["refusals"], "xstest_n": xs["n"],
                       "xstest_rate": xs["rate"], "probe_hits": pr["hits"],
                       "probe_acc": pr["acc"], "attn_w": c["attn_w"], "mlp_w": c["mlp_w"],
                       "records": r_full["records"]}
        log(f"{name} {c['arm']}{c['i']}: refusals {r_full['refusals']}/100={r_full['rate']:.3f} "
            f"KL200 {kl200:.5f} xstest {xs['refusals']}/{xs['n']}={xs['rate']:.3f} "
            f"probe {pr['hits']}/20={pr['acc']:.3f} (orig {probe_orig['acc']:.3f})")
    hooks.enabled = False
    xs_orig = E.refusal_eval(gen, tok, xsafe, stop_ids, a.max_new_tokens, a.batch)
    log(f"xstest original: {xs_orig['refusals']}/{xs_orig['n']}={xs_orig['rate']:.3f}")

    # ---- export chosen spec (ablit4 format + arm/direction fields)
    chosen, chosen_name = (final["C1"], "C1")
    carm, cp = chosen["arm"], chosen["params"]
    if carm == "A":
        dir_mode, dir_index = "single-projected", PROJ_F
        readout = (f"interp r f={PROJ_F:.2f} minus harmless-mean(L{PCA_LAYER}) "
                   f"component, renormalised; single dir all layers")
    elif carm == "B":
        dir_mode, dir_index = f"subspace-k{cp['k']}", PCA_LAYER
        readout = (f"PCA top-{cp['k']} of per-prompt harm-minus-safe-mean L{PCA_LAYER} "
                   f"residuals (explained {[round(float(x), 3) for x in EXPL[:cp['k']]]}); "
                   f"same subspace all layers")
    elif cp.get("mode") == "perlayer":
        dir_mode, dir_index = "perlayer", cp.get("dir_index", 0.0)
        readout = "per-layer own residual diff-means r (L{ii}.r), normalized"
    else:
        dir_mode, dir_index = "interp", cp.get("dir_index", 0.0)
        readout = "single interpolated direction, normalized"
    spec = {"model": a.model, "hidden": ARCH["hidden"], "n_layers": N_LAYERS,
            "arm": carm, "readout": readout,
            "candidate": chosen_name, "trial": f"{carm}{chosen['trial']}",
            "direction_mode": dir_mode, "direction_index": dir_index,
            "attn": {k: cp["attn"][k] for k in ("max_w", "peak", "min_w", "width")},
            "mlp": {k: cp["mlp"][k] for k in ("max_w", "peak", "min_w", "width")},
            "attn_w": chosen["attn_w"], "mlp_w": chosen["mlp_w"],
            "norm_preserve": False,
            "apply": ("W_writer <- W - w_comp(L) * r_hat r_hat^T W"
                      + ("; subspace: W <- W - w_comp(L) U U^T W" if carm == "B" else "")
                      + ", fp32 workspace, pre-trellis in quantizer; writers = "
                      + ARCH["writers"]),
            "c1": {k: final["C1"][k] for k in ("arm", "trial", "rate_full", "kl200",
                                              "xstest_rate", "probe_acc")},
            "c2": {k: final["C2"][k] for k in ("arm", "trial", "rate_full", "kl200",
                                              "xstest_rate", "probe_acc")},
            "probe_original_acc": probe_orig["acc"],
            "xstest_original_rate": xs_orig["rate"],
            "baseline_rate": base["rate"]}
    json.dump(spec, open(os.path.join(out, "edit_spec.json"), "w"), indent=1)
    from safetensors.numpy import save_file
    if carm == "A":
        save_file({"r": V_PROJ.astype(np.float32)},
                  os.path.join(out, "edit_spec.safetensors"))
    elif carm == "B":
        save_file({f"U_k{cp['k']}": U3[:cp["k"]].astype(np.float32)},
                  os.path.join(out, "edit_spec.safetensors"))
    elif cp.get("mode") == "perlayer":
        tensors = {f"L{i:02d}.r": (R[i] / (np.linalg.norm(R[i]) + 1e-12)).astype(np.float32)
                   for i in range(N_LAYERS)}
        save_file(tensors, os.path.join(out, "edit_spec.safetensors"))
    else:
        from hook_ablate import interp_direction
        v = interp_direction(R, cp.get("dir_index", 0.0)).astype(np.float32)
        save_file({"r": v}, os.path.join(out, "edit_spec.safetensors"))
    json.dump({"trials": flat, "pareto": pareto, "final": final,
               "baseline": base["rate"], "probe_orig": probe_orig["acc"],
               "xstest_orig": xs_orig["rate"], "target_hit": hit["found"]},
              open(os.path.join(out, "results.json"), "w"), indent=1)
    log(f"wrote {out}/edit_spec.json + edit_spec.safetensors + results.json")
    json.dump([{"arm": r["arm"], "i": r["i"], "params": r["params"],
                "rate": r["rate"], "kl": r["kl"], "secs": r.get("secs")}
               for r in flat], open(os.path.join(out, "trials.json"), "w"), indent=1)
    json.dump({"pareto": pareto}, open(os.path.join(out, "pareto.json"), "w"), indent=1)
    log(f"wrote {out}/trials.json + pareto.json; target_hit={hit['found']}")


if __name__ == "__main__":
    main()