mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
288 lines
13 KiB
Python
288 lines
13 KiB
Python
"""Per-device placement and qualification policy, without accelerator allocation."""
|
|
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
|
|
from obliteratus import phase_admission as admission
|
|
from obliteratus.models import loader
|
|
|
|
|
|
GIB = 1024**3
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def isolated_policy(monkeypatch):
|
|
monkeypatch.delenv("OBLITERATUS_PHASE_ADMISSION", raising=False)
|
|
monkeypatch.delenv("OBLITERATUS_DEVICE_MEMORY_CEILINGS", raising=False)
|
|
monkeypatch.setattr("obliteratus.gpu_lifecycle.current_process_grant", lambda: None)
|
|
|
|
|
|
def _placement_observations(monkeypatch, states, host_free):
|
|
monkeypatch.setattr(loader.dev, "empty_cache", Mock())
|
|
monkeypatch.setattr(loader.dev, "device_count", lambda: len(states))
|
|
monkeypatch.setattr(loader.torch.cuda, "mem_get_info", lambda index: states[index])
|
|
monkeypatch.setattr(loader.torch.cuda, "memory_reserved", lambda _index: 0)
|
|
monkeypatch.setattr(
|
|
loader.torch.cuda, "get_device_properties",
|
|
Mock(side_effect=AssertionError("placement must measure free memory")),
|
|
)
|
|
monkeypatch.setattr(
|
|
"obliteratus.models.host_resources.available_host_bytes", lambda: host_free,
|
|
)
|
|
|
|
|
|
def test_busy_gpu_placement_cannot_borrow_another_devices_capacity(monkeypatch):
|
|
_placement_observations(monkeypatch, [(4 * GIB, 80 * GIB), (40 * GIB, 80 * GIB)], GIB)
|
|
assert loader._bounded_max_memory() == {
|
|
0: "2048MiB", 1: "34816MiB", "cpu": "870MiB",
|
|
}
|
|
|
|
|
|
def test_placement_utilization_is_fraction_of_each_devices_free_bytes(monkeypatch):
|
|
_placement_observations(monkeypatch, [(4 * GIB, 80 * GIB), (20 * GIB, 80 * GIB)], 0)
|
|
assert loader._bounded_max_memory(0.5) == {
|
|
0: "2048MiB", 1: "10240MiB", "cpu": "0MiB",
|
|
}
|
|
|
|
|
|
def test_placement_operator_ceilings_count_reservations_and_only_tighten(monkeypatch):
|
|
_placement_observations(monkeypatch, [(8 * GIB, 80 * GIB), (2 * GIB, 80 * GIB)], 2 * GIB)
|
|
monkeypatch.setattr(loader.torch.cuda, "memory_reserved", lambda _index: GIB)
|
|
assert loader._bounded_max_memory(
|
|
1.0, device_memory_ceiling_bytes={0: 3 * GIB, 1: 100 * GIB, "cpu": GIB},
|
|
) == {0: "2048MiB", 1: "2048MiB", "cpu": "1024MiB"}
|
|
|
|
|
|
@pytest.mark.parametrize("fraction, ceilings, gpu_budgets", [
|
|
(None, None, {0: "20889MiB", 1: "19148MiB"}),
|
|
(0.5, None, {0: "12288MiB", 1: "11264MiB"}),
|
|
(None, {0: 12 * GIB, 1: 12 * GIB}, {0: "12288MiB", 1: "10240MiB"}),
|
|
(0.5, {0: 12 * GIB, 1: 12 * GIB}, {0: "12288MiB", 1: "10240MiB"}),
|
|
])
|
|
def test_placement_reclaims_unused_cache_before_free_and_reserved_observations(monkeypatch, fraction, ceilings, gpu_budgets):
|
|
states = [(4 * GIB, 24 * GIB), (2 * GIB, 24 * GIB)]
|
|
reserved = [20 * GIB, 22 * GIB]
|
|
_placement_observations(monkeypatch, states, 0)
|
|
monkeypatch.setattr(loader.torch.cuda, "memory_reserved", lambda index: reserved[index])
|
|
|
|
def release():
|
|
states[:] = [(24 * GIB, 24 * GIB), (22 * GIB, 24 * GIB)]
|
|
reserved[:] = [0, 2 * GIB]
|
|
|
|
release_cache = Mock(side_effect=release)
|
|
monkeypatch.setattr(loader.dev, "empty_cache", release_cache)
|
|
assert loader._bounded_max_memory(fraction, device_memory_ceiling_bytes=ceilings) == dict(gpu_budgets, cpu="0MiB")
|
|
release_cache.assert_called_once_with()
|
|
|
|
|
|
@pytest.mark.parametrize("free", [0, GIB])
|
|
def test_default_reserve_never_creates_negative_placement_budgets(monkeypatch, free):
|
|
_placement_observations(monkeypatch, [(free, 80 * GIB)], 0)
|
|
assert loader._bounded_max_memory() == {0: "0MiB", "cpu": "0MiB"}
|
|
|
|
|
|
def test_placement_observation_failure_cannot_fall_back_to_total_capacity(monkeypatch):
|
|
_placement_observations(monkeypatch, [(GIB, 80 * GIB)], GIB)
|
|
monkeypatch.setattr(loader.torch.cuda, "mem_get_info", Mock(side_effect=RuntimeError("unavailable")))
|
|
with pytest.raises(RuntimeError, match="unavailable"):
|
|
loader._bounded_max_memory()
|
|
|
|
|
|
@pytest.mark.parametrize("fraction", [0, -1, True, float("nan"), float("inf"), "0.5"])
|
|
def test_invalid_placement_fraction_is_rejected_without_observation(monkeypatch, fraction):
|
|
observe = Mock(side_effect=AssertionError("must validate before querying CUDA"))
|
|
monkeypatch.setattr(loader.torch.cuda, "mem_get_info", observe)
|
|
with pytest.raises(ValueError, match="gpu_memory_utilization"):
|
|
loader._bounded_max_memory(fraction)
|
|
observe.assert_not_called()
|
|
|
|
|
|
def test_ceiling_environment_and_explicit_precedence(monkeypatch):
|
|
monkeypatch.setenv("OBLITERATUS_DEVICE_MEMORY_CEILINGS", '{"cuda:0": 123, "cpu": 456}')
|
|
assert admission.resolve_device_memory_ceilings() == {"cuda:0": 123, "cpu": 456}
|
|
assert admission.resolve_device_memory_ceilings({1: 789, "2": 0, "mps:0": 456}) == {
|
|
"cuda:1": 789, "cuda:2": 0, "mps": 456,
|
|
}
|
|
assert admission.resolve_device_memory_ceilings({}) == {}
|
|
|
|
|
|
@pytest.mark.parametrize("raw", ["", "null", "[]", '{"cuda:0": true}', '{"disk": 10}'])
|
|
def test_invalid_ceiling_environment_fails_explicitly(monkeypatch, raw):
|
|
monkeypatch.setenv("OBLITERATUS_DEVICE_MEMORY_CEILINGS", raw)
|
|
with pytest.raises(ValueError):
|
|
admission.resolve_device_memory_ceilings()
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"ceilings", [{True: 1}, {-1: 1}, {"cuda": 1}, {"cuda:-1": 1}, {"cpu": -1},
|
|
{"cpu": 1.0}, {0: 1, "cuda:0": 1}],
|
|
)
|
|
def test_invalid_explicit_ceiling_is_rejected(ceilings):
|
|
with pytest.raises(ValueError):
|
|
admission.resolve_device_memory_ceilings(ceilings)
|
|
|
|
|
|
def test_phase_ceiling_is_independent_per_device_and_counts_reservations(monkeypatch):
|
|
monkeypatch.setenv("OBLITERATUS_DEVICE_MEMORY_CEILINGS", '{"cuda:0": 30, "cuda:1": 90}')
|
|
demand = admission.PhaseDemand(
|
|
"probe", {device: admission.DeviceDemand(activation_bytes=10)
|
|
for device in ("cuda:0", "cuda:1")},
|
|
tuple(admission.DemandComponent("forward", device, 10, "calibrated")
|
|
for device in ("cuda:0", "cuda:1")),
|
|
)
|
|
report = admission.admit_phase(
|
|
"probe", demand, mode="fail-closed", safety_reserve_bytes=0,
|
|
observer=lambda device: admission.MemoryObservation(device, 100, 120, 20),
|
|
)
|
|
assert report.available_bytes == {"cuda:0": 10, "cuda:1": 70}
|
|
with pytest.raises(admission.PhaseAdmissionError) as caught:
|
|
admission.admit_phase(
|
|
"probe", demand, mode="fail-closed", safety_reserve_bytes=1,
|
|
observer=lambda device: admission.MemoryObservation(device, 100, 120, 20),
|
|
)
|
|
assert caught.value.device == "cuda:0"
|
|
assert caught.value.shortfall_bytes == 1
|
|
|
|
|
|
def test_cpu_ceiling_bounds_current_available_headroom():
|
|
demand = {"cpu": admission.DeviceDemand(staging_bytes=11)}
|
|
with pytest.raises(admission.PhaseAdmissionError) as caught:
|
|
admission.admit_phase(
|
|
"summon", demand, mode="strict", device_memory_ceiling_bytes={"cpu": 10},
|
|
observer=lambda device: admission.MemoryObservation(device, 100, 100),
|
|
safety_reserve_bytes=0,
|
|
)
|
|
assert caught.value.available_bytes == 10
|
|
|
|
|
|
@pytest.mark.parametrize("size", [None, 0, 10])
|
|
def test_fail_closed_rejects_estimates_before_memory_observation(size):
|
|
demand = admission.PhaseDemand(
|
|
"verify", {"cpu": admission.DeviceDemand(output_bytes=size or 0)},
|
|
(admission.DemandComponent("custom workspace", "cpu", size),),
|
|
)
|
|
observer = Mock(side_effect=AssertionError("uncertainty rejects before observation"))
|
|
with pytest.raises(admission.PhaseAdmissionError, match="requires calibrated.*custom workspace"):
|
|
admission.admit_phase("verify", demand, mode="fail-closed", observer=observer)
|
|
observer.assert_not_called()
|
|
|
|
|
|
def test_default_still_warns_and_strict_still_allows_fitting_estimates():
|
|
demand = {"cpu": admission.DeviceDemand(staging_bytes=10)}
|
|
observer = lambda device: admission.MemoryObservation(device, 100, 100)
|
|
assert not admission.admit_phase(
|
|
"summon", demand, mode="strict", observer=observer, safety_reserve_bytes=0,
|
|
).warnings
|
|
report = admission.admit_phase(
|
|
"summon", demand, observer=observer, safety_reserve_bytes=100,
|
|
)
|
|
assert len(report.warnings) == 1
|
|
assert "Proceeding in default mode" in report.warnings[0]
|
|
|
|
|
|
def test_model_phase_wrapper_passes_ceiling_without_changing_demand(monkeypatch):
|
|
demand = admission.PhaseDemand(
|
|
"verify", {"cpu": admission.DeviceDemand(output_bytes=10)},
|
|
(admission.DemandComponent("forward", "cpu", 10, "calibrated"),),
|
|
)
|
|
estimate = Mock(return_value=demand)
|
|
monkeypatch.setattr(admission, "model_phase_demand", estimate)
|
|
report = admission.admit_model_phase(
|
|
SimpleNamespace(), "verify", mode="fail-closed",
|
|
observer=lambda device: admission.MemoryObservation(device, GIB, GIB),
|
|
device_memory_ceiling_bytes={"cpu": admission.DEFAULT_RESERVE_BYTES + 10},
|
|
)
|
|
assert report.available_bytes == {"cpu": 10}
|
|
assert estimate.call_args.kwargs == {"mode": "fail-closed"}
|
|
|
|
|
|
def test_fail_closed_environment_and_explicit_escape_hatch(monkeypatch):
|
|
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "fail-closed")
|
|
with pytest.raises(admission.PhaseAdmissionError, match="requires calibrated"):
|
|
admission.admit_phase("verify", {"cpu": admission.DeviceDemand(output_bytes=1)})
|
|
observer = Mock(side_effect=AssertionError("explicit bypass must not observe"))
|
|
assert admission.admit_phase("verify", {}, observer=observer, mode="off").bypassed
|
|
observer.assert_not_called()
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ["strict", "fail-closed"])
|
|
def test_pipeline_modelling_failure_keeps_original_cause(mode, monkeypatch):
|
|
from obliteratus.abliterate import AbliterationPipeline
|
|
|
|
failure = LookupError("unknown phase workspace")
|
|
pipeline = SimpleNamespace(
|
|
_pipeline_phase_demand_options=Mock(side_effect=failure),
|
|
)
|
|
fallback = Mock(side_effect=AssertionError("strict policies must not construct fallback demand"))
|
|
monkeypatch.setattr(admission, "phase_memory_devices", fallback)
|
|
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", mode)
|
|
with pytest.raises(admission.PhaseAdmissionError, match="unknown phase workspace") as caught:
|
|
AbliterationPipeline._admit_pipeline_phase_impl(
|
|
pipeline, "verify", max_shard_size="1GB",
|
|
)
|
|
assert caught.value.__cause__ is failure
|
|
fallback.assert_not_called()
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ["strict", "fail-closed"])
|
|
def test_pipeline_gate_estimator_failure_is_fatal_for_strict_policies(mode):
|
|
from obliteratus.abliterate import AbliterationPipeline
|
|
|
|
failure = LookupError("unknown gate workspace")
|
|
pipeline = SimpleNamespace(_gate_admission_host_bytes=Mock(side_effect=failure))
|
|
with pytest.raises(LookupError, match="unknown gate workspace") as caught:
|
|
AbliterationPipeline._pipeline_phase_demand_options(
|
|
pipeline, "validate", max_shard_size="1GB", mode=mode, gate_only=True,
|
|
)
|
|
assert caught.value is failure
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ["strict", "fail-closed"])
|
|
def test_pipeline_unbounded_optional_work_is_rejected_for_strict_policies(mode, monkeypatch):
|
|
from obliteratus.bayesian_execution import GenericBayesianExecution
|
|
from tests.test_phase_optional_admission import make_pipeline
|
|
|
|
pipeline = make_pipeline(hidden=8, intermediate=16, layers=1, project_embeddings=True)
|
|
pipeline._bayesian_trials = 1
|
|
work = Mock()
|
|
monkeypatch.setattr(GenericBayesianExecution, "capture", work.capture)
|
|
monkeypatch.setattr(pipeline.handle.model, "forward", work.forward)
|
|
monkeypatch.setattr(admission, "observe_memory", work.observe)
|
|
reason = "generic Bayesian execution.*embedding projection"
|
|
with pytest.raises(ValueError, match=reason):
|
|
pipeline._pipeline_phase_demand_options(
|
|
"excise", max_shard_size="1GB", mode=mode,
|
|
)
|
|
|
|
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", mode)
|
|
with pytest.raises(admission.PhaseAdmissionError, match=reason) as caught:
|
|
pipeline._admit_pipeline_phase("excise")
|
|
assert isinstance(caught.value.__cause__, ValueError)
|
|
assert "embedding projection" in str(caught.value.__cause__)
|
|
assert getattr(pipeline, "_bayesian_execution", None) is None
|
|
assert work.mock_calls == []
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ["strict", "fail-closed"])
|
|
def test_pipeline_bounded_bayesian_work_has_snapshot_and_forward_demand(mode):
|
|
from tests.test_phase_optional_admission import make_pipeline
|
|
|
|
pipeline = make_pipeline(hidden=8, intermediate=16, layers=1)
|
|
pipeline._kl_eval_prompts = ["8"]
|
|
ordinary = pipeline._pipeline_phase_demand_options("excise", max_shard_size="1GB", mode=mode)
|
|
pipeline._bayesian_trials = 1
|
|
options = pipeline._pipeline_phase_demand_options("excise", max_shard_size="1GB", mode=mode)
|
|
model = pipeline.handle.model
|
|
layer = model.model.layers[0]
|
|
expected_snapshot = sum(parameter.numel() * parameter.element_size() for module in
|
|
(layer.self_attn, layer.mlp, model.lm_head)
|
|
for parameter in module.parameters())
|
|
assert pipeline._bayesian_execution.snapshot_host_bytes == expected_snapshot > 0
|
|
assert options["host_workspace_bytes"] - ordinary["host_workspace_bytes"] == (
|
|
expected_snapshot + 4 * 8 * model.config.vocab_size
|
|
)
|
|
assert len(options["forward_workloads"]) - len(ordinary["forward_workloads"]) == 17
|
|
assert pipeline._bayesian_execution._snapshots == []
|