Files
OBLITERATUS/tests/test_ui_observatory.py

335 lines
14 KiB
Python

"""Actual-event provenance, bounded rendering and per-run observer isolation."""
from concurrent.futures import ThreadPoolExecutor
from html.parser import HTMLParser
import inspect
import json
from types import SimpleNamespace
import pytest
from obliteratus.ui_observatory import (
OBSERVATORY_JS, OperationJournal, current_observatory, observe_operation, render_observatory,
)
class Markup(HTMLParser):
def __init__(self, html):
super().__init__()
self.elements, self.words = [], []
self.feed(html)
def handle_starttag(self, tag, attrs):
self.elements.append((tag, dict(attrs)))
def handle_data(self, data):
self.words.append(data)
@property
def data(self):
return json.loads(next(attrs["data-observatory"] for _, attrs in self.elements
if "data-observatory" in attrs))
def event(stage="summon", status="running", message="Loading model", **details):
return SimpleNamespace(stage=stage, status=status, message=message, details=details)
@pytest.mark.cpu
def test_empty_observatory_is_off_and_makes_no_activation_or_progress_claim():
markup = Markup(render_observatory())
assert markup.data["status"] == "idle" and markup.data["phases"] == []
assert any(attrs.get("aria-expanded") == "false" for _, attrs in markup.elements)
assert any("hidden" in attrs for _, attrs in markup.elements)
assert "not activations" in " ".join(markup.words)
assert "Live adapter: Obliterate" in " ".join(markup.words)
assert "Awaiting a run" in " ".join(markup.words)
assert not any(attrs.get("role") == "progressbar" for _, attrs in markup.elements)
@pytest.mark.cpu
def test_real_reported_stages_and_details_are_preserved_without_invented_layers():
now = [10.]
journal = OperationJournal(clock=lambda: now[0])
journal.stage(event())
now[0] = 12.
journal.stage(event("summon", "done", "Loaded", duration=2.))
journal.stage(event("distill", "done", "Directions found", strong_layers=[2, 7], duration=.3))
journal.stage(event("excise", "running", "Modifying weights"))
snapshot = journal.snapshot()
assert snapshot["elapsed"] == 2.
assert [item["stage"] for item in snapshot["phases"]] == ["summon", "distill", "excise"]
assert snapshot["phases"][1]["details"]["strong_layers"] == [2, 7]
assert "layers" not in snapshot["phases"][2]
markup = Markup(render_observatory(snapshot))
assert "2 reported stages complete" in " ".join(markup.words)
assert "Apply surgery" in " ".join(markup.words)
snapshot["phases"][1]["details"]["strong_layers"].append(90)
assert journal.snapshot()["phases"][1]["details"]["strong_layers"] == [2, 7]
@pytest.mark.cpu
def test_journal_bounds_events_messages_details_and_nonfinite_numbers():
journal = OperationJournal("x" * 100)
for index in range(100):
journal.stage(event(f"phase-{index}", message="x" * 1000,
layers=list(range(1000)), nan=float("nan"), huge=10 ** 400,
tensor=object(), nested={"a": {"b": [1, 2]}}))
journal.log("y" * 1000)
snapshot = journal.snapshot()
assert len(snapshot["phases"]) == 16 and len(snapshot["logs"]) == 8
assert len(snapshot["operation"]) == 80 and len(snapshot["logs"][0]) == 240
detail = snapshot["phases"][0]["details"]
assert len(detail["layers"]) == 64
assert detail["nan"] is None and detail["huge"] is None
assert detail["tensor"] == "[unsupported detail]"
assert detail["nested"]["a"] == "[details truncated]"
assert len(snapshot["phases"][0]["message"]) == 320
json.dumps(snapshot, allow_nan=False)
before = journal.snapshot()
journal.stage(event("", "running"))
journal.stage(event("invented", "partial"))
assert journal.snapshot()["phases"] == before["phases"]
@pytest.mark.cpu
def test_all_pipeline_text_and_attributes_are_escaped():
hostile = '\"><img src=x onerror="bad"><script>bad</script>&'
journal = OperationJournal(hostile)
journal.stage(event(hostile, message=hostile, details={hostile: hostile}))
journal.log(hostile)
markup = Markup(render_observatory(journal.snapshot()))
assert hostile in " ".join(markup.words)
assert markup.data["operation"] == hostile
assert not any(tag in {"img", "script"} for tag, _ in markup.elements)
assert not any(key.startswith("on") for _, attrs in markup.elements for key in attrs)
@pytest.mark.cpu
@pytest.mark.parametrize("text,status", [
("**Error:** Invalid model", "error"),
("**Run manifest failed:** Not writable", "error"),
("**GPU admission failed (AdmissionError):** Capacity", "error"),
("**Timed out (TimeoutError, phase timeout):** Cancelled", "timed_out"),
("**Error (PipelineCancelledError, phase probe):** Cancelled", "cancelled"),
])
def test_failures_before_and_during_stages_remain_terminal(text, status):
journal = OperationJournal()
journal.output_status(text)
journal.finish()
assert journal.snapshot()["status"] == status
assert journal.snapshot()["message"] == text
@pytest.mark.cpu
def test_stage_errors_and_explicit_completion_do_not_become_guessed_progress():
journal = OperationJournal()
journal.stage(event(status="error", message="Load failed"))
journal.output_status(None)
journal.finish()
assert journal.snapshot()["status"] == "error"
success = OperationJournal()
success.stage(event(status="done"))
assert success.snapshot()["status"] == "running"
success.finish("done", "Pipeline completed")
success.finish()
assert success.snapshot()["status"] == "done"
plain = OperationJournal()
plain.finish()
assert plain.snapshot()["status"] == "ended"
@pytest.mark.cpu
def test_adapter_preserves_signature_outputs_and_owns_only_each_generator_advance():
closed = []
def original(name, progress=None):
journal = current_observatory()
try:
journal.stage(event(message=name))
yield (name, "log", 1, 2, 3, 4)
assert current_observatory() is journal
journal.finish("done", name)
yield ("complete", "log", 1, 2, 3, 4)
finally:
closed.append(name)
wrapped = observe_operation(original)
assert inspect.signature(wrapped) == inspect.signature(original)
generator = wrapped("run A")
first = next(generator)
assert first[:6] == ("run A", "log", 1, 2, 3, 4)
assert len(first) == 7 and Markup(first[6]).data["message"] == "run A"
assert current_observatory() is None
with ThreadPoolExecutor(max_workers=1) as executor:
second = executor.submit(next, generator).result()
assert Markup(second[6]).data["status"] == "done"
with pytest.raises(StopIteration):
next(generator)
assert closed == ["run A"] and current_observatory() is None
abandoned = wrapped("run B")
next(abandoned)
abandoned.close()
assert closed == ["run A", "run B"] and current_observatory() is None
@pytest.mark.cpu
def test_simultaneous_runs_cannot_cross_their_events_or_exception_cleanup():
def original(name):
journal = current_observatory()
journal.log(name)
yield (name,)
journal.stage(event(message=name))
yield (name,)
if name == "failure":
raise RuntimeError(name)
a, b = observe_operation(original)("A"), observe_operation(original)("B")
with ThreadPoolExecutor(max_workers=2) as executor:
first = [future.result() for future in [executor.submit(next, a), executor.submit(next, b)]]
second = [future.result() for future in [executor.submit(next, b), executor.submit(next, a)]]
assert [Markup(row[-1]).data["logs"] for row in first] == [["A"], ["B"]]
assert [Markup(row[-1]).data["message"] for row in second] == ["B", "A"]
a.close()
b.close()
with pytest.raises(RuntimeError, match="failure"):
list(observe_operation(original)("failure"))
assert current_observatory() is None
@pytest.mark.cpu
def test_client_motion_respects_off_hidden_paused_and_reduced_motion_states():
assert "state.open&&!state.paused&&!media.matches&&visible&&!document.hidden" in OBSERVATORY_JS
assert "cancelAnimationFrame" in OBSERVATORY_JS
assert "listeners.abort()" in OBSERVATORY_JS
assert "resizeObserver.disconnect()" in OBSERVATORY_JS
assert "fetch(" not in OBSERVATORY_JS and "setInterval" not in OBSERVATORY_JS
@pytest.mark.operator_ui
def test_real_app_callbacks_feed_stage_events_to_the_appended_panel(tmp_path):
from tests.conditional.test_operator_ui import _run_model_load_probe
_run_model_load_probe(r'''
import json
from html.parser import HTMLParser
import obliteratus.abliterate
import obliteratus.run_archive
class Archive:
def begin(self, *args, **kwargs): return 'observer-fixture'
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 mark_running(self, *args, **kwargs): pass
def fail(self, *args, **kwargs): pass
class Pipeline:
def __init__(self, **kwargs):
self.kwargs = kwargs
self.handle = SimpleNamespace(model=Model(), tokenizer=object())
def run(self):
from obliteratus.abliterate import StageResult
self.kwargs['on_stage'](StageResult('summon', 'running', 'Loading offline fixture'))
self.kwargs['on_stage'](StageResult('summon', 'done', 'Fixture loaded', details={'duration': .01}))
self.kwargs['on_stage'](StageResult('excise', 'running', 'Fixture surgery callback', details={'layer': 2}))
self.kwargs['on_log']('Reported matrix edit at layer 2')
raise RuntimeError('intentional fixture stop')
def cleanup_failed_run(self): self.handle.model = self.handle.tokenizer = None
obliteratus.run_archive.RunArchive = Archive
obliteratus.abliterate.AbliterationPipeline = Pipeline
app.load_dataset_source = lambda source: (['positive'], ['control'])
config = app.demo.get_config_file()
dependency = next(dep for dep in config['dependencies'] if (app.obliterate_btn._id, 'click') in dep['targets'])
assert dependency['outputs'][-1] == app.operation_observatory._id
assert dependency['outputs'][:6] == [app.status_md._id, app.log_box._id, app.chat_status._id,
app.session_model_dd._id, app.metrics_md._id, app.ab_session_model_dd._id]
endpoint = app.demo.get_api_info()['named_endpoints']['/obliterate']
assert len(endpoint['returns']) == 6
assert app.operation_observatory.skip_api is True
inputs = [app.demo.blocks[ident].value for ident in dependency['inputs']]
for control, value in ((app.model_dd, 'org/B'), (app.quantization_dd, 'None'), (app.dtype_dd, 'FP32')):
inputs[dependency['inputs'].index(control._id)] = value
updates = list(app._obliterate_observed(*inputs, progress=lambda *a, **k: None))
assert updates and all(len(update) == 7 for update in updates)
assert 'intentional fixture stop' in updates[-1][0]
class Payload(HTMLParser):
data = None
def handle_starttag(self, tag, attrs):
if 'data-observatory' in dict(attrs): self.data = json.loads(dict(attrs)['data-observatory'])
parsed = Payload()
parsed.feed(updates[-1][-1])
assert parsed.data['status'] == 'error'
assert [item['stage'] for item in parsed.data['phases']] == ['summon', 'excise']
assert parsed.data['phases'][1]['details'] == {'layer': 2}
assert 'Reported matrix edit at layer 2' in parsed.data['logs']
assert app._state['status'] != 'obliterating'
''', tmp_path)
@pytest.mark.operator_ui
def test_closing_the_observed_stream_requests_stop_before_worker_cleanup(tmp_path):
from tests.conditional.test_operator_ui import _run_model_load_probe
_run_model_load_probe(r'''
import obliteratus.abliterate
import obliteratus.run_archive
from obliteratus.ui_observatory import current_observatory
started, cancellation_seen, forward_finished = threading.Event(), threading.Event(), threading.Event()
workers, cleanup = [], []
class Archive:
def begin(self, *args, **kwargs): return 'observer-cancellation-fixture'
def _run_dir(self, run_id): return root / run_id
def append_log(self, *args, **kwargs): pass
def record_dataset(self, *args, **kwargs): pass
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())
started.set()
assert self.cancellation.wait(timeout=10), 'stream close did not request cancellation'
cancellation_seen.set()
assert forward_finished.wait(timeout=10), 'forward completion barrier not released'
raise RuntimeError('fixture worker accepted the stop request')
def cleanup_failed_run(self):
assert self.cancellation.is_set()
assert forward_finished.is_set(), 'model released while its forward was still live'
cleanup.append(True)
self.handle.model = self.handle.tokenizer = None
obliteratus.run_archive.RunArchive = Archive
obliteratus.abliterate.AbliterationPipeline = Pipeline
app.load_dataset_source = lambda source: (['positive'], ['control'])
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.quantization_dd, 'None'), (app.dtype_dd, 'FP32')):
inputs[dependency['inputs'].index(control._id)] = value
generator = app._obliterate_observed(*inputs, progress=lambda *a, **k: None)
try:
assert len(next(generator)) == 7
assert started.wait(timeout=5)
generator.close()
assert cancellation_seen.wait(timeout=5)
assert current_observatory() is None
assert not cleanup and app._claim_model_slot() is None
assert app._state['status'] == 'obliterating'
assert model_refs[-1]() is not None
finally:
forward_finished.set()
for worker in workers: worker.join(timeout=5)
generator.close()
assert all(not worker.is_alive() for worker in workers)
assert cleanup == [True]
assert app._state['status'] != 'obliterating'
assert app._state.get('load_token') is None
gc.collect()
assert model_refs[-1]() is None
''', tmp_path)