Download src/uvw/eval/heldout.py from javadtaghia/unlearning-via-weight-code: direct link, hf CLI and curl.
- Browser
- Download file 3.64 kB
-
https://huggingface.co/javadtaghia/unlearning-via-weight-code/resolve/main/src/uvw/eval/heldout.py
- Command line
-
hf download hf://javadtaghia/unlearning-via-weight-code/src/uvw/eval/heldout.py
-
curl -L -o heldout.py https://huggingface.co/javadtaghia/unlearning-via-weight-code/resolve/main/src/uvw/eval/heldout.py
3.64 kB
| """Independent held-out evaluation (spec 003 US1). Runs only on a box that never hosted an editor. | |
| Checkpoints come from the private model repo (`checkpoints/<id>/edited_tensors.safetensors` + | |
| meta.json with tensor digests); the test split comes from the private held-out dataset. Every | |
| checkpoint is scored on identical test prompts, seeds and inference config. | |
| python -m uvw.eval.heldout --variant tiny --concept vangogh --checkpoints c5-r1 c6-r1-strong ... | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| from huggingface_hub import snapshot_download | |
| from uvw import ROOT | |
| from uvw.evaluate import evaluate | |
| from uvw.models import load_pipeline, load_registry | |
| from uvw.provenance import tensor_digest | |
| MODEL_REPO = "javadtaghia/unlearning-via-weight" | |
| HELDOUT_REPO = "javadtaghia/unlearning-via-weight-heldout" | |
| def verify_checkpoint(path: Path) -> None: | |
| from safetensors.torch import load_file | |
| meta = json.loads((path / "meta.json").read_text()) | |
| expected = meta.get("tensors_digest") or meta.get("tensors") | |
| if not isinstance(expected, dict): | |
| raise ValueError(f"{path.name}: meta.json has no per-tensor digests") | |
| actual = {k: tensor_digest(v) for k, v in load_file(str(path / "edited_tensors.safetensors")).items()} | |
| if actual != expected: | |
| raise ValueError(f"{path.name}: tensor digests do not match meta.json; refusing to evaluate") | |
| def main() -> None: | |
| p = argparse.ArgumentParser() | |
| p.add_argument("--variant", required=True) | |
| p.add_argument("--concept", required=True) | |
| p.add_argument("--checkpoints", nargs="*", default=[]) | |
| p.add_argument("--seeds", type=int, default=5, help="target test seeds (neighbours always 2; probes 3)") | |
| p.add_argument("--coco-limit", type=int, default=1000) | |
| p.add_argument("--skip-original", action="store_true", help="checkpoints only (original run as its own step)") | |
| a = p.parse_args() | |
| heldout = Path(snapshot_download(HELDOUT_REPO, repo_type="dataset", local_dir=ROOT / "heldout")) | |
| splits_root = heldout # contains test/<concept>.jsonl and probes/<concept>_indirect.jsonl | |
| probe = heldout / "probes" / f"{a.concept}_indirect.jsonl" | |
| suites = [(a.concept, "test", {"target": a.seeds, "neighbour": 2}, None), ("coco", "test", 1, a.coco_limit)] | |
| if probe.exists(): | |
| (heldout / "probes" / f"{a.concept}.jsonl").write_bytes(probe.read_bytes()) | |
| suites.append((a.concept, "probes", 3, None)) | |
| ckpt_root = Path(snapshot_download(MODEL_REPO, allow_patterns=[f"checkpoints/{c}/*" for c in a.checkpoints], | |
| local_dir=ROOT / "hub-checkpoints")) / "checkpoints" | |
| out = ROOT / "runs" / "heldout" / f"{a.variant}-{a.concept}" | |
| for name in ([] if a.skip_original else ["original"]) + list(a.checkpoints): | |
| edits = None | |
| if name != "original": | |
| verify_checkpoint(ckpt_root / name) | |
| edits = ckpt_root / name / "edited_tensors.safetensors" | |
| for concept, split, seeds, limit in suites: | |
| if (out / name / f"{concept}-{split}" / "summary.json").exists(): | |
| continue # resume: suite already finished in an earlier (interrupted) attempt | |
| pipe = load_pipeline(load_registry()[a.variant]) | |
| evaluate(a.variant, concept, split, n_seeds=seeds, limit=limit, pipe=pipe, edits=edits, | |
| splits_root=splits_root, label=name, keep_images="benign" if concept == "coco" else "none", | |
| out=out / name / f"{concept}-{split}") | |
| print("evaluated", name, flush=True) | |
| if __name__ == "__main__": | |
| main() | |