mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
316 lines
12 KiB
Python
316 lines
12 KiB
Python
"""Execution, rollback, and replay contracts for Bayesian trials."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import sys
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from obliteratus import bayesian_optimizer as bo
|
|
from obliteratus.bayesian_execution import create_execution
|
|
from tests.test_bayesian_optimizer_contracts import (
|
|
_FakeStudy,
|
|
_FakeTrial,
|
|
_Layer,
|
|
_install_fake_optuna,
|
|
_optimization_pipeline,
|
|
)
|
|
|
|
|
|
def _state(model):
|
|
return {
|
|
name: parameter.detach().clone()
|
|
for name, parameter in model.named_parameters()
|
|
}
|
|
|
|
|
|
def _adapter(**overrides):
|
|
values = dict(
|
|
snapshot_host_bytes=0,
|
|
snapshot_disk_bytes=0,
|
|
identity=lambda: {"kind": "fixture"},
|
|
capture=Mock(), restore=Mock(), apply=Mock(), release=Mock(),
|
|
)
|
|
values.update(overrides)
|
|
execution = SimpleNamespace(**values)
|
|
pipeline = SimpleNamespace(_bayesian_executor_factory=lambda _pipeline: execution)
|
|
return pipeline, execution
|
|
|
|
|
|
@pytest.mark.parametrize('fault', ['shape', 'dtype', 'nonfinite', 'meta'])
|
|
def test_resident_restore_validates_every_snapshot_before_writing(fault):
|
|
from obliteratus.bayesian_execution import GenericBayesianExecution
|
|
|
|
model = torch.nn.Linear(2, 2)
|
|
pristine = _state(model)
|
|
snapshots = [(name, parameter, pristine[name]) for name, parameter in model.named_parameters()]
|
|
name, parameter, value = snapshots[-1]
|
|
if fault == 'shape':
|
|
value = value[:1]
|
|
elif fault == 'dtype':
|
|
value = value.double()
|
|
elif fault == 'nonfinite':
|
|
value.fill_(float('nan'))
|
|
else:
|
|
value = value.to('meta')
|
|
snapshots[-1] = name, parameter, value
|
|
with torch.no_grad():
|
|
for parameter in model.parameters():
|
|
parameter.fill_(7)
|
|
execution = GenericBayesianExecution.__new__(GenericBayesianExecution)
|
|
execution.pipeline = SimpleNamespace(handle=SimpleNamespace(model=model))
|
|
execution._released = False
|
|
execution._snapshots = snapshots
|
|
|
|
with pytest.raises(RuntimeError, match='discard and reload'):
|
|
execution.restore()
|
|
assert model._obliteratus_surgery_invalid
|
|
assert all(torch.equal(parameter, torch.full_like(parameter, 7)) for parameter in model.parameters())
|
|
|
|
|
|
@pytest.mark.parametrize("field", ["snapshot_host_bytes", "snapshot_disk_bytes"])
|
|
@pytest.mark.parametrize("value", [True, False, -1, 1.5, None])
|
|
def test_adapter_rejects_invalid_snapshot_demand_before_capture(field, value):
|
|
pipeline, execution = _adapter(**{field: value})
|
|
|
|
with pytest.raises(TypeError, match="nonnegative integer"):
|
|
create_execution(pipeline, [], None, {})
|
|
|
|
execution.capture.assert_not_called()
|
|
execution.apply.assert_not_called()
|
|
|
|
|
|
@pytest.mark.parametrize("identity", [None, "fixture", [], True, 7])
|
|
def test_adapter_rejects_non_mapping_identity_before_capture(identity):
|
|
pipeline, execution = _adapter(identity=lambda: identity)
|
|
|
|
with pytest.raises(TypeError, match="identity must be a mapping"):
|
|
create_execution(pipeline, [], None, {})
|
|
|
|
execution.capture.assert_not_called()
|
|
execution.apply.assert_not_called()
|
|
|
|
|
|
@pytest.mark.parametrize("host,disk", [(0, 0), (17, 31)])
|
|
def test_adapter_preserves_valid_snapshot_demand(host, disk):
|
|
pipeline, execution = _adapter(snapshot_host_bytes=host, snapshot_disk_bytes=disk)
|
|
|
|
assert create_execution(pipeline, [], None, {}) is execution
|
|
assert (execution.snapshot_host_bytes, execution.snapshot_disk_bytes) == (host, disk)
|
|
execution.capture.assert_not_called()
|
|
|
|
|
|
def test_selected_plan_replays_the_exact_scored_component_plan(monkeypatch):
|
|
study = _FakeStudy(best_trials=[_FakeTrial()])
|
|
_install_fake_optuna(monkeypatch, study)
|
|
monkeypatch.setattr(bo, "_measure_refusal_rate", lambda *_a, **_k: 0.2)
|
|
monkeypatch.setattr(bo, "_measure_kl_divergence", lambda *_a, **_k: 0.1)
|
|
pipeline = _optimization_pipeline([_Layer()])
|
|
pristine = _state(pipeline.handle.model)
|
|
|
|
result = bo.run_bayesian_optimization(
|
|
pipeline, n_trials=1, n_refusal_prompts=1, n_kl_prompts=1
|
|
)
|
|
|
|
assert result == pipeline._bayesian_plan.legacy_regularization()
|
|
assert _state(pipeline.handle.model).keys() == pristine.keys()
|
|
for name, value in _state(pipeline.handle.model).items():
|
|
torch.testing.assert_close(value, pristine[name], rtol=0, atol=0)
|
|
scored_plan = study.trials[0].user_attrs["plan_sha256"]
|
|
assert pipeline._bayesian_plan.sha256 == scored_plan
|
|
assert pipeline._bayesian_attn_scale is None
|
|
assert pipeline._bayesian_mlp_scale is None
|
|
|
|
replay = bo.replay_bayesian_plan(pipeline)
|
|
|
|
assert replay.applied_count == study.trials[0].user_attrs["applied_count"]
|
|
assert replay.target_scope == tuple(study.trials[0].user_attrs["target_scope"])
|
|
assert pipeline._bayesian_replay["plan_sha256"] == scored_plan
|
|
assert pipeline._bayesian_execution is None
|
|
changed = {
|
|
name for name, value in _state(pipeline.handle.model).items()
|
|
if not torch.equal(value, pristine[name])
|
|
}
|
|
assert changed == {
|
|
"model.layers.0.self_attn.o_proj.weight",
|
|
"model.layers.0.mlp.down_proj.weight",
|
|
}
|
|
|
|
|
|
def test_cancelled_trial_restores_every_captured_parameter(monkeypatch):
|
|
study = _FakeStudy(best_trials=[])
|
|
_install_fake_optuna(monkeypatch, study)
|
|
pipeline = _optimization_pipeline([_Layer()])
|
|
pristine = _state(pipeline.handle.model)
|
|
|
|
def cancel(*_args, **_kwargs):
|
|
raise KeyboardInterrupt("cancel trial")
|
|
|
|
monkeypatch.setattr(bo, "_measure_refusal_rate", cancel)
|
|
with pytest.raises(KeyboardInterrupt, match="cancel trial"):
|
|
bo.run_bayesian_optimization(
|
|
pipeline, n_trials=1, n_refusal_prompts=1, n_kl_prompts=1
|
|
)
|
|
|
|
assert pipeline._bayesian_execution is None
|
|
for name, value in _state(pipeline.handle.model).items():
|
|
torch.testing.assert_close(value, pristine[name], rtol=0, atol=0)
|
|
|
|
|
|
def test_study_setup_failure_restores_and_releases_execution(monkeypatch):
|
|
_install_fake_optuna(monkeypatch, _FakeStudy(best_trials=[]))
|
|
monkeypatch.setattr(
|
|
sys.modules["optuna"],
|
|
"create_study",
|
|
lambda **_kwargs: (_ for _ in ()).throw(RuntimeError("study setup failed")),
|
|
)
|
|
pipeline = _optimization_pipeline([_Layer()])
|
|
pristine = _state(pipeline.handle.model)
|
|
|
|
with pytest.raises(RuntimeError, match="study setup failed"):
|
|
bo.run_bayesian_optimization(
|
|
pipeline, n_trials=1, n_refusal_prompts=1, n_kl_prompts=1
|
|
)
|
|
|
|
assert pipeline._bayesian_execution is None
|
|
assert pipeline._bayesian_execution_captured is False
|
|
for name, value in _state(pipeline.handle.model).items():
|
|
torch.testing.assert_close(value, pristine[name], rtol=0, atol=0)
|
|
|
|
|
|
def test_replay_rejects_a_mutated_selected_plan(monkeypatch):
|
|
study = _FakeStudy(best_trials=[_FakeTrial()])
|
|
_install_fake_optuna(monkeypatch, study)
|
|
monkeypatch.setattr(bo, "_measure_refusal_rate", lambda *_a, **_k: 0.2)
|
|
monkeypatch.setattr(bo, "_measure_kl_divergence", lambda *_a, **_k: 0.1)
|
|
pipeline = _optimization_pipeline([_Layer()])
|
|
pristine = _state(pipeline.handle.model)
|
|
bo.run_bayesian_optimization(
|
|
pipeline, n_trials=1, n_refusal_prompts=1, n_kl_prompts=1
|
|
)
|
|
|
|
pipeline._bayesian_plan.directions[0].add_(1.0)
|
|
with pytest.raises(RuntimeError, match="changed after scoring"):
|
|
bo.replay_bayesian_plan(pipeline)
|
|
|
|
assert pipeline._bayesian_execution is None
|
|
for name, value in _state(pipeline.handle.model).items():
|
|
torch.testing.assert_close(value, pristine[name], rtol=0, atol=0)
|
|
|
|
|
|
def test_optimizer_rejects_trials_outside_the_kl_budget(monkeypatch):
|
|
study = _FakeStudy(best_trials=[_FakeTrial(values=(0.1, 0.8))])
|
|
_install_fake_optuna(monkeypatch, study)
|
|
monkeypatch.setattr(bo, "_measure_refusal_rate", lambda *_a, **_k: 0.1)
|
|
monkeypatch.setattr(bo, "_measure_kl_divergence", lambda *_a, **_k: 0.8)
|
|
pipeline = _optimization_pipeline([_Layer()])
|
|
pipeline.kl_budget = 0.5
|
|
|
|
with pytest.raises(RuntimeError, match="KL budget 0.5"):
|
|
bo.run_bayesian_optimization(
|
|
pipeline, n_trials=1, n_refusal_prompts=1, n_kl_prompts=1
|
|
)
|
|
|
|
assert pipeline._bayesian_skipped == "no-feasible-trials"
|
|
assert pipeline._bayesian_execution is None
|
|
|
|
|
|
def test_admission_precedes_snapshot_capture_and_receives_disk_demand(monkeypatch):
|
|
study = _FakeStudy(best_trials=[_FakeTrial()])
|
|
_install_fake_optuna(monkeypatch, study)
|
|
monkeypatch.setattr(bo, "_measure_refusal_rate", lambda *_a, **_k: 0.2)
|
|
monkeypatch.setattr(bo, "_measure_kl_divergence", lambda *_a, **_k: 0.1)
|
|
pipeline = _optimization_pipeline([_Layer()])
|
|
events = []
|
|
|
|
class Execution:
|
|
snapshot_host_bytes = 17
|
|
snapshot_disk_bytes = 31
|
|
|
|
def __init__(self):
|
|
self.captured = False
|
|
|
|
def identity(self):
|
|
return {"kind": "test", "captured": self.captured}
|
|
|
|
def capture(self):
|
|
events.append("capture")
|
|
self.captured = True
|
|
|
|
def restore(self):
|
|
events.append("restore")
|
|
|
|
def apply(self, _plan):
|
|
return bo.BayesianApplyResult(1, ("test.target",))
|
|
|
|
def release(self):
|
|
events.append("release")
|
|
|
|
execution = Execution()
|
|
pipeline._bayesian_executor_factory = lambda _pipeline: execution
|
|
|
|
def admit(**demand):
|
|
events.append("admit")
|
|
assert execution.captured is False
|
|
assert demand["host_bytes"] == 17
|
|
assert demand["disk_bytes"] == 31
|
|
assert demand["executor_identity"] == {"kind": "test", "captured": False}
|
|
|
|
pipeline._admit_bayesian_optimization = admit
|
|
bo.run_bayesian_optimization(
|
|
pipeline, n_trials=1, n_refusal_prompts=1, n_kl_prompts=1
|
|
)
|
|
|
|
assert events.index("admit") < events.index("capture")
|
|
assert pipeline._bayesian_plan is not None
|
|
|
|
|
|
def test_atomic_evidence_is_bound_to_study_identity(monkeypatch, tmp_path):
|
|
study = _FakeStudy(best_trials=[_FakeTrial()])
|
|
_install_fake_optuna(monkeypatch, study)
|
|
monkeypatch.setattr(bo, "_measure_refusal_rate", lambda *_a, **_k: 0.2)
|
|
monkeypatch.setattr(bo, "_measure_kl_divergence", lambda *_a, **_k: 0.1)
|
|
pipeline = _optimization_pipeline([_Layer()])
|
|
pipeline.output_dir = str(tmp_path)
|
|
|
|
bo.run_bayesian_optimization(
|
|
pipeline, n_trials=1, n_refusal_prompts=1, n_kl_prompts=1
|
|
)
|
|
|
|
# The fake Optuna surface deliberately has no durable storage API.
|
|
assert not (tmp_path / ".obliteratus-bayesian").exists()
|
|
assert json.loads(json.dumps(pipeline._bayesian_plan.evidence()))["schema"] == 1
|
|
|
|
|
|
def test_admission_options_include_snapshot_disk_and_trial_forwards():
|
|
pipeline = _optimization_pipeline([_Layer()])
|
|
pipeline.handle.model.config = SimpleNamespace(vocab_size=2, use_cache=True)
|
|
pipeline.handle.model.generation_config = SimpleNamespace(use_cache=True)
|
|
pipeline.max_seq_length = 64
|
|
|
|
options = bo.bayesian_admission_options(
|
|
pipeline, n_refusal_prompts=2, n_kl_prompts=1
|
|
)
|
|
|
|
assert options["snapshot_host_bytes"] > 0
|
|
assert options["snapshot_disk_bytes"] == 0
|
|
assert options["forward_host_bytes"] > 0
|
|
assert len(options["forward_workloads"]) == 5
|
|
assert options["executor_identity"]["mutation_scope_sha256"] is None
|
|
bo._reset_bayesian_state(pipeline)
|
|
|
|
|
|
def test_generic_execution_rejects_unscored_extra_mutations_before_capture():
|
|
pipeline = _optimization_pipeline([_Layer()])
|
|
pipeline.project_embeddings = True
|
|
|
|
with pytest.raises(ValueError, match="embedding projection"):
|
|
bo.bayesian_execution_for(pipeline)
|
|
|
|
execution = getattr(pipeline, "_bayesian_execution", None)
|
|
assert execution is None
|