Files
OBLITERATUS/tests/test_phase_tiers.py

272 lines
11 KiB
Python

"""Confidence-tiered admission semantics independent of device hardware."""
from dataclasses import replace
import logging
from unittest.mock import Mock
import pytest
from obliteratus.phase_admission import (
DemandComponent,
DeviceDemand,
MemoryObservation,
PhaseAdmissionError,
PhaseDemand,
admit_phase,
resolve_admission_mode,
unknown_phase_demand,
)
@pytest.fixture(autouse=True)
def isolated_policy(monkeypatch):
monkeypatch.delenv("OBLITERATUS_PHASE_ADMISSION", raising=False)
monkeypatch.setattr("obliteratus.gpu_lifecycle.current_process_grant", lambda: None)
def demand(device="cuda:0", calibrated=10, estimated=20, unknown=False):
components = [
DemandComponent("dense forward", device, calibrated, "calibrated"),
DemandComponent("optional workspace", device, estimated),
]
if unknown:
components.append(DemandComponent("custom scan", device, None, reason="opaque callback"))
return PhaseDemand(
"probe", {device: DeviceDemand(activation_bytes=calibrated + estimated)}, tuple(components)
)
def memory(free, *, total=1000, reserved=0):
return lambda device: MemoryObservation(device, free, total, reserved)
def check(plan, free=100, **kwargs):
return admit_phase("probe", plan, observer=memory(free), safety_reserve_bytes=0, **kwargs)
def test_default_enforces_calibrated_sum_only_and_warns_once(caplog):
with caplog.at_level(logging.WARNING):
report = check(demand(unknown=True), free=15)
assert report.mode == "default"
assert len(report.warnings) == len(caplog.records) == 1
assert report.components[0].confidence == "calibrated"
message = report.warnings[0]
for expected in (
"phase 'probe'", "cuda:0", "15 bytes", "GiB", "optional workspace", "custom scan"
):
assert expected in message
assert "opaque callback" in message
def test_calibrated_shortfall_still_rejects_default():
with pytest.raises(PhaseAdmissionError) as caught:
check(demand(), free=9)
error = caught.value
assert (error.required_bytes, error.available_bytes, error.shortfall_bytes) == (10, 9, 1)
def test_strict_enforces_total_and_rejects_unknowns():
with pytest.raises(PhaseAdmissionError) as caught:
check(demand(), free=15, mode="strict")
assert caught.value.required_bytes == 30
with pytest.raises(PhaseAdmissionError, match="custom scan.*opaque callback.*ADMISSION=off"):
check(demand(unknown=True), mode="strict")
def test_fitting_calibrated_demand_silent_except_debug(caplog):
with caplog.at_level(logging.DEBUG, logger="obliteratus.phase_admission"):
report = check(demand(estimated=0))
assert report.warnings == ()
assert any("required_bytes=10" in item.message for item in caplog.records)
assert all(item.levelno == logging.DEBUG for item in caplog.records)
@pytest.mark.parametrize(
"state", [None, MemoryObservation("cuda:0", -1, 10), MemoryObservation("cuda:1", 10, 10)]
)
def test_missing_observation_rejects_calibrated_but_warns_estimated(state):
kwargs = {"observer": lambda _: state, "safety_reserve_bytes": 0}
with pytest.raises(PhaseAdmissionError, match="observation failed"):
admit_phase("probe", demand(), **kwargs)
report = admit_phase("probe", demand(calibrated=0), **kwargs)
assert len(report.warnings) == 1
assert "observation failed" in report.warnings[0]
with pytest.raises(PhaseAdmissionError, match="observation failed"):
admit_phase("probe", demand(calibrated=0), mode="strict", **kwargs)
def test_resident_only_device_does_not_need_observation_in_default():
report = admit_phase(
"distill", {"cuda:0": DeviceDemand(resident_bytes=100)}, observer=lambda _: None
)
assert report.warnings == ()
def test_unknown_load_option_warns_default_rejects_strict_and_bypasses_off():
from obliteratus.phase_admission import admit_model_load
report = admit_model_load(object(), invalid_geometry=True, observer=memory(100))
assert report.warnings and "invalid_geometry" in report.warnings[0]
with pytest.raises(PhaseAdmissionError, match="invalid_geometry"):
admit_model_load(object(), invalid_geometry=True, mode="strict")
skipped = admit_model_load(object(), invalid_geometry=True, mode="off")
assert skipped.bypassed and skipped.warnings
def test_model_off_report_includes_the_single_emitted_warning(caplog):
from obliteratus.phase_admission import admit_model_phase
observe = Mock(side_effect=AssertionError("off must not observe"))
report = admit_model_phase(object(), "probe", observer=observe, mode="off")
assert report.bypassed and len(report.warnings) == len(caplog.records) == 1
assert report.warnings[0] == caplog.records[0].message
observe.assert_not_called()
def test_raw_demands_and_unannotated_remainder_are_estimated():
raw = {"cpu": DeviceDemand(weight_bytes=10, staging_bytes=30)}
report = check(raw, free=5)
assert {c.name for c in report.components} == {"weight", "staging"}
assert {c.confidence for c in report.components} == {"estimated"}
report = check(raw, free=10, components=(DemandComponent("known", "cpu", 10, "calibrated"),))
assert report.components[-1] == DemandComponent("unclassified workspace", "cpu", 30)
@pytest.mark.parametrize("value", ["", "on", "yes", "false", "STRICT", 1, False])
def test_invalid_mode_is_clear_value_error(value, monkeypatch):
with pytest.raises(ValueError, match="allowed values are default, strict, fail-closed, off"):
check(demand(), mode=value)
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", str(value))
with pytest.raises(ValueError, match="OBLITERATUS_PHASE_ADMISSION"):
check(demand())
def test_explicit_mode_overrides_environment_and_off_bypasses_observer(monkeypatch, caplog):
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "strict")
assert check(demand(), free=15, mode="default").mode == "default"
read = Mock(side_effect=AssertionError("must not measure when disabled"))
with caplog.at_level(logging.WARNING):
report = admit_phase("probe", demand(), observer=read, mode="off")
assert report.bypassed and report.mode == "off"
assert "DISABLED" in report.warnings[0]
read.assert_not_called()
assert resolve_admission_mode("strict") == "strict"
def test_warning_deduplicates_free_ceiling_and_broker_shortfalls():
report = admit_phase(
"probe", demand(estimated=200), observer=memory(20, total=100),
gpu_memory_utilization=0.15, broker_grant_bytes=12, safety_reserve_bytes=0,
)
assert len(report.warnings) == 1
assert "198 bytes" in report.warnings[0]
assert "broker grant" in report.warnings[0]
def test_broker_enforces_only_calibrated_sum_across_devices():
first, second = demand(), demand("cuda:1")
combined = PhaseDemand(
"probe", {**first.devices, **second.devices}, first.components + second.components
)
assert len(check(combined, broker_grant_bytes=25).warnings) == 2
with pytest.raises(PhaseAdmissionError, match="broker grant") as caught:
check(combined, broker_grant_bytes=19)
assert caught.value.required_bytes == 20
with pytest.raises(PhaseAdmissionError, match="broker grant"):
check(combined, broker_grant_bytes=25, mode="strict")
def test_broker_observation_failure_warns_only_estimates(monkeypatch):
def missing():
raise RuntimeError("lock timed out")
monkeypatch.setattr("obliteratus.gpu_lifecycle.current_process_grant", missing)
with pytest.raises(PhaseAdmissionError, match="could not read broker grant"):
check(demand())
report = check(demand(calibrated=0))
assert len(report.warnings) == 1
assert "could not read broker grant" in report.warnings[0]
def test_calibrated_broker_check_requires_all_process_reservation_observations():
plan = demand()
plan = replace(plan, devices={**plan.devices, "cuda:1": DeviceDemand(resident_bytes=90)})
read = lambda name: None if name == "cuda:1" else MemoryObservation(name, 100, 100)
with pytest.raises(PhaseAdmissionError, match="Cannot observe broker reservations on cuda:1"):
admit_phase("probe", plan, observer=read, safety_reserve_bytes=0, broker_grant_bytes=20)
def test_default_broker_error_reports_only_calibrated_bytes():
with pytest.raises(PhaseAdmissionError) as caught:
admit_phase(
"probe", demand(), observer=memory(100, reserved=100),
safety_reserve_bytes=0, broker_grant_bytes=20,
)
assert caught.value.required_bytes == 10
assert caught.value.available_bytes == 0
def test_no_grant_and_exceeded_utilization_do_not_reject_estimates():
report = admit_phase(
"probe", demand(calibrated=0), observer=memory(100, total=100, reserved=80),
safety_reserve_bytes=0, gpu_memory_utilization=0.5, broker_grant_bytes=0,
)
assert len(report.warnings) == 1
assert report.available_bytes["cuda:0"] == 0
with pytest.raises(PhaseAdmissionError):
admit_phase(
"probe", demand(), observer=memory(100, total=100, reserved=80),
safety_reserve_bytes=0, gpu_memory_utilization=0.5, broker_grant_bytes=0,
)
def test_shared_mps_host_budget_enforces_calibrated_sum_and_warns_total():
mps, cpu = demand("mps", estimated=40), demand("cpu", estimated=40)
plan = PhaseDemand("probe", {**mps.devices, **cpu.devices}, mps.components + cpu.components)
report = check(plan, free=60)
assert len(report.warnings) == 1
assert "shared host" in report.warnings[0]
assert "40 bytes" in report.warnings[0]
with pytest.raises(PhaseAdmissionError, match="shared host"):
check(plan, free=15)
with pytest.raises(PhaseAdmissionError, match="shared host"):
check(plan, free=60, mode="strict")
def test_missing_host_observation_fails_closed_for_calibrated_mps():
read = lambda device: None if device == "cpu" else MemoryObservation(device, 100, 100)
with pytest.raises(PhaseAdmissionError, match="observation failed") as caught:
admit_phase("probe", demand("mps"), observer=read, safety_reserve_bytes=0)
assert caught.value.device == "cpu"
assert caught.value.required_bytes == 10
def test_unknown_phase_helper_warns_on_each_declared_device():
plan = unknown_phase_demand("probe", ("cpu", "cuda:0", "cpu"), "unknown cache")
assert len(plan.devices) == 2
assert len(check(plan).warnings) == 2
with pytest.raises(PhaseAdmissionError, match="unknown cache"):
check(plan, mode="strict")
@pytest.mark.parametrize(
"change", [{"bytes": -1}, {"bytes": True}, {"bytes": None}, {"confidence": "guess"}]
)
def test_invalid_components_cannot_be_promoted_to_calibrated(change):
with pytest.raises(ValueError):
replace(DemandComponent("dense", "cpu", 1, "calibrated"), **change)
def test_component_totals_and_devices_are_validated():
with pytest.raises(ValueError, match="exceed"):
check({"cpu": DeviceDemand(weight_bytes=1)}, components=[DemandComponent("bad", "cpu", 2)])
with pytest.raises(ValueError, match="device must have"):
check({"cpu": DeviceDemand()}, components=[DemandComponent("bad", "cuda:0", 1)])
def test_unknown_component_creates_missing_observation_lane():
component = DemandComponent("opaque transfer", "cuda:0", None)
report = check({}, components=[component])
assert report.demands == {"cuda:0": DeviceDemand()}
assert "cuda:0" in report.observations
assert len(report.warnings) == 1