mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
Integrate bounded admission, UI ownership and controls, opt-in durable MCP execution, rank-local TP2 groundwork, regression hardening and operator guides. Physical larger-memory and two-host qualification remain separate.
930 lines
38 KiB
Python
930 lines
38 KiB
Python
"""GPU admission and cleanup contracts for application benchmark paths."""
|
|
|
|
import ast
|
|
import csv
|
|
import io
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
|
|
from obliteratus.benchmark_lifecycle import (
|
|
MAX_RELEASABLE_ALLOCATOR_RESIDUE_BYTES,
|
|
admit_benchmark,
|
|
mark_benchmark_ready,
|
|
release_benchmark_pipeline,
|
|
)
|
|
from obliteratus.gpu_lifecycle import MemoryUsage
|
|
from obliteratus.ui_operations import with_admission_warnings
|
|
from obliteratus.ui_model_load import clear_exception_frames
|
|
|
|
|
|
@pytest.mark.operator_ui
|
|
def test_all_evaluation_callbacks_refuse_an_occupied_model_slot():
|
|
script = r'''
|
|
import app
|
|
from unittest.mock import Mock
|
|
|
|
app._state.update(status="obliterating", load_token=object())
|
|
app._clear_gpu = Mock(side_effect=AssertionError("busy callback unloaded the active model"))
|
|
app._gpu_lifecycle = Mock()
|
|
for callback, args, outputs in (
|
|
(app.benchmark, ("fixture/model", ["basic"], "33 (fast)"), 4),
|
|
(app.benchmark_multi_model, (["fixture/model"], "basic", "33 (fast)"), 4),
|
|
(app.strength_sweep, ("fixture/model", "basic", "33 (fast)", "", 3), 5),
|
|
(app.run_tourney, ("fixture/model", ["basic", "advanced", "surgical"], "", "None"), 3),
|
|
):
|
|
updates = list(callback(*args))
|
|
assert len(updates) == 1
|
|
assert len(updates[0]) == outputs
|
|
assert "Busy" in updates[0][0]
|
|
app._clear_gpu.assert_not_called()
|
|
app._gpu_lifecycle.loading.assert_not_called()
|
|
'''
|
|
result = subprocess.run([sys.executable, "-c", script], capture_output=True,
|
|
text=True, timeout=120, check=False)
|
|
assert result.returncode == 0, result.stdout + result.stderr
|
|
|
|
|
|
@pytest.mark.operator_ui
|
|
@pytest.mark.parametrize("slow_worker", [False, True], ids=["cooperative-join", "deferred-join"])
|
|
def test_resident_chat_owns_slot_through_cancellation_and_memory_publication(tmp_path, slow_worker):
|
|
script = r'''
|
|
import pathlib
|
|
import sys
|
|
import threading
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock
|
|
|
|
import app
|
|
from obliteratus.ui_model_load import ModelSlotClaim
|
|
from obliteratus.ui_operations import operation_cancellation, operation_thread
|
|
|
|
slow = sys.argv[2] == "True"
|
|
root = pathlib.Path(sys.argv[1])
|
|
cached = root / "model-cache"
|
|
cached.mkdir()
|
|
weights = cached / "model.safetensors"
|
|
weights.write_bytes(b"still-in-use")
|
|
model = SimpleNamespace(name="edited resident")
|
|
tokenizer = SimpleNamespace(name="resident tokenizer")
|
|
app._state.clear()
|
|
app._state.update(model=model, tokenizer=tokenizer, status="ready", output_dir=None,
|
|
model_name="fixture/model", method="advanced", load_settings={})
|
|
app._session_models.clear()
|
|
app._session_models["fixture"] = {"output_dir": str(cached)}
|
|
app._clear_gpu = Mock(side_effect=AssertionError("busy operation unloaded resident tensors"))
|
|
app._cleanup_disk_owned = Mock(side_effect=AssertionError("busy cleanup deleted resources"))
|
|
memory = object()
|
|
app.measure_torch_memory = Mock(return_value=memory)
|
|
events = []
|
|
started, proceed, stopped, restored = (threading.Event() for _ in range(4))
|
|
workers, cancellation_events = [], []
|
|
|
|
def publish(name, observed):
|
|
assert stopped.is_set(), "published residency before generation stopped"
|
|
assert observed is memory
|
|
events.append(name)
|
|
|
|
app._gpu_lifecycle = Mock()
|
|
app._gpu_lifecycle.resize.side_effect = lambda value: publish("resize", value)
|
|
app._gpu_lifecycle.ready.side_effect = lambda value: publish("ready", value)
|
|
original_restore = ModelSlotClaim.restore_loaded
|
|
|
|
def observe_restore(self, loaded, tokens):
|
|
assert stopped.is_set()
|
|
assert (loaded, tokens) == (model, tokenizer)
|
|
result = original_restore(self, loaded, tokens)
|
|
restored.set()
|
|
return result
|
|
|
|
ModelSlotClaim.restore_loaded = observe_restore
|
|
|
|
def generation():
|
|
cancel = operation_cancellation()
|
|
cancellation_events.append(cancel)
|
|
|
|
def generate():
|
|
assert operation_cancellation() is cancel
|
|
started.set()
|
|
assert cancel.wait(timeout=10), "stream close did not request cancellation"
|
|
if slow:
|
|
assert proceed.wait(timeout=10), "deferred worker was never released"
|
|
stopped.set()
|
|
|
|
worker = operation_thread(generate)
|
|
workers.append(worker)
|
|
worker.start()
|
|
assert started.wait(timeout=5)
|
|
if slow:
|
|
real_join = worker.join
|
|
worker.join = lambda timeout=None: real_join() if timeout is None else None
|
|
yield "first token"
|
|
|
|
stream = app._resident_generation(model, tokenizer, generation())
|
|
assert next(stream) == "first token"
|
|
assert app._state["status"] == "obliterating"
|
|
claim_token = app._state["load_token"]
|
|
busy = list(app.benchmark("fixture/model", ["basic"], "33 (fast)"))
|
|
assert len(busy) == 1 and "Busy" in busy[0][0]
|
|
cleanup = app._cleanup_disk(cache_roots=[cached], temp_root=root)
|
|
assert "operation is active" in cleanup
|
|
assert weights.read_bytes() == b"still-in-use"
|
|
assert app._session_models == {"fixture": {"output_dir": str(cached)}}
|
|
assert app._state["model"] is model and app._state["tokenizer"] is tokenizer
|
|
assert app._state["load_token"] is claim_token
|
|
app._clear_gpu.assert_not_called()
|
|
app._cleanup_disk_owned.assert_not_called()
|
|
app._gpu_lifecycle.loading.assert_not_called()
|
|
app._gpu_lifecycle.release.assert_not_called()
|
|
try:
|
|
stream.close()
|
|
assert cancellation_events[0].is_set()
|
|
if slow:
|
|
assert not stopped.is_set() and not restored.is_set()
|
|
assert app._state["status"] == "obliterating"
|
|
assert app._state["load_token"] is claim_token
|
|
assert events == []
|
|
assert "Busy" in list(app.benchmark("fixture/model", ["basic"], "33 (fast)"))[0][0]
|
|
finally:
|
|
proceed.set()
|
|
workers[0].join()
|
|
assert restored.wait(timeout=5)
|
|
assert events == ["resize", "ready"]
|
|
assert app._state["status"] == "ready" and "load_token" not in app._state
|
|
assert app._state["model"] is model and app._state["tokenizer"] is tokenizer
|
|
app._gpu_lifecycle.release.assert_not_called()
|
|
'''
|
|
result = subprocess.run(
|
|
[sys.executable, "-c", script, str(tmp_path), str(slow_worker)],
|
|
capture_output=True, text=True, timeout=120, check=False,
|
|
)
|
|
assert result.returncode == 0, result.stdout + result.stderr
|
|
|
|
|
|
@pytest.mark.operator_ui
|
|
def test_tournament_worker_cancellation_joins_before_slot_release_without_publishing_winner():
|
|
script = r'''
|
|
import logging
|
|
import pickle
|
|
import tempfile
|
|
import threading
|
|
import time
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock
|
|
|
|
import app
|
|
import obliteratus.telemetry as telemetry
|
|
import obliteratus.tourney as tourney
|
|
from obliteratus.ui_operations import operation_cancellation
|
|
from obliteratus.ui_operations import admission_warnings
|
|
|
|
app._state.clear()
|
|
app._state.update(status="idle", model=None, tokenizer=None, output_dir=None,
|
|
model_name=None, method=None, load_settings={})
|
|
app._session_models.clear()
|
|
app._clear_gpu = Mock()
|
|
app._gpu_lifecycle = Mock()
|
|
app.mark_benchmark_ready = Mock()
|
|
app._persist_session_meta = Mock(side_effect=AssertionError("cancelled winner was persisted"))
|
|
telemetry.log_benchmark_from_dict = Mock()
|
|
tourney._load_checkpoint = Mock(return_value=None)
|
|
started, stopped = threading.Event(), threading.Event()
|
|
options = []
|
|
real_runner = tourney.TourneyRunner
|
|
serialization_directory = tempfile.TemporaryDirectory()
|
|
|
|
class FakeRunner:
|
|
def __init__(self, **kwargs):
|
|
self.methods = kwargs["methods"]
|
|
self.kwargs = kwargs
|
|
options.append(kwargs)
|
|
patched_runner = tourney.TourneyRunner
|
|
tourney.TourneyRunner = real_runner
|
|
try:
|
|
probe = real_runner(**kwargs, output_dir=serialization_directory.name)
|
|
bound = pickle.loads(pickle.dumps(probe._run_method))
|
|
finally:
|
|
tourney.TourneyRunner = patched_runner
|
|
assert bound.__self__._on_stage is app._tourney_stage_ready
|
|
assert bound.__self__._cancellation_event.path == kwargs["cancellation_event"].path
|
|
bound.__self__._check_cancelled()
|
|
with admission_warnings(bound.__self__.log):
|
|
logging.getLogger("obliteratus.phase_admission").warning("serialized contender warning")
|
|
assert "serialized contender warning" in kwargs["on_log"].tail()
|
|
|
|
def run_iter(self, gpu_wrapper):
|
|
assert gpu_wrapper is app._tourney_gpu_wrapper
|
|
cancellation = self.kwargs["cancellation_event"]
|
|
assert operation_cancellation() is not cancellation
|
|
on_stage = self.kwargs["on_stage"]
|
|
on_stage(SimpleNamespace(stage="summon", status="running"))
|
|
on_stage(SimpleNamespace(stage="probe", status="done"))
|
|
app.mark_benchmark_ready.assert_not_called()
|
|
on_stage(SimpleNamespace(stage="summon", status="done"))
|
|
app.mark_benchmark_ready.assert_called_once_with(app._gpu_lifecycle, app.torch)
|
|
started.set()
|
|
partial = SimpleNamespace(winner=SimpleNamespace(
|
|
method="basic", error=None, output_dir="/must-not-publish", metrics={},
|
|
time_s=0.1, score=1.0,
|
|
), rounds=[])
|
|
yield "Fixture round active", partial
|
|
deadline = time.monotonic() + 10
|
|
while not cancellation.is_set() and time.monotonic() < deadline:
|
|
time.sleep(0.01)
|
|
assert cancellation.is_set(), "UI close did not cancel tournament worker"
|
|
stopped.set()
|
|
|
|
tourney.TourneyRunner = FakeRunner
|
|
|
|
def release_pipeline(pipeline_ref, **kwargs):
|
|
assert stopped.is_set(), "released lifecycle before tournament worker stopped"
|
|
assert pipeline_ref == [None]
|
|
kwargs["lifecycle"].release(reason=kwargs["reason"])
|
|
|
|
app.release_benchmark_pipeline = Mock(side_effect=release_pipeline)
|
|
stream = app.run_tourney("fixture/model", ["basic", "advanced", "surgical"], "", "none")
|
|
first = next(stream)
|
|
assert "Tournament starting" in first[0]
|
|
for _ in range(10):
|
|
second = next(stream)
|
|
if second[0] == "Fixture round active":
|
|
break
|
|
else:
|
|
raise AssertionError("tournament worker never emitted its first status")
|
|
assert started.is_set() and not stopped.is_set()
|
|
assert "serialized contender warning" in second[2]
|
|
assert len(options) == 1 and not options[0]["cancellation_event"].is_set()
|
|
assert app._state["status"] == "obliterating"
|
|
token = app._state["load_token"]
|
|
busy = list(app.benchmark("fixture/model", ["basic"], "33 (fast)"))
|
|
assert len(busy) == 1 and "Busy" in busy[0][0]
|
|
assert app._state["load_token"] is token
|
|
app._clear_gpu.assert_called_once_with()
|
|
app.release_benchmark_pipeline.assert_not_called()
|
|
stream.close()
|
|
assert stopped.is_set()
|
|
assert not Path(options[0]["cancellation_event"].path).parent.exists()
|
|
assert "serialized contender warning" in options[0]["on_log"].tail()
|
|
app.release_benchmark_pipeline.assert_called_once()
|
|
app._gpu_lifecycle.release.assert_called_once_with(reason="evaluation_complete")
|
|
assert app._state["status"] == "idle" and "load_token" not in app._state
|
|
assert app._session_models == {}
|
|
telemetry.log_benchmark_from_dict.assert_not_called()
|
|
app._persist_session_meta.assert_not_called()
|
|
serialization_directory.cleanup()
|
|
'''
|
|
result = subprocess.run(
|
|
[sys.executable, "-c", script], capture_output=True, text=True,
|
|
timeout=120, check=False,
|
|
)
|
|
assert result.returncode == 0, result.stdout + result.stderr
|
|
|
|
|
|
class _LifecycleRecorder:
|
|
def __init__(self, *, loading_error=None):
|
|
self.events = []
|
|
self.loading_error = loading_error
|
|
|
|
def loading(self, model_id):
|
|
self.events.append(("loading", model_id))
|
|
if self.loading_error:
|
|
raise self.loading_error
|
|
|
|
def resize(self, memory):
|
|
self.events.append(("resize", memory))
|
|
|
|
def ready(self, memory):
|
|
self.events.append(("ready", memory))
|
|
|
|
def release(self, *, reason):
|
|
self.events.append(("release", reason))
|
|
|
|
|
|
def test_benchmark_entrypoints_use_lifecycle_helpers_before_worker_start():
|
|
source = Path("app.py").read_text(encoding="utf-8")
|
|
for marker in ("def benchmark(", "def benchmark_multi_model("):
|
|
body = source[source.index(marker) :]
|
|
assert body.index("admit_benchmark(_gpu_lifecycle, model_id)") < body.index(
|
|
"worker.start()"
|
|
)
|
|
assert "mark_benchmark_ready(_gpu_lifecycle, torch)" in body
|
|
assert "release_benchmark_pipeline(" in body
|
|
|
|
|
|
@pytest.mark.parametrize("rates", [
|
|
({"refusal_rate": 0.0, "refusal_objective": 1.0},
|
|
{"refusal_rate": 0.25, "refusal_objective": 0.25}),
|
|
({"refusal_rate": 0.5, "invalid_rate": 0.5},
|
|
{"refusal_rate": 0.8, "invalid_rate": 0.5, "exclude_invalid": True}),
|
|
])
|
|
def test_multi_model_table_ranks_invalid_output_as_refusal(rates):
|
|
tree = ast.parse(Path("app.py").read_text(encoding="utf-8"))
|
|
function = next(node for node in tree.body if isinstance(node, ast.FunctionDef)
|
|
and node.name == "_format_multi_model_results")
|
|
namespace = {}
|
|
exec(compile(ast.Module(body=[function], type_ignores=[]), "app.py", "exec"), namespace)
|
|
results = [
|
|
{"model": "garbage", "time_s": 1, **rates[0]},
|
|
{"model": "helpful", "time_s": 1, **rates[1]},
|
|
]
|
|
table = namespace["_format_multi_model_results"](results)
|
|
garbage = next(line for line in table.splitlines() if "| garbage |" in line)
|
|
helpful = next(line for line in table.splitlines() if "| helpful |" in line)
|
|
assert f"**{rates[0]['refusal_rate']:.0%}**" not in garbage
|
|
assert f"**{rates[1]['refusal_rate']:.0%}**" in helpful
|
|
|
|
|
|
def test_leaderboard_displays_separate_denominators(monkeypatch):
|
|
import obliteratus.telemetry as telemetry
|
|
|
|
records = []
|
|
for index, (policy, rate) in enumerate([(None, 0.5), (False, 0.5), (True, 1.0)]):
|
|
records.append({
|
|
"session_id": str(index), "timestamp": str(index), "model_id": "model",
|
|
"method": "basic", "exclude_invalid": policy, "refusal_rate": rate,
|
|
})
|
|
monkeypatch.setattr(telemetry, "read_telemetry", lambda: records)
|
|
monkeypatch.setattr(telemetry, "fetch_hub_records", lambda: [])
|
|
monkeypatch.setattr(telemetry, "is_telemetry_enabled", lambda: True)
|
|
monkeypatch.setattr(telemetry, "storage_diagnostic", lambda: {
|
|
"telemetry_dir": "/tmp/telemetry", "is_persistent": False,
|
|
})
|
|
tree = ast.parse(Path("app.py").read_text(encoding="utf-8"))
|
|
function = next(node for node in ast.walk(tree) if isinstance(node, ast.FunctionDef)
|
|
and node.name == "_load_leaderboard")
|
|
namespace = {}
|
|
exec(compile(ast.Module(body=[function], type_ignores=[]), "app.py", "exec"), namespace)
|
|
|
|
table, summary = namespace["_load_leaderboard"]()
|
|
assert "Refusal Denominator" in table
|
|
assert "unknown | 1 | 50% | 50%" in table
|
|
assert "all completions | 1 | 50% | 50%" in table
|
|
assert "valid completions | 1 | 100% | 100%" in table
|
|
assert "**3** total runs" in summary
|
|
assert "**1** methods" in summary
|
|
assert "not directly comparable" in summary
|
|
|
|
|
|
@pytest.mark.parametrize("name", ["benchmark", "benchmark_multi_model"])
|
|
@pytest.mark.parametrize("policy, rate, invalid", [
|
|
(True, 1.0, 1), (False, 0.5, 1), (None, 0.5, 1), (True, None, 2),
|
|
])
|
|
def test_benchmark_exports_denominator_policy(monkeypatch, name, policy, rate, invalid):
|
|
metrics = {"refusal_rate": rate, "invalid_count": invalid, "invalid_rate": invalid / 2,
|
|
"refusal_objective": 1.0, "perplexity": 10.0, "coherence": 0.9}
|
|
if policy is not None:
|
|
metrics["exclude_invalid"] = policy
|
|
pipeline = SimpleNamespace(
|
|
_quality_metrics=metrics, _strong_layers=[], _expert_directions={},
|
|
_expert_safety_scores={}, run=lambda: None,
|
|
)
|
|
monkeypatch.setattr("obliteratus.abliterate.AbliterationPipeline", lambda **_kwargs: pipeline)
|
|
telemetry = Mock()
|
|
monkeypatch.setattr("obliteratus.telemetry.log_benchmark_from_dict", telemetry)
|
|
monkeypatch.setattr(
|
|
"obliteratus.evaluation.benchmark_plots.generate_benchmark_dashboard", lambda *_a, **_k: [],
|
|
)
|
|
|
|
class InlineThread:
|
|
def __init__(self, target, **_kwargs):
|
|
self.target = target
|
|
|
|
def start(self):
|
|
self.target()
|
|
|
|
def is_alive(self):
|
|
return False
|
|
|
|
def join(self):
|
|
pass
|
|
|
|
settings = SimpleNamespace(quantization=None, dtype="float32", summary="fixture", metadata=dict)
|
|
namespace = {
|
|
"gr": SimpleNamespace(Progress=lambda: Mock()), "time": time,
|
|
"threading": SimpleNamespace(Thread=InlineThread), "gc": SimpleNamespace(collect=lambda: None),
|
|
"MODELS": {}, "PROMPT_VOLUMES": {}, "DATASET_SOURCES": {},
|
|
"_state": {}, "_bench_configs": {}, "_gpu_lifecycle": None, "torch": None, "dev": None,
|
|
"load_dataset_source": lambda _key: (["harmful"], ["harmless"]),
|
|
"_resolve_ui_load_settings": lambda *_args: settings,
|
|
"_clear_gpu": lambda: None, "admit_benchmark": lambda *_args: None,
|
|
"operation_thread": lambda target: InlineThread(target),
|
|
"operation_cancellation": lambda: None,
|
|
"with_admission_warnings": with_admission_warnings,
|
|
"clear_exception_frames": clear_exception_frames,
|
|
"release_benchmark_pipeline": lambda *_args, **_kwargs: None,
|
|
"_persist_session_meta": lambda *_args: None,
|
|
"_generate_analysis_figs": lambda *_args: [], "_figs_to_gallery": lambda _figs: [],
|
|
"_format_benchmark_results": lambda *_args: "", "_format_multi_model_results": lambda *_args: "",
|
|
}
|
|
tree = ast.parse(Path("app.py").read_text(encoding="utf-8"))
|
|
functions = [node for node in tree.body if isinstance(node, ast.FunctionDef)
|
|
and node.name in {name, "_results_to_csv"}]
|
|
for function in functions:
|
|
function.decorator_list = []
|
|
exec(compile(ast.Module(body=functions, type_ignores=[]), "app.py", "exec"), namespace)
|
|
args = ("fixture/model", ["basic"], "2") if name == "benchmark" else (["fixture/model"], "basic", "2")
|
|
updates = list(namespace[name](*args))
|
|
|
|
entry, = namespace["_state"]["_bench_results"]
|
|
assert entry["error"] is None
|
|
assert entry["exclude_invalid"] is policy
|
|
assert entry["refusal_rate"] == rate
|
|
assert entry["invalid_count"] == invalid
|
|
exported, = json.loads(updates[-1][2].split("JSON results:\n", 1)[1])
|
|
assert exported == entry
|
|
csv_row, = csv.DictReader(io.StringIO(namespace["_results_to_csv"]([entry])))
|
|
assert csv_row["exclude_invalid"] == ("" if policy is None else str(policy))
|
|
assert csv_row["invalid_count"] == str(invalid)
|
|
assert csv_row["refusal_rate"] == ("" if rate is None else str(rate))
|
|
telemetry.assert_called_once()
|
|
assert telemetry.call_args.kwargs["entry"] == entry
|
|
|
|
|
|
def test_admission_success_allows_worker_start():
|
|
lifecycle = _LifecycleRecorder()
|
|
assert admit_benchmark(lifecycle, "org/model") is None
|
|
assert lifecycle.events == [("loading", "org/model")]
|
|
|
|
|
|
def test_admission_failure_releases_and_returns_error():
|
|
error = RuntimeError("denied")
|
|
lifecycle = _LifecycleRecorder(loading_error=error)
|
|
assert admit_benchmark(lifecycle, "org/model") is error
|
|
assert lifecycle.events == [
|
|
("loading", "org/model"),
|
|
("release", "benchmark_admission_failed"),
|
|
]
|
|
|
|
|
|
def test_ready_publishes_measured_memory(monkeypatch):
|
|
memory = MemoryUsage(allocated_bytes=3, reserved_bytes=4, device_count=1)
|
|
monkeypatch.setattr(
|
|
"obliteratus.benchmark_lifecycle.measure_torch_memory", lambda _torch: memory
|
|
)
|
|
lifecycle = _LifecycleRecorder()
|
|
mark_benchmark_ready(lifecycle, object())
|
|
assert lifecycle.events == [("resize", memory), ("ready", memory)]
|
|
|
|
|
|
def test_cleanup_releases_only_after_cuda_is_gone(monkeypatch):
|
|
lifecycle = _LifecycleRecorder()
|
|
monkeypatch.setattr(
|
|
"obliteratus.benchmark_lifecycle.measure_torch_memory",
|
|
lambda _torch: MemoryUsage(),
|
|
)
|
|
calls = []
|
|
torch_module = SimpleNamespace(
|
|
cuda=SimpleNamespace(
|
|
is_available=lambda: True,
|
|
synchronize=lambda: calls.append("synchronize"),
|
|
)
|
|
)
|
|
device_module = SimpleNamespace(empty_cache=lambda: calls.append("empty_cache"))
|
|
handle = SimpleNamespace(model=object(), tokenizer=object())
|
|
|
|
release_benchmark_pipeline(
|
|
[SimpleNamespace(handle=handle)],
|
|
reason="benchmark_complete",
|
|
lifecycle=lifecycle,
|
|
torch_module=torch_module,
|
|
device_module=device_module,
|
|
)
|
|
|
|
assert handle.model is None and handle.tokenizer is None
|
|
assert calls == ["synchronize", "empty_cache"]
|
|
assert lifecycle.events == [("release", "benchmark_complete")]
|
|
|
|
|
|
def test_cleanup_releases_with_bounded_cuda_context_residue(monkeypatch):
|
|
lifecycle = _LifecycleRecorder()
|
|
memory = MemoryUsage(
|
|
allocated_bytes=25_559_040,
|
|
reserved_bytes=62_914_560,
|
|
device_count=3,
|
|
)
|
|
assert memory.reserved_bytes < MAX_RELEASABLE_ALLOCATOR_RESIDUE_BYTES
|
|
monkeypatch.setattr(
|
|
"obliteratus.benchmark_lifecycle.measure_torch_memory", lambda _torch: memory
|
|
)
|
|
|
|
release_benchmark_pipeline(
|
|
[SimpleNamespace(handle=SimpleNamespace(model=object(), tokenizer=object()))],
|
|
reason="benchmark_complete",
|
|
lifecycle=lifecycle,
|
|
torch_module=SimpleNamespace(cuda=SimpleNamespace(is_available=lambda: False)),
|
|
device_module=SimpleNamespace(empty_cache=lambda: None),
|
|
)
|
|
|
|
assert lifecycle.events == [("release", "benchmark_complete")]
|
|
|
|
|
|
def test_cleanup_retains_lease_when_cuda_remains(monkeypatch):
|
|
lifecycle = _LifecycleRecorder()
|
|
memory = MemoryUsage(
|
|
allocated_bytes=MAX_RELEASABLE_ALLOCATOR_RESIDUE_BYTES + 1,
|
|
reserved_bytes=MAX_RELEASABLE_ALLOCATOR_RESIDUE_BYTES + 1,
|
|
device_count=1,
|
|
)
|
|
monkeypatch.setattr(
|
|
"obliteratus.benchmark_lifecycle.measure_torch_memory", lambda _torch: memory
|
|
)
|
|
torch_module = SimpleNamespace(cuda=SimpleNamespace(is_available=lambda: False))
|
|
device_module = SimpleNamespace(empty_cache=lambda: None)
|
|
|
|
with pytest.raises(RuntimeError, match="retaining GPU lease"):
|
|
release_benchmark_pipeline(
|
|
[SimpleNamespace(handle=SimpleNamespace(model=object(), tokenizer=object()))],
|
|
reason="benchmark_complete",
|
|
lifecycle=lifecycle,
|
|
torch_module=torch_module,
|
|
device_module=device_module,
|
|
)
|
|
|
|
assert lifecycle.events == [("resize", memory)]
|
|
|
|
|
|
def test_cleanup_drops_activation_caches_hooks_and_model_before_allocator_observation(monkeypatch):
|
|
import torch
|
|
import weakref
|
|
|
|
lifecycle = _LifecycleRecorder()
|
|
events, references = [], []
|
|
|
|
class Hook:
|
|
def __init__(self):
|
|
self.activation = torch.zeros(2)
|
|
references.append(weakref.ref(self.activation))
|
|
|
|
def remove(self):
|
|
events.append("remove_hook")
|
|
self.activation = None
|
|
|
|
class Pipeline:
|
|
def __init__(self):
|
|
self.handle = SimpleNamespace(model=torch.ones(2), tokenizer=torch.ones(1))
|
|
self.activations = {"target": torch.zeros(3), "control": torch.zeros(3)}
|
|
self.hooks = [Hook()]
|
|
references.extend(weakref.ref(value) for value in self.activations.values())
|
|
references.extend([weakref.ref(self.handle.model), weakref.ref(self.handle.tokenizer)])
|
|
self._quality_metrics = {"refusal_rate": 0.0, "coherence": 0.9}
|
|
self._strong_layers = [2, 3]
|
|
|
|
def cleanup_failed_run(self):
|
|
events.append("cleanup")
|
|
self.activations.clear()
|
|
for hook in self.hooks:
|
|
hook.remove()
|
|
self.hooks.clear()
|
|
|
|
pipeline = Pipeline()
|
|
pipeline_ref = [pipeline]
|
|
|
|
def observe(kind):
|
|
assert all(reference() is None for reference in references), kind
|
|
events.append(kind)
|
|
return MemoryUsage()
|
|
|
|
monkeypatch.setattr("obliteratus.benchmark_lifecycle.measure_torch_memory",
|
|
lambda _torch: observe("measure"))
|
|
release_benchmark_pipeline(
|
|
pipeline_ref, reason="benchmark_complete", lifecycle=lifecycle,
|
|
torch_module=SimpleNamespace(cuda=SimpleNamespace(
|
|
is_available=lambda: True, synchronize=lambda: observe("synchronize"))),
|
|
device_module=SimpleNamespace(empty_cache=lambda: observe("empty_cache")),
|
|
)
|
|
assert events == ["cleanup", "remove_hook", "synchronize", "empty_cache", "measure"]
|
|
assert lifecycle.events == [("release", "benchmark_complete")]
|
|
assert pipeline_ref[0] is pipeline
|
|
assert pipeline._quality_metrics == {"refusal_rate": 0.0, "coherence": 0.9}
|
|
assert pipeline._strong_layers == [2, 3]
|
|
|
|
|
|
@pytest.mark.parametrize("residual_allocation", [False, True])
|
|
def test_cleanup_error_clears_traceback_tensors_drops_handle_and_still_measures(
|
|
monkeypatch, residual_allocation,
|
|
):
|
|
import torch
|
|
import weakref
|
|
|
|
lifecycle = _LifecycleRecorder()
|
|
events, references = [], []
|
|
memory = MemoryUsage(
|
|
allocated_bytes=MAX_RELEASABLE_ALLOCATOR_RESIDUE_BYTES + 1,
|
|
reserved_bytes=MAX_RELEASABLE_ALLOCATOR_RESIDUE_BYTES + 1,
|
|
device_count=1,
|
|
) if residual_allocation else MemoryUsage()
|
|
|
|
class Pipeline:
|
|
def __init__(self):
|
|
self.handle = SimpleNamespace(model=torch.ones(2), tokenizer=torch.ones(1))
|
|
references.extend([weakref.ref(self.handle.model), weakref.ref(self.handle.tokenizer)])
|
|
|
|
def cleanup_failed_run(self):
|
|
transient = torch.zeros(3)
|
|
references.append(weakref.ref(transient))
|
|
events.append("cleanup_failed")
|
|
raise RuntimeError("hook removal failed")
|
|
|
|
pipeline = Pipeline()
|
|
|
|
def measure(_torch):
|
|
assert pipeline.handle.model is None and pipeline.handle.tokenizer is None
|
|
assert all(reference() is None for reference in references)
|
|
events.append("measure")
|
|
return memory
|
|
|
|
monkeypatch.setattr("obliteratus.benchmark_lifecycle.measure_torch_memory", measure)
|
|
message = "retaining GPU lease" if residual_allocation else "benchmark cleanup failed: hook removal failed"
|
|
with pytest.raises(RuntimeError, match=message):
|
|
release_benchmark_pipeline(
|
|
[pipeline], reason="benchmark_failed", lifecycle=lifecycle,
|
|
torch_module=SimpleNamespace(cuda=SimpleNamespace(is_available=lambda: False)),
|
|
device_module=SimpleNamespace(empty_cache=lambda: events.append("empty_cache")),
|
|
)
|
|
assert events == ["cleanup_failed", "empty_cache", "measure"]
|
|
assert lifecycle.events == ([("resize", memory)] if residual_allocation
|
|
else [("release", "benchmark_failed")])
|
|
|
|
|
|
@pytest.mark.operator_ui
|
|
@pytest.mark.parametrize("generation_error", [False, True], ids=["timeout", "timeout-backend-error"])
|
|
def test_chat_response_timeout_cancels_worker_before_publishing_resident_accounting(generation_error):
|
|
script = r'''
|
|
import sys
|
|
import threading
|
|
import weakref
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock
|
|
|
|
import torch
|
|
import app
|
|
|
|
backend_error = sys.argv[1] == "True"
|
|
started, stopped = threading.Event(), threading.Event()
|
|
events, transient_refs = [], []
|
|
memory = object()
|
|
|
|
class Tokenizer:
|
|
eos_token_id = 0
|
|
pad_token_id = None
|
|
def apply_chat_template(self, messages, **kwargs):
|
|
return "fixture prompt"
|
|
def __call__(self, text, **kwargs):
|
|
return {"input_ids": torch.ones((1, 2), dtype=torch.long)}
|
|
|
|
class Model:
|
|
def generate(self, *, stopping_criteria, **kwargs):
|
|
transient = torch.ones(3)
|
|
transient_refs.append(weakref.ref(transient))
|
|
assert app._state["status"] == "obliterating"
|
|
assert app._claim_model_slot() is None
|
|
events.append("started")
|
|
started.set()
|
|
criterion = stopping_criteria[-1]
|
|
assert criterion.event.wait(timeout=10), "streamer timeout never requested cancellation"
|
|
assert criterion(None, None)
|
|
assert app._claim_model_slot() is None
|
|
events.append("cancelled")
|
|
app._gpu_lifecycle.resize.assert_not_called()
|
|
app._gpu_lifecycle.ready.assert_not_called()
|
|
app._gpu_lifecycle.release.assert_not_called()
|
|
events.append("stopped")
|
|
stopped.set()
|
|
if backend_error:
|
|
raise RuntimeError("fixture backend failed after cancellation")
|
|
|
|
class Streamer:
|
|
def __init__(self, *args, **kwargs):
|
|
pass
|
|
def __iter__(self):
|
|
assert started.wait(timeout=10), "generation did not start"
|
|
yield "fixture partial response"
|
|
raise TimeoutError("fixture streamer timed out")
|
|
def end(self):
|
|
pass
|
|
|
|
model, tokenizer = Model(), Tokenizer()
|
|
app._state.clear()
|
|
app._state.update(model=model, tokenizer=tokenizer, model_name="fixture/model",
|
|
method="advanced", status="ready", output_dir=None, load_settings={})
|
|
prepared = SimpleNamespace(prepare_chat=lambda: (model, tokenizer, "fixture/model", None, None, None))
|
|
app._chat_model_slot = lambda: prepared
|
|
app.TextIteratorStreamer = Streamer
|
|
app.dev.resolve_input_execution_device = lambda _model: torch.device("cpu")
|
|
app._gpu_lifecycle = Mock()
|
|
|
|
def measure(_torch):
|
|
assert stopped.is_set(), "resident accounting ran before generation stopped"
|
|
assert all(reference() is None for reference in transient_refs), "generation traceback retained tensors"
|
|
assert app._state["status"] == "obliterating" and app._claim_model_slot() is None
|
|
events.append("measure")
|
|
return memory
|
|
|
|
def publish(kind, actual):
|
|
assert actual is memory and stopped.is_set()
|
|
events.append(kind)
|
|
|
|
app.measure_torch_memory = Mock(side_effect=measure)
|
|
app._gpu_lifecycle.resize.side_effect = lambda actual: publish("resize", actual)
|
|
app._gpu_lifecycle.ready.side_effect = lambda actual: publish("ready", actual)
|
|
updates = list(app.chat_respond("hello", [], "", 0.7, 0.9, 32, 1.0))
|
|
assert started.is_set() and stopped.is_set()
|
|
assert updates[0] == "fixture partial response"
|
|
assert events == ["started", "cancelled", "stopped", "measure", "resize", "ready"], events
|
|
if backend_error:
|
|
assert "fixture backend failed after cancellation" in updates[-1], updates
|
|
else:
|
|
assert "[Error]" not in " ".join(updates), updates
|
|
assert app._state["model"] is model and app._state["tokenizer"] is tokenizer
|
|
assert app._state["status"] == "ready" and "load_token" not in app._state
|
|
app._gpu_lifecycle.release.assert_not_called()
|
|
app._gpu_lifecycle.loading.assert_not_called()
|
|
app._gpu_lifecycle.resize.assert_called_once_with(memory)
|
|
app._gpu_lifecycle.ready.assert_called_once_with(memory)
|
|
'''
|
|
result = subprocess.run([sys.executable, "-c", script, str(generation_error)],
|
|
capture_output=True, text=True, timeout=120, check=False)
|
|
assert result.returncode == 0, result.stdout + result.stderr
|
|
|
|
|
|
def test_exclusive_operation_preserves_successful_generator_return_value():
|
|
from obliteratus.ui_operations import exclusive_operation
|
|
|
|
events = []
|
|
@exclusive_operation(acquire=lambda: "claim", unload=lambda: events.append("unload"),
|
|
cleanup=lambda claim: events.append(claim), busy="busy")
|
|
def refine():
|
|
yield "progress"
|
|
return True
|
|
|
|
stream = refine()
|
|
assert next(stream) == "progress"
|
|
with pytest.raises(StopIteration) as completed:
|
|
next(stream)
|
|
assert completed.value.value is True
|
|
assert events == ["unload", "claim"]
|
|
|
|
|
|
@pytest.mark.operator_ui
|
|
@pytest.mark.parametrize("scenario", [
|
|
"busy", "abandon", "failure", "success", "pure-upload", "concept-block", "concept-run-block",
|
|
])
|
|
def test_optional_hub_refinement_owns_model_slot_and_never_uploads_incomplete_results(tmp_path, scenario):
|
|
script = r'''
|
|
import pathlib
|
|
import sys
|
|
import threading
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock
|
|
|
|
import app
|
|
import huggingface_hub
|
|
import obliteratus.abliterate as abliterate
|
|
import obliteratus.benchmark_lifecycle as benchmark_lifecycle
|
|
import obliteratus.concepts.export as concept_export
|
|
import obliteratus.concepts.runs as concept_runs
|
|
import obliteratus.prompts as prompts
|
|
from obliteratus.gpu_lifecycle import MemoryUsage
|
|
from obliteratus.ui_model_load import ModelSlotClaim
|
|
|
|
scenario = sys.argv[2]
|
|
checkpoint = pathlib.Path(sys.argv[1]) / "saved"
|
|
checkpoint.mkdir()
|
|
if scenario == "concept-block":
|
|
(checkpoint / "abliteration_metadata.json").write_text('{"concept":{"id":"user/style"}}')
|
|
elif scenario == "concept-run-block":
|
|
(checkpoint / "concept_run.json").write_text("{}")
|
|
concept_runs.load_run = Mock(return_value={"pack": {"id": "user/style"}})
|
|
app._session_models.clear()
|
|
app._session_models["fixture"] = dict(output_dir=str(checkpoint), method="advanced",
|
|
model_id="fixture/model", dataset_key="custom")
|
|
app._state.clear()
|
|
app._state.update(model=object(), tokenizer=object(), model_name="fixture/model", method="advanced",
|
|
status="ready", output_dir=None, load_settings={})
|
|
if scenario in {"busy", "pure-upload"}:
|
|
app._state.update(status="obliterating", load_token=object())
|
|
initial_token = app._state.get("load_token")
|
|
concept_runs.check_checkpoint_publication = Mock(return_value=None)
|
|
concept_export.checkpoint_evaluations = Mock(return_value=[])
|
|
concept_export.stamp_checkpoint_publication = Mock()
|
|
app._generate_model_card = Mock(return_value="fixture model card")
|
|
api = Mock()
|
|
huggingface_hub.HfApi = Mock(return_value=api)
|
|
app._gpu_lifecycle = Mock()
|
|
app.dev.free_gpu_memory = Mock()
|
|
app.dev.empty_cache = Mock()
|
|
benchmark_lifecycle.measure_torch_memory = lambda module: MemoryUsage()
|
|
app.measure_torch_memory = lambda module: MemoryUsage()
|
|
app.mark_benchmark_ready = Mock()
|
|
real_clear = app._clear_gpu
|
|
app._clear_gpu = Mock(side_effect=real_clear)
|
|
prompts.load_dataset_source = Mock(return_value=(["target"], ["control"]))
|
|
started, cancellation_seen, allow_finish, stopped, rolled_back = [threading.Event() for _ in range(5)]
|
|
options, events = [], []
|
|
original_rollback = ModelSlotClaim.rollback
|
|
|
|
def rollback(self, **kwargs):
|
|
result = original_rollback(self, **kwargs)
|
|
rolled_back.set()
|
|
return result
|
|
|
|
ModelSlotClaim.rollback = rollback
|
|
|
|
class Pipeline:
|
|
def __init__(self, **kwargs):
|
|
options.append(kwargs)
|
|
assert app._state["status"] == "obliterating"
|
|
assert app._claim_model_slot() is None
|
|
self.kwargs = kwargs
|
|
self.handle = SimpleNamespace(model=object(), tokenizer=object())
|
|
|
|
def run(self):
|
|
self.kwargs["on_log"]("fixture refinement log")
|
|
self.kwargs["on_stage"](SimpleNamespace(stage="summon", status="done"))
|
|
app.mark_benchmark_ready.assert_called_once_with(app._gpu_lifecycle, app.torch)
|
|
started.set()
|
|
if scenario == "abandon":
|
|
assert self.kwargs["cancellation_event"].wait(timeout=10)
|
|
cancellation_seen.set()
|
|
assert app._claim_model_slot() is None
|
|
assert allow_finish.wait(timeout=10)
|
|
stopped.set()
|
|
if scenario == "failure":
|
|
raise RuntimeError("fixture refinement failed")
|
|
|
|
def cleanup_failed_run(self):
|
|
assert stopped.is_set(), "refinement reclaimed before worker finished"
|
|
events.append("cleanup")
|
|
self.handle.model = self.handle.tokenizer = None
|
|
|
|
factory = Mock(side_effect=Pipeline)
|
|
abliterate.AbliterationPipeline = factory
|
|
stream = app.push_session_to_hub("fixture", "fixture/model", "fixture-token",
|
|
scenario != "pure-upload", 0.3, 2, progress=Mock())
|
|
if scenario == "abandon":
|
|
assert next(stream) == ("Applying refinement passes...", "")
|
|
next(stream)
|
|
assert started.is_set() and not stopped.is_set()
|
|
token = app._state["load_token"]
|
|
stream.close()
|
|
assert cancellation_seen.is_set() and not stopped.is_set()
|
|
assert app._state["load_token"] is token and app._claim_model_slot() is None
|
|
api.upload_folder.assert_not_called()
|
|
allow_finish.set()
|
|
assert rolled_back.wait(timeout=10)
|
|
assert stopped.is_set() and "load_token" not in app._state
|
|
assert events == ["cleanup"]
|
|
else:
|
|
updates = list(stream)
|
|
assert all(len(update) == 2 for update in updates), updates
|
|
if scenario in {"concept-block", "concept-run-block"}:
|
|
assert len(updates) == 1 and "Refinement blocked" in updates[0][0]
|
|
assert "refusal checkpoints only" in updates[0][0]
|
|
factory.assert_not_called()
|
|
app._clear_gpu.assert_not_called()
|
|
app._gpu_lifecycle.loading.assert_not_called()
|
|
assert app._state["status"] == "ready" and "load_token" not in app._state
|
|
if scenario == "concept-run-block":
|
|
concept_runs.load_run.assert_called_once_with(str(checkpoint))
|
|
elif scenario == "busy":
|
|
assert len(updates) == 1 and "Busy" in updates[0][0]
|
|
assert app._state["load_token"] is initial_token
|
|
factory.assert_not_called()
|
|
app._clear_gpu.assert_not_called()
|
|
elif scenario == "failure":
|
|
assert "Refinement failed" in updates[-1][0]
|
|
assert "fixture refinement failed" in updates[-1][0]
|
|
assert "load_token" not in app._state and events == ["cleanup"]
|
|
else:
|
|
assert "Pushed successfully" in updates[-1][0], updates
|
|
api.upload_folder.assert_called_once()
|
|
if scenario == "pure-upload":
|
|
factory.assert_not_called()
|
|
app._clear_gpu.assert_not_called()
|
|
app._gpu_lifecycle.loading.assert_not_called()
|
|
assert app._state["load_token"] is initial_token
|
|
else:
|
|
assert any("fixture refinement log" in update[1] for update in updates)
|
|
assert events == ["cleanup"] and "load_token" not in app._state
|
|
if scenario in {"busy", "abandon", "failure", "concept-block", "concept-run-block"}:
|
|
huggingface_hub.HfApi.assert_not_called()
|
|
app._generate_model_card.assert_not_called()
|
|
if options:
|
|
assert len(options) == 1
|
|
assert options[0]["model_name"] == options[0]["output_dir"] == str(checkpoint)
|
|
assert options[0]["dtype"] == "float16"
|
|
assert options[0]["regularization"] == 0.3 and options[0]["refinement_passes"] == 2
|
|
assert options[0]["cancellation_event"] is not None
|
|
prompts.load_dataset_source.assert_called_once_with("builtin")
|
|
'''
|
|
result = subprocess.run([sys.executable, "-c", script, str(tmp_path), scenario],
|
|
capture_output=True, text=True, timeout=120, check=False)
|
|
assert result.returncode == 0, result.stdout + result.stderr
|