mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 18:44:41 +01:00
317 lines
13 KiB
Python
317 lines
13 KiB
Python
"""Alternate pipeline and UI entries admit their actual stage before work."""
|
|
|
|
from dataclasses import dataclass
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
|
|
from obliteratus import phase_admission as admission
|
|
from obliteratus.ui_concept_lab import ConceptLabSession
|
|
|
|
|
|
def _phase_pipeline(trace, rejected):
|
|
pipeline = SimpleNamespace(
|
|
concept_direction=SimpleNamespace(save=Mock()), validation_report={},
|
|
cleanup_failed_run=Mock(),
|
|
)
|
|
|
|
def admit(stage, **options):
|
|
if stage == "validate":
|
|
assert options == {"gate_only": True}
|
|
pipeline._active_stage = stage
|
|
trace.append("admit:" + stage)
|
|
if stage == rejected:
|
|
raise admission.PhaseAdmissionError(stage, "cpu", 100, 0)
|
|
|
|
pipeline._admit_pipeline_phase = admit
|
|
for name, stage in (("_summon", "summon"), ("_probe", "probe"),
|
|
("_distill", "distill"), ("_validate_concept_gate", "validate")):
|
|
setattr(pipeline, name, lambda stage=stage: trace.append("work:" + stage))
|
|
return pipeline
|
|
|
|
|
|
@pytest.mark.parametrize("entry", ["lab", "baseline"])
|
|
@pytest.mark.parametrize("stage", ["probe", "distill", "validate"])
|
|
def test_extraction_entries_reject_before_matching_phase(entry, stage, tmp_path, monkeypatch):
|
|
trace = []
|
|
pipeline = _phase_pipeline(trace, stage)
|
|
if entry == "lab":
|
|
session = ConceptLabSession.__new__(ConceptLabSession)
|
|
session.pipeline, session.direction, session.run = pipeline, None, None
|
|
|
|
def run():
|
|
session.extract()
|
|
session.validate()
|
|
else:
|
|
from obliteratus.concepts import baseline
|
|
|
|
pack = Mock()
|
|
monkeypatch.setattr(baseline, "_resolve", lambda _: pack)
|
|
monkeypatch.setattr(baseline, "AbliterationPipeline", lambda *a, **kw: pipeline)
|
|
|
|
def run():
|
|
baseline.run_baseline("fixture", "local", tmp_path / "run")
|
|
|
|
with pytest.raises(admission.PhaseAdmissionError) as error:
|
|
run()
|
|
assert error.value.phase == stage
|
|
assert pipeline._active_stage == stage
|
|
assert trace[-1] == "admit:" + stage
|
|
assert "work:" + stage not in trace
|
|
if entry == "baseline":
|
|
pipeline.cleanup_failed_run.assert_called_once()
|
|
|
|
|
|
def test_saved_checkpoint_verification_rejects_before_work_and_records_failure(
|
|
tmp_path, monkeypatch,
|
|
):
|
|
from obliteratus import checkpoint_evaluation as checkpoint
|
|
|
|
trace = []
|
|
pipeline = _phase_pipeline(trace, "verify")
|
|
pipeline._quality_metrics = {}
|
|
pipeline._verify = Mock()
|
|
archive, lifecycle = Mock(), Mock()
|
|
archive.begin_evaluation.return_value = {"evaluation_id": "fixture-evaluation"}
|
|
archive.result.return_value = {"result": {"metrics": {
|
|
"baseline_perplexity": 1.0, "baseline_coherence": 1.0,
|
|
}}}
|
|
monkeypatch.setattr(checkpoint, "RunArchive", lambda _: archive)
|
|
monkeypatch.setattr(checkpoint, "from_environment", lambda: lifecycle)
|
|
monkeypatch.setattr(checkpoint, "AbliterationPipeline", lambda **kw: pipeline)
|
|
monkeypatch.setattr(checkpoint, "_verify_checkpoint_inventory", lambda *a: tmp_path)
|
|
monkeypatch.setattr(checkpoint, "_partition_pairs", lambda _: (("h", "s"),))
|
|
monkeypatch.setattr(checkpoint, "measure_torch_memory", lambda _: object())
|
|
|
|
with pytest.raises(admission.PhaseAdmissionError) as error:
|
|
checkpoint.evaluate("fixture", "optimizer_tune", str(tmp_path))
|
|
assert pipeline._active_stage == "verify"
|
|
pipeline._verify.assert_not_called()
|
|
assert archive.finish_evaluation.call_args.kwargs["failure"] is error.value
|
|
pipeline.cleanup_failed_run.assert_called_once()
|
|
lifecycle.release.assert_called_once()
|
|
|
|
|
|
@dataclass
|
|
class _RepairReport:
|
|
repair: str = "not needed"
|
|
|
|
def save(self, _directory):
|
|
pass
|
|
|
|
|
|
def _lab_action(monkeypatch, tmp_path, action):
|
|
from obliteratus import ui_concept_lab as lab
|
|
|
|
trace = []
|
|
evaluation = SimpleNamespace(metadata={}, save=Mock())
|
|
battery = SimpleNamespace(
|
|
decoding={},
|
|
generate=lambda *a: trace.append("generate") or ["text"],
|
|
finish=lambda *a: trace.append("evaluate") or evaluation,
|
|
)
|
|
pipeline = SimpleNamespace(
|
|
handle=SimpleNamespace(model=object(), tokenizer=object()),
|
|
gpu_memory_utilization=1.0, output_dir=tmp_path, _repair_backend=None,
|
|
_protected_repair_backend=None, _preserved_directions=[],
|
|
_evaluation_battery=battery,
|
|
_prepare_concept_preservation=lambda: trace.append("prepare preservation"),
|
|
_capture_concept_evaluation=lambda: trace.append("capture evaluation"),
|
|
_capture_concept_repair=lambda **kw: trace.append("capture repair"),
|
|
)
|
|
session = ConceptLabSession.__new__(ConceptLabSession)
|
|
session.pipeline = pipeline
|
|
session.direction = SimpleNamespace(validation={"mode": "enforce", "verdict": "pass"})
|
|
session.pack = object()
|
|
session.directory = tmp_path
|
|
session.preview_prompt = "hello"
|
|
session.run = SimpleNamespace(
|
|
mode="project", evaluation=evaluation, repair=None, repair_adapter=None,
|
|
)
|
|
monkeypatch.setattr(lab, "apply_concept_intervention",
|
|
lambda *a, **kw: trace.append("intervene") or SimpleNamespace())
|
|
monkeypatch.setattr(lab, "run_repair_ladder", lambda *a, **kw:
|
|
(trace.append("repair") or _RepairReport(), evaluation))
|
|
monkeypatch.setattr(lab, "save_concept_run", lambda *a, **kw: trace.append("save"))
|
|
monkeypatch.setattr(lab, "export_run", lambda *a, **kw: trace.append("export"))
|
|
monkeypatch.setattr(lab.shutil, "make_archive", lambda *a: "artifact.zip")
|
|
monkeypatch.setattr(admission, "phase_memory_devices", lambda _: ("cpu",))
|
|
monkeypatch.setattr(admission, "observe_memory", lambda device:
|
|
admission.MemoryObservation(device, 1024**3, 1024**3))
|
|
if action == "apply":
|
|
session.run = None
|
|
callback = lambda: session.apply("project", "all", "all_tokens", 1, "", "hello")
|
|
elif action == "export":
|
|
callback = lambda: session.export("direction")
|
|
else:
|
|
callback = getattr(session, action)
|
|
return session, trace, callback
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ["default", "strict"])
|
|
@pytest.mark.parametrize("action,stage", [
|
|
("apply", "validate"), ("evaluate", "verify"),
|
|
("repair", "excise"), ("export", "rebirth"),
|
|
])
|
|
def test_lab_custom_work_warns_by_default_and_strict_rejects_before_materialization(
|
|
action, stage, mode, monkeypatch, tmp_path, caplog,
|
|
):
|
|
session, trace, callback = _lab_action(monkeypatch, tmp_path, action)
|
|
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", mode)
|
|
if mode == "strict":
|
|
with pytest.raises(admission.PhaseAdmissionError, match="Concept Lab") as error:
|
|
callback()
|
|
assert error.value.phase == stage
|
|
assert session.pipeline._active_stage == stage
|
|
assert trace == []
|
|
else:
|
|
callback()
|
|
assert trace
|
|
assert "Concept Lab" in caplog.text
|
|
assert "Proceeding in default mode" in caplog.text
|
|
|
|
|
|
@pytest.mark.parametrize("stage,operation,expected_before", [
|
|
("excise", "project intervention",
|
|
["prepare preservation", "capture evaluation", "generate", "capture repair"]),
|
|
("verify", "intervened preview generation",
|
|
["prepare preservation", "capture evaluation", "generate", "capture repair", "intervene"]),
|
|
])
|
|
def test_lab_apply_refreshes_admission_before_edit_and_modified_preview(
|
|
stage, operation, expected_before, monkeypatch, tmp_path,
|
|
):
|
|
session, trace, callback = _lab_action(monkeypatch, tmp_path, "apply")
|
|
|
|
def reject(selected_stage, selected_operation):
|
|
session.pipeline._active_stage = selected_stage
|
|
if selected_operation == operation:
|
|
raise admission.PhaseAdmissionError(selected_stage, "cpu", 100, 0)
|
|
|
|
monkeypatch.setattr(session, "_admit_custom_phase", reject)
|
|
with pytest.raises(admission.PhaseAdmissionError):
|
|
callback()
|
|
assert trace == expected_before
|
|
assert session.pipeline._active_stage == stage
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ["default", "strict"])
|
|
def test_lab_weight_export_uses_raw_writer_uncertainty_before_saving(
|
|
mode, monkeypatch, tmp_path, caplog,
|
|
):
|
|
session, trace, _ = _lab_action(monkeypatch, tmp_path, "export")
|
|
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", mode)
|
|
native = Mock(side_effect=AssertionError("raw save has no native writer contract"))
|
|
monkeypatch.setattr(admission, "admit_model_phase", native)
|
|
if mode == "strict":
|
|
with pytest.raises(admission.PhaseAdmissionError, match="weights bundle"):
|
|
session.export("weights")
|
|
assert trace == []
|
|
else:
|
|
session.export("weights")
|
|
assert trace == ["save", "export"]
|
|
assert "weights bundle and adapter export" in caplog.text
|
|
native.assert_not_called()
|
|
assert session.pipeline._active_stage == "rebirth"
|
|
|
|
|
|
def test_lab_admission_off_skips_custom_metadata(monkeypatch, tmp_path):
|
|
session, trace, callback = _lab_action(monkeypatch, tmp_path, "evaluate")
|
|
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "off")
|
|
monkeypatch.setattr(admission, "phase_memory_devices", Mock(side_effect=AssertionError))
|
|
callback()
|
|
assert trace == ["evaluate"]
|
|
assert session.pipeline._active_stage == "verify"
|
|
|
|
|
|
def _public_evaluation(monkeypatch, tmp_path):
|
|
from obliteratus.concepts import evaluation, intervention, repair, repair_model, runs
|
|
|
|
trace = []
|
|
report = SimpleNamespace(metadata={}, save=Mock())
|
|
battery = SimpleNamespace(
|
|
capture=lambda: trace.append("capture"),
|
|
finish=lambda: trace.append("evaluate") or report,
|
|
)
|
|
run = SimpleNamespace(remove=lambda: trace.append("remove"))
|
|
backend = SimpleNamespace(bind=lambda: None, adapter=None)
|
|
monkeypatch.setattr(repair_model, "WeightRepair", lambda *a, **kw:
|
|
trace.append("capture repair") or backend)
|
|
monkeypatch.setattr(intervention, "apply_concept_intervention", lambda *a, **kw:
|
|
trace.append("intervene") or run)
|
|
monkeypatch.setattr(repair, "run_repair_ladder", lambda *a, **kw:
|
|
(trace.append("repair") or _RepairReport(), report))
|
|
monkeypatch.setattr(runs, "save_concept_run", lambda *a, **kw: trace.append("save"))
|
|
monkeypatch.setattr(admission, "phase_memory_devices", lambda _: ("cpu",))
|
|
monkeypatch.setattr(admission, "observe_memory", lambda device:
|
|
admission.MemoryObservation(device, 1024**3, 1024**3))
|
|
|
|
def callback(mode="orthogonalize"):
|
|
return evaluation.evaluate_intervention(
|
|
object(), object(), mode, battery, repair=True, output_dir=tmp_path,
|
|
)
|
|
|
|
return trace, callback
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ["default", "strict"])
|
|
def test_public_concept_evaluation_admits_before_capture_and_runs_default(
|
|
mode, monkeypatch, tmp_path, caplog,
|
|
):
|
|
trace, callback = _public_evaluation(monkeypatch, tmp_path)
|
|
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", mode)
|
|
if mode == "strict":
|
|
with pytest.raises(admission.PhaseAdmissionError, match="pristine and repair capture"):
|
|
callback()
|
|
assert trace == []
|
|
else:
|
|
callback()
|
|
assert trace == ["capture repair", "capture", "intervene", "evaluate", "repair", "save"]
|
|
assert "Concept evaluation" in caplog.text
|
|
assert "Proceeding in default mode" in caplog.text
|
|
|
|
|
|
@pytest.mark.parametrize("operation,phase,before", [
|
|
("project intervention", "excise", ["capture"]),
|
|
("intervention battery", "verify", ["capture", "intervene", "remove"]),
|
|
("repair candidates and evaluation", "excise",
|
|
["capture", "intervene", "evaluate", "remove"]),
|
|
("report and direction export", "rebirth",
|
|
["capture", "intervene", "evaluate", "repair", "remove"]),
|
|
])
|
|
def test_public_concept_evaluation_refreshes_each_admission_and_cleans_hooks(
|
|
operation, phase, before, monkeypatch, tmp_path,
|
|
):
|
|
from obliteratus.concepts import evaluation
|
|
|
|
trace, callback = _public_evaluation(monkeypatch, tmp_path)
|
|
|
|
def reject(model, selected_phase, selected_operation):
|
|
if selected_operation == operation:
|
|
raise admission.PhaseAdmissionError(selected_phase, "cpu", 100, 0)
|
|
|
|
monkeypatch.setattr(evaluation, "_admit_evaluation_phase", reject)
|
|
with pytest.raises(admission.PhaseAdmissionError) as error:
|
|
callback("project")
|
|
assert error.value.phase == phase
|
|
assert trace == before
|
|
|
|
|
|
@pytest.mark.parametrize("entry", ["lab", "public"])
|
|
def test_custom_concept_admission_includes_host_for_cuda_models(entry, monkeypatch, tmp_path):
|
|
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "default")
|
|
captured = Mock()
|
|
monkeypatch.setattr(admission, "admit_phase", captured)
|
|
if entry == "lab":
|
|
session, _, _ = _lab_action(monkeypatch, tmp_path, "evaluate")
|
|
invoke = lambda: session._admit_custom_phase("validate", "repair capture")
|
|
else:
|
|
from obliteratus.concepts.evaluation import _admit_evaluation_phase
|
|
|
|
invoke = lambda: _admit_evaluation_phase(object(), "validate", "repair capture")
|
|
monkeypatch.setattr(admission, "phase_memory_devices", lambda _: ("cuda:1",))
|
|
invoke()
|
|
plan = captured.call_args.args[1]
|
|
assert set(plan.devices) == {"cpu", "cuda:1"}
|
|
assert all(component.bytes is None for component in plan.components)
|