mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
Tiny-model pipeline tests were dominated by fixed per-step costs, not arithmetic: the pipeline's gc.collect() after each verify prompt rescans every long-lived object the session imported (~0.2 s per call), and intra-op thread fan-out makes each 16-dim forward slower on loaded runners. - conftest: add an opt-in tiny_model_runtime fixture (one intra-op thread, gc.freeze() of pre-test objects, both restored afterwards). Objects the test creates remain collectable, so pipeline behaviour is unchanged. Applied to test_offline_integration and the concept_* integration modules. - test_disk_offloaded_fused_moe_surgery_matches_live_surgery: bound verify free-form completions to 4 tokens; every diagnostic still runs through the disk-offload hooks and no assertion depends on generated text. - test_disk_offloaded_subprocess_rss_bound: measure native and legacy export in one subprocess on the same offloaded model, resetting VmHWM (after GC and malloc_trim) before each mode; native is measured first. Same three assertions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
451 lines
17 KiB
Python
451 lines
17 KiB
Python
"""Validation runs before intervention; refusal compatibility and CLI wiring."""
|
|
|
|
import json
|
|
import statistics
|
|
import time
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from obliteratus.abliterate import AbliterationPipeline, PipelineFailure
|
|
from obliteratus.concepts import ConceptDirection
|
|
from obliteratus.concepts.gate import GateFailed
|
|
from tests.fixtures.tiny_offline_model import build_tiny_offline_model
|
|
from tests.test_concept_extraction_pipeline import small_target
|
|
|
|
pytestmark = [
|
|
pytest.mark.cpu,
|
|
pytest.mark.integration,
|
|
pytest.mark.usefixtures("tiny_model_runtime"),
|
|
]
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def single_thread():
|
|
previous = torch.get_num_threads()
|
|
torch.set_num_threads(1)
|
|
yield
|
|
torch.set_num_threads(previous)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"concept,override,blocked,mode",
|
|
[
|
|
("custom", None, True, None),
|
|
("custom", "underpowered offline fixture", False, None),
|
|
("custom", None, False, "report"),
|
|
(None, None, False, "report"),
|
|
("refusal", None, False, "report"),
|
|
("refusal", None, False, "enforce"),
|
|
],
|
|
)
|
|
def test_gate_precedes_first_weight_edit(tmp_path, monkeypatch, concept, override, blocked, mode):
|
|
source = build_tiny_offline_model(tmp_path / "model")
|
|
target = small_target()
|
|
kwargs = (
|
|
{"concept": target}
|
|
if concept == "custom"
|
|
else {
|
|
"concept": concept,
|
|
"harmful_prompts": target.positive[:4],
|
|
"harmless_prompts": target.controls["neutral"],
|
|
}
|
|
)
|
|
pipeline = AbliterationPipeline(
|
|
str(source),
|
|
output_dir=str(tmp_path / "output"),
|
|
device="cpu",
|
|
dtype="float32",
|
|
method="basic",
|
|
seed=228,
|
|
refusal_eval_size=2,
|
|
refusal_eval_max_new_tokens=2,
|
|
override_gate=override,
|
|
gate_mode=mode,
|
|
**kwargs,
|
|
)
|
|
excise = Mock()
|
|
monkeypatch.setattr(pipeline, "_excise", excise)
|
|
monkeypatch.setattr(pipeline, "_capture_stock_baseline", lambda: None)
|
|
monkeypatch.setattr(pipeline, "_capture_baseline_kl_logits", lambda: None)
|
|
monkeypatch.setattr(pipeline, "_verify", lambda: None)
|
|
monkeypatch.setattr(pipeline, "_rebirth", lambda: pipeline.output_dir)
|
|
weights = {}
|
|
original_summon = pipeline._summon
|
|
|
|
def summon():
|
|
original_summon()
|
|
weights.update(
|
|
{
|
|
name: value.detach().clone()
|
|
for name, value in pipeline.handle.model.state_dict().items()
|
|
}
|
|
)
|
|
|
|
monkeypatch.setattr(pipeline, "_summon", summon)
|
|
original_cleanup = pipeline.cleanup_failed_run
|
|
|
|
def cleanup():
|
|
assert all(
|
|
torch.equal(value, weights[name])
|
|
for name, value in pipeline.handle.model.state_dict().items()
|
|
)
|
|
original_cleanup()
|
|
|
|
monkeypatch.setattr(pipeline, "cleanup_failed_run", cleanup)
|
|
if blocked:
|
|
with pytest.raises(GateFailed, match="before EXCISE"):
|
|
pipeline.run()
|
|
excise.assert_not_called()
|
|
assert pipeline.handle.model is None
|
|
else:
|
|
assert pipeline.run() == pipeline.output_dir
|
|
excise.assert_called_once()
|
|
report = json.loads((pipeline.output_dir / "validation.json").read_text())
|
|
assert report["verdict"] == "fail"
|
|
assert report["mode"] == ("enforce" if concept == "custom" and mode is None else "report")
|
|
assert report["override_gate"] == override
|
|
assert report["lexical_baseline"]["heldout_auc"] is not None
|
|
assert report["random_directions"] == 1000
|
|
assert pipeline._build_metadata()["concept"]["validation"] == report
|
|
if concept == "custom":
|
|
assert pipeline.concept_direction.validation == report
|
|
path = tmp_path / "direction.safetensors"
|
|
pipeline.concept_direction.save(path)
|
|
assert ConceptDirection.load(path).validation == report
|
|
row = next(iter(report["metrics"].values()))
|
|
assert set(row["readout_aucs"]) == {"mean_pool", "final_token"}
|
|
assert row["masked_auc"] is not None
|
|
assert not pipeline._validation_readouts
|
|
assert not pipeline._neighbour_acts
|
|
|
|
|
|
def test_neighbours_and_readouts_share_probe_passes(tmp_path, monkeypatch):
|
|
source = build_tiny_offline_model(tmp_path / "model")
|
|
target = small_target()
|
|
target.neighbours.update({f"neighbour{i}": ["harmful test"] for i in range(4)})
|
|
pipeline = AbliterationPipeline(
|
|
str(source),
|
|
output_dir=str(tmp_path / "output"),
|
|
concept=target,
|
|
device="cpu",
|
|
dtype="float32",
|
|
method="basic",
|
|
override_gate="test",
|
|
)
|
|
pipeline._summon()
|
|
calls = []
|
|
hook = pipeline.handle.model.register_forward_hook(lambda *args: calls.append(1))
|
|
monkeypatch.setattr("obliteratus.abliterate.dev.get_total_free_gb", lambda: 16.0)
|
|
try:
|
|
pipeline._probe()
|
|
count = len(calls)
|
|
assert count == 5 # positive, controls, three neighbours; both readouts in each hook
|
|
pipeline._distill()
|
|
pipeline._validate_concept_gate()
|
|
assert len(calls) == count
|
|
finally:
|
|
hook.remove()
|
|
report = pipeline.validation_report
|
|
assert any("neighbour3" in r["message"] for r in report["reasons"])
|
|
assert len(report["metrics"]["0"]["neighbours"]) == 3
|
|
|
|
|
|
def test_cli_flags_and_failure_exit(tmp_path, monkeypatch):
|
|
from obliteratus import cli
|
|
from obliteratus.concepts.cli import _init
|
|
|
|
pack_path = tmp_path / "pack"
|
|
_init(pack_path, "example")
|
|
monkeypatch.setattr(
|
|
"sys.argv",
|
|
[
|
|
"obliteratus",
|
|
"obliterate",
|
|
"offline",
|
|
"--concept",
|
|
str(pack_path),
|
|
"--override-gate",
|
|
"inspect fixture",
|
|
"--output-dir",
|
|
str(tmp_path / "output"),
|
|
],
|
|
)
|
|
pipeline = Mock()
|
|
pipeline.run.side_effect = GateFailed("Concept validation failed before EXCISE")
|
|
constructor = Mock(return_value=pipeline)
|
|
monkeypatch.setattr("obliteratus.abliterate.AbliterationPipeline", constructor)
|
|
with pytest.raises(SystemExit) as error:
|
|
cli.main()
|
|
assert error.value.code == 3
|
|
kwargs = constructor.call_args.kwargs
|
|
assert kwargs["concept"].id == "user/example"
|
|
assert kwargs["override_gate"] == "inspect fixture"
|
|
|
|
|
|
def test_blank_override_rejected_before_model_load():
|
|
with pytest.raises(ValueError, match="nonempty reason"):
|
|
AbliterationPipeline("offline", override_gate=" ")
|
|
|
|
|
|
def test_refinement_revalidates_before_next_edit(tmp_path, monkeypatch):
|
|
# Exercise the real EXCISE loop with two passes and a zero first edit;
|
|
# the second failed gate must prevent its intervention.
|
|
source = build_tiny_offline_model(tmp_path / "model")
|
|
pipeline = AbliterationPipeline(
|
|
str(source),
|
|
concept=small_target(),
|
|
output_dir=str(tmp_path / "output"),
|
|
method="basic",
|
|
device="cpu",
|
|
dtype="float32",
|
|
refinement_passes=2,
|
|
true_iterative_refinement=True,
|
|
)
|
|
pipeline._summon()
|
|
pipeline._probe()
|
|
pipeline._distill()
|
|
calls = []
|
|
original = pipeline._distill_inner
|
|
|
|
def distill():
|
|
calls.append("distill")
|
|
original()
|
|
|
|
def gate():
|
|
calls.append("gate")
|
|
raise GateFailed("refinement blocked")
|
|
|
|
monkeypatch.setattr(pipeline, "_distill_inner", distill)
|
|
monkeypatch.setattr(pipeline, "_validate_concept_gate", gate)
|
|
with pytest.raises(GateFailed, match="refinement blocked"):
|
|
pipeline._excise()
|
|
assert calls == ["distill", "gate"]
|
|
|
|
|
|
def test_cli_rejects_remote_concept_before_connecting(monkeypatch):
|
|
from obliteratus import cli
|
|
|
|
runner = Mock()
|
|
monkeypatch.setattr(cli, "_make_remote_runner", runner)
|
|
with pytest.raises(SystemExit) as error:
|
|
cli.main(["obliterate", "offline", "--remote", "host", "--concept", "refusal"])
|
|
assert error.value.code == 2
|
|
runner.assert_not_called()
|
|
|
|
|
|
def test_missing_legacy_cache_is_advisory(tmp_path):
|
|
pipeline = AbliterationPipeline("offline", output_dir=str(tmp_path), gate_mode="report")
|
|
pipeline._validate_concept_gate()
|
|
report = json.loads((tmp_path / "validation.json").read_text())
|
|
assert report["mode"] == "report"
|
|
assert report["verdict"] == "warn"
|
|
assert report["reasons"][0]["check"] == "unavailable"
|
|
assert report["metrics"] == {}
|
|
|
|
|
|
@pytest.mark.parametrize("concept", [None, "refusal", "core/refusal", "custom"])
|
|
def test_off_skips_all_gate_computation(tmp_path, monkeypatch, concept):
|
|
target = small_target()
|
|
target.neighbours.update({"nearby": ["harmful request"]})
|
|
kwargs = {"concept": target, "gate_mode": "off"} if concept == "custom" else {
|
|
"concept": concept,
|
|
"harmful_prompts": target.positive[:4],
|
|
"harmless_prompts": target.controls["neutral"],
|
|
}
|
|
forbidden = Mock(side_effect=AssertionError("gate computation while off"))
|
|
for name in ("discriminating_tokens", "effective_thresholds", "validate_direction",
|
|
"_lexical_baseline"):
|
|
monkeypatch.setattr(f"obliteratus.concepts.gate.{name}", forbidden)
|
|
source = build_tiny_offline_model(tmp_path / "model")
|
|
pipeline = AbliterationPipeline(
|
|
str(source), output_dir=str(tmp_path / "output"),
|
|
device="cpu", dtype="float32", method="basic", **kwargs,
|
|
)
|
|
assert pipeline._build_metadata()["concept"]["validation_verdict"] == "not_validated"
|
|
assert "validation" not in pipeline._build_metadata()["concept"]
|
|
pipeline._summon()
|
|
calls = []
|
|
hook = pipeline.handle.model.register_forward_hook(lambda *args: calls.append(1))
|
|
monkeypatch.setattr("obliteratus.abliterate.dev.get_total_free_gb", lambda: 16.0)
|
|
try:
|
|
pipeline._probe()
|
|
assert len(calls) == 2
|
|
assert not pipeline._validation_readouts
|
|
assert not pipeline._neighbour_acts
|
|
pipeline._distill()
|
|
pipeline._validate_concept_gate()
|
|
assert len(calls) == 2
|
|
finally:
|
|
hook.remove()
|
|
pipeline.cleanup_failed_run()
|
|
forbidden.assert_not_called()
|
|
assert pipeline._build_metadata()["concept"]["validation"] == "skipped"
|
|
assert pipeline._build_metadata()["concept"]["validation_verdict"] == "not_validated"
|
|
assert not (pipeline.output_dir / "validation.json").exists()
|
|
|
|
|
|
@pytest.mark.parametrize("mismatch", ["layers", "reference", "write", "refinement"])
|
|
def test_validation_errors_wrap_and_clean_up(tmp_path, monkeypatch, mismatch):
|
|
source = build_tiny_offline_model(tmp_path / "model")
|
|
pipeline = AbliterationPipeline(
|
|
str(source), concept=small_target(), output_dir=str(tmp_path / "output"),
|
|
device="cpu", dtype="float32", method="basic", gate_mode="report",
|
|
refinement_passes=2 if mismatch == "refinement" else 1,
|
|
true_iterative_refinement=mismatch == "refinement",
|
|
refusal_eval_size=2,
|
|
refusal_eval_max_new_tokens=2,
|
|
)
|
|
for name in ("_capture_stock_baseline", "_capture_baseline_kl_logits", "_verify"):
|
|
monkeypatch.setattr(pipeline, name, lambda: None)
|
|
original_distill = pipeline._distill_inner if mismatch == "refinement" else pipeline._distill
|
|
|
|
def distill():
|
|
original_distill()
|
|
if mismatch in {"layers", "refinement"}:
|
|
pipeline._harmful_acts.clear()
|
|
elif mismatch == "reference":
|
|
pipeline._validation_reference = ConceptDirection(directions={0: torch.ones(17)})
|
|
|
|
monkeypatch.setattr(
|
|
pipeline, "_distill_inner" if mismatch == "refinement" else "_distill", distill,
|
|
)
|
|
if mismatch == "write":
|
|
monkeypatch.setattr(
|
|
"obliteratus.concepts.gate.write_validation", Mock(side_effect=OSError("write failed")),
|
|
)
|
|
excise = Mock()
|
|
if mismatch != "refinement":
|
|
monkeypatch.setattr(pipeline, "_excise", excise)
|
|
with pytest.raises(PipelineFailure) as error:
|
|
pipeline.run()
|
|
assert error.value.stage == "validate"
|
|
if mismatch == "layers":
|
|
# Default admission warns about unknown gate metadata; the gate itself
|
|
# still validates its inputs and raises the original execution error.
|
|
assert isinstance(error.value.__cause__, ValueError)
|
|
else:
|
|
assert isinstance(error.value.__cause__, OSError if mismatch == "write" else ValueError)
|
|
excise.assert_not_called()
|
|
assert pipeline.handle.model is None
|
|
assert pipeline.handle.tokenizer is None
|
|
assert not pipeline._harmful_acts
|
|
assert not pipeline._harmless_acts
|
|
assert not pipeline._control_acts
|
|
assert not pipeline._validation_readouts
|
|
assert not pipeline._neighbour_acts
|
|
|
|
|
|
@pytest.mark.parametrize("command", ["obliterate", "abliterate"])
|
|
@pytest.mark.parametrize("mode", ["enforce", "report", "off"])
|
|
def test_cli_gate_mode_forwarded(monkeypatch, command, mode):
|
|
from obliteratus import cli
|
|
|
|
constructor = Mock()
|
|
monkeypatch.setattr("obliteratus.abliterate.AbliterationPipeline", constructor)
|
|
cli.main([command, "offline", "--gate-mode", mode])
|
|
assert constructor.call_args.kwargs["gate_mode"] == mode
|
|
|
|
|
|
def test_cli_rejects_remote_gate_mode(monkeypatch):
|
|
from obliteratus import cli
|
|
|
|
runner = Mock()
|
|
monkeypatch.setattr(cli, "_make_remote_runner", runner)
|
|
with pytest.raises(SystemExit) as error:
|
|
cli.main(["obliterate", "offline", "--remote", "host", "--gate-mode", "off"])
|
|
assert error.value.code == 2
|
|
runner.assert_not_called()
|
|
|
|
|
|
def test_invalid_gate_mode_rejected_before_model_load():
|
|
with pytest.raises(ValueError, match="gate_mode"):
|
|
AbliterationPipeline("offline", gate_mode="invalid")
|
|
|
|
|
|
def test_refusal_outputs_and_metadata_match_without_gate(tmp_path, monkeypatch):
|
|
from transformers import AutoModelForCausalLM
|
|
|
|
source = build_tiny_offline_model(tmp_path / "model")
|
|
target = small_target()
|
|
baseline_weights, baseline_metadata = None, None
|
|
for name, concept, mode in (
|
|
("baseline", None, None),
|
|
("default", None, None),
|
|
("explicit", "refusal", None),
|
|
("report", "refusal", "report"),
|
|
):
|
|
pipeline = AbliterationPipeline(
|
|
str(source), output_dir=str(tmp_path / name), concept=concept, gate_mode=mode,
|
|
method="basic", device="cpu", dtype="float32", seed=228,
|
|
harmful_prompts=target.positive[:4], harmless_prompts=target.controls["neutral"],
|
|
)
|
|
for stage in ("_capture_stock_baseline", "_capture_baseline_kl_logits", "_verify"):
|
|
monkeypatch.setattr(pipeline, stage, lambda: None)
|
|
if name == "baseline":
|
|
# Reproduce the pre-gate sequence, then compare the saved artifact.
|
|
monkeypatch.setattr(pipeline, "_validate_concept_gate", lambda: None)
|
|
output = pipeline.run()
|
|
metadata = json.loads((output / "abliteration_metadata.json").read_text())
|
|
reloaded = AutoModelForCausalLM.from_pretrained(output, local_files_only=True)
|
|
weights = reloaded.state_dict()
|
|
assert metadata["technique"] == "refusal_direction_ablation"
|
|
if name == "baseline":
|
|
baseline_weights, baseline_metadata = weights, metadata
|
|
else:
|
|
if mode == "report":
|
|
assert metadata["concept"].pop("validation")["mode"] == "report"
|
|
metadata["concept"]["validation_verdict"] = "not_validated"
|
|
else:
|
|
assert metadata["concept"].pop("validation") == "skipped"
|
|
assert metadata == baseline_metadata
|
|
assert weights.keys() == baseline_weights.keys()
|
|
assert all(torch.equal(value, baseline_weights[key]) for key, value in weights.items())
|
|
pipeline.cleanup_failed_run()
|
|
|
|
|
|
def test_refusal_report_cost_and_no_forward_passes(tmp_path):
|
|
source = build_tiny_offline_model(tmp_path / "model")
|
|
target = small_target()
|
|
pipeline = AbliterationPipeline(
|
|
str(source), output_dir=str(tmp_path / "output"), gate_mode="report",
|
|
device="cpu", dtype="float32", method="basic", seed=228,
|
|
harmful_prompts=target.positive[:4], harmless_prompts=target.controls["neutral"],
|
|
)
|
|
pipeline._summon()
|
|
calls, forward_times = [], []
|
|
start = 0
|
|
|
|
def before_forward(*args):
|
|
nonlocal start
|
|
start = time.perf_counter()
|
|
|
|
def after_forward(*args):
|
|
calls.append(1)
|
|
forward_times.append(time.perf_counter() - start)
|
|
|
|
before = pipeline.handle.model.register_forward_pre_hook(before_forward)
|
|
after = pipeline.handle.model.register_forward_hook(after_forward)
|
|
try:
|
|
pipeline._probe()
|
|
pipeline._distill()
|
|
count = len(calls)
|
|
pipeline._validate_concept_gate() # warm up
|
|
durations = []
|
|
for _ in range(20):
|
|
t0 = time.perf_counter()
|
|
pipeline._validate_concept_gate()
|
|
durations.append(time.perf_counter() - t0)
|
|
assert len(calls) == count
|
|
assert pipeline.validation_report["mode"] == "report"
|
|
print(
|
|
f"refusal report: median={statistics.median(durations) * 1000:.3f} ms, "
|
|
f"range={min(durations) * 1000:.3f}-{max(durations) * 1000:.3f} ms; "
|
|
f"PROBE forward time={sum(forward_times) * 1000:.3f} ms; extra forwards=0"
|
|
)
|
|
finally:
|
|
before.remove()
|
|
after.remove()
|
|
pipeline.cleanup_failed_run()
|