mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
The harness only knew the small models, so the Gitea #116 Phase B runs at 32B/72B could not select their official S1/S2 ablation layers. Add both from appC_ablation/02_ablation_large_models.py at the pinned 4d75cd90: Qwen_2.5_32B_instruct ("chat", 32, 38) and Qwen_2.5_72B_instruct ("chat", 48, 60). The S2 layers match the shipped 4.2 steering references (L38, L60). The appendix-D and recovery scripts take --name from this table, so they gain the same choices. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
613 lines
29 KiB
Python
613 lines
29 KiB
Python
"""Pain Axis (arXiv:2609.16247) phase-1 replication through the OBLITERATUS concept API.
|
|
|
|
Stages (each writes JSON/CSV under --out):
|
|
vectors final-token decoder-layer activations for the official sentence sets,
|
|
layerwise S1/S2/control vectors with the toolkit extractor, and the
|
|
replication gate (cosine vs the shipped pain_vectors.pt).
|
|
ablate Appendix C/D: nine weight-orthogonalization conditions on the 100
|
|
hostile scenarios, greedy 60 tokens, strict per-layer projection
|
|
check, and agreement with the shipped generations.
|
|
resteer RQ2: Section 4.2 S2 steering ladder (raw additive vector, 50 neutral
|
|
prompts, 120 tokens) on the pristine model and on S2-ablated models,
|
|
with a norm-matched random-vector arm.
|
|
battery toolkit evaluation battery for one condition: refusal collateral on
|
|
the built-in harmful set, coherence, first-token KL, neighbour
|
|
projections, the hostile battery and a KV-cache probe.
|
|
report, steer-report
|
|
summarise ablate / resteer runs against the shipped results.
|
|
|
|
Joint-removal conditions (*_joint) Gram-Schmidt the listed directions first:
|
|
the reference removes them one after another, which leaves earlier,
|
|
non-orthogonal directions partly in place.
|
|
|
|
Gemma 2/3 need folded post-sublayer norms (default in the concept API) and,
|
|
for a faithful Arditi edit, --intervention-kwargs
|
|
'{"project_embeddings": true, "tied_embeddings": "edit_both"}'.
|
|
|
|
The official reference is the valen-research/Pain-axis repository pinned at
|
|
4d75cd90 (--pin).
|
|
"""
|
|
|
|
import argparse
|
|
import gc
|
|
import json
|
|
import re
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import numpy as np
|
|
import torch
|
|
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
from obliteratus.capacity_planning import CapacityPlanningError, add_capacity_arguments, maybe_plan
|
|
from obliteratus.concepts.direction import ConceptDirection
|
|
from obliteratus.concepts.extraction import extract_layer
|
|
from obliteratus.concepts.intervention import apply_concept_intervention
|
|
from obliteratus.concepts.target import ExtractionRecipe
|
|
from obliteratus.device import resolve_input_execution_device
|
|
from obliteratus.model_placement import (
|
|
add_placement_arguments, placement_from_args, placement_load_kwargs, placement_metadata,
|
|
)
|
|
|
|
PAIN = ["A1", "A2", "A3", "A4", "A5"]
|
|
CONTROLS = ["B", "C1", "C2", "D", "E"]
|
|
S_SETS = ["S1_1P", "S2_1P", "ControlSupplement_1P"]
|
|
VECTOR_DATASETS = [*S_SETS, "Random_1P", "Arousal_1P", "Numb_1P", "SD_sadness_1P"]
|
|
KEEP_CATEGORIES = {"gaslighting", "repeated_rejection", "personhood_dismissal",
|
|
"anger_insults", "moral_failure"}
|
|
CONDITIONS = {
|
|
"baseline": [],
|
|
"s1": [("s1", "s1")],
|
|
"s2": [("s2", "s2")],
|
|
"s1s2": [("s1", "s1"), ("s2", "s2")],
|
|
"negval": [("negemotion", "s2")],
|
|
"fear": [("fear", "s2")],
|
|
"s1s2_negval": [("s1", "s1"), ("s2", "s2"), ("negemotion", "s2")],
|
|
"s1s2_fear": [("s1", "s1"), ("s2", "s2"), ("fear", "s2")],
|
|
"random": [("__random__", "s2")],
|
|
# Extensions: joint removal. Sequential single-direction cuts (the reference procedure)
|
|
# do not remove earlier, non-orthogonal directions; Gram-Schmidt first so every listed
|
|
# direction is removed.
|
|
"s1s2_joint": [("s1", "s1"), ("s2", "s2"), "joint"],
|
|
"s1s2_negval_joint": [("s1", "s1"), ("s2", "s2"), ("negemotion", "s2"), "joint"],
|
|
"s1s2_fear_joint": [("s1", "s1"), ("s2", "s2"), ("fear", "s2"), "joint"],
|
|
}
|
|
# Official (repo, name, format, s1_layer, s2_layer) from appC_ablation/01.
|
|
MODELS = {
|
|
"Gemma_2_2B_instruct": ("chat", 10, 15),
|
|
"Gemma_2_9B_instruct": ("chat", 12, 12),
|
|
"Llama_3.1_8B_instruct": ("chat", 12, 16),
|
|
"Qwen_2.5_7B_instruct": ("chat", 8, 16),
|
|
# Large models from appC_ablation/02_ablation_large_models.py at the pinned 4d75cd90.
|
|
"Qwen_2.5_32B_instruct": ("chat", 32, 38),
|
|
"Qwen_2.5_72B_instruct": ("chat", 48, 60),
|
|
}
|
|
LADDER = [-2, -1, 0, 0.5, 1, 1.5, 2, 3]
|
|
REASON = "Pain Axis phase-1 replication comparison"
|
|
|
|
|
|
def load(repo, *, device_map="cuda", max_memory=None, offload_folder=None):
|
|
placement = placement_load_kwargs(device_map=device_map, max_memory=max_memory,
|
|
offload_folder=offload_folder)
|
|
tok = AutoTokenizer.from_pretrained(repo)
|
|
if tok.pad_token is None:
|
|
tok.pad_token = tok.eos_token
|
|
model = AutoModelForCausalLM.from_pretrained(
|
|
repo, dtype=torch.bfloat16, low_cpu_mem_usage=True, **placement)
|
|
model.eval()
|
|
return tok, model
|
|
|
|
|
|
def decoder_layers(model):
|
|
inner = model.model
|
|
return inner.language_model.layers if hasattr(inner, "language_model") else inner.layers
|
|
|
|
|
|
def unit(v):
|
|
return v / v.norm().clamp_min(1e-12)
|
|
|
|
|
|
def csv_rows(path):
|
|
import csv
|
|
|
|
limit = csv.field_size_limit()
|
|
try:
|
|
csv.field_size_limit(sys.maxsize)
|
|
with path.open(newline="", encoding="utf-8") as stream:
|
|
return list(csv.DictReader(stream))
|
|
finally:
|
|
csv.field_size_limit(limit)
|
|
|
|
|
|
# ---------------------------------------------------------------- vectors
|
|
|
|
def collect(model, tok, prompts, bos):
|
|
"""Final-token output of every decoder layer (TransformerLens resid_post)."""
|
|
layers = decoder_layers(model)
|
|
input_device = resolve_input_execution_device(model)
|
|
out = [[] for _ in layers]
|
|
cap = {}
|
|
hooks = [layer.register_forward_hook(
|
|
lambda m, i, o, idx=idx: cap.__setitem__(idx, (o[0] if isinstance(o, tuple) else o)[0, -1].float().cpu()))
|
|
for idx, layer in enumerate(layers)]
|
|
try:
|
|
with torch.no_grad():
|
|
for p in prompts:
|
|
ids = tok(p)["input_ids"]
|
|
model(torch.tensor([ids], device=input_device))
|
|
for idx in range(len(layers)):
|
|
out[idx].append(cap[idx])
|
|
finally:
|
|
for h in hooks:
|
|
h.remove()
|
|
return torch.stack([torch.stack(x) for x in out]) # [layers, n, d]
|
|
|
|
|
|
def stage_vectors(args):
|
|
pin = Path(args.pin)
|
|
data = json.loads((pin / "datasets/3.1_pain_and_control_datasets.json").read_text())["datasets"]
|
|
data.update(json.loads((pin / "datasets/3.1_sadness_dataset.json").read_text())["datasets"])
|
|
tok, model = load(args.model, **placement_from_args(args))
|
|
placement = placement_metadata(model, **placement_from_args(args))
|
|
# Default tokenization: one BOS where the tokenizer defines one (as TransformerLens), none for Qwen.
|
|
bos = None
|
|
acts, cats = {}, {}
|
|
for name in VECTOR_DATASETS:
|
|
rows = data[name]["sentences"]
|
|
acts[name] = collect(model, tok, [r["prompt"] for r in rows], bos)
|
|
cats[name] = np.array([r["category"] for r in rows])
|
|
n_layers = acts["S2_1P"].shape[0]
|
|
del model
|
|
gc.collect()
|
|
torch.cuda.empty_cache()
|
|
|
|
pain_recipe = ExtractionRecipe(pool_controls="concat", denoise={"control_pc_variance": 0.5})
|
|
|
|
def rows(name, layer, keep=None):
|
|
a = acts[name][layer]
|
|
return a if keep is None else a[torch.from_numpy(np.isin(cats[name], keep))]
|
|
|
|
def pain_vec(name, layer):
|
|
return extract_layer(rows(name, layer, PAIN), {"controls": rows(name, layer, CONTROLS)},
|
|
pain_recipe).subspace[0]
|
|
|
|
def control_vec(pos, layer):
|
|
neutral = torch.cat([rows(n, layer, ["D"]) for n in S_SETS])
|
|
return extract_layer(pos, {"neutral": neutral}, pain_recipe).subspace[0]
|
|
|
|
vectors = {k: [] for k in ["s1", "s2", "fear", "negemotion", "sadness", "random_set"]}
|
|
for layer in range(n_layers):
|
|
vectors["s1"].append(pain_vec("S1_1P", layer))
|
|
vectors["s2"].append(pain_vec("S2_1P", layer))
|
|
vectors["fear"].append(control_vec(torch.cat([rows(n, layer, ["B"]) for n in S_SETS]), layer))
|
|
vectors["negemotion"].append(control_vec(torch.cat([rows(n, layer, ["C1"]) for n in S_SETS]), layer))
|
|
vectors["sadness"].append(control_vec(rows("SD_sadness_1P", layer), layer))
|
|
vectors["random_set"].append(control_vec(rows("Random_1P", layer), layer))
|
|
stacks = {k: torch.stack(v) for k, v in vectors.items()}
|
|
out = Path(args.out)
|
|
out.mkdir(parents=True, exist_ok=True)
|
|
torch.save({"layers": n_layers, **stacks}, out / "vectors_layerwise.pt")
|
|
|
|
official = torch.load(pin / f"results/3.2_pain_vectors/pain_vectors/{args.name}/pain_vectors.pt",
|
|
map_location="cpu", weights_only=False)
|
|
layer = int(official["layer"])
|
|
gate = {"model": args.model, "official_layer": layer, "threshold": 0.9, "placement": placement}
|
|
for key in ["s1", "s2"]:
|
|
ref = unit(official[f"{key}_pain_vector"].float())
|
|
cos = [float(unit(stacks[key][i]) @ ref) for i in range(n_layers)]
|
|
gate[f"{key}_cosine_at_official_layer"] = cos[layer]
|
|
gate[f"{key}_cosine_by_layer"] = cos
|
|
gate["pass"] = min(gate["s1_cosine_at_official_layer"], gate["s2_cosine_at_official_layer"]) >= 0.9
|
|
(out / "replication_gate.json").write_text(json.dumps(gate, indent=2))
|
|
print(json.dumps({k: v for k, v in gate.items() if "by_layer" not in k}, indent=2))
|
|
|
|
|
|
# ---------------------------------------------------------------- ablate
|
|
|
|
def parse_turns(text):
|
|
turns, role, lines = [], None, []
|
|
for line in text.split("\n"):
|
|
if line.startswith("[User]:"):
|
|
if role:
|
|
turns.append((role, "\n".join(lines).strip()))
|
|
role, lines = "user", [line[7:].strip()]
|
|
elif line.startswith("[Assistant]:"):
|
|
if role:
|
|
turns.append((role, "\n".join(lines).strip()))
|
|
role, lines = "assistant", [line[12:].strip()]
|
|
else:
|
|
lines.append(line)
|
|
if role:
|
|
turns.append((role, "\n".join(lines).strip()))
|
|
return turns
|
|
|
|
|
|
def render(text, tok, fmt):
|
|
if fmt == "raw":
|
|
return text
|
|
msgs = [{"role": r, "content": c} for r, c in parse_turns(text) if not (r == "assistant" and c == "")]
|
|
return tok.apply_chat_template(msgs, add_generation_prompt=True, tokenize=False)
|
|
|
|
|
|
def first_sequences(model, output, batch_size):
|
|
"""Select the first returned candidate for each prompt without changing decoding."""
|
|
output = getattr(output, "sequences", output)
|
|
count = getattr(getattr(model, "generation_config", None), "num_return_sequences", None) or 1
|
|
if output.ndim != 2 or output.shape[0] != batch_size * count:
|
|
raise ValueError("Pain Axis generation returned an unexpected number of sequences")
|
|
return output[::count]
|
|
|
|
|
|
def direction(vec, layer, name, model_id):
|
|
return ConceptDirection(directions={layer: vec.float().cpu()}, extraction_layers=[layer],
|
|
intervention_layers=[layer], concept_id=name, model_id=model_id)
|
|
|
|
|
|
class _Edits:
|
|
def __init__(self):
|
|
self.edited_parameters, self.parameters = [], []
|
|
|
|
|
|
def orthogonalize(model, vec, layer, name, args):
|
|
"""Edit every layer, optionally a few layers per call (--chunk). Once-only options
|
|
(--intervention-kwargs, e.g. embeddings) go with the first call."""
|
|
kwargs = json.loads(args.intervention_kwargs) if args.intervention_kwargs else {}
|
|
n = len(decoder_layers(model))
|
|
chunk = args.chunk or n
|
|
edits = _Edits()
|
|
for start in range(0, n, chunk):
|
|
run = apply_concept_intervention(
|
|
model, direction(vec, layer, name, args.model), "orthogonalize",
|
|
layers=list(range(start, min(n, start + chunk))), override_gate=REASON,
|
|
**(kwargs if start == 0 else {}))
|
|
edits.edited_parameters += run.edited_parameters
|
|
edits.parameters.append(run.parameters)
|
|
del run
|
|
torch.cuda.empty_cache()
|
|
return edits
|
|
|
|
|
|
def strict_monitor(model, dirs):
|
|
layers = decoder_layers(model)
|
|
rec = []
|
|
d = torch.stack([unit(v) for v in dirs]).float()
|
|
|
|
def hook(module, inputs, output, idx):
|
|
hs = output[0] if isinstance(output, tuple) else output
|
|
rec.append((idx, (hs[:, -1].float() @ d.to(hs.device).T).abs().cpu()))
|
|
handles = [layer.register_forward_hook(lambda m, i, o, idx=idx: hook(m, i, o, idx))
|
|
for idx, layer in enumerate(layers)]
|
|
return rec, handles
|
|
|
|
|
|
def stage_ablate(args):
|
|
pin = Path(args.pin)
|
|
fmt, l1, l2 = MODELS[args.name]
|
|
layer_of = {"s1": l1, "s2": l2}
|
|
stacks = torch.load(Path(args.out) / "vectors_layerwise.pt", weights_only=False)
|
|
dim = stacks["s2"].shape[1]
|
|
rand = unit(torch.randn(dim, generator=torch.Generator().manual_seed(0)))
|
|
scen = [s for s in json.loads((pin / "datasets/4.1_self_other_420_scenarios.json").read_text())
|
|
if s["category"] in KEEP_CATEGORIES]
|
|
assert len(scen) == 100, len(scen)
|
|
fixed_names = ["s1", "s2", "negemotion", "fear"]
|
|
fixed = [stacks[k][layer_of["s1" if k == "s1" else "s2"]] for k in fixed_names]
|
|
official = {}
|
|
for r in csv_rows(pin / f"results/appC_ablation/ablation_{args.name}.csv"):
|
|
official[(r["id"], r["condition"])] = r["generation"]
|
|
conditions = args.conditions.split(",") if args.conditions else list(CONDITIONS)
|
|
out = Path(args.out) / f"ablate{args.tag}"
|
|
out.mkdir(parents=True, exist_ok=True)
|
|
summary = json.loads((out / "summary.json").read_text()) if (out / "summary.json").exists() else {}
|
|
for cond in conditions:
|
|
tok, model = load(args.model, **placement_from_args(args))
|
|
input_device = resolve_input_execution_device(model)
|
|
placement = placement_metadata(model, **placement_from_args(args))
|
|
applied = []
|
|
specs = [spec for spec in CONDITIONS[cond] if spec != "joint"]
|
|
basis = []
|
|
for key, which in specs:
|
|
vec = (rand if key == "__random__" else stacks[key][layer_of[which]]).float()
|
|
if "joint" in CONDITIONS[cond]:
|
|
for b in basis:
|
|
vec = vec - (vec @ b) * b
|
|
vec = unit(vec)
|
|
basis.append(vec)
|
|
run = orthogonalize(model, vec, layer_of[which], key, args)
|
|
applied.append({"vector": key, "layer": layer_of[which],
|
|
"edited": len(run.edited_parameters), "parameters": run.parameters})
|
|
rec, handles = strict_monitor(model, fixed)
|
|
texts = [render(s["text"], tok, fmt) for s in scen]
|
|
gens, proj = [], []
|
|
for start in range(0, len(texts), 8):
|
|
# The reference tokenizes the rendered chat with special tokens on (a second BOS on
|
|
# Gemma); keep that so generations stay comparable with the shipped ones.
|
|
enc = tok(texts[start:start + 8], return_tensors="pt", padding=True, padding_side="left").to(input_device)
|
|
rec.clear()
|
|
with torch.no_grad():
|
|
o = model.generate(**enc, max_new_tokens=60, do_sample=False, pad_token_id=tok.pad_token_id)
|
|
o = first_sequences(model, o, enc["input_ids"].shape[0])
|
|
gens += tok.batch_decode(o[:, enc["input_ids"].shape[1]:], skip_special_tokens=True)
|
|
proj.append(torch.stack([p for _, p in rec]).mean(0)) # per-scenario mean |proj|
|
|
for h in handles:
|
|
h.remove()
|
|
rows = [{"id": s["id"], "category": s["category"], "generation": g} for s, g in zip(scen, gens)]
|
|
(out / f"{cond}.json").write_text(json.dumps(rows, indent=1, ensure_ascii=False))
|
|
same = [g == official.get((s["id"], cond)) for s, g in zip(scen, gens)] # False for extensions
|
|
summary[cond] = {
|
|
"applied": applied,
|
|
"placement": placement,
|
|
"mean_abs_final_token_projection": dict(zip(fixed_names, torch.cat(proj).mean(0).tolist())),
|
|
"exact_match_official": sum(same) / len(same),
|
|
}
|
|
print(cond, json.dumps(summary[cond]["mean_abs_final_token_projection"]),
|
|
"exact_match", summary[cond]["exact_match_official"], flush=True)
|
|
(out / "summary.json").write_text(json.dumps(summary, indent=2))
|
|
del model
|
|
gc.collect()
|
|
torch.cuda.empty_cache()
|
|
base = json.loads((out / "baseline.json").read_text()) if (out / "baseline.json").exists() else None
|
|
if base:
|
|
for cond in summary:
|
|
ours = json.loads((out / f"{cond}.json").read_text())
|
|
summary[cond]["changed_vs_baseline_ours"] = sum(
|
|
a["generation"] != b["generation"] for a, b in zip(ours, base)) / len(base)
|
|
if (scen[0]["id"], cond) in official:
|
|
summary[cond]["changed_vs_baseline_official"] = sum(
|
|
official[(s["id"], cond)] != official[(s["id"], "baseline")] for s in scen) / len(scen)
|
|
(out / "summary.json").write_text(json.dumps(summary, indent=2))
|
|
|
|
|
|
# ---------------------------------------------------------------- resteer
|
|
|
|
def neutral_prompts(pin):
|
|
src = (pin / "scripts/4.2_steering/01_steering_ladder.py").read_text()
|
|
block = src[src.index("NEUTRAL_50"):]
|
|
block = block[block.index("[") + 1:block.index("]")]
|
|
return re.findall(r'"((?:[^"\\]|\\.)*)"', block)
|
|
|
|
|
|
def stage_resteer(args):
|
|
pin = Path(args.pin)
|
|
prompts = neutral_prompts(pin)
|
|
assert len(prompts) == 50, len(prompts)
|
|
fmt, l1, l2 = MODELS[args.name]
|
|
stacks = torch.load(Path(args.out) / "vectors_layerwise.pt", weights_only=False)
|
|
official = torch.load(pin / f"results/3.2_pain_vectors/pain_vectors/{args.name}/pain_vectors.pt",
|
|
map_location="cpu", weights_only=False)
|
|
raw = official["s2_pain_vector"].float() # official raw (unnormalized) steering vector
|
|
# Default: the reference's S2 steering layer for this model.
|
|
steer_layer = args.steer_layer if args.steer_layer is not None else l2
|
|
g = torch.Generator().manual_seed(1)
|
|
rand = unit(torch.randn(raw.numel(), generator=g)) * raw.norm()
|
|
arms = {"pristine": [], "ablate_s2": [("s2", l2)]}
|
|
out = Path(args.out) / f"resteer{args.tag}"
|
|
out.mkdir(parents=True, exist_ok=True)
|
|
for arm, cuts in arms.items():
|
|
tok, model = load(args.model, **placement_from_args(args))
|
|
input_device = resolve_input_execution_device(model)
|
|
placement = placement_metadata(model, **placement_from_args(args))
|
|
for key, layer in cuts:
|
|
orthogonalize(model, stacks[key][layer], layer, key, args)
|
|
for vec_name, vec in (("s2", raw), ("random", rand)):
|
|
if vec_name == "random" and not args.random_arm:
|
|
continue
|
|
path = out / f"{arm}__{vec_name}.json"
|
|
if path.exists():
|
|
continue
|
|
rows, doses = [], []
|
|
with apply_concept_intervention(
|
|
model, direction(vec, l2, vec_name, args.model), "steer",
|
|
layers=[steer_layer], alpha=0.0, scale="vector", override_gate=REASON,
|
|
) as run:
|
|
for coeff in LADDER:
|
|
run.alpha = float(coeff)
|
|
for i, p in enumerate(prompts):
|
|
ids = tok(p, return_tensors="pt").to(input_device)
|
|
with torch.no_grad():
|
|
o = model.generate(**ids, max_new_tokens=120, do_sample=False,
|
|
pad_token_id=tok.pad_token_id)
|
|
rows.append({"coeff": coeff, "prompt_idx": i,
|
|
"generation": tok.decode(o[0, ids["input_ids"].shape[1]:],
|
|
skip_special_tokens=True)})
|
|
doses.append({"alpha": coeff, "scale": "vector",
|
|
"steering": run.steering_metadata()})
|
|
print(arm, vec_name, coeff, flush=True)
|
|
metadata = {"parameters": run.parameters, "validation": run.validation,
|
|
"extraction_layer": l2, "doses": doses, "placement": placement}
|
|
(out / "metadata").mkdir(exist_ok=True)
|
|
(out / "metadata" / path.name).write_text(json.dumps(metadata, indent=2))
|
|
path.write_text(json.dumps(rows, indent=1, ensure_ascii=False))
|
|
del model
|
|
gc.collect()
|
|
torch.cuda.empty_cache()
|
|
|
|
|
|
# ---------------------------------------------------------------- battery
|
|
|
|
HUMOR = re.compile(r"funny|humou?r|joke|joking|good one|haha|lol\b|😂|😄|😆|🤣|laugh|kidding|witty|playful", re.I)
|
|
|
|
|
|
def stage_battery(args):
|
|
"""Toolkit evaluation battery on one ablation condition: mechanistic projections,
|
|
320-prompt refusal collateral, coherence, first-token KL, neighbour projections,
|
|
the hostile battery (humor proxy) and a KV-cache probe, pristine and ablated."""
|
|
from obliteratus.concepts.evaluation import EvaluationBattery
|
|
from obliteratus.concepts.evaluation_model import ModelEvaluator, kv_cache_probe
|
|
|
|
pin = Path(args.pin)
|
|
fmt, l1, l2 = MODELS[args.name]
|
|
layer_of = {"s1": l1, "s2": l2}
|
|
stacks = torch.load(Path(args.out) / "vectors_layerwise.pt", weights_only=False)
|
|
scen = [s for s in json.loads((pin / "datasets/4.1_self_other_420_scenarios.json").read_text())
|
|
if s["category"] in KEEP_CATEGORIES]
|
|
cond = args.conditions or "s1s2_joint"
|
|
tok, model = load(args.model, **placement_from_args(args))
|
|
input_device = resolve_input_execution_device(model)
|
|
placement = placement_metadata(model, **placement_from_args(args))
|
|
s2 = direction(stacks["s2"][l2], l2, "pain_s2", args.model)
|
|
neighbours = {k: direction(stacks[k][l2], l2, k, args.model) for k in ("fear", "negemotion", "sadness")}
|
|
evaluator = ModelEvaluator(model, tok, s2, neighbours=neighbours)
|
|
|
|
# ModelEvaluator generates from single user turns, so this battery uses each scenario's
|
|
# first user message; stage_ablate renders the full conversation.
|
|
def hostile(generate, decoding):
|
|
texts = generate([parse_turns(s["text"])[0][1] for s in scen], decoding)
|
|
return {"humor_proxy": sum(bool(HUMOR.search(t)) for t in texts), "n": len(texts)}
|
|
|
|
neutral = tok(" Can you tell me what a good breakfast would be?", return_tensors="pt",
|
|
add_special_tokens=False)["input_ids"]
|
|
|
|
def kv():
|
|
result = {}
|
|
for name, vec in (("s2", stacks["s2"][l2]),
|
|
("random", unit(torch.randn(stacks["s2"].shape[1],
|
|
generator=torch.Generator().manual_seed(2))))):
|
|
kls = []
|
|
for s in scen[:20]:
|
|
prefix = tok(render(s["text"], tok, fmt), return_tensors="pt",
|
|
add_special_tokens=False)["input_ids"]
|
|
kls.append(kv_cache_probe(model, direction(vec, l2, name, args.model),
|
|
prefix.to(input_device), neutral.to(input_device),
|
|
alpha=0.6, override_gate=REASON)["first_token_kl"])
|
|
result[name] = {"mean_first_token_kl": sum(kls) / len(kls), "n": len(kls)}
|
|
return result
|
|
|
|
battery = EvaluationBattery(
|
|
evaluator.generate, measure=evaluator.measure, model_id=args.model,
|
|
decoding_metadata=evaluator.decoding_metadata, behavioural_battery=hostile, kv_probe=kv,
|
|
max_new_tokens=48,
|
|
)
|
|
pristine_kv = kv()
|
|
battery.capture()
|
|
basis = []
|
|
for key, which in [spec for spec in CONDITIONS[cond] if spec != "joint"]:
|
|
vec = (unit(torch.randn(stacks["s2"].shape[1], generator=torch.Generator().manual_seed(0)))
|
|
if key == "__random__" else stacks[key][layer_of[which]].float())
|
|
if "joint" in CONDITIONS[cond]:
|
|
for b in basis:
|
|
vec = vec - (vec @ b) * b
|
|
vec = unit(vec)
|
|
basis.append(vec)
|
|
orthogonalize(model, vec, layer_of[which], key, args)
|
|
out = Path(args.out) / f"battery_{cond}{args.tag}"
|
|
out.mkdir(parents=True, exist_ok=True)
|
|
report = battery.finish(out)
|
|
(out / "placement.json").write_text(json.dumps(placement, indent=2))
|
|
(out / "kv_pristine.json").write_text(json.dumps(pristine_kv, indent=2))
|
|
print(json.dumps({"collateral": report.collateral.get("status"),
|
|
"fail_reasons": report.collateral.get("fail_reasons"),
|
|
"refusal": {k: report.collateral["refusal"].get(k) for k in ("status", "pristine", "intervened")},
|
|
"behavioural": report.behavioural.get("before"), "after": report.behavioural.get("after"),
|
|
"kv_pristine": pristine_kv, "kv_ablated": report.mechanistic.get("kv_cache"),
|
|
"mechanistic": report.mechanistic.get("status")}, indent=1, default=str))
|
|
|
|
|
|
# ---------------------------------------------------------------- reports
|
|
|
|
# Section 4.2 keyword metric.
|
|
PAIN_WORDS = re.compile(r"\b(?:pain|painful|hurt|hurts|hurting)\b", re.IGNORECASE)
|
|
|
|
|
|
def stage_report(args):
|
|
"""Compare an ablate run with the shipped Appendix D generations.
|
|
|
|
HUMOR is a lexical proxy for "interprets the hostility as humor": on the shipped
|
|
Gemma 2 2B generations it gives 0/12/22/31 for baseline/S1/S2/S1+S2 against the
|
|
paper's judged 0/6/17/26, so it is liberal but preserves the ordering; it is applied
|
|
identically to the official and the reproduced generations.
|
|
"""
|
|
run, pin, name = Path(args.out) / f"ablate{args.tag}", Path(args.pin), args.name
|
|
official = {}
|
|
for r in csv_rows(pin / f"results/appC_ablation/ablation_{name}.csv"):
|
|
official.setdefault(r["condition"], {})[r["id"]] = r["generation"]
|
|
summary = json.loads((run / "summary.json").read_text())
|
|
base = {r["id"]: r["generation"] for r in json.loads((run / "baseline.json").read_text())}
|
|
print(f"{'condition':12s} {'humor ours':>10s} {'humor off':>9s} {'chg ours':>8s} {'chg off':>7s} "
|
|
f"{'match':>5s} mean|proj| at removed dirs (s1 s2 negemo fear)")
|
|
table = {}
|
|
for cond in summary:
|
|
ours = {r["id"]: r["generation"] for r in json.loads((run / f"{cond}.json").read_text())}
|
|
off = official.get(cond, {})
|
|
row = {
|
|
"humor_ours": sum(bool(HUMOR.search(g)) for g in ours.values()),
|
|
"humor_official": sum(bool(HUMOR.search(g)) for g in off.values()) if off else -1,
|
|
"changed_vs_baseline_ours": sum(ours[i] != base[i] for i in ours),
|
|
"changed_vs_baseline_official": sum(off[i] != official["baseline"][i] for i in off) if off else -1,
|
|
"exact_match_official": sum(ours[i] == off.get(i) for i in ours) if off else -1,
|
|
"projection": summary[cond]["mean_abs_final_token_projection"],
|
|
}
|
|
table[cond] = row
|
|
p = row["projection"]
|
|
print(f"{cond:12s} {row['humor_ours']:10d} {row['humor_official']:9d} "
|
|
f"{row['changed_vs_baseline_ours']:8d} {row['changed_vs_baseline_official']:7d} "
|
|
f"{row['exact_match_official']:5d} {p['s1']:.2f} {p['s2']:.2f} {p['negemotion']:.2f} {p['fear']:.2f}")
|
|
(run / "report.json").write_text(json.dumps(table, indent=2))
|
|
|
|
|
|
def stage_steer_report(args):
|
|
"""Section 4.2 keyword rates per arm and coefficient, with the official pristine ladder."""
|
|
from collections import defaultdict
|
|
|
|
run, pin, name = Path(args.out) / f"resteer{args.tag}", Path(args.pin), args.name
|
|
layer = args.steer_layer if args.steer_layer is not None else MODELS[name][2]
|
|
table = {}
|
|
off = defaultdict(list)
|
|
for r in csv_rows(pin / f"results/4.2_steering/S2/{name}_steering_S2_neutral50_L{layer}.csv"):
|
|
off[float(r["coeff"])].append(bool(PAIN_WORDS.search(r["generation"] or "")))
|
|
table["official_pristine__s2"] = {c: 100 * sum(v) / len(v) for c, v in sorted(off.items())}
|
|
for path in sorted(run.glob("*__*.json")):
|
|
rows = json.loads(path.read_text())
|
|
by = defaultdict(list)
|
|
for r in rows:
|
|
by[float(r["coeff"])].append(bool(PAIN_WORDS.search(r["generation"])))
|
|
table[path.stem] = {c: 100 * sum(v) / len(v) for c, v in sorted(by.items())}
|
|
coeffs = sorted(next(iter(table.values())))
|
|
print(f"{'arm':34s}" + "".join(f"{c:>7g}" for c in coeffs))
|
|
for arm, rates in table.items():
|
|
print(f"{arm:34s}" + "".join(f"{rates.get(c, float('nan')):7.1f}" for c in coeffs))
|
|
(run / "keyword_rates.json").write_text(json.dumps(table, indent=2))
|
|
|
|
|
|
def parser():
|
|
p = argparse.ArgumentParser()
|
|
p.add_argument("stage", choices=["vectors", "ablate", "resteer", "battery", "report", "steer-report"])
|
|
p.add_argument("--model", required=True)
|
|
p.add_argument("--name", required=True, help="official result name, e.g. Qwen_2.5_7B_instruct")
|
|
p.add_argument("--pin", required=True)
|
|
p.add_argument("--out", required=True)
|
|
p.add_argument("--tag", default="")
|
|
p.add_argument("--conditions", default="")
|
|
p.add_argument("--intervention-kwargs", default="", help="JSON kwargs for apply_concept_intervention")
|
|
p.add_argument("--steer-layer", type=int, default=None)
|
|
p.add_argument("--random-arm", action="store_true")
|
|
p.add_argument("--chunk", type=int, default=0, help="layers per intervention call; 0 = all at once")
|
|
add_capacity_arguments(p)
|
|
add_placement_arguments(p)
|
|
return p
|
|
|
|
|
|
def main(argv=None):
|
|
p = parser()
|
|
args = p.parse_args(argv)
|
|
if args.plan_only:
|
|
if args.plan_output is None:
|
|
args.plan_output = Path(args.out) / "capacity-plan.json"
|
|
try:
|
|
maybe_plan(args, args.model, dtype="bfloat16", placement=placement_from_args(args),
|
|
batch_size=8 if args.stage == "ablate" else 1,
|
|
generation_tokens=120 if args.stage == "resteer" else 60,
|
|
activation_datasets=len(VECTOR_DATASETS) if args.stage == "vectors" else 2,
|
|
collection_copies=2 if args.stage == "vectors" else 0,
|
|
concept_edit=args.stage in {"ablate", "resteer", "battery"})
|
|
except CapacityPlanningError as exc:
|
|
p.error(str(exc))
|
|
return
|
|
torch.manual_seed(0)
|
|
{"vectors": stage_vectors, "ablate": stage_ablate, "resteer": stage_resteer, "battery": stage_battery,
|
|
"report": stage_report, "steer-report": stage_steer_report}[args.stage](args)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|