Files
OBLITERATUS/tests/test_phase_cache_admission.py

116 lines
4.9 KiB
Python

"""Reusable CUDA cache is released before observing alternate phase boundaries."""
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
import torch
from obliteratus import phase_admission as admission
from obliteratus.informed_pipeline import InformedAbliterationPipeline
from obliteratus.phase_admission import observe_memory
from scripts import benchmark_sota_comparison as benchmark
from tests import test_phase_benchmark_admission
pytestmark = pytest.mark.cpu
MIB = 1024**2
benchmark_run = test_phase_benchmark_admission.benchmark_run
@pytest.fixture
def cached_allocator(monkeypatch):
state = {}
def prime():
state.update(free=64 * MIB, reserved=512 * MIB, allocated=256 * MIB)
def release():
state["free"] += state["reserved"] - state["allocated"]
state["reserved"] = state["allocated"]
prime()
empty = Mock(side_effect=release)
monkeypatch.setattr("obliteratus.device.gc.collect", lambda: None)
monkeypatch.setattr(torch.cuda, "is_available", lambda: True)
monkeypatch.setattr(torch.cuda, "empty_cache", empty)
monkeypatch.setattr(torch.cuda, "device_count", lambda: 1)
monkeypatch.setattr(torch.cuda, "reset_peak_memory_stats", lambda: None)
monkeypatch.setattr(torch.cuda, "max_memory_allocated", lambda: 0)
monkeypatch.setattr(torch.cuda, "mem_get_info", lambda device: (state["free"], 1024 * MIB))
monkeypatch.setattr(torch.cuda, "memory_reserved", lambda device: state["reserved"])
monkeypatch.setattr(torch.cuda, "memory_allocated", lambda device: state["allocated"])
monkeypatch.setattr("obliteratus.models.host_resources.available_host_bytes", lambda: 1024 * MIB)
monkeypatch.setattr("obliteratus.gpu_lifecycle.current_process_grant", lambda: None)
monkeypatch.setattr(admission, "observe_memory", observe_memory)
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "default")
return SimpleNamespace(state=state, prime=prime, empty=empty)
def admit_forward(stage, **options):
return admission.admit_phase(stage, admission.PhaseDemand(
stage, {"cuda:0": admission.DeviceDemand(activation_bytes=128 * MIB)},
(admission.DemandComponent("dense forward", "cuda:0", 128 * MIB, "calibrated"),),
), mode="default", **options)
@pytest.mark.parametrize("budget", ["driver", "utilization", "broker"])
def test_cache_release_precedes_default_observation(cached_allocator, budget):
options = ({"gpu_memory_utilization": 0.5} if budget == "utilization" else
{"broker_grant_bytes": 448 * MIB} if budget == "broker" else {})
report = admit_forward("probe", **options)
assert not report.warnings
assert cached_allocator.state == {
"free": 320 * MIB, "reserved": 256 * MIB, "allocated": 256 * MIB,
}
cached_allocator.empty.assert_called_once()
def test_informed_baseline_cache_does_not_reject_probe(cached_allocator):
# Run the real alternate driver and admission wrapper; stub only work and
# metadata estimation, so no model or CUDA tensor is ever materialized.
pipeline = object.__new__(InformedAbliterationPipeline)
pipeline.handle = SimpleNamespace(model=SimpleNamespace(config=SimpleNamespace()))
pipeline._report = SimpleNamespace(total_duration=0)
pipeline._admit_pipeline_phase_impl = lambda stage, **kw: admit_forward(stage)
pipeline._summon = lambda: None
pipeline._capture_stock_baseline = cached_allocator.prime
pipeline.use_kl_optimization = True
pipeline._capture_baseline_kl_logits = lambda: None
pipeline._validate_architecture_surgery_support = lambda: None
pipeline._probe = Mock()
pipeline._analyze = lambda: None
pipeline._distill_informed = lambda: None
pipeline._excise_informed = lambda: None
pipeline._verify_and_compensate = lambda: None
pipeline._rebirth_informed = lambda: "export"
output, _ = pipeline._run_informed_pipeline()
assert output == "export"
pipeline._probe.assert_called_once()
assert cached_allocator.state["reserved"] == 256 * MIB
def test_benchmark_baseline_cache_does_not_reject_probe(
benchmark_run, monkeypatch, request,
):
# Install the simulated allocator after the benchmark's ordinary CPU mocks.
cached_allocator = request.getfixturevalue("cached_allocator")
pipeline, trace, run = benchmark_run
original_capture = benchmark.collect_baseline_logits
original_admit = pipeline._admit_pipeline_phase
def capture(*args):
logits = original_capture(*args)
cached_allocator.prime()
return logits
def admit(stage, **options):
original_admit(stage, **options)
admit_forward(stage, gpu_memory_utilization=pipeline.gpu_memory_utilization)
monkeypatch.setattr(benchmark, "collect_baseline_logits", capture)
pipeline._admit_pipeline_phase = admit
result = run()
assert result.error is None
assert "work:probe" in trace
assert trace.index("work:baseline") < trace.index("work:probe")