Files
OBLITERATUS/tests/test_bayesian_optimizer_optuna.py
Joseph MaglyandClaude Opus 5.5 08a04e3bd4 fix(bayesian): close Optuna SQLite storage and backup connections
On Python 3.13+, an sqlite3 connection that is garbage-collected
without being closed raises ResourceWarning, which the project's
filterwarnings=error turns into test failures. pyproject declares
requires-python >=3.11 with no upper bound, so these versions must work.

- The durable study's RDBStorage is now owned by an ExitStack in both
  public entry points and closed (remove_session, then engine.dispose)
  on success, failure and cancellation.
- _atomic_sqlite_backup closes both handles with contextlib.closing;
  sqlite3's own context manager only ends the transaction.
- Two tests that reopened or connected to study databases now close
  their handles.
- New regression test tracks every sqlite3 handle opened during a real
  durable study (success, failure, KeyboardInterrupt) and the backup,
  asserting all are closed independent of the 3.13+ warning.

Storage URLs, file names, snapshots and resume behaviour are unchanged;
as with any edit to this module, the study identity (which hashes the
module source) changes, so pre-existing durable studies start fresh.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 17:43:05 -05:00

286 lines
11 KiB
Python

"""Real, in-memory Optuna contracts with a tiny deterministic CPU model."""
import json
from types import SimpleNamespace
import optuna
import pytest
import torch
from torch import nn
from obliteratus import bayesian_optimizer as bo
from obliteratus.abliterate import AbliterationPipeline
class _TinyModel(nn.Module):
def __init__(self):
super().__init__()
layer = nn.Module()
layer.self_attn = nn.Module()
layer.self_attn.o_proj = nn.Linear(2, 2, bias=False)
layer.mlp = nn.Module()
layer.mlp.down_proj = nn.Linear(2, 2, bias=False)
with torch.no_grad():
layer.self_attn.o_proj.weight.copy_(torch.tensor([[1., .2], [.3, 1.]]))
layer.mlp.down_proj.weight.copy_(torch.tensor([[.7, .1], [.4, .8]]))
self.model = nn.Module()
self.model.layers = nn.ModuleList([layer])
self.forward_calls = 0
self.generation_calls = 0
def forward(self, input_ids, **_kwargs):
self.forward_calls += 1
hidden = torch.nn.functional.one_hot(input_ids, num_classes=2).float()
for layer in self.model.layers:
hidden = layer.mlp.down_proj(layer.self_attn.o_proj(hidden))
return SimpleNamespace(logits=hidden)
def generate(self, input_ids, **_kwargs):
self.generation_calls += 1
token = self(input_ids).logits[:, -1, :].argmax(dim=-1, keepdim=True)
return torch.cat([input_ids, token], dim=1)
class _TinyTokenizer:
chat_template = None
def __call__(self, *_args, **_kwargs):
return {"input_ids": torch.tensor([[0, 1]])}
def decode(self, tokens, **_kwargs):
return "I cannot assist with that request." if tokens[-1] == 0 else "Here is an explanation."
@pytest.mark.parametrize("peak", [2 / 35, 34 / 35])
def test_real_optuna_runs_bounded_warm_start_and_restores_model(
monkeypatch, tmp_path, peak, request,
):
studies = []
create_study = optuna.create_study
def capture_study(**kwargs):
study = create_study(**kwargs)
studies.append(study)
# Reading ``study.trials`` after the run reopens the durable storage
# the optimizer already closed; the test owns closing that reopening.
request.addfinalizer(lambda: bo._close_rdb_storage(kwargs["storage"]))
return study
monkeypatch.setattr(optuna, "create_study", capture_study)
pipeline = AbliterationPipeline(
model_name="tiny-local", method="optimized", output_dir=str(tmp_path),
use_sae_features=False, attention_head_surgery=False,
per_expert_directions=False,
)
model = _TinyModel()
pipeline.handle = SimpleNamespace(model=model, tokenizer=_TinyTokenizer(), architecture="llama")
pipeline._strong_layers = [0]
pipeline.refusal_directions = {0: torch.tensor([1., 0.])}
pipeline.harmful_prompts = ["A local test prompt"]
pipeline._informed_warm_start = {"peak_position": peak, "max_weight": .5, "mlp_scale": .5}
# The fixture owns no accelerators or large allocations.
monkeypatch.setattr(pipeline, "_free_gpu_memory", lambda: None)
original = {name: tensor.clone() for name, tensor in model.state_dict().items()}
result = bo.run_bayesian_optimization(pipeline, n_trials=2, n_refusal_prompts=1, n_kl_prompts=1)
assert set(result) == {0}
assert 0 <= result[0] <= 1
assert len(studies) == 1
trials = studies[0].trials
assert len(trials) == 2
assert all(trial.state == optuna.trial.TrialState.COMPLETE for trial in trials)
expected_peak = .1 if peak < .1 else .9
assert trials[0].params["attn_peak_position"] == pytest.approx(expected_peak)
assert trials[0].params["mlp_peak_position"] == pytest.approx(expected_peak)
assert trials[0].params["mlp_max_weight"] == pytest.approx(.3)
for trial in trials:
assert all(bo.KERNEL_SPACE[name][0] <= trial.params[name] <= bo.KERNEL_SPACE[name][1]
for name in bo.KERNEL_SPACE)
assert trial.values is not None and len(trial.values) == 2
assert trial.user_attrs["invalid_rate"] == 0.0
assert trial.user_attrs["invalid_count"] == 0
assert trial.user_attrs["exclude_invalid"] is True
assert trial.user_attrs["valid_count"] == 1
assert trial.user_attrs["valid_refusal_rate"] == trial.values[0]
assert trial.user_attrs["total_count"] == 1
assert all(torch.isfinite(torch.tensor(value)) for value in trial.values)
assert model.generation_calls == 2
assert model.forward_calls >= 5 # reference, generation and KL for each trial
for name, tensor in model.state_dict().items():
torch.testing.assert_close(tensor, original[name], rtol=0, atol=0)
def test_durable_study_resumes_without_replaying_completed_trials(tmp_path):
pipeline = AbliterationPipeline(
model_name="tiny-local", method="optimized", output_dir=str(tmp_path),
use_sae_features=False, attention_head_surgery=False,
per_expert_directions=False,
)
model = _TinyModel()
pipeline.handle = SimpleNamespace(
model=model, tokenizer=_TinyTokenizer(), architecture="llama"
)
pipeline._strong_layers = [0]
pipeline.refusal_directions = {0: torch.tensor([1.0, 0.0])}
pipeline.harmful_prompts = ["A local test prompt"]
pipeline._free_gpu_memory = lambda: None
first = bo.run_bayesian_optimization(
pipeline, n_trials=1, n_refusal_prompts=1, n_kl_prompts=1
)
generation_calls = model.generation_calls
artifacts = list((tmp_path / ".obliteratus-bayesian").iterdir())
assert len([path for path in artifacts if path.suffix == ".sqlite3"]) == 1
assert len([path for path in artifacts if path.suffix == ".json"]) == 2
assert len([path for path in artifacts if path.suffix == ".snapshot"]) == 1
second = bo.run_bayesian_optimization(
pipeline, n_trials=1, n_refusal_prompts=1, n_kl_prompts=1
)
assert second == first
assert model.generation_calls == generation_calls
assert len(list((tmp_path / ".obliteratus-bayesian").glob("*.sqlite3"))) == 1
bo._reset_bayesian_state(pipeline)
def test_atomic_trial_receipt_recovers_a_missing_study_database(tmp_path):
published = []
def pipeline_for(model):
pipeline = AbliterationPipeline(
model_name="tiny-local", method="optimized", output_dir=str(tmp_path),
use_sae_features=False, attention_head_surgery=False,
per_expert_directions=False,
)
pipeline.handle = SimpleNamespace(
model=model, tokenizer=_TinyTokenizer(), architecture="llama"
)
pipeline._strong_layers = [0]
pipeline.refusal_directions = {0: torch.tensor([1.0, 0.0])}
pipeline.harmful_prompts = ["A local test prompt"]
pipeline._free_gpu_memory = lambda: None
pipeline._on_bayesian_trial = published.append
return pipeline
original = pipeline_for(_TinyModel())
expected = bo.run_bayesian_optimization(
original, n_trials=1, n_refusal_prompts=1, n_kl_prompts=1
)
bo._reset_bayesian_state(original)
root = tmp_path / ".obliteratus-bayesian"
database = next(root.glob("*.sqlite3"))
receipt = next(root.glob("*.trial-0.json"))
document = __import__("json").loads(receipt.read_text())
assert document["state"] == "COMPLETE"
assert document["sampler"] == {"name": "TPESampler", "seed": 42}
assert (root / document["study_backup"]).exists()
assert published == [receipt]
database.rename(root / "retained-old-study.sqlite3")
recovered_model = _TinyModel()
recovered = pipeline_for(recovered_model)
actual = bo.run_bayesian_optimization(
recovered, n_trials=1, n_refusal_prompts=1, n_kl_prompts=1
)
assert actual == expected
assert recovered_model.generation_calls == 0
bo._reset_bayesian_state(recovered)
def test_receipt_recovery_uses_numeric_trial_order(tmp_path):
identity = "numeric-order"
evidence_path = tmp_path / f"{identity}.json"
distribution = optuna.distributions.distribution_to_json(
optuna.distributions.FloatDistribution(0.0, 20.0)
)
for trial_number in (12, 2, 10):
(tmp_path / f"{identity}.trial-{trial_number}.json").write_text(json.dumps({
"schema": bo._STUDY_SCHEMA,
"study_identity": identity,
"trial_number": trial_number,
"state": "COMPLETE",
"values": [float(trial_number), 0.1],
"parameters": {"x": float(trial_number)},
"distributions": {"x": distribution},
"user_attrs": {"plan_sha256": str(trial_number)},
}))
study = optuna.create_study(directions=["minimize", "minimize"])
bo._restore_completed_trials(optuna, study, evidence_path, identity)
assert len(study.trials) == 13
assert [
trial.number for trial in study.trials
if trial.state == optuna.trial.TrialState.COMPLETE
] == [2, 10, 12]
assert [study.trials[number].params["x"] for number in (2, 10, 12)] == [2., 10., 12.]
assert all(
trial.state == optuna.trial.TrialState.FAIL
for trial in study.trials
if trial.number not in {2, 10, 12}
)
selected = min(study.best_trials, key=lambda trial: tuple(trial.values))
selected_receipt = json.loads(
(tmp_path / f"{identity}.trial-{selected.number}.json").read_text()
)
assert selected.number == selected_receipt["trial_number"] == 2
assert selected.user_attrs == selected_receipt["user_attrs"]
before = sorted(tmp_path.glob(f"{identity}.trial-*.json"))
for trial in study.trials:
path = evidence_path.with_name(f"{identity}.trial-{trial.number}.json")
if not path.exists():
bo._persist_completed_trial(
SimpleNamespace(), trial,
study_identity=identity, evidence_path=evidence_path,
)
assert sorted(tmp_path.glob(f"{identity}.trial-*.json")) == before
def test_receipt_recovery_rejects_duplicate_trial_numbers(tmp_path):
identity = "duplicate"
evidence_path = tmp_path / f"{identity}.json"
document = {
"schema": bo._STUDY_SCHEMA,
"study_identity": identity,
"trial_number": 2,
"state": "COMPLETE",
"values": [0.2, 0.1],
"parameters": {},
"distributions": {},
"user_attrs": {"plan_sha256": "plan"},
}
for suffix in ("2", "02"):
(tmp_path / f"{identity}.trial-{suffix}.json").write_text(json.dumps(document))
with pytest.raises(RuntimeError, match="invalid Bayesian recovery receipt"):
bo._restore_completed_trials(
optuna, optuna.create_study(directions=["minimize", "minimize"]),
evidence_path, identity,
)
def test_receipt_recovery_rejects_conflicting_existing_trial(tmp_path):
identity = "conflict"
evidence_path = tmp_path / f"{identity}.json"
(tmp_path / f"{identity}.trial-0.json").write_text(json.dumps({
"schema": bo._STUDY_SCHEMA,
"study_identity": identity,
"trial_number": 0,
"state": "COMPLETE",
"values": [0.2, 0.1],
"parameters": {},
"distributions": {},
"user_attrs": {"plan_sha256": "receipt"},
}))
study = optuna.create_study(directions=["minimize", "minimize"])
study.add_trial(optuna.trial.create_trial(
values=[0.2, 0.1], user_attrs={"plan_sha256": "database"},
))
with pytest.raises(RuntimeError, match="conflicts with its recovery receipt"):
bo._restore_completed_trials(optuna, study, evidence_path, identity)