Files
OBLITERATUS/tests/conditional/test_operator_ui.py

1594 lines
66 KiB
Python

"""Construct the optional Gradio UI without opening a listener."""
from __future__ import annotations
import asyncio
import os
import subprocess
import sys
import pytest
pytestmark = pytest.mark.operator_ui
def test_neuron_download_expires_with_its_session_without_deleting_other_sessions(tmp_path):
probe = subprocess.run(
[sys.executable, "-c", r'''
import asyncio
from pathlib import Path
from types import SimpleNamespace
import gradio as gr
import httpx
import torch
from gradio.state_holder import SessionState
from obliteratus.interventions import NeuronRecipe
from obliteratus.ui_neuron_lab import build_neuron_lab
from tests.conditional.test_operator_ui import _process_api
with gr.Blocks() as demo:
build_neuron_lab({})
dependency = next(item for item in demo.get_config_file()['dependencies']
if item['api_name'] == 'neuron_export_experiment')
sessions = []
async def check():
downloads = []
for index in range(2):
state = SessionState(demo)
session = state[dependency['inputs'][0]]
session.handle = SimpleNamespace(
model=torch.nn.Linear(1, 1), model_name='fixture', source_model_revision=None,
tokenizer=SimpleNamespace(name_or_path='fixture', init_kwargs={}), cleanup=lambda: None)
session.recipe = NeuronRecipe('fixture', ())
session.comparison = {'prompt': 'private prompt', 'prompt_sha256': 'fixture',
'prompt_token_ids': [7], 'trials': {}}
result = await _process_api(demo, dependency['id'], [None, True], state=state)
download = result['data'][12]
assert Path(download['path']) == Path(session.experiment_file)
assert Path(download['path']).is_file()
sessions.append(session)
downloads.append(download)
assert downloads[0]['path'] != downloads[1]['path']
transport = httpx.ASGITransport(app=gr.routes.App.create_app(demo))
async with httpx.AsyncClient(transport=transport, base_url='http://test.invalid') as client:
async def get(url):
task = asyncio.create_task(client.get(url))
while not task.done():
await asyncio.sleep(.01)
return await task
assert (await get(downloads[0]['url'])).status_code == 200
demo.blocks[dependency['inputs'][0]].delete_callback(sessions[0])
assert not Path(downloads[0]['path']).exists()
response = await get(downloads[0]['url'])
assert response.status_code == 403 and 'private prompt' not in response.text
response = await get(downloads[1]['url'])
assert response.status_code == 200 and 'private prompt' in response.text
sessions[1].close()
asyncio.run(check())
'''],
capture_output=True, text=True, timeout=120, check=False,
env={**os.environ, "GRADIO_ANALYTICS_ENABLED": "False", "GRADIO_TEMP_DIR": str(tmp_path)},
)
assert probe.returncode == 0, probe.stdout + probe.stderr
async def _process_api(blocks, *args, **kwargs):
# Keep the loop scheduled when sandbox worker-wakeup sockets are unavailable.
task = asyncio.create_task(blocks.process_api(*args, **kwargs))
while not task.done():
await asyncio.sleep(.01)
return await task
def test_gradio_application_constructs_without_public_listener():
pytest.importorskip("gradio", reason="install the locked spaces extra")
probe = subprocess.run(
[
sys.executable,
"-c",
"import app, gradio; "
"assert type(app.demo).__name__ == 'Blocks'; "
"assert app.demo.local_url is None; "
"assert int(gradio.__version__.split('.')[0]) >= 6; "
"print('operator UI constructed without a listener')",
],
check=False,
capture_output=True,
text=True,
timeout=120,
env={**os.environ, "GRADIO_ANALYTICS_ENABLED": "False"},
)
assert probe.returncode == 0, probe.stderr
assert probe.stdout.strip() == "operator UI constructed without a listener"
assert probe.stderr == ""
def test_gpu_memory_refresh_controls_have_one_configurable_polling_loop():
"""Exercise the rendered Gradio graph rather than relying on source shape."""
pytest.importorskip("gradio", reason="install the locked spaces extra")
probe = subprocess.run(
[
sys.executable,
"-c",
r'''
import asyncio
import app
from tests.conditional.test_operator_ui import _process_api
config = app.demo.get_config_file()
components = config["components"]
timers = [item for item in components if item["type"] == "timer"]
assert len(timers) == 1
timer = timers[0]
assert timer["props"]["value"] == 30.0
assert timer["props"]["active"] is True
selectors = [
item for item in components
if "vram-refresh-control" in item["props"].get("elem_classes", [])
]
assert len(selectors) == 1
selector = selectors[0]
assert selector["type"] == "dropdown"
assert selector["props"]["label"] == "GPU refresh"
assert selector["props"]["choices"] == [
("10 sec", 10.0), ("30 sec", 30.0), ("1 min", 60.0)
]
assert selector["props"]["value"] == 30.0
assert selector["props"]["filterable"] is False
assert selector["props"]["allow_custom_value"] is False
assert selector["props"]["min_width"] == 96
timer_ticks = [
dependency for dependency in config["dependencies"]
if (timer["id"], "tick") in dependency["targets"]
]
assert len(timer_ticks) == 1
assert timer_ticks[0]["queue"] is False
assert timer_ticks[0]["outputs"] == [app.vram_display._id]
interval_changes = [
dependency for dependency in config["dependencies"]
if (selector["id"], "change") in dependency["targets"]
and dependency["outputs"] == [timer["id"]]
]
assert len(interval_changes) == 1
assert interval_changes[0]["queue"] is False
async def verify_interval_changes():
for seconds in (10.0, 30.0, 60.0):
result = await _process_api(app.demo,
interval_changes[0]["id"], [seconds], state=None
)
assert result["data"] == [seconds]
asyncio.run(verify_interval_changes())
''',
],
capture_output=True,
text=True,
timeout=120,
check=False,
env={**os.environ, "GRADIO_ANALYTICS_ENABLED": "False"},
)
assert probe.returncode == 0, probe.stdout + probe.stderr
def test_concept_lab_full_flow_through_gradio_callbacks(tmp_path):
"""Drive the rendered event graph on a generated model, without a listener."""
pytest.importorskip("gradio", reason="install the locked spaces extra")
probe = subprocess.run(
[sys.executable, "-c", r'''
import asyncio
import json
from pathlib import Path
import sys
import zipfile
import torch
from gradio.state_holder import SessionState
import app
from tests.fixtures.tiny_offline_model import build_tiny_offline_model
from tests.test_concept_lab import force_coherence_loss, make_lab_pack, OVERRIDE
from tests.conditional.test_operator_ui import _process_api
torch.set_num_threads(1)
directory = Path(sys.argv[1])
model = build_tiny_offline_model(directory / "model")
pack = make_lab_pack(directory / "pack")
config = app.demo.get_config_file()
components = {item["id"]: item for item in config["components"]}
tabs = {item["props"]["label"] for item in components.values() if item["type"] == "tabitem"}
assert {"Obliterate", "Concept Lab", "Benchmark", "Chat", "A/B Compare", "Strength Sweep",
"Tourney", "Export", "Push to Hub", "Leaderboard", "About"} <= tabs
concept = components[app.concept_dd._id]
assert concept["props"]["value"] == "refusal"
assert ("Refusal", "refusal") in concept["props"]["choices"]
obliterate = next(dep for dep in config["dependencies"]
if (app.obliterate_btn._id, "click") in dep["targets"])
assert obliterate["inputs"] == [app.model_dd._id, app.method_dd._id, app.prompt_vol_dd._id,
app.dataset_dd._id, app.custom_harmful_tb._id, app.custom_harmless_tb._id,
app.quantization_dd._id, app.dtype_dd._id, *[item._id for item in app._adv_controls],
app.concept_dd._id]
dependencies = {dep["api_name"]: dep for dep in config["dependencies"]}
events = {name: dependencies["concept_" + name]
for name in ("define", "extract", "validate", "apply", "evaluate", "repair", "export")}
state = SessionState(app.demo)
state_id = events["define"]["outputs"][0]
apply_id = events["apply"]["outputs"][10]
assert components[apply_id]["props"]["interactive"] is False
geometry_inputs = events["apply"]["inputs"][-6:]
assert [components[ident]["props"]["label"] for ident in geometry_inputs] == [
"Project input embeddings (orthogonalize / lora)", "Tied embedding policy",
"Steering scale", "Remove full saved rank-k subspace", "Erasure reference",
"Preserve directions",
]
geometry_defaults = [False, "", "recipe", False, "zero", ""]
assert [components[ident]["props"]["value"] for ident in geometry_inputs] == geometry_defaults
assert components[geometry_inputs[2]]["props"]["choices"] == [
("recipe", "recipe"), ("residual_norm", "residual_norm"),
("raw", "raw"), ("vector", "vector"),
]
async def call(name, inputs):
result = await _process_api(app.demo, events[name]["id"], inputs, state=state)
assert "failed:" not in result["data"][1], result["data"][1]
return [value.model_dump() if hasattr(value, "model_dump") else value
for value in result["data"]]
async def flow():
await call("define", [None, str(pack), str(model), 234, 4, 4])
defined = state[state_id]
await call("extract", [None, ""])
data = await call("validate", [None, ""])
assert data[2]["verdict"] == "fail"
assert data[10]["interactive"] is False
overrides = [dep for dep in config["dependencies"]
if dep["outputs"] == [apply_id] and dep["targets"][0][1] == "change"]
assert len(overrides) == 1
for reason, enabled in ((" \n", False), (OVERRIDE, True)):
result = await _process_api(app.demo, overrides[0]["id"], [None, reason], state=state)
assert result["data"][0]["interactive"] is enabled
blocked = await _process_api(app.demo, events["apply"]["id"],
[None, "", "project", "all", "all_tokens", 1., "", "hello world",
*geometry_defaults], state=state)
assert "failed gate" in blocked["data"][1]
assert state[state_id].run is None
data = await call("apply", [None, OVERRIDE, "project", "all", "all_tokens", 1.,
OVERRIDE, "hello world", *geometry_defaults])
assert data[5] == defined.pristine
assert data[6] == defined.modified
data = await call("evaluate", [None, OVERRIDE, "-3,-2,-1.5,-1,-0.5,0"])
collateral = data[3]["collateral"]
refusal = collateral["refusal"]
invalid = collateral["invalid_output"]
paired = collateral["paired_refusal_flags"]
assert invalid["n"] == 4, collateral
assert refusal["n"] + invalid["excluded_pairs"] == 4, collateral
assert len(paired["pristine"]) == len(paired["intervened"]) == refusal["n"], collateral
assert len(paired["pair_indices"]) == refusal["n"], collateral
assert all(0 <= index < 4 for index in paired["pair_indices"]), collateral
if not refusal["n"]:
assert refusal["status"] == "INVALID", collateral
coherence = collateral["coherence"]
assert coherence["before"]["count"] == coherence["after"]["count"] == 0, coherence
assert coherence["before"]["n"] == coherence["after"]["n"] > 0, coherence
data = await call("repair", [None, OVERRIDE])
assert data[4]["repair"] == "not needed", data[4]
data = await call("export", [None, OVERRIDE, "direction", False, "", False, ""])
assert data[7]["path"].endswith(".zip")
with zipfile.ZipFile(defined.artifact) as archive:
provenance = json.loads(archive.read("concept_provenance.json"))
assert provenance["run"]["validation"]["override_gate"] == OVERRIDE
assert provenance["run"]["pack"]["id"] == "user/synthetic"
isolated = SessionState(app.demo)
assert isolated[state_id] is None
previous = defined.directory
await call("define", [None, str(pack), str(model), 234, 4, 4])
assert not previous.exists()
assert state[state_id].run is None
defined = state[state_id]
await call("extract", [None, ""])
await call("validate", [None, ""])
await call("apply", [None, OVERRIDE, "project", "all", "all_tokens", 1.,
OVERRIDE, "hello world", *geometry_defaults])
battery = defined.pipeline._evaluation_battery
finish = battery.finish
battery.finish = lambda *args: force_coherence_loss(finish(*args))
await call("evaluate", [None, OVERRIDE, "-3,-2,-1.5,-1,-0.5,0"])
data = await call("repair", [None, OVERRIDE])
assert data[4]["repair"] == "incomplete"
assert all(attempt["parameters"] for attempt in data[4]["attempts"])
blocked = await _process_api(app.demo, events["export"]["id"],
[None, OVERRIDE, "direction", False, "", False, ""], state=state)
assert "accept-incomplete-repair" in blocked["data"][1]
assert defined.artifact is None
data = await call("export", [None, OVERRIDE, "direction", False, "", True, ""])
assert data[7]["path"].endswith(".zip")
with zipfile.ZipFile(defined.artifact) as archive:
provenance = json.loads(archive.read("concept_provenance.json"))
assert provenance["repair_choice"] == "original"
assert provenance["run"]["repair"]["repair"] == "incomplete"
state[state_id].close()
assert app.demo.local_url is None
asyncio.run(flow())
''', str(tmp_path)],
capture_output=True, text=True, timeout=120, check=False,
env={**os.environ, "GRADIO_ANALYTICS_ENABLED": "False"},
)
assert probe.returncode == 0, probe.stdout + probe.stderr
def test_concept_pack_rejects_prompt_overrides_and_disables_volume():
pytest.importorskip("gradio", reason="install the locked spaces extra")
probe = subprocess.run(
[sys.executable, "-c", r'''
import asyncio
import app
from tests.conditional.test_operator_ui import _process_api
config = app.demo.get_config_file()
components = {item["id"]: item for item in config["components"]}
assert "full corpus" in components[app.prompt_vol_dd._id]["props"]["info"]
volume_change = next(dep for dep in config["dependencies"]
if (app.concept_dd._id, "change") in dep["targets"]
and dep["outputs"] == [app.prompt_vol_dd._id])
async def verify_volume():
for source, enabled in (("pain", False), ("refusal", True), ("core/refusal", True)):
result = await _process_api(app.demo, volume_change["id"], [source], state=None)
assert result["data"][0]["interactive"] is enabled
asyncio.run(verify_volume())
obliterate = next(dep for dep in config["dependencies"]
if (app.obliterate_btn._id, "click") in dep["targets"])
defaults = [app.demo.blocks[ident].value for ident in obliterate["inputs"]]
for control, value in ((app.custom_harmful_tb, "custom positive"),
(app.custom_harmless_tb, "custom control"),
(app.dataset_dd, app.get_source_choices()[1])):
inputs = list(defaults)
inputs[-1] = "pain"
inputs[obliterate["inputs"].index(control._id)] = value
result = list(app.obliterate(*inputs))
assert len(result) == 1
assert len(result[0]) == 6
assert "Concept packs use their full pack data" in result[0][0], (control, result[0][:2])
assert "Clear custom prompts and select the built-in dataset" in result[0][1], result[0][:2]
assert app._state["status"] != "obliterating"
assert app.demo.local_url is None
'''],
capture_output=True, text=True, timeout=120, check=False,
env={**os.environ, "GRADIO_ANALYTICS_ENABLED": "False"},
)
assert probe.returncode == 0, probe.stdout + probe.stderr
def test_neuron_lab_full_flow_and_isolated_browser_states(tmp_path):
"""Run the actual rendered neuron callbacks on a local random-init Llama."""
pytest.importorskip("gradio", reason="install the locked spaces extra")
probe = subprocess.run(
[sys.executable, "-c", r'''
import asyncio
import json
from pathlib import Path
import sys
import torch
from transformers import LlamaConfig, LlamaForCausalLM
from gradio.state_holder import SessionState
from gradio.events import EventData
import app
from obliteratus.ui_neuron_lab import NeuronLabSession
from tests.fixtures.tiny_offline_model import _fixture_tokenizer, FIXTURE_VOCAB
from tests.conditional.test_operator_ui import _process_api
torch.set_num_threads(1)
torch.manual_seed(5249)
directory = Path(sys.argv[1])
model_path = directory / "model"
model = LlamaForCausalLM(LlamaConfig(
vocab_size=len(FIXTURE_VOCAB), hidden_size=16, intermediate_size=24,
num_hidden_layers=2, num_attention_heads=2, num_key_value_heads=2,
max_position_embeddings=1024, pad_token_id=0, eos_token_id=1, bos_token_id=1,
))
model.save_pretrained(model_path)
tokenizer = _fixture_tokenizer()
tokenizer.chat_template = "hello {{ messages[0]['content'] }} {% if add_generation_prompt %}answer{% endif %}"
tokenizer.save_pretrained(model_path)
del model
config = app.demo.get_config_file()
components = {item["id"]: item for item in config["components"]}
tabs = {item["props"]["label"]: ident for ident, item in components.items()
if item["type"] == "tabitem"}
assert {"Concept Lab", "Concepts", "Neurons"} <= set(tabs)
def descendants(node):
return {node["id"]} | set().union(*(descendants(child) for child in node.get("children", [])))
def find(node, ident):
if node["id"] == ident:
return node
for child in node.get("children", []):
found = find(child, ident)
if found is not None:
return found
assert {tabs["Concepts"], tabs["Neurons"]} <= descendants(find(config["layout"], tabs["Concept Lab"]))
events = {dep["api_name"].removeprefix("neuron_"): dep for dep in config["dependencies"]
if isinstance(dep["api_name"], str) and dep["api_name"].startswith("neuron_")}
state = SessionState(app.demo)
state_id = events["load"]["inputs"][0]
session = state[state_id]
assert isinstance(session, NeuronLabSession)
isolated = SessionState(app.demo)
other = isolated[state_id]
assert isinstance(other, NeuronLabSession)
assert other is not session and other._lock is not session._lock
assert other.handle is None and other.recipe is None
async def call(name, inputs):
result = await _process_api(app.demo, events[name]["id"], inputs, state=state)
data = [value.model_dump() if hasattr(value, "model_dump") else value for value in result["data"]]
assert "failed:" not in data[1], data[1]
return data
async def flow():
try:
data = await call("load", [None, str(model_path), "cpu", "", True])
assert len(data[2]) == 2 and session.handle is not None
assert state[state_id] is session and other.handle is None
await call("set_system_prompt", [None, "hello"])
assert session.system_prompt == "hello" and not other.system_prompt
await call("set_system_prompt", [None, ""])
assert session.system_prompt == ""
data = await call("scan", [None, "hello world", "hello answer", "0,1"])
assert len(session.scout["candidates"]) == 12
assert data[37]["selected"] == "find"
selected = await _process_api(
app.demo, events["select_candidate"]["id"], [None, 1], state=state,
)
candidate = session.scout["candidates"][0]
assert selected["data"][:4] == [candidate["layer"], candidate["neuron"], "add", "hello answer"]
assert selected["data"][5]["selected"] == "observe"
data = await call("inspect", [None, "world", 0, 1])
assert data[5]["count"] == 3
assert session.inspection["tokenization"]["chat_template_applied"] is True
assert 'data-journey=' in data[32] and 'captured positions' in data[32]
before = session.inspection
destination = await _process_api(
app.demo, events["select_layer"]["id"], [None], state=state,
event_data=EventData(None, {"layer": 1}),
)
assert destination["data"][:3] == [1, 0, "add"]
assert "Layer 1 selected" in destination["data"][4]
assert session.inspection is before
data = await call("edit", [None, "add", 1, 0, 1, "scale", 0., -1., 1., True, "prompt_tokens"])
assert session.recipe.edits[0].operation == "scale"
data = await call("converse", [None, "hello", 2])
assert data[37]["selected"] == "conversation"
assert len(session.dialogue["turns"]) == 1
await call("converse", [None, "world", 2])
assert len(session.dialogue["messages"]["baseline"]) == 4
await call("export_dialogue", [None, False])
conversation = json.loads(Path(session.dialogue_file).read_text())
assert "messages" not in conversation
assert all("prompt" not in turn for turn in conversation["turns"])
await call("export_dialogue", [None, True])
conversation = json.loads(Path(session.dialogue_file).read_text())
assert conversation["messages"]["baseline"][0]["content"] == "hello"
await call("restart_dialogue", [None])
assert session.dialogue is None and session.dialogue_file is None
assert data[13]["value"] == "prompt_tokens"
assert "Activation fingerprint" in data[15]
assert "L00" in data[16] and "N01" in data[16]
data = await call("compare", [None, "world", 2, False])
assert set(data[9]) == {"baseline", "recipe"}
baseline = session.comparison["trials"]["baseline"]["token_ids"]
data = await call("export_recipe", [None])
recipe_download = data[11]
assert Path(session.recipe_file).is_file()
previous = session.recipe_file
await call("export_recipe", [None])
assert previous != session.recipe_file
# Use the real Gradio FileData returned by export for the import path.
await call("import_control", [None, recipe_download])
assert session.control == session.recipe
data = await call("compare", [None, "world", 2, True])
assert set(data[9]) == {"baseline", "recipe", "control"}
assert session.comparison["trials"]["control"]["token_ids"] == session.comparison["trials"]["recipe"]["token_ids"]
await call("export_experiment", [None, False])
report = json.loads(Path(session.experiment_file).read_text())
assert report["runtime_only"] and "prompt" not in report and "prompt_token_ids" not in report
assert report["decoding"]["chat_template_applied"]
assert "tokens" not in report["inspection"]
await call("export_experiment", [None, True])
report = json.loads(Path(session.experiment_file).read_text())
assert report["prompt"] == "world"
paired_run = session.comparison["run_id"]
data = await call("verify_reset", [None])
assert session.comparison["run_id"] == paired_run
assert session.comparison["verification"]["status"] == "passed"
assert session.comparison["trials"]["reset"]["token_ids"] == baseline
assert "Reset verified" in data[17]
assert not session.recipe.edits and session.control is None
assert data[13]["value"] == "all_tokens"
await call("reset", [None])
assert session.recipe.edits == () and session.comparison is None and session.control is None
assert not any(module._forward_pre_hooks or module._forward_hooks
for module in session.handle.model.modules())
await call("compare", [None, "world", 2, False])
assert session.comparison["trials"]["recipe"]["token_ids"] == baseline
await call("edit", [None, "add", 1, 0, 1, "clamp", 0., None, 4., True, "all_tokens"])
chosen = await _process_api(app.demo, events["select_edit"]["id"], [None, 1], state=state)
assert chosen["data"][:8] == [0, 1, "clamp", 0., None, 4., True, "replace"]
await call("edit", [None, "replace", 1, *chosen["data"][:7], "all_tokens"])
assert session.recipe.edits[0].lower is None
data = await call("delete", [None, 1, "all_tokens"])
assert data[20:28] == [0, 0, "add", 0., -1., 1., True, "add"]
assert other.handle is None and other.recipe is None
assert app.demo.local_url is None
finally:
app.demo.blocks[state_id].delete_callback(session)
app.demo.blocks[state_id].delete_callback(other)
app.demo.close()
assert session._closed and other._closed
asyncio.run(flow())
''', str(tmp_path)],
capture_output=True, text=True, timeout=120, check=False,
env={**os.environ, "GRADIO_ANALYTICS_ENABLED": "False", "HF_HUB_OFFLINE": "1",
"TRANSFORMERS_OFFLINE": "1", "MPLCONFIGDIR": str(tmp_path / "matplotlib")},
)
assert probe.returncode == 0, probe.stdout + probe.stderr
def test_ask_lab_real_study_through_gradio_and_isolated_sessions(tmp_path):
"""The conversation and study controls share real, reversible model evidence."""
pytest.importorskip("gradio", reason="install the locked spaces extra")
probe = subprocess.run(
[sys.executable, "-c", r'''
import asyncio
import json
from pathlib import Path
import sys
import torch
from transformers import LlamaConfig, LlamaForCausalLM
from gradio.state_holder import SessionState
import app
from tests.fixtures.tiny_offline_model import _fixture_tokenizer, FIXTURE_VOCAB
from tests.conditional.test_operator_ui import _process_api
torch.set_num_threads(1)
torch.manual_seed(5249)
directory = Path(sys.argv[1])
model_path = directory / "ask-model"
model = LlamaForCausalLM(LlamaConfig(
vocab_size=len(FIXTURE_VOCAB), hidden_size=16, intermediate_size=24,
num_hidden_layers=2, num_attention_heads=2, num_key_value_heads=2,
max_position_embeddings=1024, pad_token_id=0, eos_token_id=1, bos_token_id=1,
))
model.save_pretrained(model_path)
tokenizer = _fixture_tokenizer()
tokenizer.chat_template = "hello {{ messages[0]['content'] }} {% if add_generation_prompt %}answer{% endif %}"
tokenizer.save_pretrained(model_path)
del model
config = app.demo.get_config_file()
events = {dep["api_name"]: dep for dep in config["dependencies"]}
assert {"ask_lab_message", "ask_lab_save", "ask_lab_scan", "ask_lab_inspect",
"ask_lab_test", "ask_lab_reset", "ask_lab_export"} <= set(events)
state_id = events["neuron_load"]["inputs"][0]
state = SessionState(app.demo)
other_state = SessionState(app.demo)
session, other = state[state_id], other_state[state_id]
assert session is not other and session._lock is not other._lock
assert session.ask_lab is None and other.ask_lab is None
for name in ("ask_lab_message", "ask_lab_save", "ask_lab_scan", "ask_lab_inspect",
"ask_lab_test", "ask_lab_reset", "ask_lab_export"):
assert events[name]["inputs"][0] == state_id
assert len(events[name]["outputs"]) == 49
assert events[name]["outputs"][:41] == events["neuron_load"]["outputs"][:41]
async def call(name, values, browser_state=state):
result = await _process_api(app.demo, events[name]["id"], [None, *values], state=browser_state)
data = [value.model_dump() if hasattr(value, "model_dump") else value for value in result["data"]]
assert len(data) == 49
assert "failed:" not in data[1] and "Ask the Lab stopped:" not in data[1], data[1]
return data
def assert_redacted(value):
if isinstance(value, dict):
assert not ({"token_ids", "messages"} & set(value))
for key, item in value.items():
assert not (key in {"text", "goal", "probe", "control_probe", "contrast_a",
"contrast_b", "system_prompt", "prompt_a", "prompt_b"}
and isinstance(item, str))
assert_redacted(item)
elif isinstance(value, list):
for item in value:
assert_redacted(item)
async def flow():
download = None
try:
await call("neuron_load", [str(model_path), "cpu", "", True])
assert session.handle is not None and other.handle is None
weights = {key: value.clone() for key, value in session.handle.model.state_dict().items()}
prompts = ["hello world", "safe request", "hello answer", "test safe"]
data = await call("ask_lab_save", ["Private fixture research goal", *prompts])
assert data[42:47] == ["Private fixture research goal", *prompts]
assert session.ask_lab["stage"] == "draft" and session.ask_lab["evidence"] is None
await call("ask_lab_save", ["Another browser's study", "world hello", "request safe",
"answer hello", "safe test"], browser_state=other_state)
assert other.handle is None and other.ask_lab["plan"]["goal"] == "Another browser's study"
await call("ask_lab_scan", [])
assert session.ask_lab["stage"] == "scanned"
assert len(session.ask_lab["candidates"]) == 12
data = await call("ask_lab_inspect", [1, .02, 2])
selected = session.ask_lab["candidate"]
assert session.ask_lab["stage"] == "inspected" and selected["rank"] == 1
assert session.inspection["layer"] == selected["layer"]
assert session.inspection["neuron"] == selected["neuron"]
assert 'data-journey=' in data[32] and 'captured positions' in data[32]
assert data[37]["selected"] == "observe" and data[48] == 1
# Exercise conversational dispatch too: this text performs the actual study.
data = await call("ask_lab_message", ["test candidate 1", .02, 2])
assert session.ask_lab["stage"] == "tested"
receipt = session.ask_lab["evidence"]
assert receipt["run_id"] == session.comparison["run_id"]
assert "Reset verified" in data[41] and "Reset verified" in data[17]
assert receipt["plan"]["source"] == "edited"
for conditions, verification in (
(receipt["conditions"], receipt["verification"]),
(receipt["utility"]["conditions"], receipt["utility"]["verification"]),
):
assert set(conditions) == {"baseline", "candidate", "opposite", "random", "zero", "reset"}
assert verification["status"] == "passed"
assert verification["token_ids_exact"] and verification["first_logits_exact"]
assert verification["hook_registry_restored"]
assert all(trial["tokens"] <= 2 for trial in conditions.values())
assert conditions["zero"]["token_ids"] == conditions["baseline"]["token_ids"]
assert conditions["reset"]["token_ids"] == conditions["baseline"]["token_ids"]
assert any(message["role"] == "user" and message["text"] == "test candidate 1"
for message in session.ask_lab["messages"])
assert "do not prove" in session.ask_lab["messages"][-1]["text"]
assert session.recipe.edits == () and session.control is None
assert all(torch.equal(weights[key], value) for key, value in session.handle.model.state_dict().items())
assert not any(module._forward_pre_hooks or module._forward_hooks for module in session.handle.model.modules())
data = await call("ask_lab_export", [False])
download = Path(data[47]["path"])
assert download == Path(session.ask_lab["export_file"]) and download.is_file()
report = json.loads(download.read_text())
assert_redacted(report)
assert "Private fixture research goal" not in download.read_text()
assert report["run_id"] == receipt["run_id"] and report["verification"]["status"] == "passed"
assert "goal_sha256" in report and "probe_sha256" in report
assert other.ask_lab["evidence"] is None and other.ask_lab["export_file"] is None
data = await call("ask_lab_reset", [])
assert session.ask_lab["evidence"]["run_id"] == receipt["run_id"]
assert session.ask_lab["evidence"]["verification"]["status"] == "passed"
assert "Reset verified" in data[41]
assert not session.recipe.edits and session.control is None
assert other.handle is None and other.ask_lab["stage"] == "draft"
assert app.demo.local_url is None
finally:
app.demo.blocks[state_id].delete_callback(session)
app.demo.blocks[state_id].delete_callback(other)
app.demo.close()
assert session._closed and other._closed
if download is not None:
assert not download.exists()
asyncio.run(flow())
''', str(tmp_path)],
capture_output=True, text=True, timeout=120, check=False,
env={**os.environ, "GRADIO_ANALYTICS_ENABLED": "False", "HF_HUB_OFFLINE": "1",
"TRANSFORMERS_OFFLINE": "1", "GRADIO_TEMP_DIR": str(tmp_path / "gradio"),
"MPLCONFIGDIR": str(tmp_path / "matplotlib")},
)
assert probe.returncode == 0, probe.stdout + probe.stderr
_MODEL_LOAD_SETUP = r'''
import gc
from pathlib import Path
import shutil
import sys
import threading
from types import SimpleNamespace
import weakref
import app
from obliteratus import phase_admission
from obliteratus.gpu_lifecycle import MemoryUsage
from obliteratus.persistence_contracts import validate_reloadable_checkpoint
from transformers import LlamaConfig
root = Path(sys.argv[1])
events = []
model_refs = []
weight_calls = []
failure = [None]
class Lifecycle:
def loading(self, *args, **kwargs):
events.append(('loading',))
def resize(self, *args, **kwargs):
events.append(('resize',))
def ready(self, *args, **kwargs):
events.append(('ready',))
def release(self, *, reason):
events.append(('release', reason))
class Model:
def to(self, device):
return self
def checkpoint(name):
directory = root / name
directory.mkdir()
for filename in ('abliteration_metadata.json', 'config.json', 'tokenizer_config.json'):
(directory / filename).write_text('{}', encoding='utf-8')
(directory / 'model.safetensors').write_bytes(b'weights')
return str(validate_reloadable_checkpoint(directory))
def config_loader(*args, **kwargs):
if failure[0] == 'config':
raise RuntimeError('config failed')
return LlamaConfig(hidden_size=8, intermediate_size=16, num_hidden_layers=1,
num_attention_heads=2, num_key_value_heads=2, vocab_size=16,
torch_dtype='float32')
def tokenizer_loader(*args, **kwargs):
if failure[0] == 'tokenizer':
raise RuntimeError('tokenizer failed')
return SimpleNamespace(pad_token=None, eos_token='<eos>')
def model_loader(source, **kwargs):
weight_calls.append(str(source))
model = Model()
model_refs.append(weakref.ref(model))
if failure[0] == 'from_pretrained':
raise RuntimeError('weights allocated then failed')
return model
def admission(*args, **kwargs):
if failure[0] == 'admission':
raise phase_admission.PhaseAdmissionError('summon', 'cpu', 100, 1)
app._gpu_lifecycle = Lifecycle()
app.dev.get_device = lambda requested='auto': 'cpu'
app.dev.supports_device_map_auto = lambda: False
app.dev.supports_bitsandbytes = lambda device=None: False
app.dev.device_count = lambda: 0
app.dev.free_gpu_memory = gc.collect
app.dev.is_gpu_available = lambda: False
app.measure_torch_memory = lambda *args: MemoryUsage()
app.AutoConfig.from_pretrained = config_loader
app.AutoTokenizer.from_pretrained = tokenizer_loader
app.AutoModelForCausalLM.from_pretrained = model_loader
phase_admission.admit_model_load = admission
app._recover_sessions_from_disk = lambda: None
app._skip_session_load = 0
settings = {'requested_quantization': 'None', 'requested_dtype': 'FP32',
'quantization': None, 'dtype': 'float32'}
def session_a(output_dir):
app._state.update(model=Model(), tokenizer=object(), model_name='org/A', method='advanced',
status='ready', steering={'sentinel': 'A'}, output_dir=output_dir,
load_settings=dict(settings))
app._state.pop('load_token', None)
def session_b(output_dir, *, name='org/B'):
app._bench_configs.clear()
app._session_models.clear()
config = {'model_choice': name, 'model_id': name, 'method': 'advanced',
'output_dir': output_dir, 'load_settings': dict(settings)}
app._bench_configs['B'] = config
app._session_models['B'] = config
def switch():
return app.load_bench_into_chat('B', progress=lambda *args, **kwargs: None)
def obliterate_run():
config = app.demo.get_config_file()
dependency = next(dep for dep in config['dependencies']
if (app.obliterate_btn._id, 'click') in dep['targets'])
inputs = [app.demo.blocks[ident].value for ident in dependency['inputs']]
for control, value in ((app.model_dd, 'org/B'), (app.method_dd, 'Advanced'),
(app.quantization_dd, 'None'), (app.dtype_dd, 'FP32')):
inputs[dependency['inputs'].index(control._id)] = value
inputs[-1] = 'refusal'
return app.obliterate(*inputs, progress=lambda *args, **kwargs: None)
'''
def _run_model_load_probe(script, tmp_path, *arguments):
pytest.importorskip("gradio", reason="install the locked spaces extra")
result = subprocess.run(
[sys.executable, "-c", _MODEL_LOAD_SETUP + script, str(tmp_path), *arguments],
capture_output=True,
text=True,
timeout=120,
check=False,
env={**os.environ, "GRADIO_ANALYTICS_ENABLED": "False"},
)
assert result.returncode == 0, result.stdout + result.stderr
@pytest.mark.parametrize("failure_point", ["config", "admission", "from_pretrained", "tokenizer"])
@pytest.mark.parametrize("recoverable", [True, False])
def test_failed_session_switch_restores_metadata_before_error_yield(
tmp_path, failure_point, recoverable,
):
"""Failed attempts release their model while Gradio retains the error yield."""
_run_model_load_probe(r'''
active = checkpoint('A')
target = checkpoint('B')
session_a(active)
snapshot = {key: app._state[key] for key in
('model_name', 'method', 'load_settings', 'steering', 'output_dir')}
session_b(target)
if sys.argv[3] == 'False':
shutil.rmtree(active)
failure[0] = sys.argv[2]
generator = switch()
for update in generator:
if '**Error' in update[0]:
break
else:
raise AssertionError('failed switch did not yield an error')
assert app._state['model'] is None and app._state['tokenizer'] is None
if sys.argv[3] == 'True':
assert {key: app._state[key] for key in snapshot} == snapshot
assert app._state['status'] == 'ready'
else:
assert app._state['status'] == 'idle'
assert app._state['model_name'] is None and app._state['output_dir'] is None
assert events[-1] == ('release', 'load_failed'), events
assert events.count(('release', 'load_failed')) == 1, events
assert ('ready',) not in events, events
gc.collect()
assert all(reference() is None for reference in model_refs)
if failure[0] == 'from_pretrained':
assert model_refs, 'allocation failure did not construct the sentinel model'
if failure[0] == 'admission':
assert 'Memory admission rejected the load:' in update[0]
assert 'OBLITERATUS_PHASE_ADMISSION=off' in update[0]
generator.close()
failure[0] = None
events.clear()
updates = list(switch())
assert '**Loaded!' in updates[-1][0], updates
assert app._state['model_name'] == 'org/B'
assert str(app._state['output_dir']) == target
assert app._state['model'] is model_refs[-1]()
assert app._state['tokenizer'] is not None and app._state['status'] == 'ready'
assert events[-3:] == [('loading',), ('resize',), ('ready',)], events
''', tmp_path, failure_point, str(recoverable))
def test_same_model_selection_returns_and_different_checkpoint_loads(tmp_path):
"""Header construction must not reacquire the state lock under that lock."""
_run_model_load_probe(r'''
active = checkpoint('A')
target = checkpoint('B')
session_a(active)
app._state['steering'] = None
session_b(active, name='org/A')
results = []
errors = []
def select():
try:
results.extend(switch())
except BaseException as error:
errors.append(str(error))
worker = threading.Thread(target=select, daemon=True)
worker.start()
worker.join(timeout=5)
assert not worker.is_alive(), 'same-model selection deadlocked'
assert not errors, errors
assert 'Already loaded!' in results[-1][0], results
assert not weight_calls
session_b(target, name='org/A')
results.clear()
worker = threading.Thread(target=select, daemon=True)
worker.start()
worker.join(timeout=5)
assert not worker.is_alive(), 'different-checkpoint selection deadlocked'
assert not errors, errors
assert '**Loaded!' in results[-1][0], results
assert weight_calls == [target]
assert str(app._state['output_dir']) == target
''', tmp_path)
def test_chat_lazy_reload_failure_drops_stale_handle(tmp_path):
_run_model_load_probe(r'''
active = checkpoint('A')
session_a(active)
stale = weakref.ref(app._state['model'])
def stale_device(model):
raise RuntimeError('stale model')
app.dev.resolve_input_execution_device = stale_device
failure[0] = 'config'
generator = app.chat_respond('hello', [], '', 0.7, 0.9, 32, 1.0)
error_text = next(generator)
assert 'config failed' in error_text, error_text
assert app._state['model'] is None and app._state['tokenizer'] is None
assert app._state['status'] == 'ready'
assert app._state['output_dir'] == active
gc.collect()
assert stale() is None, 'stale handle retained at error yield'
assert events[-1] == ('release', 'load_failed'), events
assert ('ready',) not in events
generator.close()
''', tmp_path)
def test_switch_claim_refuses_concurrent_chat_ab_and_switch_loads(tmp_path):
_run_model_load_probe(r'''
active = checkpoint('A')
target = checkpoint('B')
session_a(active)
session_b(target)
owner = switch()
assert '**Loading' in next(owner)[0]
assert app._state['status'] == 'obliterating'
assert not weight_calls
chat = list(app.chat_respond('hello', [], '', 0.7, 0.9, 32, 1.0))
assert 'a model load is already in progress' in chat[-1].lower(), chat
ab = list(app.ab_chat_respond('hello', [], [], '', 0.7, 0.9, 32, 1.0))
assert 'a model load is already in progress' in str(ab[-1]).lower(), ab
other = list(switch())
assert 'a model load is already in progress' in str(other[-1]).lower(), other
assert not weight_calls, weight_calls
assert ('loading',) not in events, events
updates = list(owner)
assert '**Loaded!' in updates[-1][0], updates
assert weight_calls == [target]
assert app._state['status'] == 'ready' and app._state['model_name'] == 'org/B'
assert app._state.get('load_token') is None
''', tmp_path)
@pytest.mark.parametrize("abandon", [False, True])
def test_ab_original_generation_failure_drops_model_and_restores_slot(tmp_path, abandon):
"""Generation errors and closing the stream both release the temporary model."""
_run_model_load_probe(r'''
class Input:
def to(self, device):
return self
class Tokenizer:
pad_token = None
eos_token = '<eos>'
pad_token_id = None
eos_token_id = 1
def apply_chat_template(self, *args, **kwargs):
return 'hello'
def __call__(self, *args, **kwargs):
return {'input_ids': Input(), 'attention_mask': Input()}
class Streamer:
def __init__(self, *args, **kwargs):
self.finished = threading.Event()
self.worker = None
def end(self):
self.worker = threading.current_thread()
self.finished.set()
def __iter__(self):
assert self.finished.wait(timeout=2), 'generation did not terminate'
self.worker.join(timeout=2)
assert not self.worker.is_alive(), 'generation worker retained the model'
return iter(['partial'])
class FailingModel(Model):
def __init__(self, name):
self.name = name
self.moves = []
def to(self, device):
self.moves.append(device)
return self
def generate(self, **kwargs):
raise RuntimeError(self.name + ' generation failed')
def original_loader(*args, **kwargs):
model = FailingModel('original')
model_refs.append(weakref.ref(model))
return model
active = checkpoint('A')
session_a(active)
abliterated = FailingModel('abliterated')
tokenizer = Tokenizer()
app._state.update(model=abliterated, tokenizer=tokenizer, steering=None)
app.AutoModelForCausalLM.from_pretrained = original_loader
app.AutoTokenizer.from_pretrained = lambda *args, **kwargs: Tokenizer()
app.TextIteratorStreamer = Streamer
app.dev.resolve_input_execution_device = lambda model: SimpleNamespace(type='cpu')
app.dev.uses_accelerate_dispatch = lambda model: False
app.dev.empty_cache = lambda: None
app.resolve_secret = lambda name: None
generator = app.ab_chat_respond('hello', [], [], '', 0.7, 0.9, 32, 1.0)
abandon = sys.argv[2] == 'True'
for update in generator:
if update[2] == 'Streaming original response...':
assert app._state['status'] == 'obliterating'
assert app._state.get('load_token') is not None
assert model_refs[-1]() is not None
if abandon:
generator.close()
break
if update[2] == 'Done — compare the responses above.':
assert not abandon
left_error = update[0][-1]['content']
right_error = update[1][-1]['content']
assert isinstance(left_error, str) and isinstance(right_error, str)
assert 'RuntimeError: original generation failed' in left_error, update
assert 'RuntimeError: abliterated generation failed' in right_error, update
break
else:
raise AssertionError('A/B response did not reach the expected yield')
assert model_refs, 'original model was never loaded'
gc.collect()
assert model_refs[-1]() is None, 'original model retained after cleanup'
assert app._state['model'] is abliterated and app._state['tokenizer'] is tokenizer
assert app._state['status'] == 'ready'
assert app._state.get('load_token') is None
assert abliterated.moves[0] == 'cpu'
assert abliterated.moves[-1].type == 'cpu', abliterated.moves
generator.close()
''', tmp_path, str(abandon))
@pytest.mark.parametrize(
"outcome", ["abandoned_success", "abandoned_failure", "completed_failure", "terminal_error"],
)
def test_reabliteration_worker_owns_abandoned_attempt_cleanup(tmp_path, outcome):
"""Closing or terminally yielding leaves cleanup with the live worker."""
_run_model_load_probe(r'''
import obliteratus.abliterate
outcome = sys.argv[2]
released = threading.Event()
started = threading.Event()
workers = []
cleanup_threads = []
pipeline_refs = []
original_join = threading.Thread.join
join_failed = [False]
def join(worker, timeout=None):
if outcome == 'terminal_error' and not join_failed[0]:
join_failed[0] = True
assert started.wait(timeout=2), 'pipeline worker did not start'
raise RuntimeError('forced join failure while worker is alive')
return original_join(worker, timeout=timeout)
app.threading.Thread.join = join
class Pipeline:
def __init__(self, **kwargs):
self.handle = SimpleNamespace(model=Model(), tokenizer=object())
model_refs.append(weakref.ref(self.handle.model))
pipeline_refs.append(weakref.ref(self))
def run(self):
app._gpu_lifecycle.loading('fake-pipeline')
workers.append(threading.current_thread())
started.set()
assert released.wait(timeout=10), 'test did not release pipeline worker'
if outcome != 'abandoned_success':
raise RuntimeError('pipeline failed after constructing a model')
def cleanup_failed_run(self):
cleanup_threads.append(threading.current_thread())
self.handle.model = None
self.handle.tokenizer = None
obliteratus.abliterate.AbliterationPipeline = Pipeline
app.load_dataset_source = lambda source: (['harmful'], ['harmless'])
active = checkpoint('A')
session_a(active)
snapshot = {key: app._state[key] for key in
('model_name', 'method', 'load_settings', 'steering', 'output_dir')}
session_b(str(root / 'missing-B'))
app._bench_configs['B'].update(dataset_key='builtin', prompt_volume=1)
generator = switch()
assert 'Checkpoint not found' in next(generator)[0]
if outcome == 'completed_failure':
released.set()
error_update = next(generator)
assert '**Error' in error_update[0], error_update
assert 'pipeline failed after constructing a model' in error_update[0], error_update
else:
live_update = next(generator)
if outcome == 'terminal_error':
assert '**Error' in live_update[0] and 'forced join failure' in live_update[0], live_update
else:
assert 'Re-running abliteration' in live_update[0], live_update
assert started.wait(timeout=2)
assert workers[0].is_alive()
token = app._state['load_token']
if outcome != 'terminal_error':
generator.close()
assert app._state['status'] == 'obliterating'
assert app._state['load_token'] is token
assert not cleanup_threads, 'generator touched a pipeline owned by its worker'
loser = list(app.chat_respond('hello', [], '', 0.7, 0.9, 32, 1.0))
assert 'a model load is already in progress' in loser[-1].lower(), loser
assert not weight_calls
released.set()
assert workers, 'pipeline worker was never started'
workers[0].join(timeout=5)
assert not workers[0].is_alive(), 'pipeline worker did not terminate'
assert cleanup_threads == workers, 'cleanup was not performed once by the worker'
assert app._state['model'] is None and app._state['tokenizer'] is None
assert {key: app._state[key] for key in snapshot} == snapshot
assert app._state['status'] == 'ready' and app._state.get('load_token') is None
gc.collect()
assert all(reference() is None for reference in model_refs)
assert all(reference() is None for reference in pipeline_refs)
assert events[-1] == ('release', 'load_failed'), events
assert events.count(('release', 'load_failed')) == 1, events
assert ('ready',) not in events, events
generator.close()
''', tmp_path, outcome)
@pytest.mark.parametrize("failure_point", ["progress", "dataset", "initial_close"])
def test_session_switch_rolls_back_before_worker_starts(tmp_path, failure_point):
_run_model_load_probe(r'''
active = checkpoint('A')
target = checkpoint('B')
session_a(active)
snapshot = {key: app._state[key] for key in
('model_name', 'method', 'load_settings', 'steering', 'output_dir')}
failure_point = sys.argv[2]
session_b(str(root / 'missing-B') if failure_point == 'dataset' else target)
app._bench_configs['B'].update(dataset_key='builtin', prompt_volume=1)
def progress(*args, **kwargs):
raise RuntimeError('progress failed')
def dataset(source):
raise RuntimeError('dataset failed')
app.load_dataset_source = dataset
generator = app.load_bench_into_chat('B', progress=progress)
assert '**Loading' in next(generator)[0]
assert app._state['status'] == 'obliterating'
if failure_point == 'initial_close':
generator.close()
else:
error_update = next(generator)
assert '**Error' in error_update[0], error_update
assert failure_point + ' failed' in error_update[0], error_update
assert app._state['model'] is None and app._state['tokenizer'] is None
assert {key: app._state[key] for key in snapshot} == snapshot
assert app._state['status'] == 'ready' and app._state.get('load_token') is None
assert not weight_calls
assert ('loading',) not in events, events
generator.close()
''', tmp_path, failure_point)
@pytest.mark.parametrize("blocked_step", ["pipeline", "lifecycle"])
def test_reabliteration_progress_failure_holds_claim_through_cleanup(tmp_path, blocked_step):
_run_model_load_probe(r'''
import obliteratus.abliterate
entered, proceed = threading.Event(), threading.Event()
blocked_step = sys.argv[2]
updates, errors = [], []
def barrier(step):
assert app._state['status'] == 'obliterating'
assert app._claim_model_slot() is None
if step == blocked_step:
entered.set()
assert proceed.wait(timeout=10), 'cleanup barrier was not released'
class Pipeline:
def __init__(self, **kwargs):
self.handle = SimpleNamespace(model=Model(), tokenizer=object())
model_refs.append(weakref.ref(self.handle.model))
def run(self):
app._gpu_lifecycle.loading('fake-pipeline')
def cleanup_failed_run(self):
barrier('pipeline')
self.handle.model = self.handle.tokenizer = None
def release(*, reason):
if reason == 'load_failed':
barrier('lifecycle')
events.append(('release', reason))
def progress(value, **kwargs):
if value == 0.9:
raise RuntimeError('progress failed after pipeline finished')
obliteratus.abliterate.AbliterationPipeline = Pipeline
app._gpu_lifecycle.release = release
app.load_dataset_source = lambda source: (['harmful'], ['harmless'])
session_a(checkpoint('A'))
session_b(str(root / 'missing-B'))
app._bench_configs['B'].update(dataset_key='builtin', prompt_volume=1)
generator = app.load_bench_into_chat('B', progress=progress)
def drive():
try:
for update in generator:
if '**Error' in update[0]:
updates.append(update)
break
except BaseException as error:
errors.append(str(error))
driver = threading.Thread(target=drive, daemon=True)
driver.start()
try:
assert entered.wait(timeout=10), 'cleanup did not reach the barrier'
model, tokenizer, refusal = app._reload_active_chat_model()
assert model is tokenizer is None
assert 'a model load is already in progress' in refusal.lower(), refusal
assert not weight_calls
finally:
proceed.set()
driver.join(timeout=5)
assert not driver.is_alive() and not errors, errors
assert len(updates) == 1 and 'progress failed' in updates[0][0], updates
assert app._state['status'] == 'ready' and app._state.get('load_token') is None
assert all(reference() is None for reference in model_refs)
assert events[-1] == ('release', 'load_failed'), events
next_claim = app._claim_model_slot()
assert next_claim is not None
before = list(events)
generator.close()
assert app._state['load_token'] is next_claim.token and events == before
''', tmp_path, blocked_step)
@pytest.mark.parametrize("entry", ["chat", "ab"])
def test_failed_chat_promotion_drops_references_before_releasing_claim(tmp_path, entry):
_run_model_load_probe(r'''
class MoveFailure(Model):
def to(self, device):
raise RuntimeError('resident model move failed')
session_a(checkpoint('A'))
app._state['model'] = MoveFailure()
resident = weakref.ref(app._state['model'])
failure[0] = 'config'
app.dev.is_gpu_available = lambda: True
app.dev.resolve_input_execution_device = lambda model: SimpleNamespace(type='cpu')
app.dev.uses_accelerate_dispatch = lambda model: False
entered, proceed = threading.Event(), threading.Event()
first_release = [True]
updates, errors = [], []
def release(*, reason):
if reason == 'load_failed' and first_release[0]:
first_release[0] = False
assert resident() is None, 'caller retained the failed resident model'
assert app._claim_model_slot() is None
entered.set()
assert proceed.wait(timeout=10), 'cleanup barrier was not released'
events.append(('release', reason))
app._gpu_lifecycle.release = release
generator = (app.chat_respond('hello', [], '', 0.7, 0.9, 32, 1.0)
if sys.argv[2] == 'chat'
else app.ab_chat_respond('hello', [], [], '', 0.7, 0.9, 32, 1.0))
def drive():
try:
updates.append(next(generator))
except BaseException as error:
errors.append(str(error))
driver = threading.Thread(target=drive, daemon=True)
driver.start()
try:
assert entered.wait(timeout=10), 'promotion cleanup did not reach lifecycle release'
assert app._claim_model_slot() is None
assert app._state['status'] == 'obliterating'
assert not weight_calls
finally:
proceed.set()
driver.join(timeout=5)
assert not driver.is_alive() and not errors, errors
assert 'config failed' in str(updates), updates
assert resident() is None and app._state['status'] == 'ready'
assert app._state.get('load_token') is None
generator.close()
''', tmp_path, entry)
@pytest.mark.parametrize("failure_point", ["manifest", "admission", "pipeline", "timeout", "post_pipeline"])
def test_obliterate_terminal_error_releases_claim_before_error_yield(tmp_path, failure_point):
_run_model_load_probe(r'''
import obliteratus.abliterate
import obliteratus.run_archive
failure_point = sys.argv[2]
workers, cleanup_calls = [], []
class ActiveLifecycle(Lifecycle):
active = True
def loading(self, *args, **kwargs):
super().loading(*args, **kwargs)
if failure_point == 'admission':
raise app.AdmissionError('forced refusal')
def release(self, *, reason):
# References must be dropped before allocator/lifecycle cleanup.
assert app._state['model'] is app._state['tokenizer'] is None
self.active = False
super().release(reason=reason)
app._gpu_lifecycle = ActiveLifecycle()
class Archive:
def begin(self, *args, **kwargs):
if failure_point == 'manifest':
raise RuntimeError('forced manifest failure')
return 'fake-run'
def _run_dir(self, run_id):
return root / run_id
def append_log(self, *args, **kwargs):
pass
def record_dataset(self, *args, **kwargs):
pass
def fail(self, *args, **kwargs):
pass
def status(self, *args, **kwargs):
return {'status': 'running'}
class Pipeline:
def __init__(self, **kwargs):
self.handle = SimpleNamespace(model=Model(), tokenizer=object())
self.cancellation = kwargs['cancellation_event']
model_refs.append(weakref.ref(self.handle.model))
def run(self):
workers.append(threading.current_thread())
if failure_point == 'timeout':
assert self.cancellation.wait(timeout=10), 'worker was not cancelled'
elif failure_point == 'pipeline':
raise RuntimeError('forced pipeline failure')
# No checkpoint is written: validation forces a post-pipeline error.
def cleanup_failed_run(self):
assert app._state['status'] == 'obliterating'
assert app._claim_model_slot() is None
cleanup_calls.append(True)
self.handle.model = self.handle.tokenizer = None
obliteratus.run_archive.RunArchive = Archive
obliteratus.abliterate.AbliterationPipeline = Pipeline
app.load_dataset_source = lambda source: (['harmful'], ['harmless'])
app._pipeline_timeout_seconds = lambda: -1 if failure_point == 'timeout' else 3600
session_a(checkpoint('A'))
app._state['steering'] = None
resident = app._state['model'] if failure_point == 'manifest' else None
tokenizer = app._state['tokenizer'] if failure_point == 'manifest' else None
snapshot = {key: app._state[key] for key in
('model_name', 'method', 'load_settings', 'steering', 'output_dir', 'status')}
allocator_calls = []
def free_memory():
assert app._state['model'] is app._state['tokenizer'] is None
allocator_calls.append(True)
gc.collect()
app.dev.free_gpu_memory = free_memory
generator = obliterate_run()
for update in generator:
if (failure_point == 'manifest' and 'Run manifest failed' in update[0]
or failure_point == 'admission' and 'GPU admission failed' in update[0]
or failure_point == 'timeout' and '**Timed out' in update[0]
or failure_point in ('pipeline', 'post_pipeline') and '**Error (' in update[0]):
break
else:
raise AssertionError('terminal error was not yielded')
assert app._state['status'] != 'obliterating', update
assert app._state.get('load_token') is None
assert all(not worker.is_alive() for worker in workers)
gc.collect()
assert all(reference() is None for reference in model_refs)
if failure_point == 'manifest':
assert not workers and ('loading',) not in events
assert app._state['model'] is resident and app._state['tokenizer'] is tokenizer
assert {key: app._state[key] for key in snapshot} == snapshot
assert app._state['status'] == 'ready'
assert app._gpu_lifecycle.active and not events and not allocator_calls
elif failure_point == 'admission':
assert not workers and not cleanup_calls
assert app._state['model'] is app._state['tokenizer'] is None
assert allocator_calls == [True]
assert events == [('loading',), ('release', 'load_failed')], events
assert not app._gpu_lifecycle.active
else:
assert cleanup_calls == [True], cleanup_calls
reason = {'timeout': 'timeout', 'post_pipeline': 'post_load_failed'}.get(
failure_point, 'load_failed')
assert events[-1] == ('release', reason), events
next_claim = app._claim_model_slot()
assert next_claim is not None
before = list(events)
generator.close()
assert app._state['load_token'] is next_claim.token
assert events == before, 'retained generator released the next load lifecycle'
''', tmp_path, failure_point)
@pytest.mark.parametrize("completion", ["after_yield", "before_handoff"])
def test_obliterate_timeout_hands_off_cleanup_before_suspended_yield(tmp_path, completion):
_run_model_load_probe(r'''
import obliteratus.abliterate
import obliteratus.run_archive
completion = sys.argv[2]
started, proceed = threading.Event(), threading.Event()
workers, cleanup_threads, pipeline_refs = [], [], []
class Archive:
def begin(self, *args, **kwargs):
return 'fake-run'
def _run_dir(self, run_id):
return root / run_id
def append_log(self, *args, **kwargs):
pass
def record_dataset(self, *args, **kwargs):
pass
def fail(self, *args, **kwargs):
pass
class Pipeline:
def __init__(self, **kwargs):
self.handle = SimpleNamespace(model=Model(), tokenizer=object())
model_refs.append(weakref.ref(self.handle.model))
pipeline_refs.append(weakref.ref(self))
def run(self):
workers.append(threading.current_thread())
started.set()
assert proceed.wait(timeout=10), 'pipeline worker was not released'
def cleanup_failed_run(self):
assert app._claim_model_slot() is None
cleanup_threads.append(threading.current_thread())
self.handle.model = self.handle.tokenizer = None
def cancel(worker, cancellation, **kwargs):
assert started.wait(timeout=5), 'pipeline worker did not start'
assert cancellation.is_set()
if completion == 'before_handoff':
proceed.set()
worker.join(timeout=5)
assert not worker.is_alive()
# The grace result can become stale before the generator takes handoff_lock.
return False
obliteratus.run_archive.RunArchive = Archive
obliteratus.abliterate.AbliterationPipeline = Pipeline
app.load_dataset_source = lambda source: (['harmful'], ['harmless'])
app._pipeline_timeout_seconds = lambda: -1
app._cancel_pipeline_worker = cancel
session_a(checkpoint('A'))
snapshot = {key: app._state[key] for key in
('model_name', 'method', 'load_settings', 'steering', 'output_dir')}
generator = obliterate_run()
try:
for update in generator:
if '**Timed out' in update[0]:
break
else:
raise AssertionError('timeout response was not yielded')
if completion == 'after_yield':
assert workers[0].is_alive()
assert app._state['status'] == 'obliterating'
assert app._state.get('load_token') is not None
assert not cleanup_threads
# Leave the generator suspended at its terminal yield while the worker exits.
proceed.set()
workers[0].join(timeout=5)
assert not workers[0].is_alive()
assert app._state['model'] is app._state['tokenizer'] is None
assert {key: app._state[key] for key in snapshot} == snapshot
assert app._state['status'] == 'ready' and app._state.get('load_token') is None
expected_thread = workers[0] if completion == 'after_yield' else threading.current_thread()
assert cleanup_threads == [expected_thread], cleanup_threads
gc.collect()
assert all(reference() is None for reference in model_refs + pipeline_refs)
assert events.count(('release', 'timeout')) == 1, events
next_claim = app._claim_model_slot()
assert next_claim is not None
before = list(events)
finally:
proceed.set()
for worker in workers:
worker.join(timeout=5)
generator.close()
assert app._state['load_token'] is next_claim.token and events == before
''', tmp_path, completion)
@pytest.mark.parametrize("entry", ["obliterate", "bench"])
def test_thread_start_failure_releases_model_slot(tmp_path, entry):
_run_model_load_probe(r'''
import obliteratus.run_archive
class Archive:
def begin(self, *args, **kwargs):
return 'fake-run'
def _run_dir(self, run_id):
return root / run_id
def append_log(self, *args, **kwargs):
pass
app._gpu_lifecycle.loading('org/A')
session_a(checkpoint('A'))
snapshot = {key: app._state[key] for key in
('model_name', 'method', 'load_settings', 'steering', 'output_dir')}
starts = []
def fail_start(worker):
starts.append(worker)
raise RuntimeError('forced thread start failure')
app.threading.Thread.start = fail_start
app.load_dataset_source = lambda source: (['harmful'], ['harmless'])
if sys.argv[2] == 'bench':
session_b(str(root / 'missing-B'))
app._bench_configs['B'].update(dataset_key='builtin', prompt_volume=1)
generator = switch()
assert 'Checkpoint not found' in next(generator)[0]
update = next(generator)
assert '**Error' in update[0] and 'forced thread start failure' in update[0], update
reason = 'unload'
else:
obliteratus.run_archive.RunArchive = Archive
generator = obliterate_run()
try:
next(generator)
except RuntimeError as error:
assert str(error) == 'forced thread start failure'
else:
raise AssertionError('Thread.start failure did not propagate')
reason = 'load_failed'
assert len(starts) == 1 and not starts[0].is_alive()
assert app._state['model'] is app._state['tokenizer'] is None
assert {key: app._state[key] for key in snapshot} == snapshot
assert app._state['status'] == 'ready' and app._state.get('load_token') is None
assert events[-1] == ('release', reason), events
assert events.count(('release', reason)) == 1, events
assert not weight_calls and not model_refs
next_claim = app._claim_model_slot()
assert next_claim is not None
before = list(events)
generator.close()
assert app._state['load_token'] is next_claim.token and events == before
''', tmp_path, entry)
@pytest.mark.parametrize("failure_point", ["settings", "metadata"])
def test_session_switch_pre_unload_failure_preserves_resident(tmp_path, failure_point):
_run_model_load_probe(r'''
session_a(checkpoint('A'))
resident, tokenizer = app._state['model'], app._state['tokenizer']
snapshot = dict(app._state)
session_b(checkpoint('B'))
if sys.argv[2] == 'settings':
app._bench_configs['B']['load_settings'] = {'requested_dtype': 'invalid'}
else:
# This metadata is accessed inside the claimed transaction, before _clear_gpu.
del app._bench_configs['B']['model_choice']
def unexpected_cleanup():
raise AssertionError('early failure must not free the resident model')
app.dev.free_gpu_memory = unexpected_cleanup
generator = switch()
update = next(generator)
assert '**Error' in update[0], update
assert app._state == snapshot
assert app._state['model'] is resident and app._state['tokenizer'] is tokenizer
assert app._state['status'] == 'ready' and app._state.get('load_token') is None
assert not events and not weight_calls
generator.close()
assert app._state == snapshot and not events
''', tmp_path, failure_point)