mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
Tag each demand component calibrated or estimated. Default mode rejects only when calibrated demand exceeds free memory and warns once per phase and device about estimated or unmodelled overage. OBLITERATUS_PHASE_ADMISSION gains strict (reject on the total) next to off. A data-driven matrix test checks that default mode never rejects runs main can complete. Refs #245 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
180 lines
7.1 KiB
Python
180 lines
7.1 KiB
Python
"""Confidence classification for the shared metadata-only load entry point."""
|
|
|
|
import logging
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
from transformers import LlamaConfig, Qwen2Config
|
|
|
|
from obliteratus.models import _load_admission
|
|
from obliteratus.phase_admission import MemoryObservation, PhaseAdmissionError, admit_model_load
|
|
|
|
|
|
MIB = 1024 ** 2
|
|
|
|
|
|
def _config(family="llama"):
|
|
cls = LlamaConfig if family == "llama" else Qwen2Config
|
|
return cls(
|
|
hidden_size=64, intermediate_size=128, num_hidden_layers=2,
|
|
num_attention_heads=4, num_key_value_heads=2, vocab_size=128,
|
|
dtype="bfloat16", tie_word_embeddings=False,
|
|
)
|
|
|
|
|
|
def _roomy(device):
|
|
return MemoryObservation(device, 1024 * MIB, 1024 * MIB)
|
|
|
|
|
|
@pytest.mark.parametrize("family", ["llama", "qwen2"])
|
|
@pytest.mark.parametrize("placement", [
|
|
{"": "cuda:0"}, {"": "cuda:0", "model.layers.1": "cuda:1"},
|
|
])
|
|
def test_dense_cuda_load_only_resident_weights_are_calibrated(family, placement):
|
|
report = admit_model_load(
|
|
_config(family), device="cuda:0", device_map=placement, observer=_roomy,
|
|
dtype="bfloat16", safety_reserve_bytes=0,
|
|
)
|
|
calibrated = [item for item in report.components if item.confidence == "calibrated"]
|
|
assert calibrated
|
|
for item in calibrated:
|
|
assert item.name == "resident load weights"
|
|
assert item.device.startswith("cuda:")
|
|
assert item.bytes == report.demands[item.device].weight_bytes
|
|
assert all(
|
|
item.confidence == "estimated" for item in report.components
|
|
if item.name == "checkpoint streaming/conversion staging"
|
|
)
|
|
assert sum(item.bytes for item in report.components) == sum(
|
|
demand.additional_bytes for demand in report.demands.values()
|
|
)
|
|
assert not report.warnings
|
|
|
|
|
|
@pytest.mark.parametrize("placement", [
|
|
{"": "cpu"}, {"": "mps"},
|
|
{"": "cuda:0", "model.layers.1": "cpu"},
|
|
{"": "cuda:0", "model.layers.1": "disk"},
|
|
])
|
|
def test_uncalibrated_load_placements_warn_instead_of_rejecting(placement, caplog):
|
|
def tight(device):
|
|
return MemoryObservation(device, 1, 1024 * MIB)
|
|
|
|
report = admit_model_load(
|
|
_config(), device="cpu", device_map=placement, observer=tight,
|
|
safety_reserve_bytes=0,
|
|
)
|
|
assert all(item.confidence == "estimated" for item in report.components)
|
|
assert report.warnings
|
|
assert all("summon" in warning for warning in report.warnings)
|
|
assert any(record.levelno == logging.WARNING for record in caplog.records)
|
|
with pytest.raises(PhaseAdmissionError, match="shortfall"):
|
|
admit_model_load(
|
|
_config(), device="cpu", device_map=placement, observer=tight,
|
|
safety_reserve_bytes=0, mode="strict",
|
|
)
|
|
|
|
|
|
def test_dense_calibrated_weights_still_reject_before_loading():
|
|
config = _config()
|
|
baseline = admit_model_load(config, device="cuda:0", observer=_roomy)
|
|
required = baseline.demands["cuda:0"].weight_bytes
|
|
|
|
def tight(device):
|
|
return (
|
|
MemoryObservation(device, required - 1, 1024 * MIB)
|
|
if device == "cuda:0" else _roomy(device)
|
|
)
|
|
|
|
with pytest.raises(PhaseAdmissionError) as caught:
|
|
admit_model_load(config, device="cuda:0", observer=tight, safety_reserve_bytes=0)
|
|
assert caught.value.device == "cuda:0"
|
|
assert caught.value.required_bytes == required
|
|
assert caught.value.shortfall_bytes == 1
|
|
|
|
|
|
def test_dense_calibrated_load_survives_estimated_staging_shortfall():
|
|
config = _config()
|
|
baseline = admit_model_load(config, device="cuda:0", observer=_roomy)
|
|
resident = baseline.demands["cuda:0"].weight_bytes
|
|
|
|
def enough_for_weights(device):
|
|
free = resident if device == "cuda:0" else 1
|
|
return MemoryObservation(device, free, 1024 * MIB)
|
|
|
|
report = admit_model_load(
|
|
config, device="cuda:0", observer=enough_for_weights, safety_reserve_bytes=0,
|
|
)
|
|
assert len(report.warnings) == 2
|
|
assert all("checkpoint streaming/conversion staging" in warning for warning in report.warnings)
|
|
with pytest.raises(PhaseAdmissionError, match="shortfall"):
|
|
admit_model_load(
|
|
config, device="cuda:0", observer=enough_for_weights,
|
|
safety_reserve_bytes=0, mode="strict",
|
|
)
|
|
|
|
|
|
def test_source_mapping_failure_keeps_known_calibrated_resident_demand(monkeypatch):
|
|
config = _config()
|
|
baseline = admit_model_load(config, device="cuda:0", observer=_roomy)
|
|
resident = baseline.demands["cuda:0"].weight_bytes
|
|
monkeypatch.setattr(_load_admission, "_consumed_checkpoint_sources", Mock(
|
|
side_effect=ValueError("opaque source conversion"),
|
|
))
|
|
report = admit_model_load(config, device="cuda:0", observer=_roomy)
|
|
assert any(
|
|
item.bytes == resident and item.confidence == "calibrated" for item in report.components
|
|
)
|
|
assert any(
|
|
item.bytes is None and "opaque source conversion" in item.reason
|
|
for item in report.components
|
|
)
|
|
assert report.warnings
|
|
|
|
def busy(device):
|
|
return MemoryObservation(device, 0, 1024 * MIB) if device == "cuda:0" else _roomy(device)
|
|
|
|
with pytest.raises(PhaseAdmissionError) as caught:
|
|
admit_model_load(config, device="cuda:0", observer=busy)
|
|
assert caught.value.required_bytes == resident
|
|
with pytest.raises(PhaseAdmissionError, match="opaque source conversion"):
|
|
admit_model_load(config, device="cuda:0", observer=_roomy, mode="strict")
|
|
|
|
|
|
def test_load_mode_validation_runs_before_unknown_config_or_observation(monkeypatch):
|
|
observer = Mock(side_effect=AssertionError("must not observe"))
|
|
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "typo")
|
|
with pytest.raises(ValueError, match="default.*strict.*off"):
|
|
admit_model_load(object(), observer=observer)
|
|
observer.assert_not_called()
|
|
report = admit_model_load(object(), observer=observer, mode="off")
|
|
assert report.bypassed and report.mode == "off"
|
|
|
|
|
|
def test_quantized_weights_are_estimated_even_with_exact_parameter_metadata(monkeypatch):
|
|
config = _config()
|
|
config.quantization_config = {"quant_method": "gptq"}
|
|
quantizer = SimpleNamespace(
|
|
quantization_config=SimpleNamespace(quant_method="gptq", dequantize=False),
|
|
param_element_size=lambda model, name, tensor: tensor.element_size() / 4,
|
|
update_weight_conversions=lambda mappings: mappings,
|
|
)
|
|
monkeypatch.setattr(_load_admission, "_make_quantizer", lambda *args: quantizer)
|
|
report = admit_model_load(config, device="cuda:0", observer=_roomy)
|
|
assert report.demands["cuda:0"].weight_bytes > 0
|
|
assert all(item.confidence == "estimated" for item in report.components)
|
|
|
|
|
|
def test_unknown_load_placement_warns_for_each_known_destination(monkeypatch):
|
|
monkeypatch.setattr(_load_admission, "_make_meta_model", Mock(
|
|
side_effect=ValueError("custom metadata unavailable"),
|
|
))
|
|
report = admit_model_load(
|
|
_config(), device="cuda:0", device_map={"": "cuda:1", "model.layers.0": "disk"},
|
|
observer=_roomy,
|
|
)
|
|
assert {item.device for item in report.components} == {"cpu", "cuda:0", "cuda:1"}
|
|
assert all(item.bytes is None and item.confidence == "estimated" for item in report.components)
|
|
assert len(report.warnings) == 3
|