Files
OBLITERATUS/tests/test_phase_informed_admission.py

198 lines
8.7 KiB
Python

"""The informed driver admits its actual phases, including nested reruns."""
from collections import Counter
from unittest.mock import Mock
import pytest
import torch
from obliteratus import phase_admission as admission
from obliteratus.abliterate import PipelineFailure
from obliteratus.informed_pipeline import InformedAbliterationPipeline
from tests.test_phase_memory_helpers import torch_thread_count
from tests.test_phase_optional_admission import make_pipeline
pytestmark = pytest.mark.cpu
@pytest.fixture
def pipeline():
with torch_thread_count(1):
base = make_pipeline(layers=1)
result = InformedAbliterationPipeline(
"local", harmful_prompts=["8"] * 8, harmless_prompts=["8"] * 8,
on_log=lambda _message: None,
)
result.handle = base.handle
result._strong_layers = [0]
result.refusal_directions = base.refusal_directions
result.refusal_subspaces = base.refusal_subspaces
result._free_gpu_memory = lambda: None
yield result
def traced_admission(pipeline, monkeypatch, trace, reject=None):
counts = Counter()
def admit(stage, **options):
pipeline._active_stage = stage
counts[stage] += 1
trace.append("admit:" + stage)
if reject == (stage, counts[stage]):
raise admission.PhaseAdmissionError(stage, "cpu", 100, 0)
monkeypatch.setattr(pipeline, "_admit_pipeline_phase", admit)
@pytest.mark.parametrize("rejected", [None, "baseline", "probe", "analyze", "distill",
"excise", "verify", "rebirth"])
def test_informed_driver_admits_before_each_stage_and_reports_rejection(
pipeline, monkeypatch, rejected,
):
trace = []
stages = ["baseline", "probe", "analyze", "distill", "excise", "verify", "rebirth"]
methods = ["_capture_stock_baseline", "_probe", "_analyze", "_distill_informed",
"_excise", "_verify", "_rebirth_informed"]
for stage, method in zip(stages, methods):
if method == "_verify":
monkeypatch.setattr(pipeline, method,
lambda stage=stage, **_kwargs: trace.append("work:" + stage))
else:
monkeypatch.setattr(pipeline, method, lambda stage=stage: trace.append("work:" + stage))
monkeypatch.setattr(pipeline, "_summon", lambda: None)
monkeypatch.setattr(pipeline, "_validate_architecture_surgery_support", lambda: None)
monkeypatch.setattr(pipeline, "_configure_bayesian_warm_start", lambda: None)
cleanup = Mock()
monkeypatch.setattr(pipeline, "cleanup_failed_run", cleanup)
pipeline.use_kl_optimization = False
traced_admission(pipeline, monkeypatch, trace, (rejected, 1))
if rejected is None:
pipeline.run_informed()
cleanup.assert_not_called()
assert trace == [entry for stage in stages for entry in ("admit:" + stage, "work:" + stage)]
else:
with pytest.raises(PipelineFailure) as caught:
pipeline.run_informed()
assert caught.value.stage == rejected
assert caught.value.metric == "memory_admission"
assert isinstance(caught.value.__cause__, admission.PhaseAdmissionError)
cleanup.assert_called_once()
index = stages.index(rejected)
assert trace == [entry for stage in stages[:index]
for entry in ("admit:" + stage, "work:" + stage)] + ["admit:" + rejected]
def test_bayesian_admission_sees_configuration_before_materialization(pipeline, monkeypatch):
pipeline._bayesian_trials = 0
work = Mock()
monkeypatch.setattr(pipeline, "_excise", work)
def reject(stage, **options):
assert stage == "excise"
assert options == {"work_scope": "surgery"}
assert pipeline._bayesian_trials == 50
raise admission.PhaseAdmissionError(stage, "cpu", 100, 0)
monkeypatch.setattr(pipeline, "_admit_pipeline_phase", reject)
with pytest.raises(admission.PhaseAdmissionError):
pipeline._excise_informed()
work.assert_not_called()
@pytest.mark.parametrize("rejected", [None, ("excise", 1), ("probe", 1), ("distill", 1),
("excise", 2)])
def test_sparse_refinement_refreshes_admission_before_each_operation(
pipeline, monkeypatch, rejected,
):
trace = []
pipeline._insights.use_sparse_surgery = True
pipeline.refinement_passes = 2
monkeypatch.setattr(pipeline, "_probe", lambda: trace.append("work:probe"))
monkeypatch.setattr(pipeline, "_distill_inner", lambda: trace.append("work:distill"))
def project(self, weight, direction):
trace.append("work:excise")
return weight
monkeypatch.setattr("obliteratus.analysis.sparse_surgery.SparseDirectionSurgeon.apply_sparse_projection",
project)
traced_admission(pipeline, monkeypatch, trace, rejected)
expected = ["admit:excise", "work:excise", "admit:probe", "work:probe",
"admit:distill", "work:distill", "admit:excise", "work:excise"]
if rejected is None:
pipeline._excise_informed()
else:
with pytest.raises(admission.PhaseAdmissionError):
pipeline._excise_informed()
indices = [i for i, entry in enumerate(expected) if entry == "admit:" + rejected[0]]
expected = expected[:indices[rejected[1] - 1] + 1]
assert trace == expected
@pytest.mark.parametrize("rejected", [None, ("verify", 1), ("probe", 1), ("distill", 1),
("excise", 1), ("verify", 2)])
def test_ouroboros_refreshes_admission_before_each_operation(pipeline, monkeypatch, rejected):
trace = []
pipeline._max_ouroboros_passes = 1
pipeline._quality_metrics = {"refusal_rate": 1.0, "kl_divergence": 0.1}
monkeypatch.setattr(pipeline, "_verify", lambda **_kwargs: trace.append("work:verify"))
monkeypatch.setattr(pipeline, "_probe", lambda: trace.append("work:probe"))
monkeypatch.setattr(pipeline, "_distill_inner", lambda: trace.append("work:distill"))
monkeypatch.setattr(pipeline, "_configure_bayesian_warm_start", lambda: None)
monkeypatch.setattr(pipeline, "_excise", lambda: trace.append("work:excise"))
traced_admission(pipeline, monkeypatch, trace, rejected)
expected = ["admit:verify", "work:verify", "admit:probe", "work:probe",
"admit:distill", "work:distill", "admit:excise", "admit:excise", "work:excise",
"admit:verify", "work:verify"]
if rejected is None:
pipeline._verify_and_compensate()
else:
with pytest.raises(admission.PhaseAdmissionError):
pipeline._verify_and_compensate()
indices = [i for i, entry in enumerate(expected) if entry == "admit:" + rejected[0]]
expected = expected[:indices[rejected[1] - 1] + 1]
assert trace == expected
@pytest.mark.parametrize("stage,reason", [("analyze", "informed analysis"),
("excise", "informed sparse surgery"),
("rebirth", "informed save_pretrained export"),
("distill", "informed distillation")])
def test_custom_workspaces_warn_by_default_and_strict_rejects_before_work(
pipeline, monkeypatch, caplog, stage, reason,
):
pipeline._insights.use_sparse_surgery = True
pipeline._insights.cone_is_polyhedral = True
pipeline._insights.per_category_directions = {"fixture": torch.ones(64)}
pipeline.n_directions = 2
monkeypatch.setattr(admission, "observe_memory",
lambda device: admission.MemoryObservation(device, 2**30, 2**30))
monkeypatch.setattr("obliteratus.gpu_lifecycle.current_process_grant", lambda: None)
options = {"work_scope": "informed"} if stage == "distill" else {}
pipeline._admit_pipeline_phase(stage, **options)
assert pipeline._active_stage == stage
assert reason in caplog.text and "Proceeding in default mode" in caplog.text
work = Mock()
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "strict")
with pytest.raises(admission.PhaseAdmissionError, match=reason):
pipeline._admit_pipeline_phase(stage, **options)
work()
work.assert_not_called()
assert pipeline._active_stage == stage
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "off")
pipeline._admit_pipeline_phase(stage, **options)
assert "PHASE MEMORY ADMISSION DISABLED" in caplog.text
def test_plain_informed_rerun_does_not_plan_category_enrichment(pipeline, monkeypatch):
pipeline.n_directions = 2
pipeline._insights.cone_is_polyhedral = True
pipeline._insights.per_category_directions = {"fixture": torch.ones(64)}
captured = Mock()
monkeypatch.setattr(admission, "admit_model_phase", captured)
pipeline._admit_pipeline_phase("distill")
assert not captured.call_args.kwargs["unknown_components"]