Files
OBLITERATUS/tests/test_bayesian_budget.py

255 lines
11 KiB
Python

"""Completed search evidence survives time admission and immediate replay."""
import json
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
import torch
from obliteratus import bayesian_optimizer as bo
from obliteratus.abliterate import AbliterationPipeline, PipelineQualityRejectedError
from obliteratus.execution_budget import ExecutionBudget, SearchBudgetExhausted
from tests.test_bayesian_optimizer_optuna import _TinyModel, _TinyTokenizer
def _pipeline(tmp_path):
pipeline = AbliterationPipeline(
model_name="tiny-budget", method="optimized", output_dir=str(tmp_path),
use_sae_features=False, attention_head_surgery=False, per_expert_directions=False,
)
pipeline.handle = SimpleNamespace(model=_TinyModel(), tokenizer=_TinyTokenizer(), architecture="llama")
pipeline._strong_layers = [0]
pipeline.refusal_directions = {0: torch.tensor([1., 0.])}
pipeline.harmful_prompts = ["Local fixture"]
pipeline._free_gpu_memory = lambda: None
return pipeline
def _state(pipeline):
return {name: value.detach().clone() for name, value in pipeline.handle.model.state_dict().items()}
def _assert_state(pipeline, expected):
for name, value in _state(pipeline).items():
torch.testing.assert_close(value, expected[name], rtol=0, atol=0)
def _timed(pipeline, monkeypatch, *, kl=.1):
now = [0.0]
events = []
def measure(*args, **kwargs):
now[0] += 20
return .2
def checkpoint(_trial):
now[0] += 5
events.append("persisted")
def remaining():
if now[0]:
assert events[-1] == "persisted"
return 70 - now[0]
monkeypatch.setattr(bo, "_measure_refusal_rate", measure)
monkeypatch.setattr(bo, "_measure_kl_divergence", lambda *args: kl)
pipeline._bayesian_trial_checkpoint = checkpoint
pipeline.execution_budget = ExecutionBudget(
remaining, 20, {"bayesian_trial": 10}, duration_margin=1.2, clock=lambda: now[0],
)
return now, events
def test_given_no_budget_when_searching_then_requested_trials_complete(monkeypatch, tmp_path):
pipeline = _pipeline(tmp_path)
original = _state(pipeline)
monkeypatch.setattr(bo, "_measure_kl_divergence", lambda *args: .1)
bo.run_bayesian_optimization(pipeline, n_trials=3, n_refusal_prompts=1, n_kl_prompts=1)
assert pipeline._bayesian_search == dict(requested_trials=3, completed_trials=3, stopped_for_budget=False)
_assert_state(pipeline, original)
bo._reset_bayesian_state(pipeline)
def test_given_trial_and_checkpoint_cost_when_next_trial_does_not_fit_then_select_completed(monkeypatch, tmp_path):
pipeline = _pipeline(tmp_path)
original = _state(pipeline)
_, events = _timed(pipeline, monkeypatch)
bo.run_bayesian_optimization(pipeline, n_trials=4, n_refusal_prompts=1, n_kl_prompts=1)
assert events == ["persisted"]
assert pipeline.execution_budget.observed_seconds["bayesian_trial"] == 25
assert pipeline._bayesian_search == dict(requested_trials=4, completed_trials=1, stopped_for_budget=True)
selected = next(path for path in (tmp_path / ".obliteratus-bayesian").glob("*.json") if ".trial-" not in path.name)
assert json.loads(selected.read_text())["search"] == pipeline._bayesian_search
_assert_state(pipeline, original)
result = bo.replay_bayesian_plan(pipeline)
assert result.applied_count > 0
assert any(not torch.equal(value, original[name]) for name, value in _state(pipeline).items())
def test_given_durable_feasible_trial_when_no_new_trial_fits_then_resume_without_search(monkeypatch, tmp_path):
pipeline = _pipeline(tmp_path)
_timed(pipeline, monkeypatch)
expected = bo.run_bayesian_optimization(pipeline, n_trials=4, n_refusal_prompts=1, n_kl_prompts=1)
bo._reset_bayesian_state(pipeline)
monkeypatch.setattr(bo, "_measure_refusal_rate", Mock(side_effect=AssertionError("must reuse completed trial")))
pipeline.execution_budget = ExecutionBudget(lambda: 0, 20, {"bayesian_trial": 10})
actual = bo.run_bayesian_optimization(pipeline, n_trials=4, n_refusal_prompts=1, n_kl_prompts=1)
assert actual == expected
assert pipeline._bayesian_search["completed_trials"] == 1
assert pipeline._bayesian_search["stopped_for_budget"]
bo._reset_bayesian_state(pipeline)
def test_given_infeasible_partial_search_when_time_exhausted_then_not_quality_rejection(monkeypatch, tmp_path):
pipeline = _pipeline(tmp_path)
original = _state(pipeline)
_timed(pipeline, monkeypatch, kl=.8)
with pytest.raises(SearchBudgetExhausted) as error:
bo.optimize_and_replay_bayesian_plan(pipeline, n_trials=4, n_refusal_prompts=1, n_kl_prompts=1)
assert not isinstance(error.value, PipelineQualityRejectedError)
assert pipeline._bayesian_execution is None
assert len(list((tmp_path / ".obliteratus-bayesian").glob("*.trial-*.json"))) == 1
_assert_state(pipeline, original)
@pytest.mark.parametrize("mode", ["missing", "setup"])
def test_given_unusable_budget_when_starting_then_reject_before_reference_capture(monkeypatch, tmp_path, mode):
pipeline = _pipeline(tmp_path)
estimates = {"unrelated": 1} if mode == "missing" else {"bayesian_setup": 20, "bayesian_trial": 10}
pipeline.execution_budget = ExecutionBudget(lambda: 0, 20, estimates)
capture = Mock(side_effect=AssertionError("expensive reference capture"))
monkeypatch.setattr(bo, "_capture_kl_evidence", capture)
with pytest.raises(ValueError if mode == "missing" else SearchBudgetExhausted):
bo.run_bayesian_optimization(pipeline, n_trials=4)
capture.assert_not_called()
def test_given_no_completed_trial_when_budget_disallows_first_trial_then_incomplete(monkeypatch, tmp_path):
pipeline = _pipeline(tmp_path)
original = _state(pipeline)
pipeline.execution_budget = ExecutionBudget(lambda: 0, 20, {"bayesian_trial": 10})
measurement = Mock(side_effect=AssertionError("trial should not run"))
monkeypatch.setattr(bo, "_measure_refusal_rate", measurement)
with pytest.raises(SearchBudgetExhausted):
bo.run_bayesian_optimization(pipeline, n_trials=4, n_kl_prompts=1)
measurement.assert_not_called()
_assert_state(pipeline, original)
def test_given_setup_estimate_when_setup_completes_then_duration_is_observed(monkeypatch, tmp_path):
pipeline = _pipeline(tmp_path)
now = [0.0]
capture = bo._capture_kl_evidence
def timed_capture(*args):
now[0] += 5
return capture(*args)
monkeypatch.setattr(bo, "_capture_kl_evidence", timed_capture)
monkeypatch.setattr(bo, "_measure_kl_divergence", lambda *args: .1)
pipeline.execution_budget = ExecutionBudget(
lambda: 100 - now[0], 20, {"bayesian_setup": 10, "bayesian_trial": 10}, clock=lambda: now[0],
)
bo.run_bayesian_optimization(pipeline, n_trials=1, n_refusal_prompts=1, n_kl_prompts=1)
assert pipeline.execution_budget.observed_seconds["bayesian_setup"] == 5
bo._reset_bayesian_state(pipeline)
@pytest.mark.parametrize("informed", [False, True])
def test_given_initial_search_exhaustion_when_running_pipeline_then_cleanup_and_propagate(monkeypatch, tmp_path, informed):
from obliteratus.informed_pipeline import InformedAbliterationPipeline
pipeline = InformedAbliterationPipeline("tiny-budget", output_dir=str(tmp_path)) if informed else _pipeline(tmp_path)
worker = "_run_informed_pipeline" if informed else "_run_pipeline"
monkeypatch.setattr(pipeline, worker, Mock(side_effect=SearchBudgetExhausted("unfinished search")))
cleanup = Mock()
monkeypatch.setattr(pipeline, "cleanup_failed_run", cleanup)
with pytest.raises(SearchBudgetExhausted, match="unfinished search"):
(pipeline.run_informed if informed else pipeline.run)()
cleanup.assert_called_once()
def test_given_checkpoint_failure_when_trial_finishes_then_rollback_and_propagate(monkeypatch, tmp_path):
pipeline = _pipeline(tmp_path)
original = _state(pipeline)
_timed(pipeline, monkeypatch)
pipeline._bayesian_trial_checkpoint = Mock(side_effect=OSError("disk unavailable"))
with pytest.raises(OSError, match="disk unavailable"):
bo.optimize_and_replay_bayesian_plan(pipeline, n_trials=4, n_refusal_prompts=1, n_kl_prompts=1)
assert pipeline._bayesian_execution is None
_assert_state(pipeline, original)
def test_given_immediate_replay_when_compared_to_separate_calls_then_same_weights_one_less_restore(monkeypatch, tmp_path):
factory = bo.bayesian_execution_for
executions = []
def tracked(pipeline):
execution = factory(pipeline)
execution.restore = Mock(wraps=execution.restore)
executions.append(execution)
return execution
monkeypatch.setattr(bo, "bayesian_execution_for", tracked)
monkeypatch.setattr(bo, "_measure_kl_divergence", lambda *args: .1)
separate = _pipeline(tmp_path / "separate")
combined = _pipeline(tmp_path / "combined")
bo.run_bayesian_optimization(separate, n_trials=2, n_refusal_prompts=1, n_kl_prompts=1)
first = bo.replay_bayesian_plan(separate)
second = bo.optimize_and_replay_bayesian_plan(combined, n_trials=2, n_refusal_prompts=1, n_kl_prompts=1)
assert first == second
assert executions[0].restore.call_count == executions[1].restore.call_count + 1
assert combined._bayesian_execution is None
_assert_state(combined, _state(separate))
def test_given_replay_failure_when_combined_then_restore_pristine_and_release(monkeypatch, tmp_path):
pipeline = _pipeline(tmp_path)
original = _state(pipeline)
factory = bo.bayesian_execution_for
def failing(pipeline):
execution = factory(pipeline)
apply = execution.apply
calls = [0]
def apply_once(plan):
calls[0] += 1
result = apply(plan)
if calls[0] == 2:
raise RuntimeError("final replay failed")
return result
execution.apply = apply_once
return execution
monkeypatch.setattr(bo, "bayesian_execution_for", failing)
monkeypatch.setattr(bo, "_measure_kl_divergence", lambda *args: .1)
with pytest.raises(RuntimeError, match="final replay failed"):
bo.optimize_and_replay_bayesian_plan(pipeline, n_trials=1, n_refusal_prompts=1, n_kl_prompts=1)
assert pipeline._bayesian_execution is None
_assert_state(pipeline, original)
def test_given_interrupted_handoff_when_combined_then_restore_pristine(monkeypatch, tmp_path):
pipeline = _pipeline(tmp_path)
original = _state(pipeline)
monkeypatch.setattr(bo, "_measure_kl_divergence", lambda *args: .1)
monkeypatch.setattr(bo, "replay_bayesian_plan", Mock(side_effect=KeyboardInterrupt("replay interrupted")))
with pytest.raises(KeyboardInterrupt, match="replay interrupted"):
bo.optimize_and_replay_bayesian_plan(pipeline, n_trials=1, n_refusal_prompts=1, n_kl_prompts=1)
assert pipeline._bayesian_execution is None
_assert_state(pipeline, original)
def test_given_missing_selected_plan_when_replay_attempted_then_release_snapshots(monkeypatch, tmp_path):
pipeline = _pipeline(tmp_path)
original = _state(pipeline)
monkeypatch.setattr(bo, "_measure_kl_divergence", lambda *args: .1)
bo.run_bayesian_optimization(pipeline, n_trials=1, n_refusal_prompts=1, n_kl_prompts=1)
pipeline._bayesian_plan = None
with pytest.raises(RuntimeError, match="no selected Bayesian plan"):
bo.replay_bayesian_plan(pipeline)
assert pipeline._bayesian_execution is None
_assert_state(pipeline, original)