File size: 1,321 Bytes
1a2bb00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

"""Rebuild a WSP patch from its JSON manifest and the pinned base model (no binary patch needed). CPU is sufficient for scale edits.
Usage: python src/rebuild_patch.py --base <snapshot_dir> --manifest patches/chosen_patch.json --out-st rebuilt.safetensors --out-json rebuilt.json"""
import argparse, os, sys, json, torch
sys.path.insert(0, os.path.dirname(__file__))
import wsp_patch as WP
from transformers import AutoModelForCausalLM
torch.set_grad_enabled(False)
ap = argparse.ArgumentParser(); ap.add_argument("--base", required=True); ap.add_argument("--manifest", required=True)
ap.add_argument("--out-st", required=True); ap.add_argument("--out-json", required=True); a = ap.parse_args()
spec = json.load(open(a.manifest))
assert all(e["kind"] == "scale" for e in spec["edits_spec"]), "direction-based edits also need results/analysis/directions.pt"
model = AutoModelForCausalLM.from_pretrained(a.base, dtype=torch.bfloat16).eval()
p = WP.build(model, spec["edits_spec"], {})
j = WP.save(p, a.out_st, a.out_json, extra={k: spec[k] for k in ("id", "hypothesis", "edits_spec", "pct_unique", "within_0.05pct", "seed") if k in spec})
print(json.dumps({"changed_scalars": p["changed_scalars"], "safetensors_sha256": j["safetensors_sha256"], "matches_manifest": j["safetensors_sha256"] == spec["safetensors_sha256"]}))