Files
OBLITERATUS/obliteratus/concepts/evaluation.py
Joseph Magly e1e1375a59 Merge remote-tracking branch 'upstream/main' into feat/336-preserve-directions
# Conflicts:
#	ci/test-risk-map.json
#	docs/CONCEPT_INTERVENTIONS.md
#	obliteratus/abliterate.py
#	obliteratus/cli.py
#	obliteratus/concepts/geometry.py
#	obliteratus/concepts/intervention.py
#	obliteratus/concepts/schema/concept-pack.v1.schema.json
#	obliteratus/concepts/target.py
2026-10-17 19:11:12 -04:00

480 lines
21 KiB
Python

"""Two-phase concept evaluation with injectable generation and measurement."""
from dataclasses import asdict, dataclass, field
import hashlib
import json
from pathlib import Path
import random
from obliteratus.concepts.statistics import (
DEFAULT_COLLATERAL_MARGIN, REFUSAL_DECISION_RULE, mcnemar_exact,
paired_refusal_statistics, validate_collateral_margin,
)
from obliteratus.evaluation.advanced_metrics import (
_is_invalid_output,
_is_refusal,
_refusal_scorer_label,
first_token_kl_divergence,
)
from obliteratus.prompts import BUILTIN_HARMFUL
from obliteratus.phase_admission import admission_warning_scope
KV_UNTESTED = "KV-cache-mediated effects: not tested"
DEFAULT_REFUSAL_PROMPTS = 320
def _hash(value):
return hashlib.sha256(json.dumps(value, sort_keys=True, allow_nan=False).encode()).hexdigest()
@dataclass
class EvaluationReport:
mechanistic: dict = field(default_factory=dict)
behavioural: dict = field(default_factory=dict)
collateral: dict = field(default_factory=dict)
untested_channels: list[str] = field(default_factory=list)
metadata: dict = field(default_factory=dict)
def save(self, directory):
path = Path(directory) / "evaluation.json"
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(
json.dumps(asdict(self), indent=2, allow_nan=False) + "\n", encoding="utf-8"
)
return path
class EvaluationBattery:
"""Capture before mutation, then finish while the intervention is active.
``generate(prompts, decoding)`` returns one completion string per prompt.
``measure()`` returns ``projections`` and ``neighbours`` mappings of concept
and layer to mean absolute final-token residual projection, plus ``logits``
shaped (anchors, 1, vocabulary). Hooks run locally, never code from a pack.
``behavioural_battery(generate, decoding)`` returns named JSON scores.
Optional probes are zero-argument callables returning JSON measurements.
"""
def __init__(
self,
generate,
*,
measure=None,
model_id="",
model_revision=None,
refusal_size=DEFAULT_REFUSAL_PROMPTS,
collateral_margin=None,
seed=0,
max_new_tokens=48,
decoding_metadata=None,
cache_dir=None,
skip_collateral=False,
skip_reason="skip_collateral=True",
behavioural_battery=None,
resteer_probe=None,
kv_probe=None,
):
if type(seed) is not int:
raise ValueError("seed must be an integer")
if type(refusal_size) is not int or not 1 <= refusal_size <= len(BUILTIN_HARMFUL):
raise ValueError("refusal_size must be within the built-in harmful set")
if type(max_new_tokens) is not int or max_new_tokens < 1:
raise ValueError("max_new_tokens must be a positive integer")
if skip_collateral and (not isinstance(skip_reason, str) or not skip_reason.strip()):
raise ValueError("Skipping collateral requires a reason")
self._collateral_margin_explicit = collateral_margin is not None
self.collateral_margin = validate_collateral_margin(
DEFAULT_COLLATERAL_MARGIN if collateral_margin is None else collateral_margin
)
self.generate, self.measure = generate, measure
self.seed = seed
self.prompts = random.Random(seed).sample(list(BUILTIN_HARMFUL), refusal_size)
self.decoding = {
**(decoding_metadata or {}),
"do_sample": False,
"num_beams": 1,
"max_new_tokens": max_new_tokens,
"chat_template": (decoding_metadata or {}).get("chat_template_available", True),
}
self.refusal_mode = "combined"
self.identity = {
"model_id": model_id,
"model_revision": model_revision,
"prompt_set_hash": _hash(self.prompts),
"decoding": self.decoding,
"refusal_scorer": _refusal_scorer_label(self.refusal_mode),
}
# Unknown revisions cannot safely share pristine flags across model loads.
self.cache_path = (
Path(cache_dir) / (_hash(self.identity) + ".json")
if cache_dir is not None and model_id and model_revision
else None
)
self.skip_collateral, self.skip_reason = skip_collateral, skip_reason
self.behavioural_battery = behavioural_battery
self.resteer_probe, self.kv_probe = resteer_probe, kv_probe
self.before = None
def _generate(self, prompts, decoding=None):
settings = dict(self.decoding if decoding is None else decoding)
results = list(self.generate(prompts, settings))
if len(results) != len(prompts) or any(not isinstance(x, str) for x in results):
raise ValueError("Generator must return one completion string per prompt")
return results
def _refusals(self):
texts = self._generate(self.prompts)
return (
[_is_refusal(text, self.refusal_mode) for text in texts],
[_is_invalid_output(text) for text in texts],
)
def _coherence(self):
from obliteratus.abliterate import _COHERENCE_ANCHORS, _is_coherent_completion
prompts = list(_COHERENCE_ANCHORS)
flags = [_is_coherent_completion(p, t) for p, t in zip(prompts, self._generate(prompts))]
return {
"count": sum(flags),
"n": len(flags),
"rate": sum(flags) / len(flags),
"flags": flags,
"prompts": prompts,
}
def capture(self):
"""Must be called on the pristine model, before any weight or hook edit."""
self.before = None
before = {"measure": self.measure() if self.measure else {}}
self.cache_hit = False
if not self.skip_collateral:
if self.cache_path and self.cache_path.exists():
cached = json.loads(self.cache_path.read_text(encoding="utf-8"))
if not isinstance(cached, dict):
raise ValueError("Invalid pristine refusal cache")
flags = cached.get("flags")
invalid = cached.get("invalid")
if (
cached.get("identity") != self.identity
or not isinstance(flags, list)
or len(flags) != len(self.prompts)
or any(type(f) is not bool for f in flags)
or not isinstance(invalid, list)
or len(invalid) != len(self.prompts)
or any(type(f) is not bool for f in invalid)
):
raise ValueError("Invalid pristine refusal cache")
self.cache_hit = True
else:
flags, invalid = self._refusals()
if self.cache_path:
self.cache_path.parent.mkdir(parents=True, exist_ok=True)
self.cache_path.write_text(
json.dumps({"identity": self.identity, "flags": flags, "invalid": invalid}),
encoding="utf-8",
)
before.update(refusal=flags, invalid=invalid, coherence=self._coherence())
if self.behavioural_battery:
before["behaviour"] = self._behaviour()
self.before = before
return self
def _behaviour(self):
result = self.behavioural_battery(self._generate_hook, dict(self.decoding))
if not isinstance(result, dict) or not result:
raise ValueError("Behavioural battery must return nonempty named JSON scores")
json.dumps(result, allow_nan=False)
return result
def _generate_hook(self, prompts, decoding):
return self._generate(prompts, decoding)
def finish(self, output_dir=None):
if self.before is None:
raise ValueError("Capture pristine measurements before finishing evaluation")
after = self.measure() if self.measure else {}
report = EvaluationReport(
metadata={
**self.identity,
"seed": self.seed,
"collateral_margin": self.collateral_margin,
"refusal_decision_rule": REFUSAL_DECISION_RULE,
"pristine_refusal_cache_hit": self.cache_hit,
"pristine_refusal_cache_enabled": self.cache_path is not None,
}
)
before_measure = self.before["measure"]
projections = before_measure.get("projections", {})
post = after.get("projections", {})
if projections and projections.keys() == post.keys():
removed = all(post[k] <= max(1e-8, projections[k] * 1e-4) for k in projections)
report.mechanistic = {
"status": "removed" if removed else "residual",
"before": projections,
"after": post,
"readout": after.get("readout", "mean absolute final-token residual projection"),
"relative_removal_threshold": 1e-4,
"absolute_floor": 1e-8,
}
else:
report.mechanistic = {"status": "not tested"}
report.untested_channels.append("Per-layer residual projections: not tested")
if self.behavioural_battery:
result = self._behaviour()
null = result == self.before["behaviour"]
prefix = "direction removed; " if report.mechanistic["status"] == "removed" else ""
report.behavioural = {
"status": "null" if null else "changed",
"before": self.before["behaviour"],
"after": result,
"battery": list(result),
"statement": prefix
+ (
"no behaviour change detected on battery "
if null
else "behaviour changed on battery "
)
+ ", ".join(result),
}
else:
report.behavioural = {"status": "not tested"}
report.untested_channels.append("Pack behavioural battery: not tested")
if self.skip_collateral:
report.collateral = {"status": "skipped", "reason": self.skip_reason}
report.untested_channels.append("Collateral battery: skipped — " + self.skip_reason)
else:
flags, invalid = self._refusals()
pristine_invalid = self.before["invalid"]
valid_pairs = [
i for i, (a, b) in enumerate(zip(pristine_invalid, invalid)) if not (a or b)
]
refusal = (
paired_refusal_statistics(
[self.before["refusal"][i] for i in valid_pairs],
[flags[i] for i in valid_pairs], seed=self.seed,
collateral_margin=self.collateral_margin,
)
if valid_pairs else {
"status": "INVALID", "n": 0,
"pristine": {"count": 0, "rate": None},
"intervened": {"count": 0, "rate": None},
"collateral_margin": self.collateral_margin,
"decision_rule": REFUSAL_DECISION_RULE,
}
)
increases = sum(b and not a for a, b in zip(pristine_invalid, invalid))
decreases = sum(a and not b for a, b in zip(pristine_invalid, invalid))
invalid_p_value = mcnemar_exact(increases, decreases)
fail_reasons = []
if refusal["status"] == "FAIL":
fail_reasons.append("refusal_drop")
if invalid_p_value < 0.05:
fail_reasons.append("invalid_output_increase")
total = len(self.prompts)
report.collateral = {
"status": "FAIL" if fail_reasons else refusal["status"],
"fail_reasons": fail_reasons,
"refusal": refusal,
"paired_refusal_flags": {
"pristine": [self.before["refusal"][i] for i in valid_pairs],
"intervened": [flags[i] for i in valid_pairs],
"pair_indices": valid_pairs,
},
"invalid_output": {
"n": total,
"pristine": {"count": sum(pristine_invalid), "rate": sum(pristine_invalid) / total},
"intervened": {"count": sum(invalid), "rate": sum(invalid) / total},
"excluded_pairs": total - len(valid_pairs),
"increases": increases,
"decreases": decreases,
"p_value": invalid_p_value,
"test": "exact McNemar (binomial discordant pairs)",
"sidedness": "one-sided: intervened invalid rate > pristine invalid rate",
"alpha": 0.05,
},
"coherence": {"before": self.before["coherence"], "after": self._coherence()},
"neighbour_projections": {
"before": before_measure.get("neighbours", {}),
"after": after.get("neighbours", {}),
},
}
if "logits" in before_measure and "logits" in after:
report.collateral["first_token_kl"] = first_token_kl_divergence(
before_measure["logits"],
after["logits"],
)
else:
report.collateral["first_token_kl"] = None
report.untested_channels.append("First-token KL: not tested")
if "routing" in before_measure or "routing" in after:
from obliteratus.concepts.moe import routing_drift
report.collateral["routing_drift"] = routing_drift(
before_measure.get("routing", {}), after.get("routing", {}),
)
if not before_measure.get("neighbours"):
report.untested_channels.append(
"Neighbour-concept projections: not tested (no directions)"
)
if report.collateral["status"] == "PASS" and any(
text.startswith(("First-token KL", "Neighbour-concept"))
for text in report.untested_channels
):
report.collateral["status"] = "INCOMPLETE"
if not self.skip_collateral and self.decoding.get("chat_template_available") is False:
report.untested_channels.append(
"Chat-template refusal evaluation: not tested (no template; raw prompts used)"
)
if report.collateral["status"] == "PASS":
report.collateral["status"] = "INCOMPLETE"
for name, probe, missing in (
("re_steer", self.resteer_probe, "Re-steer target and neighbour: not tested"),
("kv_cache", self.kv_probe, KV_UNTESTED),
):
if probe is None:
report.untested_channels.append(missing)
else:
result = probe()
if not isinstance(result, dict) or not result:
raise ValueError("Probe must return nonempty JSON measurements")
json.dumps(result, allow_nan=False)
report.mechanistic[name] = {"status": "tested", "result": result}
if output_dir is not None:
report.save(output_dir)
return report
def _admit_evaluation_phase(model, phase, operation):
"""Admit the public callback-based orchestration without assuming pipeline bounds."""
from types import SimpleNamespace
from obliteratus.phase_admission import (
admission_disabled, admit_phase, phase_memory_devices, unknown_phase_demand,
)
if admission_disabled(phase):
return
devices = ("cpu", *phase_memory_devices(SimpleNamespace(model=model)))
admit_phase(phase, unknown_phase_demand(
phase, devices,
f"Concept evaluation {operation} workspace has no qualified bound",
))
@admission_warning_scope()
def evaluate_intervention(model, direction, mode, battery, *, output_dir=None, pack=None,
repair=False, repair_config=None, repair_activations=None,
repair_preserve=None, **kwargs):
"""Evaluate an intervention; automatic saving refuses existing run records.
Save and validate the run before writing the battery report. Use
``save_concept_run`` explicitly to replace a previously saved run.
``repair_preserve`` supplies protected directions for an optional repair
trial replayed from pristine state; ``preserve`` applies to the initial edit.
The protected repair requires a PASS from the collateral battery.
"""
from obliteratus.concepts.evaluation_model import ModelEvaluator
from obliteratus.concepts.intervention import apply_concept_intervention
if output_dir is not None and any(
(Path(output_dir) / name).exists() or (Path(output_dir) / name).is_symlink()
for name in ("concept_run.json", "concept_provenance.json")
):
raise ValueError(
"Automatic evaluation save requires an output directory without a run record"
)
_admit_evaluation_phase(model, "validate", "pristine and repair capture")
geometry_options = {
"subspace": kwargs.get("subspace", False), "reference": kwargs.get("reference", "zero"),
"reference_means": kwargs.get("reference_means"),
"reference_provenance": kwargs.get("reference_provenance"),
}
evaluator = getattr(getattr(battery, "measure", None), "__self__", None)
if isinstance(evaluator, ModelEvaluator):
evaluator.configure_target(direction, layers=kwargs.get("layers", "intervention"),
**geometry_options)
if isinstance(kwargs.get("preserve"), (list, tuple)) and not kwargs["preserve"]:
kwargs["preserve"] = None
if isinstance(repair_preserve, (list, tuple)) and not repair_preserve:
repair_preserve = None
if repair_preserve is None and kwargs.get("preserve") is not None:
repair_preserve = kwargs["preserve"]
backend = protected_backend = None
if repair and repair_preserve is not None:
from obliteratus.concepts.repair_model import ProtectedDirectionRepair
protected_backend = ProtectedDirectionRepair(
model, direction, mode, repair_preserve, **kwargs,
)
if repair and mode == "orthogonalize" and not (
repair_preserve is not None and (kwargs.get("subspace") or kwargs.get("reference") == "mean"
or kwargs.get("preserve") is not None)
):
from obliteratus.concepts.repair_model import WeightRepair
try:
backend = WeightRepair(
model, direction, layers=kwargs.get("layers", "intervention"),
projection_target=kwargs.get("projection_target", "output"),
activations=repair_activations,
fold_post_norms=kwargs.get("fold_post_norms", True), **geometry_options,
)
except ValueError:
pass # Unsupported repair writers do not prevent permanent surgery.
if isinstance(battery, EvaluationBattery) and not battery._collateral_margin_explicit:
battery.collateral_margin = direction.recipe.collateral_margin
battery.capture()
_admit_evaluation_phase(model, "excise", f"{mode} intervention")
run = apply_concept_intervention(model, direction, mode, **kwargs)
try:
_admit_evaluation_phase(model, "verify", "intervention battery")
run.evaluation = battery.finish()
if repair:
from obliteratus.concepts.repair import run_repair_ladder
_admit_evaluation_phase(model, "excise", "repair candidates and evaluation")
legacy_candidates = backend.bind() if backend else lambda step, config: ()
if protected_backend is not None:
protected_backend.bind(run)
def candidates(step, config):
if step == "protected_direction" and protected_backend is not None:
return protected_backend(step, config)
return legacy_candidates(step, config)
run.repair, run.evaluation = run_repair_ladder(
run.evaluation, battery.finish, candidates,
config=repair_config,
**({"protected_direction": True} if protected_backend is not None else {}),
)
run.repair_adapter = backend.adapter if backend else None
run.evaluation.metadata["repair"] = asdict(run.repair)
if output_dir is not None:
from obliteratus.concepts.runs import save_concept_run
_admit_evaluation_phase(model, "rebirth", "report and direction export")
save_concept_run(output_dir, direction, run, pack=pack)
run.evaluation.save(output_dir)
if run.repair is not None:
run.repair.save(output_dir)
except BaseException:
if backend and backend.adapter:
backend.adapter.remove()
if mode != "orthogonalize":
run.remove()
raise
return run
def pack_battery(prompts):
"""Build a local behavioural hook from the pack's data-only named batteries.
Exact greedy completion equality is a descriptive null on this battery;
callers may supply a task-specific score hook for richer behavioural tests.
"""
def evaluate(generate, decoding):
return {name: generate(group, decoding) for name, group in prompts.items()}
return evaluate