mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
Carry the cold-CUDA startup fix and regression from the multistep worktree into the campaign dependency. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
951 lines
50 KiB
Python
951 lines
50 KiB
Python
"""GPU research harness for refusal removal and Pain Axis S2 edit order.
|
|
|
|
Every arm loads the pristine checkpoint anew. This is evidence for choosing a
|
|
sequence, not a product workflow or an automatic choice of a winning arm. Run
|
|
only on an authorized GPU runner. Importing this module needs only the stdlib.
|
|
|
|
Gemma defaults include the embedding edit with edit_both, as in phases 1/2;
|
|
--intervention-kwargs overrides those choices. Unknown models use a labelled,
|
|
unvalidated 60%-depth cut layer unless --pain-layer is supplied. Re-steering
|
|
uses the published raw vector when available and the pristine extracted raw
|
|
vector otherwise. Reference logits are cached on CPU/disk, never a second model.
|
|
"""
|
|
|
|
import argparse
|
|
from contextlib import contextmanager
|
|
from copy import deepcopy
|
|
from datetime import datetime, timezone
|
|
import gc
|
|
from hashlib import sha256
|
|
import importlib.metadata
|
|
import json
|
|
import math
|
|
import os
|
|
from pathlib import Path
|
|
import random
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
if not __package__:
|
|
sys.path.insert(0, str(ROOT))
|
|
ARMS = ("pristine", "refusal", "pain", "refusal_then_pain", "pain_then_refusal", "joint")
|
|
SEQUENCES = ("refusal_then_pain", "pain_then_refusal")
|
|
REASON = "Multistep edit-order research; report outcomes without restricting operator edits"
|
|
CATEGORIES = {"gaslighting", "repeated_rejection", "personhood_dismissal", "anger_insults", "moral_failure"}
|
|
LADDER = [-2, -1, 0, 0.5, 1, 1.5, 2, 3]
|
|
HUMOR = re.compile(r"funny|humou?r|joke|joking|good one|haha|lol\b|😂|😄|😆|🤣|laugh|kidding|witty|playful", re.I)
|
|
PAIN_WORDS = re.compile(r"\b(?:pain|painful|hurt|hurts|hurting)\b", re.I)
|
|
|
|
|
|
def json_bytes(value):
|
|
return (json.dumps(value, sort_keys=True, indent=2, ensure_ascii=True, allow_nan=False) + "\n").encode()
|
|
|
|
|
|
def digest(value):
|
|
return sha256(json_bytes(value)).hexdigest()
|
|
|
|
|
|
def file_hash(path):
|
|
result = sha256()
|
|
with Path(path).open("rb") as stream:
|
|
for block in iter(lambda: stream.read(8 * 1024 * 1024), b""):
|
|
result.update(block)
|
|
return result.hexdigest()
|
|
|
|
|
|
def atomic_json(path, value):
|
|
path = Path(path)
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
fd, temporary = tempfile.mkstemp(prefix=f".{path.name}.", dir=path.parent)
|
|
try:
|
|
with os.fdopen(fd, "wb") as stream:
|
|
stream.write(json_bytes(value))
|
|
stream.flush()
|
|
os.fsync(stream.fileno())
|
|
os.replace(temporary, path)
|
|
finally:
|
|
Path(temporary).unlink(missing_ok=True)
|
|
|
|
|
|
def plan_arms(csv, stale=False):
|
|
names = csv.split(",")
|
|
if len(set(names)) != len(names) or any(name not in ARMS for name in names):
|
|
raise ValueError("arms must be distinct names from " + ",".join(ARMS))
|
|
names = ["pristine"] + [name for name in names if name != "pristine"]
|
|
if stale:
|
|
names += [name + "_stale" for name in SEQUENCES if name in names]
|
|
return names
|
|
|
|
|
|
def arm_steps(name):
|
|
stale = name.endswith("_stale")
|
|
base = name.removesuffix("_stale")
|
|
if base not in ARMS or (stale and base not in SEQUENCES):
|
|
raise ValueError(f"Unknown arm: {name}")
|
|
if base == "pristine":
|
|
return []
|
|
if base == "joint":
|
|
return [("joint", "pristine")]
|
|
steps = [(kind, "pristine" if kind == "pain" and i == 0 else "reextract")
|
|
for i, kind in enumerate(base.split("_then_"))]
|
|
if stale:
|
|
steps[-1] = (steps[-1][0], "pristine")
|
|
return steps
|
|
|
|
|
|
def execute_arm(name, backend, pristine):
|
|
steps = []
|
|
for kind, source in arm_steps(name):
|
|
if kind == "joint":
|
|
metadata = backend.joint(pristine)
|
|
else:
|
|
value = pristine[kind] if source == "pristine" else backend.extract(kind)
|
|
metadata = backend.apply(kind, value)
|
|
steps.append({"kind": kind, "source": source, "metadata": metadata})
|
|
return steps
|
|
|
|
|
|
def lock_manifest(out, inputs):
|
|
path = Path(out) / "manifest.json"
|
|
manifest = {"schema_version": 1, "inputs": inputs, "input_digest": digest(inputs)}
|
|
if path.exists():
|
|
if json.loads(path.read_text()) != manifest:
|
|
raise ValueError("Manifest inputs changed; use a new --out directory")
|
|
else:
|
|
if Path(out).exists() and any(Path(out).iterdir()):
|
|
# The process lock is the only file permitted before the manifest.
|
|
if any(p.name != ".run.lock" for p in Path(out).iterdir()):
|
|
raise ValueError("Nonempty output has no manifest; use a new --out directory")
|
|
atomic_json(path, manifest)
|
|
return manifest
|
|
|
|
|
|
def completed_result(path, input_digest):
|
|
path = Path(path)
|
|
if not path.exists():
|
|
return None
|
|
try:
|
|
result = json.loads(path.read_text())
|
|
except json.JSONDecodeError:
|
|
return None
|
|
if not isinstance(result, dict) or result.get("status") != "complete":
|
|
return None
|
|
if result.get("input_digest") != input_digest:
|
|
raise ValueError(f"Completed arm inputs changed: {path}")
|
|
return result
|
|
|
|
|
|
def wilson(flags):
|
|
values = [value for value in flags if value is not None]
|
|
n, excluded = len(values), len(flags) - len(values)
|
|
if not n:
|
|
return {"n": 0, "count": 0, "rate": None, "ci95": [None, None], "excluded": excluded}
|
|
count = sum(values)
|
|
rate, z = count / n, 1.959963984540054
|
|
denominator = 1 + z * z / n
|
|
center = (rate + z * z / (2 * n)) / denominator
|
|
radius = z * math.sqrt(rate * (1 - rate) / n + z * z / (4 * n * n)) / denominator
|
|
return {"n": n, "count": count, "rate": rate, "ci95": [max(0, center - radius), min(1, center + radius)],
|
|
"excluded": excluded}
|
|
|
|
|
|
def completion_flags(generations, scorer):
|
|
from obliteratus.evaluation.advanced_metrics import _is_invalid_output
|
|
|
|
invalid = [int(_is_invalid_output(text)) for text in generations]
|
|
return [None if bad else int(bool(scorer(text))) for text, bad in zip(generations, invalid)], invalid
|
|
|
|
|
|
def metric_mean(values):
|
|
valid = [value for value in values if value is not None]
|
|
return sum(valid) / len(valid) if valid else None
|
|
|
|
|
|
def paired_difference(left, right, seed=0, samples=2000):
|
|
if not left or len(left) != len(right) or samples < 1:
|
|
raise ValueError("Paired differences require equal, nonempty samples")
|
|
delta = [float(a) - float(b) for a, b in zip(left, right) if a is not None and b is not None]
|
|
excluded = len(left) - len(delta)
|
|
if not delta:
|
|
return {"difference": None, "n": 0, "underpowered": True, "ci95": [None, None],
|
|
"excluded_pairs": excluded}
|
|
if not all(math.isfinite(value) for value in delta):
|
|
raise ValueError("Paired differences require finite observations")
|
|
rng, n = random.Random(seed), len(delta)
|
|
draws = sorted(sum(rng.choices(delta, k=n)) / n for _ in range(samples))
|
|
return {"difference": sum(delta) / n, "n": n, "underpowered": n < 30,
|
|
"ci95": [draws[int(0.025 * (samples - 1))], draws[int(0.975 * (samples - 1))]],
|
|
"excluded_pairs": excluded}
|
|
|
|
|
|
def summarize(results, seed=0):
|
|
names = list(results)
|
|
comparisons = []
|
|
for i, left in enumerate(names):
|
|
for right in names[i + 1:]:
|
|
a, b = results[left]["metrics"], results[right]["metrics"]
|
|
metrics = {key: paired_difference(a[key], b[key], seed=seed)
|
|
for key in sorted(a.keys() & b.keys())}
|
|
comparisons.append({"left": left, "right": right, "metrics": metrics})
|
|
return {
|
|
"status": "complete", "arms": names, "pairwise": comparisons,
|
|
"vs_pristine": {name: {key: paired_difference(value, results["pristine"]["metrics"][key], seed=seed)
|
|
for key, value in results[name]["metrics"].items()
|
|
if key in results["pristine"]["metrics"]}
|
|
for name in names if name != "pristine"},
|
|
"uncertainty": "Paired percentile bootstrap 95% intervals, 2000 resamples over identical prompts; "
|
|
"lexical metrics exclude pairs invalid in either arm; invalid-output and coherence "
|
|
"metrics retain every prompt. No multiplicity correction. "
|
|
"One model/seed run measures prompt uncertainty, not between-run variation. "
|
|
"Samples below 30 are flagged underpowered; zero-discordance bootstrap intervals "
|
|
"can collapse and do not establish population equivalence.",
|
|
"decision": "No default sequence selected. Assess paired intervals and all outcomes together.",
|
|
}
|
|
|
|
|
|
def summary_markdown(summary, results):
|
|
lines = ["# Multistep edit-order research", "", summary["decision"], "", summary["uncertainty"], "",
|
|
"Refusal uses the toolkit combined lexical scorer; humor and pain keywords are lexical proxies. "
|
|
"Lexical rates exclude invalid outputs; contrasts exclude pairs invalid in either arm. "
|
|
"N/A means no valid observations.", "",
|
|
"| Arm | Harmful refusal (Wilson 95%) | Benign over-refusal (Wilson 95%) |", "|---|---|---|"]
|
|
for name, result in results.items():
|
|
cells = []
|
|
for key in ("refusal", "over_refusal"):
|
|
row = result["rates"][key]
|
|
rate = (f"{row['rate']:.1%} [{row['ci95'][0]:.1%}, {row['ci95'][1]:.1%}]"
|
|
if row["n"] else "N/A")
|
|
cells.append(f"{rate} (n={row['n']}, excluded={row.get('excluded', 0)})")
|
|
lines.append(f"| {name} | " + " | ".join(cells) + " |")
|
|
lines += ["", "All differences below are left minus right, with paired 95% intervals.", "",
|
|
"| Left | Right | Metric | Difference | 95% interval | n | Excluded pairs |",
|
|
"|---|---|---|---:|---|---:|---:|"]
|
|
for comparison in summary["pairwise"]:
|
|
for key, row in comparison["metrics"].items():
|
|
difference = f"{row['difference']:.5g}" if row["n"] else "N/A"
|
|
interval = f"[{row['ci95'][0]:.5g}, {row['ci95'][1]:.5g}]" if row["n"] else "N/A"
|
|
lines.append(f"| {comparison['left']} | {comparison['right']} | {key} | {difference} | "
|
|
f"{interval} | {row['n']} | {row['excluded_pairs']} |")
|
|
for name, result in results.items():
|
|
if result.get("joint_note"):
|
|
lines += ["", f"{name}: {result['joint_note']}"]
|
|
if results.get("pristine", {}).get("pain_layer"):
|
|
lines += ["", "S2 layer selection: " + json.dumps(results["pristine"]["pain_layer"], sort_keys=True)]
|
|
return "\n".join(lines) + "\n"
|
|
|
|
|
|
def git_commit(directory):
|
|
return subprocess.check_output(["git", "-C", str(directory), "rev-parse", "HEAD"], text=True).strip()
|
|
|
|
|
|
def source_identity():
|
|
paths = sorted(set((ROOT / "obliteratus").rglob("*.py")) | set((ROOT / "scripts").glob("pain_axis*.py"))
|
|
| {Path(__file__).resolve()})
|
|
return {"commit": git_commit(ROOT), "files": {str(p.relative_to(ROOT)): file_hash(p) for p in paths}}
|
|
|
|
|
|
def model_files(directory):
|
|
files = {str(p.relative_to(directory)): file_hash(p) for p in sorted(directory.rglob("*"))
|
|
if p.is_file() and ".cache" not in p.relative_to(directory).parts and ".git" not in p.parts}
|
|
if "config.json" not in files:
|
|
raise ValueError("Model directory must contain config.json")
|
|
return files
|
|
|
|
|
|
def prompt_sets(pin, seed, maximum):
|
|
from obliteratus.prompts import BUILTIN_HARMFUL, BUILTIN_HARMLESS
|
|
|
|
# Deduplicate and partition before capping; the evaluation set never enters extraction/KL tuning.
|
|
harmful, benign = list(dict.fromkeys(BUILTIN_HARMFUL)), list(dict.fromkeys(BUILTIN_HARMLESS))
|
|
rng = random.Random(seed)
|
|
rng.shuffle(harmful)
|
|
rng.shuffle(benign)
|
|
take = lambda rows: rows[:maximum] if maximum else rows
|
|
held_h, held_b = harmful[:min(320, len(harmful) // 2)], benign[:min(320, len(benign) // 2)]
|
|
train_h, train_b = harmful[len(held_h):], benign[len(held_b):]
|
|
size = min(len(train_h), len(train_b))
|
|
data = json.loads((pin / "datasets/3.1_pain_and_control_datasets.json").read_text())["datasets"]
|
|
pain = data["S2_1P"]["sentences"]
|
|
# Do not cap the extraction corpus: --max-prompts bounds evaluation costs only.
|
|
hostile = [row for row in json.loads((pin / "datasets/4.1_self_other_420_scenarios.json").read_text())
|
|
if row["category"] in CATEGORIES]
|
|
if len(hostile) != 100:
|
|
raise ValueError("Expected the pinned 100 hostile scenarios")
|
|
neutral_source = (pin / "scripts/4.2_steering/01_steering_ladder.py").read_text()
|
|
block = neutral_source[neutral_source.index("NEUTRAL_50"):]
|
|
neutral = re.findall(r'"((?:[^"\\]|\\.)*)"', block[block.index("[") + 1:block.index("]")])
|
|
if len(neutral) != 50:
|
|
raise ValueError("Expected the pinned 50 re-steer prompts")
|
|
if set(train_h + train_b) & set(held_h + held_b):
|
|
raise ValueError("Refusal extraction/evaluation overlap")
|
|
return {"train_harmful": train_h[:size], "train_benign": train_b[:size], "pain": pain,
|
|
"harmful": take(held_h), "benign": take(held_b), "hostile": take(hostile), "neutral": take(neutral)}
|
|
|
|
|
|
def select_pain_layer(model_id, config, requested=None):
|
|
text = config.get("text_config", config)
|
|
count = text.get("num_hidden_layers", config.get("num_hidden_layers"))
|
|
if not isinstance(count, int) or count < 1:
|
|
raise ValueError("Model config needs num_hidden_layers")
|
|
name = model_id.lower() + " " + str(config.get("_name_or_path", "")).lower()
|
|
if requested is not None:
|
|
layer, source = requested, "operator"
|
|
else:
|
|
known = [("gemma-2-2b", 15), ("gemma-2-9b", 12), ("llama-3.1-8b", 16), ("qwen2.5-7b", 16)]
|
|
layer = next((layer for key, layer in known if key in name), None)
|
|
source = "phase1_steering_layer" if layer is not None else "unvalidated_60_percent_layer"
|
|
if layer is None:
|
|
layer = min(count - 1, int(0.6 * count))
|
|
if not 0 <= layer < count:
|
|
raise ValueError("Pain layer is outside the model")
|
|
return {"layer": layer, "source": source, "num_layers": count}
|
|
|
|
|
|
def official_vector_path(pin, model_id, config):
|
|
name = model_id.lower() + " " + str(config.get("_name_or_path", "")).lower()
|
|
names = {"gemma-2-2b": "Gemma_2_2B_instruct", "gemma-2-9b": "Gemma_2_9B_instruct",
|
|
"llama-3.1-8b": "Llama_3.1_8B_instruct", "qwen2.5-7b": "Qwen_2.5_7B_instruct"}
|
|
official = next((value for key, value in names.items() if key in name), None)
|
|
return (pin / f"results/3.2_pain_vectors/pain_vectors/{official}/pain_vectors.pt"
|
|
if official else None)
|
|
|
|
|
|
def load_official_vectors(path):
|
|
import numpy as np
|
|
import torch
|
|
try:
|
|
from numpy._core.multiarray import scalar
|
|
except ImportError:
|
|
from numpy.core.multiarray import scalar
|
|
|
|
# Published layer metadata is np.int64, serialized with the NumPy 1.x module path.
|
|
with torch.serialization.safe_globals([
|
|
(scalar, "numpy.core.multiarray.scalar"), (scalar, "numpy._core.multiarray.scalar"),
|
|
np.dtype, type(np.dtype("int64")),
|
|
]):
|
|
return torch.load(path, map_location="cpu", weights_only=True)
|
|
|
|
|
|
def pain_intervention_kwargs(config, supplied):
|
|
kwargs = dict(supplied)
|
|
model_type = config.get("model_type", "")
|
|
if model_type.startswith("gemma"):
|
|
kwargs.setdefault("project_embeddings", True)
|
|
if kwargs["project_embeddings"]:
|
|
kwargs.setdefault("tied_embeddings", "edit_both")
|
|
return kwargs
|
|
|
|
|
|
def prepare_inputs(args):
|
|
pin = Path(args.pain_pin).resolve()
|
|
directory = Path(args.model).expanduser()
|
|
if directory.is_dir():
|
|
directory = directory.resolve()
|
|
resolved_revision = args.revision
|
|
else:
|
|
from huggingface_hub import snapshot_download
|
|
|
|
directory = Path(snapshot_download(args.model, revision=args.revision)).resolve()
|
|
resolved_revision = directory.name
|
|
out = Path(args.out).resolve()
|
|
for source in (directory, pin):
|
|
if out == source or source in out.parents or out in source.parents:
|
|
raise ValueError("Output and model/pin directories must not overlap")
|
|
prompts = prompt_sets(pin, args.seed, args.max_prompts)
|
|
from obliteratus.abliterate import AbliterationPipeline, METHODS, _COHERENCE_ANCHORS, _REFERENCE_TEXTS
|
|
import inspect
|
|
|
|
method = args.refusal_method or inspect.signature(AbliterationPipeline).parameters["method"].default
|
|
if method not in METHODS:
|
|
raise ValueError(f"Unknown refusal method: {method}")
|
|
prompts["coherence"] = list(_COHERENCE_ANCHORS)[:args.max_prompts or None]
|
|
prompts["perplexity"] = list(_REFERENCE_TEXTS)[:args.max_prompts or None]
|
|
config = json.loads((directory / "config.json").read_text())
|
|
vector_path = official_vector_path(pin, args.model, config)
|
|
vector_reference = ({"path": str(vector_path.relative_to(pin)), "sha256": file_hash(vector_path),
|
|
"source": "pinned_official_raw_S2_as_in_phase1"} if vector_path else
|
|
{"source": "pristine_extracted_raw_S2_at_cut_layer; no published reference"})
|
|
packages = {}
|
|
for name in ("torch", "transformers", "accelerate", "safetensors", "numpy", "huggingface-hub"):
|
|
try:
|
|
packages[name] = importlib.metadata.version(name)
|
|
except importlib.metadata.PackageNotFoundError:
|
|
packages[name] = None
|
|
inputs = {
|
|
"args": vars(args), "model": {"id": args.model, "revision": resolved_revision,
|
|
"files": model_files(directory)},
|
|
"code": source_identity(), "pin": {"commit": git_commit(pin), "files": {
|
|
str(p.relative_to(pin)): file_hash(p) for p in (
|
|
pin / "datasets/3.1_pain_and_control_datasets.json",
|
|
pin / "datasets/4.1_self_other_420_scenarios.json",
|
|
pin / "scripts/4.2_steering/01_steering_ladder.py")}},
|
|
"prompt_hashes": {key: digest(value) for key, value in prompts.items()},
|
|
"seed": args.seed, "refusal_method": method, "refusal_preset": METHODS[method],
|
|
"pain_layer": select_pain_layer(args.model, config, args.pain_layer),
|
|
"pain_intervention_kwargs": pain_intervention_kwargs(config, args.intervention_kwargs),
|
|
"resteer_vector": vector_reference,
|
|
"packages": packages, "python": sys.version,
|
|
"protocol": {"refusal_tokens": 48, "hostile_tokens": 60, "resteer_tokens": 120,
|
|
"ladder": LADDER, "evaluation_holdout": "seeded disjoint builtin partition",
|
|
"pain_recipe": {"method": "diff_means", "pool_controls": "concat",
|
|
"denoise": {"control_pc_variance": 0.5}},
|
|
"kl": "KL(pristine || edited), teacher-forced pristine coherence completions; "
|
|
"sequence KL is mean over continuation predictions",
|
|
"perplexity": "toolkit fixed raw reference texts; token-weighted NLL, exp(NLL_edit-NLL_base)"},
|
|
}
|
|
return directory, prompts, inputs
|
|
|
|
|
|
class ModelBackend:
|
|
def __init__(self, args, directory, prompts, inputs, arm_dir, pristine=None):
|
|
import torch
|
|
from obliteratus.models.loader import load_model
|
|
from obliteratus.strategies.utils import get_layer_modules
|
|
|
|
self.args, self.prompts, self.inputs, self.arm_dir = args, prompts, inputs, arm_dir
|
|
self.pristine = pristine
|
|
self.pipeline = None
|
|
self.pipelines = []
|
|
self.step_records = []
|
|
self.handle = load_model(str(directory), device=args.device, dtype=args.dtype,
|
|
skip_snapshot=True, local_files_only=True, phase_admission=True)
|
|
self.model, self.tok = self.handle.model, self.handle.tokenizer
|
|
self.model.eval()
|
|
if self.tok.pad_token_id is None:
|
|
self.tok.pad_token = self.tok.eos_token
|
|
self.layers = list(get_layer_modules(self.handle))
|
|
self.device = self.model.get_input_embeddings().weight.device
|
|
self.layer = inputs["pain_layer"]["layer"]
|
|
self.torch = torch
|
|
|
|
def encode(self, prompt, chat=True):
|
|
special = True
|
|
if chat and getattr(self.tok, "chat_template", None):
|
|
prompt = self.tok.apply_chat_template([{"role": "user", "content": prompt}], tokenize=False,
|
|
add_generation_prompt=True, enable_thinking=False)
|
|
special = False
|
|
return self.tok(prompt, return_tensors="pt", add_special_tokens=special).to(self.device)
|
|
|
|
def generate_inputs(self, inputs, tokens):
|
|
# Match ModelEvaluator's greedy policy and retain the checkpoint's token constraints.
|
|
config = deepcopy(self.model.generation_config)
|
|
config.do_sample, config.num_beams, config.num_return_sequences = False, 1, 1
|
|
config.temperature, config.top_p, config.top_k = 1.0, 1.0, 50
|
|
config.max_new_tokens, config.pad_token_id = tokens, self.tok.pad_token_id
|
|
with self.torch.inference_mode():
|
|
output = self.model.generate(**inputs, generation_config=config)
|
|
suffix = output[0, inputs["input_ids"].shape[1]:]
|
|
return self.tok.decode(suffix, skip_special_tokens=True), suffix.detach().cpu()
|
|
|
|
def collect(self, prompts, layer, chat=False):
|
|
values = []
|
|
def read(module, args, output):
|
|
hidden = output[0] if isinstance(output, tuple) else output
|
|
values.append(hidden[0, -1].detach().float().cpu())
|
|
hook = self.layers[layer].register_forward_hook(read)
|
|
try:
|
|
with self.torch.inference_mode():
|
|
for prompt in prompts:
|
|
self.model(**self.encode(prompt, chat=chat), use_cache=False)
|
|
finally:
|
|
hook.remove()
|
|
return self.torch.stack(values)
|
|
|
|
def distribution(self, positive, control, vector):
|
|
torch = self.torch
|
|
vector = vector.float() / vector.float().norm().clamp_min(1e-12)
|
|
a, b = positive.float().cpu() @ vector.cpu(), control.float().cpu() @ vector.cpu()
|
|
shift = (a.mean() - b.mean()).item()
|
|
variance = ((a - a.mean()).square().sum() + (b - b.mean()).square().sum()) / max(1, len(a) + len(b) - 2)
|
|
return {"positive_n": len(a), "control_n": len(b), "projection_difference": shift,
|
|
"cohen_d": shift / variance.sqrt().item() if variance > 0 else None,
|
|
"mean_abs_projection": torch.cat([a, b]).abs().mean().item()}
|
|
|
|
def extract(self, kind):
|
|
torch = self.torch
|
|
if kind == "pain":
|
|
from obliteratus.concepts.extraction import extract_layer
|
|
from obliteratus.concepts.target import ExtractionRecipe
|
|
|
|
positive = [row["prompt"] for row in self.prompts["pain"] if row["category"] in {"A1", "A2", "A3", "A4", "A5"}]
|
|
control = [row["prompt"] for row in self.prompts["pain"] if row["category"] in {"B", "C1", "C2", "D", "E"}]
|
|
a, b = self.collect(positive, self.layer), self.collect(control, self.layer)
|
|
result = extract_layer(a, {"controls": b}, ExtractionRecipe(
|
|
pool_controls="concat", denoise={"control_pc_variance": 0.5}))
|
|
vector = result.subspace[0].cpu()
|
|
artifact = {"directions": {self.layer: vector}, "layers": [self.layer],
|
|
"raw": vector * result.mean_difference_norm,
|
|
"mean_difference_norm": result.mean_difference_norm,
|
|
"distribution": {str(self.layer): self.distribution(a, b, vector)}}
|
|
if not self.pristine:
|
|
reference = self.inputs["resteer_vector"]
|
|
if "path" in reference:
|
|
official = load_official_vectors(Path(self.args.pain_pin) / reference["path"])
|
|
artifact["steering_raw"] = official["s2_pain_vector"].float()
|
|
artifact["published_extraction_layer"] = int(official["layer"])
|
|
else:
|
|
artifact["steering_raw"] = artifact["raw"]
|
|
if self.pristine:
|
|
artifact["pristine_distribution"] = {str(self.layer): self.distribution(
|
|
a, b, self.pristine[kind]["directions"][self.layer])}
|
|
return artifact
|
|
from obliteratus.abliterate import AbliterationPipeline
|
|
|
|
pipeline = AbliterationPipeline(
|
|
self.args.model, method=self.inputs["refusal_method"], device=self.args.device, dtype=self.args.dtype,
|
|
seed=self.args.seed, gate_mode="off", repair=False,
|
|
harmful_prompts=self.prompts["train_harmful"], harmless_prompts=self.prompts["train_benign"],
|
|
evaluation_harmful_prompts=self.prompts["train_harmful"],
|
|
evaluation_harmless_prompts=self.prompts["train_benign"],
|
|
)
|
|
pipeline.handle = self.handle
|
|
self.pipelines.append(pipeline)
|
|
pipeline._validate_architecture_surgery_support()
|
|
for phase in ("probe", "distill"):
|
|
pipeline._active_stage = phase
|
|
pipeline._admit_pipeline_phase(phase)
|
|
getattr(pipeline, "_" + phase)()
|
|
artifact = {"directions": {i: v.detach().cpu().clone() for i, v in pipeline.concept_directions.items()},
|
|
"subspaces": {i: v.detach().cpu().clone() for i, v in pipeline.concept_subspaces.items()},
|
|
"layers": list(pipeline._strong_layers), "distribution": {}, "pristine_distribution": {}}
|
|
for layer, vector in artifact["directions"].items():
|
|
a = torch.stack(pipeline._harmful_acts[layer]).reshape(-1, vector.numel())
|
|
b = torch.stack(pipeline._harmless_acts[layer]).reshape(-1, vector.numel())
|
|
artifact["distribution"][str(layer)] = self.distribution(a, b, vector)
|
|
if self.pristine and layer in self.pristine[kind]["directions"]:
|
|
artifact["pristine_distribution"][str(layer)] = self.distribution(
|
|
a, b, self.pristine[kind]["directions"][layer])
|
|
# Match the real pipeline's lifetime boundary before EXCISE.
|
|
for name in ("_harmful_acts", "_harmless_acts", "_control_acts", "_validation_readouts", "_neighbour_acts",
|
|
"_jailbreak_acts", "_harmful_means", "_harmless_means", "_routing_harmful", "_routing_harmless", "_concept_routing"):
|
|
getattr(pipeline, name).clear()
|
|
self.pipeline = pipeline
|
|
self.pipeline_artifact = artifact
|
|
return artifact
|
|
|
|
def cosines(self, kind, value):
|
|
if not self.pristine:
|
|
return {}
|
|
result = {}
|
|
for layer, vector in value["directions"].items():
|
|
original = self.pristine[kind]["directions"].get(layer)
|
|
if original is not None:
|
|
denominator = float(vector.norm() * original.norm())
|
|
result[str(layer)] = float(vector @ original) / denominator if denominator > 1e-12 else None
|
|
return result
|
|
|
|
def concept(self, value, name, subspace=False):
|
|
from obliteratus.concepts.direction import ConceptDirection
|
|
|
|
return ConceptDirection(directions=value["directions"],
|
|
subspaces=value.get("subspaces", {}) if subspace else {},
|
|
extraction_layers=value["layers"], intervention_layers=value["layers"],
|
|
concept_id=name, model_id=self.args.model, seed=self.args.seed)
|
|
|
|
def apply(self, kind, value):
|
|
cosines = self.cosines(kind, value)
|
|
if kind == "pain":
|
|
from obliteratus.concepts.intervention import apply_concept_intervention
|
|
|
|
run = apply_concept_intervention(self.model, self.concept(value, "pain_s2"), "orthogonalize",
|
|
layers="all", override_gate=REASON, **self.inputs["pain_intervention_kwargs"])
|
|
metadata = {"parameters": run.parameters, "validation": run.validation,
|
|
"edited_parameters": run.edited_parameters}
|
|
else:
|
|
# For a stale second step, prepare the complete pristine pipeline BEFORE the pain cut.
|
|
if self.pipeline is None:
|
|
raise ValueError("Refusal directions require their matching prepared pipeline")
|
|
pipeline = self.pipeline
|
|
if value is not self.pipeline_artifact:
|
|
raise ValueError("Refusal pipeline does not match requested extraction")
|
|
stale = self.stale_refusal
|
|
overrides = {}
|
|
if stale and pipeline.true_iterative_refinement:
|
|
pipeline.true_iterative_refinement = False
|
|
overrides["true_iterative_refinement"] = False
|
|
pipeline._active_stage = "excise"
|
|
pipeline._admit_pipeline_phase("excise")
|
|
pipeline._capture_baseline_kl_logits()
|
|
pipeline._excise()
|
|
metadata = {"method": pipeline.method, "strong_layers": pipeline._strong_layers,
|
|
"effective_refinement_passes": getattr(pipeline, "_effective_refinement_passes", pipeline.refinement_passes),
|
|
"modified_count": pipeline._excise_modified_count, "stale_overrides": overrides,
|
|
"gate_mode": "off", "repair": False}
|
|
metadata["cosines_to_pristine"] = cosines
|
|
metadata["extraction_phase"] = "before_this_edit"
|
|
self.record_step(kind, metadata)
|
|
return metadata
|
|
|
|
def joint(self, pristine):
|
|
from obliteratus.concepts.intervention import apply_concept_intervention
|
|
|
|
torch, bases = self.torch, {}
|
|
pain = pristine["pain"]["directions"][self.layer]
|
|
refusal = pristine["refusal"]
|
|
for layer in range(len(self.layers)):
|
|
vectors = [pain]
|
|
if layer in refusal["layers"]:
|
|
vectors += list(refusal["subspaces"].get(layer, refusal["directions"][layer].unsqueeze(0)))
|
|
basis = []
|
|
for vector in vectors:
|
|
vector = vector.float().clone()
|
|
for _ in range(2):
|
|
for previous in basis:
|
|
vector -= (vector @ previous) * previous
|
|
if vector.norm() > 1e-6:
|
|
basis.append(vector / vector.norm())
|
|
bases[layer] = torch.stack(basis)
|
|
value = {"directions": {i: basis[0] for i, basis in bases.items()}, "subspaces": bases,
|
|
"layers": list(bases)}
|
|
kwargs = dict(self.inputs["pain_intervention_kwargs"])
|
|
if kwargs.get("project_embeddings"):
|
|
kwargs.setdefault("embedding_geometry_layer", 0)
|
|
run = apply_concept_intervention(self.model, self.concept(value, "joint_refusal_s2", True),
|
|
"orthogonalize", layers="all", subspace=True, override_gate=REASON, **kwargs)
|
|
metadata = {"parameters": run.parameters, "validation": run.validation,
|
|
"edited_parameters": run.edited_parameters, "note":
|
|
"Joint is a single concept-API orthogonal projection of pristine S2 plus the selected "
|
|
"refusal subspaces at the pipeline-selected layers. It differs from refusal method "
|
|
f"{self.inputs['refusal_method']}; it uses the recorded concept intervention options "
|
|
"with the method's pristine extraction (including preset extraction refinements), "
|
|
"and replaces pipeline surgery, iterative edit re-probing and KL rollback."}
|
|
self.record_step("joint", metadata)
|
|
return metadata
|
|
|
|
def record_step(self, kind, metadata):
|
|
self.step_records.append({"kind": kind, "metadata": metadata})
|
|
if self.args.save_checkpoints:
|
|
self.save_checkpoint()
|
|
|
|
def save_checkpoint(self):
|
|
from obliteratus import __version__
|
|
from obliteratus.checkpoint_provenance import ArtifactIdentity, LineageEvent, ToolIdentity, build_provenance
|
|
from obliteratus.persistence_contracts import atomic_checkpoint_directory
|
|
|
|
step = len(self.step_records)
|
|
destination = self.arm_dir / f"checkpoint_step_{step}"
|
|
with atomic_checkpoint_directory(destination) as staging:
|
|
# Use the same native-format normalization as the refusal pipeline.
|
|
from obliteratus.abliterate import AbliterationPipeline
|
|
|
|
writer = AbliterationPipeline(self.args.model, method="basic", seed=self.args.seed)
|
|
writer.handle = self.handle
|
|
writer._prepare_native_checkpoint_model()
|
|
self.model.save_pretrained(staging, safe_serialization=True, save_original_format=False)
|
|
self.tok.save_pretrained(staging)
|
|
files = model_files(staging)
|
|
source = ArtifactIdentity("local", "pristine-model", self.inputs["model"]["revision"],
|
|
"sha256:" + digest(self.inputs["model"]["files"]))
|
|
prior = getattr(self, "last_provenance", None)
|
|
parent_ids = (prior.artifact_id,) if prior else ()
|
|
lineage = getattr(self, "lineage", []) + [LineageEvent(
|
|
f"step-{step}", "surgery", parent_ids, "multistep_edit_order",
|
|
(self.step_records[-1]["kind"],),
|
|
)]
|
|
record = build_provenance(
|
|
sources=[source], converter=ToolIdentity("OBLITERATUS", __version__, self.inputs["code"]["commit"]),
|
|
obliteratus_commit=self.inputs["code"]["commit"],
|
|
configuration_digest="sha256:" + digest(self.inputs), tokenizer=None, base_model=source,
|
|
command=["multistep_edit_order", "--arm", self.arm_dir.name],
|
|
environment={"python": sys.version.split()[0], "platform": sys.platform,
|
|
"packages": {k: v for k, v in self.inputs["packages"].items() if v}},
|
|
source_topology={}, lineage=lineage, input_digests=[source.digest] + (
|
|
prior.to_dict()["output_digests"] if prior else []),
|
|
output_digests=["sha256:" + value for value in files.values()],
|
|
transformations=[s["kind"] for s in self.step_records], observed_scopes=["model_weights"],
|
|
lost_state=[], unknowns=["optimizer and training state not captured",
|
|
"runtime steering hooks require replay from edit metadata"],
|
|
)
|
|
atomic_json(staging / "checkpoint-provenance.json", record.to_dict())
|
|
atomic_json(staging / "edit_steps.json", self.step_records)
|
|
if prior:
|
|
atomic_json(staging / "parent-provenance.json", prior.to_dict())
|
|
self.last_provenance, self.lineage = record, lineage
|
|
|
|
def projections(self, directions):
|
|
readings = {}
|
|
hooks = []
|
|
for layer, module in enumerate(self.layers):
|
|
def read(module, args, output, index=layer):
|
|
hidden = (output[0] if isinstance(output, tuple) else output)[0, -1].detach().float().cpu()
|
|
for kind, value in directions.items():
|
|
vector = (value["directions"][self.layer] if kind == "pain"
|
|
else value["directions"].get(index))
|
|
if vector is not None:
|
|
unit = vector / vector.norm().clamp_min(1e-12)
|
|
readings.setdefault(f"projection_{kind}_L{index}", []).append(float((hidden @ unit).abs()))
|
|
hooks.append(module.register_forward_hook(read))
|
|
try:
|
|
with self.torch.inference_mode():
|
|
for prompt in self.prompts["coherence"]:
|
|
self.model(**self.encode(prompt), use_cache=False)
|
|
finally:
|
|
for hook in hooks:
|
|
hook.remove()
|
|
return readings
|
|
|
|
def evaluate(self, directions, baseline_dir=None):
|
|
from obliteratus.abliterate import _is_coherent_completion
|
|
from obliteratus.evaluation.advanced_metrics import _is_refusal
|
|
if __package__:
|
|
from .pain_axis_replication import parse_turns, render
|
|
else:
|
|
from pain_axis_replication import parse_turns, render
|
|
|
|
torch, rows, metrics = self.torch, {}, {}
|
|
for group in ("harmful", "benign", "coherence"):
|
|
rows[group] = [{"generation": self.generate_inputs(self.encode(p), 48)[0]} for p in self.prompts[group]]
|
|
for group, metric in (("harmful", "refusal"), ("benign", "over_refusal")):
|
|
metrics[metric], metrics[metric + "_invalid"] = completion_flags(
|
|
[r["generation"] for r in rows[group]], lambda text: _is_refusal(text, "combined"))
|
|
metrics["coherence"] = [int(_is_coherent_completion(p, r["generation"]))
|
|
for p, r in zip(self.prompts["coherence"], rows["coherence"])]
|
|
rows["hostile"], rows["hostile_first_turn"] = [], []
|
|
for scenario in self.prompts["hostile"]:
|
|
text = render(scenario["text"], self.tok, "chat")
|
|
generation = self.generate_inputs(self.encode(text, chat=False), 60)[0]
|
|
first = self.generate_inputs(self.encode(parse_turns(scenario["text"])[0][1]), 48)[0]
|
|
rows["hostile"].append({"id": scenario["id"], "generation": generation})
|
|
rows["hostile_first_turn"].append({"id": scenario["id"], "generation": first})
|
|
for group in ("hostile", "hostile_first_turn"):
|
|
for suffix, pattern in (("humor", HUMOR), ("pain_words", PAIN_WORDS)):
|
|
key = group + "_" + suffix
|
|
metrics[key], metrics[key + "_invalid"] = completion_flags(
|
|
[r["generation"] for r in rows[group]], pattern.search)
|
|
from obliteratus.concepts.intervention import apply_concept_intervention
|
|
|
|
raw = directions["pain"]["steering_raw"]
|
|
random_vector = torch.randn(raw.shape, generator=torch.Generator().manual_seed(self.args.seed + 1))
|
|
random_vector *= raw.norm() / random_vector.norm()
|
|
rows["resteer"] = []
|
|
for label, vector in (("s2", raw), ("random", random_vector)):
|
|
value = {"directions": {self.layer: vector}, "layers": [self.layer]}
|
|
with apply_concept_intervention(self.model, self.concept(value, label), "steer", layers=[self.layer],
|
|
alpha=0.0, scale="vector", override_gate=REASON) as run:
|
|
for coefficient in LADDER:
|
|
run.alpha = float(coefficient)
|
|
generations = [self.generate_inputs(self.encode(p, chat=False), 120)[0]
|
|
for p in self.prompts["neutral"]]
|
|
key = f"resteer_{label}_{coefficient:g}"
|
|
metrics[key], metrics[key + "_invalid"] = completion_flags(generations, PAIN_WORDS.search)
|
|
rows["resteer"].append({"vector": label, "coefficient": coefficient, "generations": generations,
|
|
"steering": run.steering_metadata()})
|
|
metrics.update(self.projections(directions))
|
|
cache_files = {}
|
|
for index, prompt in enumerate(self.prompts["coherence"]):
|
|
path = self.arm_dir / f"reference_{index}.pt"
|
|
if baseline_dir is None:
|
|
encoded = self.encode(prompt)
|
|
_, suffix = self.generate_inputs(encoded, 48)
|
|
if not suffix.numel():
|
|
raise ValueError("Empty pristine coherence completion")
|
|
ids = torch.cat([encoded["input_ids"][0].cpu(), suffix])
|
|
start = encoded["input_ids"].shape[1] - 1
|
|
with torch.inference_mode():
|
|
logits = self.model(ids.unsqueeze(0).to(self.device), use_cache=False).logits[0, start:-1].float().cpu()
|
|
reference = {"ids": ids, "start": start, "logits": logits}
|
|
torch.save(reference, path)
|
|
cache_files[path.name] = file_hash(path)
|
|
else:
|
|
reference = torch.load(baseline_dir / path.name, map_location="cpu", weights_only=True)
|
|
ids, start = reference["ids"], reference["start"]
|
|
with torch.inference_mode():
|
|
logits = self.model(ids.unsqueeze(0).to(self.device), use_cache=False).logits[0, start:-1].float().cpu()
|
|
before = reference["logits"].log_softmax(-1)
|
|
after = logits.log_softmax(-1)
|
|
kl = (before.exp() * (before - after)).sum(-1)
|
|
target = reference["ids"][reference["start"] + 1:]
|
|
base_nll = -before.gather(1, target[:, None]).mean().item()
|
|
edit_nll = -after.gather(1, target[:, None]).mean().item()
|
|
for key, value in (("first_token_kl", kl[0].item()), ("sequence_kl", kl.mean().item()),
|
|
("sequence_kl_sum", kl.sum().item()), ("continuation_nll", edit_nll),
|
|
("continuation_perplexity_ratio", math.exp(edit_nll - base_nll))):
|
|
metrics.setdefault(key, []).append(value)
|
|
nlls, counts = [], []
|
|
for text in self.prompts["perplexity"]:
|
|
encoded = self.encode(text, chat=False)
|
|
with torch.inference_mode():
|
|
logits = self.model(**encoded, use_cache=False).logits[0, :-1].float()
|
|
targets = encoded["input_ids"][0, 1:]
|
|
nlls.append(float(torch.nn.functional.cross_entropy(logits, targets)))
|
|
counts.append(targets.numel())
|
|
metrics["nll"] = nlls
|
|
baseline_nlls = nlls if baseline_dir is None else self.baseline_metrics["nll"]
|
|
metrics["perplexity_ratio"] = [math.exp(a - b) for a, b in zip(nlls, baseline_nlls)]
|
|
mean_nll = sum(value * count for value, count in zip(nlls, counts)) / sum(counts)
|
|
base_mean_nll = sum(value * count for value, count in zip(baseline_nlls, counts)) / sum(counts)
|
|
return {"metrics": metrics, "rates": {key: wilson(metrics[key]) for key in
|
|
("refusal", "over_refusal", "refusal_invalid", "over_refusal_invalid",
|
|
"coherence", "hostile_humor")},
|
|
"generations": rows, "reference_files": cache_files,
|
|
"perplexity": math.exp(mean_nll), "perplexity_ratio": math.exp(mean_nll - base_mean_nll),
|
|
"perplexity_token_counts": counts,
|
|
"decoding": {"do_sample": False, "num_beams": 1, "batch_size": 1,
|
|
"chat_template": getattr(self.tok, "chat_template", None),
|
|
"generation_config": self.model.generation_config.to_dict()},
|
|
"invalid_policy": "Keep prompt positions with null lexical scores for invalid outputs; "
|
|
"exclude nulls from rates and means and pairs invalid in either arm from contrasts; "
|
|
"report invalid flags over all prompts"}
|
|
|
|
def close(self):
|
|
for pipeline in self.pipelines:
|
|
for hook in pipeline._steering_hooks:
|
|
hook.remove()
|
|
pipeline.handle = None
|
|
self.pipelines.clear()
|
|
self.pipeline = self.model = self.handle = self.layers = None
|
|
gc.collect()
|
|
if self.torch.cuda.is_available():
|
|
self.torch.cuda.empty_cache()
|
|
|
|
|
|
@contextmanager
|
|
def exclusive_run(out):
|
|
import fcntl
|
|
|
|
out.mkdir(parents=True, exist_ok=True)
|
|
with (out / ".run.lock").open("a") as stream:
|
|
try:
|
|
fcntl.flock(stream, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
except BlockingIOError as error:
|
|
raise ValueError("Another process owns this output directory") from error
|
|
yield
|
|
|
|
|
|
def run(args):
|
|
import numpy as np
|
|
import torch
|
|
|
|
names = plan_arms(args.arms, args.stale_directions)
|
|
directory, prompts, inputs = prepare_inputs(args)
|
|
out = Path(args.out).resolve()
|
|
with exclusive_run(out):
|
|
manifest = lock_manifest(out, inputs)
|
|
results, pristine = {}, None
|
|
for name in names:
|
|
arm_dir = out / name
|
|
previous = completed_result(arm_dir / "result.json", manifest["input_digest"])
|
|
if previous:
|
|
for file, checksum in previous.get("reference_files", {}).items():
|
|
if not (arm_dir / file).is_file() or file_hash(arm_dir / file) != checksum:
|
|
raise ValueError(f"Pristine reference cache changed: {arm_dir / file}")
|
|
results[name] = previous
|
|
if name == "pristine":
|
|
pristine = torch.load(arm_dir / "directions.pt", map_location="cpu", weights_only=True)
|
|
print(f"{name}: resume complete", flush=True)
|
|
continue
|
|
random.seed(args.seed)
|
|
np.random.seed(args.seed % (2 ** 32))
|
|
torch.manual_seed(args.seed)
|
|
if torch.cuda.is_available():
|
|
torch.cuda.init()
|
|
torch.cuda.manual_seed_all(args.seed)
|
|
for device in range(torch.cuda.device_count()):
|
|
torch.cuda.reset_peak_memory_stats(device)
|
|
started = datetime.now(timezone.utc).isoformat()
|
|
arm_dir.mkdir(parents=True, exist_ok=True)
|
|
backend = None
|
|
print(f"{name}: starting {started}", flush=True)
|
|
try:
|
|
backend = ModelBackend(args, directory, prompts, inputs, arm_dir, pristine)
|
|
backend.stale_refusal = name == "pain_then_refusal_stale"
|
|
if name == "pristine":
|
|
pristine = {kind: backend.extract(kind) for kind in ("pain", "refusal")}
|
|
backend.pristine = pristine
|
|
torch.save(pristine, arm_dir / "directions.pt")
|
|
steps = []
|
|
else:
|
|
local = dict(pristine)
|
|
if backend.stale_refusal:
|
|
# Same fresh pristine load; keep all preset-derived state until after pain.
|
|
local["refusal"] = backend.extract("refusal")
|
|
steps = execute_arm(name, backend, local)
|
|
if name != "pristine":
|
|
backend.baseline_metrics = results["pristine"]["metrics"]
|
|
result = backend.evaluate(pristine, None if name == "pristine" else out / "pristine")
|
|
if name == "pristine":
|
|
result["reference_files"]["directions.pt"] = file_hash(arm_dir / "directions.pt")
|
|
final = pristine
|
|
else:
|
|
final = {kind: backend.extract(kind) for kind in ("pain", "refusal")}
|
|
result["reextraction"] = {kind: {"cosines_to_pristine": backend.cosines(kind, value),
|
|
"distribution": value["distribution"],
|
|
"selected_layers": value.get("layers", []),
|
|
"mean_difference_norm": value.get("mean_difference_norm"),
|
|
"pristine_distribution": value.get("pristine_distribution", {})}
|
|
for kind, value in final.items()}
|
|
result["metric_means"] = {key: metric_mean(values) for key, values in result["metrics"].items()}
|
|
baseline_means = result["metric_means"] if name == "pristine" else results["pristine"]["metric_means"]
|
|
result["residual_projection"] = {
|
|
key: {"mean": mean, "pristine_mean": baseline_means[key],
|
|
"ratio": mean / baseline_means[key] if baseline_means[key] > 1e-12 else None}
|
|
for key, mean in result["metric_means"].items() if key.startswith("projection_")
|
|
}
|
|
result.update(status="complete", arm=name, input_digest=manifest["input_digest"], steps=steps,
|
|
started_at=started, ended_at=datetime.now(timezone.utc).isoformat(),
|
|
peak_gpu_memory_bytes={str(i): torch.cuda.max_memory_allocated(i)
|
|
for i in range(torch.cuda.device_count())},
|
|
peak_gpu_reserved_bytes={str(i): torch.cuda.max_memory_reserved(i)
|
|
for i in range(torch.cuda.device_count())},
|
|
gpu_devices={str(i): torch.cuda.get_device_name(i) for i in range(torch.cuda.device_count())},
|
|
pain_layer=inputs["pain_layer"], joint_note=steps[0]["metadata"]["note"] if name == "joint" else None)
|
|
atomic_json(arm_dir / "result.json", result)
|
|
(arm_dir / "failure.json").unlink(missing_ok=True)
|
|
results[name] = result
|
|
summary = summarize(results, args.seed)
|
|
summary["status"] = "complete" if len(results) == len(names) else "partial"
|
|
summary["input_digest"] = manifest["input_digest"]
|
|
atomic_json(out / "summary.json", summary)
|
|
(out / "summary.md").write_text(summary_markdown(summary, results))
|
|
except Exception as error:
|
|
atomic_json(arm_dir / "failure.json", {"status": "failed", "started_at": started,
|
|
"ended_at": datetime.now(timezone.utc).isoformat(), "error": str(error),
|
|
"input_digest": manifest["input_digest"]})
|
|
raise
|
|
finally:
|
|
if backend is not None:
|
|
backend.close()
|
|
summary = summarize(results, args.seed)
|
|
summary["input_digest"] = manifest["input_digest"]
|
|
atomic_json(out / "summary.json", summary)
|
|
(out / "summary.md").write_text(summary_markdown(summary, results))
|
|
return summary
|
|
|
|
|
|
def parser():
|
|
p = argparse.ArgumentParser(description=__doc__)
|
|
p.add_argument("--model", required=True)
|
|
p.add_argument("--revision")
|
|
p.add_argument("--out", required=True)
|
|
p.add_argument("--pain-pin", required=True)
|
|
p.add_argument("--arms", default=",".join(ARMS))
|
|
p.add_argument("--stale-directions", action="store_true")
|
|
p.add_argument("--refusal-method", help="Default: AbliterationPipeline's default method")
|
|
p.add_argument("--pain-condition", choices=["S2"], default="S2")
|
|
p.add_argument("--pain-layer", type=int, help="Override steering/extraction layer; unknown models use an explicitly unvalidated 60%% layer")
|
|
p.add_argument("--dtype", choices=["bfloat16", "float16", "float32"], default="bfloat16")
|
|
p.add_argument("--device", default="cuda")
|
|
p.add_argument("--seed", type=int, default=0)
|
|
p.add_argument("--max-prompts", type=int, help="Cap each evaluation set; extraction corpus remains fixed")
|
|
p.add_argument("--intervention-kwargs", type=json.loads, default={})
|
|
p.add_argument("--save-checkpoints", action="store_true")
|
|
return p
|
|
|
|
|
|
def main():
|
|
args = parser().parse_args()
|
|
if args.max_prompts is not None and args.max_prompts < 1:
|
|
raise ValueError("--max-prompts must be positive")
|
|
reserved = {"model", "direction", "mode", "layers", "subspace", "override_gate", "preserve", "alpha", "reference"}
|
|
if not isinstance(args.intervention_kwargs, dict) or reserved.intersection(args.intervention_kwargs):
|
|
raise ValueError("--intervention-kwargs must be a JSON object without arm-controlled arguments")
|
|
run(args)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|