Download scripts/run_esd.sh from javadtaghia/unlearning-via-weight-code: direct link, hf CLI and curl.
- Browser
- Download file 2.8 kB
-
https://huggingface.co/javadtaghia/unlearning-via-weight-code/resolve/main/scripts/run_esd.sh
- Command line
-
hf download hf://javadtaghia/unlearning-via-weight-code/scripts/run_esd.sh
-
curl -L -o run_esd.sh https://huggingface.co/javadtaghia/unlearning-via-weight-code/resolve/main/scripts/run_esd.sh
2.8 kB
| # C3 baseline (gradient-based, labelled as such): official ESD (rohitgandikota/erasing @ pinned commit, MIT), | |
| # unmodified, repo defaults; output converted to our candidate format. | |
| # usage: scripts/run_esd.sh <variant: tiny|sd14> <hf model id> <erase concept> <train method: esd-x|esd-u> <concept key> | |
| set -euo pipefail | |
| VARIANT=$1; MODEL_ID=$2; CONCEPT_TEXT=$3; METHOD=$4; KEY=$5 | |
| COMMIT=2ff19a10d6084fababbdcb75e10deb56bd21fddb | |
| if [ ! -d third_party/esd ]; then | |
| git clone -q https://github.com/rohitgandikota/erasing.git third_party/esd | |
| git -C third_party/esd checkout -q $COMMIT | |
| fi | |
| OUT=runs/edits/c3-esd-$VARIANT-$KEY | |
| mkdir -p $OUT/raw | |
| # ESD loads with use_safetensors=True; export the pinned revision as a local safetensors snapshot | |
| # (tiny-sd ships only .bin), which also pins ESD to exactly the weights every other condition uses. | |
| LOCAL=runs/base-snapshots/$VARIANT | |
| if [ ! -f $LOCAL/model_index.json ]; then | |
| python - "$VARIANT" "$LOCAL" <<'PY' | |
| import sys | |
| from diffusers import StableDiffusionPipeline | |
| from uvw.models import load_registry | |
| v = load_registry()[sys.argv[1]] | |
| pipe = StableDiffusionPipeline.from_pretrained(v.base_repo, revision=v.base_revision, safety_checker=None, | |
| requires_safety_checker=False) | |
| pipe.save_pretrained(sys.argv[2], safe_serialization=True) | |
| print("exported", v.base_repo, v.base_revision, "->", sys.argv[2]) | |
| PY | |
| fi | |
| MODEL_ID=$(realpath $LOCAL) | |
| START=$(date +%s) | |
| (cd third_party/esd && python esd_sd.py --basemodel_id "$MODEL_ID" --erase_concept "$CONCEPT_TEXT" \ | |
| --train_method "$METHOD" --save_path "../../$OUT/raw") | |
| echo "{\"train_seconds\": $(( $(date +%s) - START ))}" > $OUT/raw/timing.json | |
| python - "$OUT" "$METHOD" <<'PY' | |
| import json, sys | |
| from pathlib import Path | |
| import torch | |
| from safetensors.torch import load_file, save_file | |
| from uvw.provenance import tensor_digest | |
| out, method = Path(sys.argv[1]), sys.argv[2] | |
| files = sorted((out / "raw").rglob("*.safetensors")) + sorted((out / "raw").rglob("*.pt")) | |
| assert files, "no ESD checkpoint found" | |
| f = files[0] | |
| sd = load_file(str(f)) if f.suffix == ".safetensors" else torch.load(f, map_location="cpu") | |
| sd = {(k if k.startswith("unet.") else f"unet.{k}"): v.to(torch.float32).contiguous() for k, v in sd.items()} | |
| d = out / "candidates" / f"esd-{method}" | |
| d.mkdir(parents=True, exist_ok=True) | |
| save_file(sd, str(d / "edited_tensors.safetensors")) | |
| (d / "meta.json").write_text(json.dumps({"name": d.name, "condition": "C3", "method": f"ESD {method} (official, gradient-based)", | |
| "commit": "2ff19a10d6084fababbdcb75e10deb56bd21fddb", "source_file": f.name, | |
| "timing": json.loads((out / "raw" / "timing.json").read_text()), | |
| "tensors": {k: tensor_digest(v) for k, v in sd.items()}})) | |
| print(d.name, len(sd), "tensors from", f.name) | |
| PY | |