mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
89 lines
3.5 KiB
Python
89 lines
3.5 KiB
Python
"""Focused orchestration contracts for study execution."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
|
|
from obliteratus import runner
|
|
from obliteratus.config import DatasetConfig, ModelConfig, StrategyConfig, StudyConfig
|
|
|
|
|
|
def test_run_study_passes_configured_quantization_to_loader(tmp_path, monkeypatch):
|
|
config = StudyConfig(
|
|
model=ModelConfig(name="fixture", quantization="4bit"),
|
|
dataset=DatasetConfig(name="fixture"),
|
|
strategies=[StrategyConfig(name="layer_removal")],
|
|
output_dir=str(tmp_path),
|
|
)
|
|
load_model = Mock(side_effect=RuntimeError("stop after loader boundary"))
|
|
monkeypatch.setattr(runner, "load_model", load_model)
|
|
|
|
with pytest.raises(RuntimeError, match="stop after loader boundary"):
|
|
runner.run_study(config)
|
|
|
|
assert load_model.call_args.kwargs["quantization"] == "4bit"
|
|
|
|
|
|
@pytest.mark.parametrize("failure", [None, "apply", "evaluate", "restore", "admission"])
|
|
def test_study_restore_lifecycle_with_skipped_initial_snapshot(tmp_path, monkeypatch, failure):
|
|
from types import SimpleNamespace
|
|
import torch
|
|
from obliteratus.models.loader import ModelHandle
|
|
from obliteratus.models.snapshot import SnapshotError
|
|
|
|
model = torch.nn.Linear(3, 2)
|
|
pristine = model.weight.detach().clone()
|
|
handle = ModelHandle(model, None, SimpleNamespace(model_type="fixture"), "fixture", "causal_lm")
|
|
config = StudyConfig(
|
|
model=ModelConfig(name="fixture"), dataset=DatasetConfig(name="fixture"),
|
|
strategies=[StrategyConfig(name="layer_removal")], output_dir=str(tmp_path),
|
|
)
|
|
evaluations = []
|
|
|
|
class Evaluation:
|
|
def __init__(self, **kwargs):
|
|
pass
|
|
|
|
def evaluate(self):
|
|
assert handle._restore_point.state == "ready"
|
|
evaluations.append(model.weight.detach().clone())
|
|
if len(evaluations) == 2 and failure == "evaluate":
|
|
raise RuntimeError("evaluate failure")
|
|
return {"perplexity": 1.0}
|
|
|
|
spec = SimpleNamespace(component="fixture", strategy_name="layer_removal", description="fixture", metadata={})
|
|
|
|
def apply(*_):
|
|
assert handle._restore_point.state == "ready"
|
|
with torch.no_grad():
|
|
model.weight.zero_()
|
|
if failure == "apply":
|
|
raise RuntimeError("apply failure")
|
|
|
|
monkeypatch.setattr(runner, "load_model", lambda **_: handle)
|
|
monkeypatch.setattr(runner, "load_dataset", lambda **_: ["fixture"])
|
|
monkeypatch.setattr(runner, "Evaluator", Evaluation)
|
|
monkeypatch.setattr(runner, "get_strategy", lambda _: SimpleNamespace(enumerate=lambda *_: [spec], apply=apply))
|
|
report = Mock()
|
|
monkeypatch.setattr(runner, "AblationReport", lambda **_: report)
|
|
if failure == "admission":
|
|
monkeypatch.setattr(handle, "ensure_snapshot", Mock(side_effect=SnapshotError("admission failure")))
|
|
if failure == "restore":
|
|
monkeypatch.setattr(handle, "restore", Mock(side_effect=SnapshotError("restore failure")))
|
|
|
|
if failure:
|
|
with pytest.raises(RuntimeError, match=failure):
|
|
runner.run_study(config)
|
|
report.save_json.assert_not_called()
|
|
report.add_result.assert_not_called()
|
|
else:
|
|
assert runner.run_study(config) is report
|
|
report.add_result.assert_called_once()
|
|
report.save_json.assert_called_once()
|
|
if failure != "restore":
|
|
torch.testing.assert_close(model.weight, pristine)
|
|
assert handle._restore_point is None
|
|
assert len(evaluations) == (0 if failure == "admission" else 1 if failure == "apply" else 2)
|