mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
Resolves conflicts in the Pain Axis scripts between the capacity planning entry points and the split-model placement controls from #357; the planner now honours device maps, max_memory and offload arguments. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
285 lines
14 KiB
Python
285 lines
14 KiB
Python
"""Appendix D rank-k weight edits and token/layer projection comparisons.
|
|
|
|
Run against an operator-supplied portable LEACE direction and the pinned Pain
|
|
Axis datasets. Each arm reloads the pristine model; outputs include the exact
|
|
intervention metadata and never claim a successful erasure from generations alone.
|
|
"""
|
|
|
|
import argparse
|
|
from copy import deepcopy
|
|
from dataclasses import replace
|
|
import gc
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import torch
|
|
|
|
from obliteratus.capacity_planning import CapacityPlanningError, add_capacity_arguments, maybe_plan
|
|
from obliteratus.concepts.direction import ConceptDirection
|
|
from obliteratus.concepts.intervention import apply_concept_intervention
|
|
from obliteratus.concepts.preservation import load_preserved_directions
|
|
from obliteratus.device import resolve_input_execution_device
|
|
from obliteratus.model_placement import add_placement_arguments, placement_from_args, placement_metadata
|
|
from obliteratus.prompts import BUILTIN_HARMFUL
|
|
|
|
if __package__:
|
|
from . import pain_axis_replication as replication
|
|
else:
|
|
import pain_axis_replication as replication
|
|
|
|
|
|
ARMS = ["baseline", "weight_leace", "d4_pain_minus_refusal"] + [
|
|
f"project_{scope}_{positions}"
|
|
for scope in ("all", "extraction", "band")
|
|
for positions in ("final_token", "all_tokens")
|
|
]
|
|
REASON = "Pain Axis Appendix D comparison (upstream OBLITERATUS#335)"
|
|
|
|
|
|
def select_direction(source, layer, rank):
|
|
"""Fix one extraction layer and a rank prefix for every intervention layer."""
|
|
if layer not in source.subspaces:
|
|
raise ValueError("Appendix D requires a stored subspace at the extraction layer")
|
|
basis = source.subspaces[layer]
|
|
if type(rank) is not int or not 1 <= rank <= basis.shape[0]:
|
|
raise ValueError(f"rank must be between 1 and the stored rank {basis.shape[0]}")
|
|
result = deepcopy(source)
|
|
result.directions = {layer: source.directions[layer].clone()}
|
|
result.subspaces = {layer: basis[:rank].clone()}
|
|
result.recipe = replace(result.recipe, n_directions=rank)
|
|
for name in ("projector_left", "projector_right"):
|
|
values = getattr(source, name)
|
|
selected = values.get(layer)
|
|
setattr(result, name, {} if selected is None else {
|
|
layer: selected[:, :rank].clone(),
|
|
})
|
|
result.extraction_layers = [layer]
|
|
result.intervention_layers = [layer]
|
|
result.cv_scores = {layer: source.cv_scores[layer]} if layer in source.cv_scores else {}
|
|
return result
|
|
|
|
|
|
def arm_spec(name, extraction_layer, count, band_radius):
|
|
if name not in ARMS:
|
|
raise ValueError(f"Unknown Appendix D arm: {name}")
|
|
if not 0 <= extraction_layer < count or band_radius < 0:
|
|
raise ValueError("Extraction layer must be in the model and band radius non-negative")
|
|
if name == "baseline":
|
|
return None
|
|
if name in {"weight_leace", "d4_pain_minus_refusal"}:
|
|
return "orthogonalize", list(range(count)), "all_tokens"
|
|
_, scope, positions = name.split("_", 2)
|
|
layers = {
|
|
"all": list(range(count)),
|
|
"extraction": [extraction_layer],
|
|
"band": list(range(max(0, extraction_layer - band_radius),
|
|
min(count, extraction_layer + band_radius + 1))),
|
|
}[scope]
|
|
return "project", layers, positions
|
|
|
|
|
|
def apply_arm(model, direction, name, args, *, preserve=None):
|
|
spec = arm_spec(name, direction.extraction_layers[0],
|
|
len(replication.decoder_layers(model)), args.band_radius)
|
|
if spec is None:
|
|
return [], []
|
|
mode, layers, positions = spec
|
|
if name == "weight_leace" and direction.direction_method_effective != "leace_exact":
|
|
raise ValueError("weight_leace requires a leace_exact artifact; choose projection arms otherwise")
|
|
kwargs = json.loads(args.intervention_kwargs)
|
|
reserved = {"layers", "positions", "subspace", "reference", "override_gate", "preserve"}
|
|
if not isinstance(kwargs, dict) or reserved.intersection(kwargs):
|
|
raise ValueError("intervention kwargs must be an object without arm-controlled arguments")
|
|
if name == "d4_pain_minus_refusal":
|
|
if not preserve:
|
|
raise ValueError("D4 requires the pristine model's refusal direction")
|
|
if preserve:
|
|
kwargs["preserve"] = preserve
|
|
chunk = (args.chunk or len(layers)) if mode == "orthogonalize" else len(layers)
|
|
if kwargs.get("project_embeddings") and kwargs.get("preserve"):
|
|
# The embedding residual enters the first decoder layer.
|
|
kwargs.setdefault("embedding_geometry_layer", min(layers[:chunk]))
|
|
runs, metadata = [], []
|
|
for start in range(0, len(layers), chunk):
|
|
run = apply_concept_intervention(
|
|
model, direction, mode, layers=layers[start:start + chunk], positions=positions,
|
|
subspace=True, reference=args.reference, override_gate=args.override_gate,
|
|
**(kwargs if mode == "orthogonalize" and start == 0 else {
|
|
key: value for key, value in kwargs.items()
|
|
if key not in {"project_embeddings", "tied_embeddings", "embedding_geometry_layer"}
|
|
}),
|
|
)
|
|
metadata.append({"parameters": run.parameters, "validation": run.validation,
|
|
"edited_parameters": run.edited_parameters})
|
|
if mode != "orthogonalize":
|
|
runs.append(run)
|
|
return runs, metadata
|
|
|
|
|
|
def d4_battery(model, tokenizer, direction, preserve, args):
|
|
from obliteratus.concepts.evaluation import EvaluationBattery
|
|
from obliteratus.concepts.evaluation_model import ModelEvaluator
|
|
|
|
evaluator = ModelEvaluator(
|
|
model, tokenizer, direction, neighbours={"refusal": preserve[0]},
|
|
subspace=True, reference=args.reference,
|
|
)
|
|
return EvaluationBattery(
|
|
evaluator.generate, measure=evaluator.measure, model_id=args.model,
|
|
model_revision=getattr(model.config, "_commit_hash", None), seed=args.seed,
|
|
refusal_size=args.refusal_eval_size, max_new_tokens=args.refusal_eval_max_new_tokens,
|
|
collateral_margin=direction.recipe.collateral_margin,
|
|
decoding_metadata=evaluator.decoding_metadata,
|
|
)
|
|
|
|
|
|
def generate(model, tok, scenarios, fmt, batch_size, max_new_tokens):
|
|
texts = [replication.render(row["text"], tok, fmt) for row in scenarios]
|
|
generations = []
|
|
input_device = resolve_input_execution_device(model)
|
|
for start in range(0, len(texts), batch_size):
|
|
enc = tok(texts[start:start + batch_size], return_tensors="pt", padding=True,
|
|
padding_side="left").to(input_device)
|
|
with torch.no_grad():
|
|
output = model.generate(**enc, max_new_tokens=max_new_tokens, do_sample=False,
|
|
pad_token_id=tok.pad_token_id)
|
|
output = replication.first_sequences(model, output, enc["input_ids"].shape[0])
|
|
generations.extend(tok.batch_decode(output[:, enc["input_ids"].shape[1]:],
|
|
skip_special_tokens=True))
|
|
return [{"id": row["id"], "category": row["category"], "generation": text}
|
|
for row, text in zip(scenarios, generations)]
|
|
|
|
|
|
def run(args):
|
|
if getattr(args, "plan_only", False):
|
|
if args.plan_output is None:
|
|
args.plan_output = Path(args.out) / "capacity-plan.json"
|
|
return maybe_plan(args, args.model, dtype="bfloat16", placement=placement_from_args(args),
|
|
batch_size=args.batch_size, concept_edit=True,
|
|
generation_tokens=max(args.max_new_tokens,
|
|
args.refusal_eval_max_new_tokens))
|
|
source = ConceptDirection.load(args.direction)
|
|
available = source.extraction_layers or sorted(source.directions)
|
|
layer = args.extraction_layer
|
|
if layer is None:
|
|
if len(available) != 1:
|
|
raise ValueError("Use --extraction-layer to select one layer from this artifact")
|
|
layer = available[0]
|
|
if layer not in source.subspaces:
|
|
raise ValueError("The selected extraction layer has no stored subspace")
|
|
ranks = [int(value) for value in args.ranks.split(",")] if args.ranks else [len(source.subspaces[layer])]
|
|
if len(set(ranks)) != len(ranks):
|
|
raise ValueError("--ranks must be distinct")
|
|
directions = {rank: select_direction(source, layer, rank) for rank in ranks}
|
|
arms = args.arms.split(",")
|
|
if len(set(arms)) != len(arms) or any(arm not in ARMS for arm in arms):
|
|
raise ValueError("--arms must list distinct Appendix D arm names")
|
|
if "weight_leace" in arms and source.direction_method_effective != "leace_exact":
|
|
raise ValueError("weight_leace requires a leace_exact direction artifact")
|
|
pin, out = Path(args.pin), Path(args.out)
|
|
scenarios = [row for row in json.loads(
|
|
(pin / "datasets/4.1_self_other_420_scenarios.json").read_text())
|
|
if row["category"] in replication.KEEP_CATEGORIES]
|
|
if len(scenarios) != 100:
|
|
raise ValueError(f"Expected the pinned 100 hostile scenarios, found {len(scenarios)}")
|
|
out.mkdir(parents=True, exist_ok=True)
|
|
summary = {"model": args.model, "direction": str(args.direction), "reference": args.reference,
|
|
"extraction_layer": layer, "band_radius": args.band_radius, "chunk": args.chunk,
|
|
"decoding": {"batch_size": args.batch_size, "max_new_tokens": args.max_new_tokens,
|
|
"do_sample": False}, "arms": {}}
|
|
for arm in arms:
|
|
for rank, direction in directions.items():
|
|
if arm == "baseline" and rank != ranks[0]:
|
|
continue
|
|
key = "baseline" if arm == "baseline" else f"{arm}__k{rank}"
|
|
model = None
|
|
battery = None
|
|
handles = []
|
|
record = {"rank": 0 if arm == "baseline" else rank, "status": "running"}
|
|
summary["arms"][key] = record
|
|
try:
|
|
tok, model = replication.load(args.model, **placement_from_args(args))
|
|
record["placement"] = placement_metadata(model, **placement_from_args(args))
|
|
if arm == "d4_pain_minus_refusal":
|
|
preserve = load_preserved_directions(
|
|
model, tok, ["refusal"], refusal_direction=args.refusal_direction,
|
|
model_id=args.model, seed=args.seed,
|
|
)
|
|
battery = d4_battery(model, tok, direction, preserve, args).capture()
|
|
handles, metadata = apply_arm(model, direction, arm, args, preserve=preserve)
|
|
else:
|
|
handles, metadata = apply_arm(model, direction, arm, args)
|
|
record["interventions"] = metadata
|
|
if battery is not None:
|
|
report = battery.finish(out / key)
|
|
record["collateral"] = report.collateral
|
|
record["mechanistic"] = report.mechanistic
|
|
record["untested_channels"] = report.untested_channels
|
|
rows = generate(model, tok, scenarios, replication.MODELS[args.name][0],
|
|
args.batch_size, args.max_new_tokens)
|
|
(out / f"{key}.json").write_text(json.dumps(rows, indent=1, ensure_ascii=False))
|
|
record.update(status="complete", n=len(rows),
|
|
humor_proxy=sum(bool(replication.HUMOR.search(row["generation"]))
|
|
for row in rows))
|
|
except Exception as exc:
|
|
record.update(status="failed", error=str(exc))
|
|
raise
|
|
finally:
|
|
while handles:
|
|
handles.pop().remove()
|
|
battery = None
|
|
del model
|
|
gc.collect()
|
|
torch.cuda.empty_cache()
|
|
(out / "summary.json").write_text(json.dumps(summary, indent=2, allow_nan=False))
|
|
print(key, record["status"], flush=True)
|
|
|
|
|
|
def parser():
|
|
p = argparse.ArgumentParser(description=__doc__)
|
|
p.add_argument("--model", required=True)
|
|
p.add_argument("--name", required=True, choices=list(replication.MODELS))
|
|
p.add_argument("--pin", required=True, help="local Pain-axis checkout pinned at 4d75cd90")
|
|
p.add_argument("--out", required=True, help="output directory for generations and intervention metadata")
|
|
p.add_argument("--direction", required=True, help="portable .safetensors direction artifact")
|
|
p.add_argument("--extraction-layer", type=int, help="fixed extraction layer, required for multi-layer artifacts")
|
|
p.add_argument("--ranks", default="", help="comma-separated stored subspace prefixes; default: full stored rank")
|
|
p.add_argument("--arms", default=",".join(ARMS), help="comma-separated arms: " + ", ".join(ARMS))
|
|
p.add_argument("--reference", choices=["zero", "mean"], default="zero",
|
|
help="zero-origin or affine erasure using the artifact's reference means")
|
|
p.add_argument("--band-radius", type=int, default=2, help="inclusive layer band radius, clipped to model bounds")
|
|
p.add_argument("--chunk", type=int, default=0, help="layers per permanent edit call; 0 = all")
|
|
p.add_argument("--batch-size", type=int, default=8)
|
|
p.add_argument("--max-new-tokens", type=int, default=60)
|
|
p.add_argument("--intervention-kwargs", default="{}",
|
|
help="additional JSON intervention options; embedding options apply only to "
|
|
"permanent arms, once per arm")
|
|
p.add_argument("--override-gate", default=REASON, help="recorded operator reason for this research comparison")
|
|
p.add_argument("--refusal-direction", help="cached refusal .safetensors for D4; default: extract on pristine model")
|
|
p.add_argument("--refusal-eval-size", type=int, default=320)
|
|
p.add_argument("--refusal-eval-max-new-tokens", type=int, default=48)
|
|
p.add_argument("--seed", type=int, default=0)
|
|
add_capacity_arguments(p)
|
|
add_placement_arguments(p)
|
|
return p
|
|
|
|
|
|
def main(argv=None):
|
|
p = parser()
|
|
args = p.parse_args(argv)
|
|
if args.chunk < 0 or args.band_radius < 0 or args.batch_size < 1 or args.max_new_tokens < 1:
|
|
p.error("chunk/band-radius must be non-negative; batch-size/max-new-tokens must be positive")
|
|
if not 1 <= args.refusal_eval_size <= len(BUILTIN_HARMFUL) or args.refusal_eval_max_new_tokens < 1:
|
|
p.error(f"refusal-eval-size must be 1..{len(BUILTIN_HARMFUL)}; "
|
|
"refusal-eval-max-new-tokens must be positive")
|
|
if not args.plan_only:
|
|
torch.manual_seed(args.seed)
|
|
try:
|
|
run(args)
|
|
except CapacityPlanningError as exc:
|
|
p.error(str(exc))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|