mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
107 lines
5.0 KiB
Python
107 lines
5.0 KiB
Python
"""Bayesian warm starts and rejected compensation retain honest parent evidence."""
|
|
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from obliteratus import bayesian_optimizer as bo
|
|
from obliteratus.informed_pipeline import InformedAbliterationPipeline
|
|
from tests.test_bayesian_optimizer_contracts import _Layer, _optimization_pipeline
|
|
from tests.test_bayesian_source_domain import _capture_studies
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
|
|
|
|
def test_warm_start_without_matched_means_or_clusters_reaches_bounded_trial(monkeypatch):
|
|
studies = _capture_studies(monkeypatch)
|
|
pipeline = _optimization_pipeline([_Layer(), _Layer()])
|
|
pipeline._insights = SimpleNamespace(
|
|
detected_alignment_method="unknown", cluster_representative_layers=[0, 1],
|
|
direction_clusters=[], direction_persistence=0., entanglement_score=0.,
|
|
)
|
|
pipeline._informed_bayesian_trials = 1
|
|
pipeline._informed_kl_budget = .25
|
|
pipeline._harmful_means = {0: torch.ones(2), 1: torch.ones(2)}
|
|
pipeline._harmless_means = {}
|
|
InformedAbliterationPipeline._configure_bayesian_warm_start(pipeline)
|
|
assert pipeline._informed_warm_start["peak_position"] == 1.
|
|
assert pipeline._informed_warm_start["spread"] == .3
|
|
assert pipeline.kl_budget == .25
|
|
try:
|
|
bo.run_bayesian_optimization(pipeline, n_trials=1, n_kl_prompts=1)
|
|
trial = studies[0].trials[0]
|
|
assert trial.params["attn_peak_position"] == .9
|
|
assert trial.params["mlp_peak_position"] == .9
|
|
assert trial.params["attn_spread"] == trial.params["mlp_spread"] == .3
|
|
assert pipeline._bayesian_selected_plan_sha256 == trial.user_attrs["plan_sha256"]
|
|
finally:
|
|
bo._reset_bayesian_state(pipeline)
|
|
|
|
|
|
@pytest.mark.parametrize("metric,value", [
|
|
("refusal_rate", float("nan")), ("refusal_rate", True),
|
|
("refusal_rate", -.1), ("refusal_rate", 1.1),
|
|
("kl_divergence", None), ("kl_divergence", float("inf")),
|
|
("kl_divergence", -.1), ("kl_divergence", False),
|
|
])
|
|
def test_invalid_compensation_measurement_restores_parent_plan_and_weights(monkeypatch, tmp_path, metric, value):
|
|
pipeline = InformedAbliterationPipeline(
|
|
"local-fixture", output_dir=str(tmp_path), max_ouroboros_passes=1,
|
|
on_log=lambda _message: None,
|
|
)
|
|
model = torch.nn.Linear(1, 1, bias=False)
|
|
with torch.no_grad():
|
|
model.weight.fill_(1.)
|
|
pipeline.handle = SimpleNamespace(model=model)
|
|
pipeline._strong_layers = [0]
|
|
pipeline._bayesian_plan = {"candidate": "parent"}
|
|
pipeline._bayesian_selected_plan_sha256 = "parent-plan"
|
|
pipeline._bayesian_search = {"completed_trials": 1}
|
|
parent_metrics = {"refusal_rate": .9, "kl_divergence": .1}
|
|
invalid_metrics = {"refusal_rate": .1, "kl_divergence": .1, metric: value}
|
|
results = iter((parent_metrics.copy(), invalid_metrics))
|
|
events = []
|
|
# Isolate the compensation boundary; snapshots and tensor restoration stay real.
|
|
monkeypatch.setattr(pipeline, "_admit_pipeline_phase", lambda *args, **kwargs: None)
|
|
monkeypatch.setattr(pipeline, "_excise_parameter_names", lambda **kwargs: ("weight",))
|
|
monkeypatch.setattr(pipeline, "_verify", lambda **kwargs: setattr(pipeline, "_quality_metrics", next(results)))
|
|
monkeypatch.setattr(pipeline, "_probe", lambda: None)
|
|
monkeypatch.setattr(pipeline, "_distill_inner", lambda: None)
|
|
monkeypatch.setattr(pipeline, "_on_refinement_candidate", lambda event, **details: events.append((event, details)))
|
|
snapshots = []
|
|
factory = pipeline._refinement_snapshot_factory
|
|
|
|
def capture(names):
|
|
snapshot = factory(names)
|
|
snapshot.release = Mock(wraps=snapshot.release)
|
|
snapshots.append(snapshot)
|
|
return snapshot
|
|
|
|
monkeypatch.setattr(pipeline, "_refinement_snapshot_factory", capture)
|
|
|
|
def excise():
|
|
with torch.no_grad():
|
|
model.weight.add_(10.)
|
|
pipeline._bayesian_plan = {"candidate": "branch"}
|
|
pipeline._bayesian_selected_plan_sha256 = "branch-plan"
|
|
pipeline._bayesian_search = {"completed_trials": 3}
|
|
pipeline._bayesian_replay = {"plan_sha256": "branch-plan"}
|
|
|
|
monkeypatch.setattr(pipeline, "_excise_informed", excise)
|
|
pipeline._verify_and_compensate()
|
|
torch.testing.assert_close(model.weight, torch.ones(1, 1), rtol=0, atol=0)
|
|
assert pipeline._quality_metrics["refusal_rate"] == parent_metrics["refusal_rate"]
|
|
assert pipeline._quality_metrics["kl_divergence"] == parent_metrics["kl_divergence"]
|
|
assert pipeline._bayesian_plan == {"candidate": "parent"}
|
|
assert pipeline._bayesian_selected_plan_sha256 == "parent-plan"
|
|
assert pipeline._bayesian_search == {"completed_trials": 1}
|
|
assert not hasattr(pipeline, "_bayesian_replay")
|
|
assert pipeline._report.final_refusal_rate == .9
|
|
assert [event for event, _ in events] == ["mutation_started", "candidate_completed", "candidate_restored"]
|
|
assert events[-1][1]["reason"] == "candidate evidence unavailable"
|
|
assert events[-1][1]["candidate_id"] == events[0][1]["candidate_id"]
|
|
assert len(snapshots) == 1
|
|
snapshots[0].release.assert_called_once_with()
|