Files
OBLITERATUS/tests/test_phase_admission_contracts.py

355 lines
16 KiB
Python

"""Input contracts and fail-closed edges of phase admission, without accelerators.
Every accelerator observation is injected or monkeypatched; nothing touches a
real CUDA/MPS allocator.
"""
from __future__ import annotations
from types import SimpleNamespace
import pytest
import torch
from obliteratus import phase_admission as admission
from obliteratus.phase_admission import (
ActivationBatch,
DemandComponent,
DeviceDemand,
DistillationWorkload,
ForwardWorkload,
GIB,
MemoryObservation,
PhaseAdmissionError,
PhaseDemand,
admit_phase,
model_phase_demand,
observe_memory,
phase_memory_devices,
transformer_demands,
)
pytestmark = pytest.mark.cpu
@pytest.fixture(autouse=True)
def default_admission(monkeypatch):
monkeypatch.delenv("OBLITERATUS_PHASE_ADMISSION", raising=False)
monkeypatch.setattr("obliteratus.gpu_lifecycle.current_process_grant", lambda: None)
def ample(device):
return MemoryObservation(device, 100 * GIB, 100 * GIB)
# --- dataclass validation -------------------------------------------------
@pytest.mark.parametrize("build, message", [
(lambda: DeviceDemand(weight_bytes=-1), "nonnegative integer"),
(lambda: DeviceDemand(kv_bytes=1.5), "nonnegative integer"),
(lambda: DemandComponent("", "cpu", 1), "name must be nonempty"),
(lambda: DemandComponent("x", "", 1), "device must be nonempty"),
(lambda: DemandComponent("x", "cpu", 1, confidence="guess"), "calibrated or estimated"),
(lambda: DemandComponent("x", "cpu", None, confidence="calibrated"), "known byte counts"),
(lambda: DemandComponent("x", "cpu", -2), "nonnegative integer or None"),
(lambda: ForwardWorkload(1, 1, use_cache=1), "use_cache must be boolean"),
(lambda: ForwardWorkload(1, 1, attention_mask=1), "attention_mask must be boolean"),
(lambda: ForwardWorkload(1, 1, retained_hidden_tokens=-1), "nonnegative integer"),
(lambda: ForwardWorkload(0, 1), "positive integer"),
(lambda: ActivationBatch(0, 4), "positive integers"),
(lambda: ActivationBatch(1, 4, last_token_view=1), "last_token_view must be boolean"),
(lambda: ActivationBatch(1, 4, auxiliary_copies=-1), "nonnegative integer"),
])
def test_demand_metadata_rejects_invalid_values(build, message):
with pytest.raises(ValueError, match=message):
build()
def test_device_demand_additional_bytes_excludes_resident():
demand = DeviceDemand(resident_bytes=100, weight_bytes=3, kv_bytes=4, output_bytes=5)
assert demand.additional_bytes == 12
# --- admit_phase argument contracts ----------------------------------------
@pytest.mark.parametrize("kwargs, message", [
(dict(components=["not a component"]), "DemandComponent instances"),
(dict(components=[DemandComponent("x", "cuda:0", 1)]), "must have a DeviceDemand"),
(dict(gpu_memory_utilization=0), "within \\(0, 1\\]"),
(dict(gpu_memory_utilization=True), "within \\(0, 1\\]"),
(dict(gpu_memory_utilization=float("nan")), "within \\(0, 1\\]"),
(dict(safety_reserve_bytes=-1), "safety reserve"),
])
def test_admit_phase_rejects_invalid_arguments(kwargs, message):
with pytest.raises(ValueError, match=message):
admit_phase("probe", {"cpu": DeviceDemand(activation_bytes=1)}, observer=ample, **kwargs)
def test_raw_byte_counts_are_not_device_demands():
with pytest.raises(ValueError, match="DeviceDemand instances"):
admit_phase("probe", {"cpu": 1024}, observer=ample)
def test_phase_demand_must_match_phase_and_carry_its_own_components():
demand = PhaseDemand("probe", {"cpu": DeviceDemand()})
with pytest.raises(ValueError, match="does not match"):
admit_phase("distill", demand, observer=ample)
with pytest.raises(ValueError, match="not both"):
admit_phase("probe", demand, observer=ample, components=())
def test_invalid_mode_is_a_caller_error():
with pytest.raises(ValueError, match="allowed values are default, strict, fail-closed, off"):
admit_phase("probe", {}, mode="lenient")
def test_negative_broker_grant_is_rejected():
with pytest.raises(ValueError, match="broker grant"):
admit_phase("probe", {"cuda:0": DeviceDemand(activation_bytes=1)},
observer=ample, broker_grant_bytes=-1)
def test_repeated_reason_is_reported_once_per_device():
components = [
DemandComponent("a", "cpu", None, reason="same"),
DemandComponent("a", "cpu", None, reason="same"),
]
def failing(device):
raise RuntimeError("probe offline")
report = admit_phase("probe", {"cpu": DeviceDemand()}, observer=failing,
components=components)
message, = report.warnings
assert message.count("unmodelled components: a: same; a: same") == 1
assert "Memory observation failed: probe offline" in message
assert "plus unknown demand" in message
def test_unreadable_grant_on_idle_cuda_device_is_silent(monkeypatch):
def unreadable():
raise OSError("broker socket gone")
monkeypatch.setattr("obliteratus.gpu_lifecycle.current_process_grant", unreadable)
report = admit_phase("probe", {"cuda:0": DeviceDemand(), "cpu": DeviceDemand()},
observer=ample)
assert report.warnings == ()
with pytest.raises(PhaseAdmissionError, match="could not read broker grant"):
admit_phase("probe", {"cuda:0": DeviceDemand()}, observer=ample, mode="strict")
def test_default_observer_reservation_failure_warns_for_estimated_cuda(monkeypatch):
"""Without an injected observer, an unreadable allocator never invents headroom."""
freed = []
monkeypatch.setattr("obliteratus.device.free_gpu_memory", lambda: freed.append(True))
def no_cuda(*args, **kwargs):
raise RuntimeError("no CUDA driver")
monkeypatch.setattr(torch.cuda, "mem_get_info", no_cuda)
monkeypatch.setattr(torch.cuda, "device_count", no_cuda)
report = admit_phase("probe", {"cuda:0": DeviceDemand(activation_bytes=GIB)},
broker_grant_bytes=8 * GIB)
assert freed == [True]
assert report.available_bytes == {"cuda:0": 0}
message, = report.warnings
assert "Memory observation failed: no CUDA driver" in message
assert "Cannot observe broker reservations: no CUDA driver" in message
calibrated = [DemandComponent("weights", "cuda:0", GIB, "calibrated")]
with pytest.raises(PhaseAdmissionError, match="Memory observation failed"):
admit_phase("probe", {"cuda:0": DeviceDemand(activation_bytes=GIB)},
broker_grant_bytes=8 * GIB, components=calibrated)
def test_default_observer_reservation_failure_rejects_calibrated_cuda(monkeypatch):
monkeypatch.setattr("obliteratus.device.free_gpu_memory", lambda: None)
monkeypatch.setattr(torch.cuda, "mem_get_info", lambda target: (90 * GIB, 100 * GIB))
monkeypatch.setattr(torch.cuda, "memory_reserved", lambda target=None: 0)
def no_count():
raise RuntimeError("device enumeration failed")
monkeypatch.setattr(torch.cuda, "device_count", no_count)
calibrated = [DemandComponent("weights", "cuda:0", GIB, "calibrated")]
with pytest.raises(PhaseAdmissionError, match="device enumeration failed") as caught:
admit_phase("probe", {"cuda:0": DeviceDemand(activation_bytes=GIB)},
broker_grant_bytes=8 * GIB, components=calibrated)
assert caught.value.required_bytes == GIB
# --- observe_memory --------------------------------------------------------
@pytest.mark.parametrize("device", ["mps:1", "meta", "cuda"])
def test_unsupported_memory_observation_devices_fail(device):
with pytest.raises(RuntimeError, match="unsupported memory observation device"):
observe_memory(device)
# --- geometry helpers ------------------------------------------------------
def test_per_layer_geometry_must_match_layer_count():
config = SimpleNamespace(intermediate_size=[8, 16, 32])
with pytest.raises(ValueError, match="does not match decoder layer count"):
admission._layer_geometry(config, ("intermediate_size",), 0, 2)
assert admission._layer_geometry(config, ("intermediate_size",), 2, 3) == 32
@pytest.mark.parametrize("config, count, expected", [
(dict(model_type="mamba"), 2, ["mamba", "mamba"]),
(dict(model_type="rwkv"), 2, ["rwkv", "rwkv"]),
(dict(model_type="falcon_h1"), 2, ["hybrid", "hybrid"]),
(dict(model_type="recurrent_gemma", block_types=["recurrent", "attention"]), 3,
["recurrent", "attention", "recurrent"]),
(dict(model_type="nemotron_h", hybrid_override_pattern="M*-E"), 4,
["mamba", "full_attention", "mlp", "moe"]),
(dict(model_type="jamba", attn_layer_period=2, attn_layer_offset=1), 4,
["mamba", "full_attention", "mamba", "full_attention"]),
(dict(model_type="lfm2", full_attn_idxs=[1]), 3, ["conv", "full_attention", "conv"]),
(dict(model_type="llama4_text", attention_chunk_size=8), 2,
["chunked_attention", "chunked_attention"]),
])
def test_layer_kinds_follow_family_schedules(config, count, expected):
assert admission._layer_kinds(config, count) == expected
@pytest.mark.parametrize("config, message", [
(dict(model_type="llama", layer_types=["full_attention"]), "do not match realized"),
(dict(model_type="zamba2"), "requires explicit recurrent layer types"),
(dict(model_type="llama", layer_types=["full_attention", "teleport"]), "teleport"),
])
def test_layer_kinds_reject_inconsistent_schedules(config, message):
with pytest.raises(ValueError, match=message):
admission._layer_kinds(config, 2)
def _config(**overrides):
values = dict(hidden_size=16, num_hidden_layers=2, num_attention_heads=2,
num_key_value_heads=1, vocab_size=64, intermediate_size=32,
model_type="llama")
values.update(overrides)
return SimpleNamespace(**values)
def _demands(config, devices=("cpu", "cpu"), **overrides):
options = dict(batch_size=1, tokens=4, dtype_bytes=2, resident_bytes={},
largest_tensor_bytes={}, output_device="cpu")
options.update(overrides)
return transformer_demands(config, list(devices), **options)
def test_transformer_demands_baseline_is_finite_and_positive():
demands = _demands(_config())
assert set(demands) == {"cpu"}
assert demands["cpu"].kv_bytes > 0 and demands["cpu"].output_bytes > 0
@pytest.mark.parametrize("config, overrides, message", [
({}, dict(batch_size=0), "must be positive integers"),
({}, dict(cache_tokens=0), "cache/logit lengths"),
({}, dict(retained_hidden_tokens=-1), "retained_hidden_tokens"),
({}, dict(retained_logits_copies=-1), "retained_logits_copies"),
({}, dict(output_hidden_states="yes"), "output_hidden_states must be boolean"),
({}, dict(mamba_scan_backend="triton"), "selective-scan backend"),
({}, dict(devices=("cpu",)), "does not match configuration"),
({}, dict(compute_dtype=[torch.float32]), "compute dtypes do not match"),
(dict(num_local_experts=-1), {}, "invalid expert count"),
({}, dict(devices=("cpu", [])), "empty decoder layer placement"),
(dict(num_local_experts=2, num_experts_per_tok=3), {}, "top-k exceeds"),
(dict(num_local_experts=2, num_experts_per_tok=1, shared_expert_intermediate_size=-1), {},
"invalid shared expert width"),
(dict(num_key_value_heads=4), {}, "inconsistent attention/KV head geometry"),
(dict(sliding_window=-1), {}, "invalid attention cache window"),
(dict(model_type="falcon", new_decoder_architecture=True, num_kv_heads=3,
num_key_value_heads=None, num_attention_heads=4), {}, "must divide attention heads"),
(dict(model_type="falcon", new_decoder_architecture=False, multi_query=False,
num_kv_heads=1, num_key_value_heads=None), {}, "requires num_kv_heads=heads"),
(dict(model_type="deepseek_v2", kv_lora_rank=4, qk_rope_head_dim=2, v_head_dim=4,
qk_nope_head_dim=-1), {}, "qk_nope_head_dim"),
(dict(model_type="gemma3n_text", num_kv_shared_layers=2), {}, "shared KV layer count"),
(dict(model_type="gemma3n_text", num_kv_shared_layers=1,
layer_types=["sliding_attention", "full_attention"]), {}, "has no source KV layer"),
(dict(model_type="mamba", ssm_state_size=4, conv_kernel=4, mamba_d_ssm=-1), {},
"invalid Mamba intermediate width"),
(dict(model_type="mamba2", ssm_state_size=4, conv_kernel=4, num_heads=3, head_dim=4,
mamba_d_ssm=16), {}, "Mamba2 scan head geometry"),
(dict(model_type="mamba2", ssm_state_size=4, conv_kernel=4, num_heads=2, head_dim=4),
dict(mamba_scan_backend="mamba_ssm"), "recurrent scan backend"),
(dict(hidden_size_per_layer_input=-1), {}, "per-layer embedding width"),
])
def test_transformer_demands_reject_unmodellable_geometry(config, overrides, message):
devices = overrides.pop("devices", ("cpu", "cpu"))
with pytest.raises(ValueError, match=message):
_demands(_config(**config), devices, **overrides)
def test_gemma3n_shared_layers_reuse_source_cache():
shared = _demands(_config(model_type="gemma3n_text", num_kv_shared_layers=1,
layer_types=["full_attention", "full_attention"]))
unshared = _demands(_config(model_type="gemma3n_text", num_kv_shared_layers=0,
layer_types=["full_attention", "full_attention"]))
assert shared["cpu"].kv_bytes * 2 == unshared["cpu"].kv_bytes
def test_cuda_device_name_resolves_current_index(monkeypatch):
monkeypatch.setattr(torch.cuda, "current_device", lambda: 3)
assert admission._device_name("cuda") == "cuda:3"
assert admission._device_name("mps:0") == "mps"
with pytest.raises(ValueError, match="unmodelled execution device"):
admission._device_name("meta")
def test_quantized_logical_shape_must_be_positive():
tensor = torch.zeros(4, dtype=torch.uint8)
tensor.quant_state = SimpleNamespace(shape=(2, 0))
with pytest.raises(ValueError, match="invalid logical quantized tensor shape"):
admission._expanded_tensor_bytes(torch.nn.Module(), tensor)
# --- distillation workspace ------------------------------------------------
@pytest.mark.parametrize("options, message", [
("diff_means", "must be a DistillationWorkload"),
(DistillationWorkload(n_directions=0), "invalid distillation n_directions"),
(DistillationWorkload(sae_expansion=-1), "invalid distillation sae_expansion"),
(DistillationWorkload(method=7), "unmodelled distillation method None"),
(DistillationWorkload(sae_rows_by_layer=(16, -1)), "SAE training counts"),
])
def test_distillation_workspace_rejects_invalid_workloads(options, message):
with pytest.raises(ValueError, match=message):
admission._distillation_workspace(16, 2, 8, options)
def test_distillation_basic_method_matches_diff_means():
basic = admission._distillation_workspace(16, 2, 8, DistillationWorkload(method="basic"))
assert basic == admission._distillation_workspace(16, 2, 8, DistillationWorkload())
# --- model_phase_demand modes and device discovery -------------------------
def test_model_phase_demand_off_and_strict_unknown_skip_estimation():
handle = SimpleNamespace(model=None)
assert model_phase_demand(handle, "probe", mode="off") == PhaseDemand("probe", {})
unknown = [DemandComponent("prep", "cuda:1", None, reason="no bound for prep")]
with pytest.raises(PhaseAdmissionError, match="no bound for prep") as caught:
model_phase_demand(handle, "probe", unknown_components=unknown, mode="strict")
assert caught.value.device == "cuda:1"
def test_phase_memory_devices_ignores_unreadable_metadata():
class Model:
@property
def hf_device_map(self):
raise RuntimeError("device map unavailable")
def parameters(self):
raise RuntimeError("parameters unavailable")
assert phase_memory_devices(SimpleNamespace(model=Model())) == ("cpu",)