mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
178 lines
7.4 KiB
Python
178 lines
7.4 KiB
Python
"""Invocation scopes suppress logs, never repeated observations or rejections."""
|
|
|
|
from collections import Counter
|
|
import re
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
|
|
from obliteratus import phase_admission as admission
|
|
from tests.test_phase_benchmark_admission import benchmark_run as benchmark_run
|
|
from tests.test_phase_entry_admission import _lab_action, _public_evaluation
|
|
from tests.test_phase_informed_admission import pipeline as pipeline
|
|
|
|
|
|
DEVICES = ("cpu", "cuda:0")
|
|
|
|
|
|
def observe_boundaries(monkeypatch):
|
|
observations = []
|
|
|
|
def observe(device):
|
|
observations.append(device)
|
|
return admission.MemoryObservation(device, 2**30, 2**30)
|
|
|
|
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "default")
|
|
monkeypatch.setattr(admission, "phase_memory_devices", lambda _: DEVICES)
|
|
monkeypatch.setattr(admission, "observe_memory", observe)
|
|
monkeypatch.setattr("obliteratus.gpu_lifecycle.current_process_grant", lambda: None)
|
|
# These synthetic CUDA boundaries exercise warning scopes, not allocator
|
|
# cleanup. Real GC on every check dominates runtime under coverage.
|
|
monkeypatch.setattr("obliteratus.device.free_gpu_memory", lambda: None)
|
|
return observations
|
|
|
|
|
|
def warnings_by_phase_device(caplog):
|
|
return Counter(
|
|
match.groups()
|
|
for record in caplog.records
|
|
if record.name == admission.logger.name
|
|
and (match := re.search(r"phase '([^']+)' on ([^;]+);", record.getMessage()))
|
|
)
|
|
|
|
|
|
def assert_warning_counts(caplog, phases, invocations):
|
|
assert warnings_by_phase_device(caplog) == Counter({
|
|
(phase, device): invocations for phase in phases for device in DEVICES
|
|
})
|
|
|
|
|
|
def test_benchmark_warns_once_per_method_but_observes_each_boundary(
|
|
benchmark_run, monkeypatch, caplog,
|
|
):
|
|
_, _, run = benchmark_run
|
|
observations = observe_boundaries(monkeypatch)
|
|
for invocation in (1, 2):
|
|
assert run().error is None
|
|
assert_warning_counts(caplog, {"baseline", "verify"}, invocation)
|
|
assert Counter(observations) == Counter({device: 5 * invocation for device in DEVICES})
|
|
|
|
|
|
@pytest.mark.parametrize("action,phases,checks", [
|
|
("apply", {"validate", "verify", "excise"}, 5),
|
|
("evaluate", {"verify"}, 1),
|
|
("repair", {"excise", "verify"}, 2),
|
|
("export", {"rebirth"}, 1),
|
|
])
|
|
def test_lab_invocations_have_independent_scopes_and_fresh_observations(
|
|
action, phases, checks, monkeypatch, tmp_path, caplog,
|
|
):
|
|
session, _, callback = _lab_action(monkeypatch, tmp_path, action)
|
|
initial_run = session.run
|
|
observations = observe_boundaries(monkeypatch)
|
|
for invocation in (1, 2):
|
|
session.run = initial_run
|
|
if initial_run is not None:
|
|
initial_run.repair = None
|
|
callback()
|
|
assert_warning_counts(caplog, phases, invocation)
|
|
assert Counter(observations) == Counter({device: checks * invocation for device in DEVICES})
|
|
|
|
|
|
def test_concept_evaluation_repair_shares_scope_but_next_invocation_warns(
|
|
monkeypatch, tmp_path, caplog,
|
|
):
|
|
_, callback = _public_evaluation(monkeypatch, tmp_path)
|
|
observations = observe_boundaries(monkeypatch)
|
|
for invocation in (1, 2):
|
|
callback()
|
|
assert_warning_counts(caplog, {"validate", "excise", "verify", "rebirth"}, invocation)
|
|
assert Counter(observations) == Counter({device: 5 * invocation for device in DEVICES})
|
|
|
|
|
|
@pytest.mark.parametrize("entry", ["run_informed", "_excise_sparse", "_verify_and_compensate"])
|
|
def test_informed_nested_loops_share_the_enclosing_invocation_scope(
|
|
entry, pipeline, monkeypatch, caplog,
|
|
):
|
|
observations = observe_boundaries(monkeypatch)
|
|
checks = []
|
|
|
|
def admit(stage, **options):
|
|
checks.append(stage)
|
|
admission.admit_phase(stage, admission.unknown_phase_demand(
|
|
stage, DEVICES, "fixture workspace",
|
|
))
|
|
|
|
monkeypatch.setattr(pipeline, "_admit_pipeline_phase_impl", admit)
|
|
pipeline._insights.use_sparse_surgery = True
|
|
pipeline.refinement_passes = 2
|
|
pipeline._max_ouroboros_passes = 1
|
|
pipeline._quality_metrics = {"refusal_rate": 1.0, "kl_divergence": 0.1}
|
|
capture = Mock()
|
|
monkeypatch.setattr(pipeline, "_capture_baseline_kl_logits", capture)
|
|
for method in ("_summon", "_capture_stock_baseline", "_validate_architecture_surgery_support",
|
|
"_probe", "_analyze", "_distill_informed", "_distill_inner", "_verify",
|
|
"_rebirth_informed"):
|
|
monkeypatch.setattr(pipeline, method, lambda **kwargs: None)
|
|
monkeypatch.setattr(
|
|
"obliteratus.analysis.sparse_surgery.SparseDirectionSurgeon.apply_sparse_projection",
|
|
lambda self, weight, direction: weight,
|
|
)
|
|
for invocation in (1, 2):
|
|
getattr(pipeline, entry)()
|
|
assert Counter(checks)["excise"] >= 2 * invocation
|
|
assert_warning_counts(caplog, set(checks), invocation)
|
|
assert Counter(observations) == Counter({device: len(checks) for device in DEVICES})
|
|
assert capture.call_count == (invocation if entry == "run_informed" else 0)
|
|
|
|
|
|
def test_inherited_scope_restores_after_exception_and_does_not_hide_rejection(monkeypatch, caplog):
|
|
observations = observe_boundaries(monkeypatch)
|
|
demand = admission.unknown_phase_demand("verify", DEVICES, "fixture workspace")
|
|
|
|
@admission.admission_warning_scope(inherit=True)
|
|
def nested(mode="default"):
|
|
return admission.admit_phase("verify", demand, mode=mode)
|
|
|
|
with admission.admission_warning_scope():
|
|
assert len(nested().warnings) == 2
|
|
with pytest.raises(admission.PhaseAdmissionError):
|
|
nested(mode="strict")
|
|
assert len(nested().warnings) == 2
|
|
assert_warning_counts(caplog, {"verify"}, 1)
|
|
assert len(nested().warnings) == 2
|
|
assert_warning_counts(caplog, {"verify"}, 2)
|
|
# Strict may reject before observations; the three admitted checks still
|
|
# each observe both devices even though only two sets of warnings are logged.
|
|
assert all(observations.count(device) >= 3 for device in DEVICES)
|
|
|
|
|
|
def test_main_driver_shares_scope_with_refinement_and_resets_on_next_run(monkeypatch, caplog):
|
|
from tests.test_phase_memory_helpers import torch_thread_count
|
|
from tests.test_phase_optional_admission import make_pipeline
|
|
|
|
with torch_thread_count(1):
|
|
pipeline = make_pipeline(layers=1, true_iterative_refinement=True, refinement_passes=2)
|
|
observations = observe_boundaries(monkeypatch)
|
|
|
|
def admit(stage, **options):
|
|
admission.admit_phase(stage, admission.unknown_phase_demand(
|
|
stage, DEVICES, "fixture workspace",
|
|
))
|
|
|
|
def stages():
|
|
pipeline._admit_pipeline_phase("excise")
|
|
pipeline._excise()
|
|
|
|
monkeypatch.setattr(pipeline, "_admit_pipeline_phase_impl", admit)
|
|
monkeypatch.setattr(pipeline, "_run_pipeline_stages", stages)
|
|
# Keep the real EXCISE/refinement scope and admission boundaries while
|
|
# replacing numerical surgery, just like probe/distill/gate below.
|
|
monkeypatch.setattr(pipeline, "_project_out_advanced", Mock(return_value=1))
|
|
for method in ("_probe", "_distill_inner", "_validate_concept_gate"):
|
|
monkeypatch.setattr(pipeline, method, lambda: None)
|
|
for invocation in (1, 2):
|
|
pipeline._run_pipeline()
|
|
assert_warning_counts(caplog, {"probe", "distill", "validate", "excise"}, invocation)
|
|
assert Counter(observations) == Counter({device: 5 * invocation for device in DEVICES})
|