mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
147 lines
6.0 KiB
Python
147 lines
6.0 KiB
Python
"""Checkpoint-metadata edges of load admission, offline and on the meta device."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
import torch
|
|
from transformers import GPT2Config
|
|
|
|
from obliteratus.models import _load_admission
|
|
from obliteratus.phase_admission import MemoryObservation, PhaseAdmissionError, admit_model_load
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def no_broker(monkeypatch):
|
|
monkeypatch.delenv("OBLITERATUS_PHASE_ADMISSION", raising=False)
|
|
monkeypatch.setattr("obliteratus.gpu_lifecycle.current_process_grant", lambda: None)
|
|
|
|
|
|
@pytest.fixture
|
|
def config():
|
|
return GPT2Config(n_embd=8, n_head=2, n_layer=1, n_positions=16, vocab_size=32,
|
|
bos_token_id=0, eos_token_id=1)
|
|
|
|
|
|
def roomy(device):
|
|
return MemoryObservation(device, 100 * 1024 ** 3, 100 * 1024 ** 3)
|
|
|
|
|
|
def write_safetensors(path, header):
|
|
"""Write a header-only safetensors file; payload bytes are never read."""
|
|
data = json.dumps(header).encode()
|
|
path.write_bytes(len(data).to_bytes(8, "little") + data)
|
|
|
|
|
|
def test_non_float_declared_dtype_falls_back_to_fp32_source_width():
|
|
tensors = [("w", torch.empty(2, 3, device="meta", dtype=torch.float16)),
|
|
("ids", torch.empty(4, device="meta", dtype=torch.int64))]
|
|
shards, eager, mounted = _load_admission._checkpoint_layout(
|
|
None, SimpleNamespace(dtype="int8"), tensors, None,
|
|
)
|
|
assert (eager, mounted) == (0, False)
|
|
fallback, = shards
|
|
assert fallback["w"].size_bytes == 6 * 4 and fallback["w"].dtype == "float32"
|
|
assert fallback["ids"].size_bytes == 4 * 8 and fallback["ids"].dtype == "int64"
|
|
|
|
|
|
def test_empty_checkpoint_index_is_rejected(tmp_path):
|
|
(tmp_path / "model.safetensors.index.json").write_text(json.dumps({"weight_map": {}}))
|
|
tensors = [("w", torch.empty(2, device="meta"))]
|
|
with pytest.raises(ValueError, match="invalid checkpoint index"):
|
|
_load_admission._checkpoint_layout(tmp_path, SimpleNamespace(), tensors, None)
|
|
|
|
|
|
def test_reversed_safetensors_offsets_are_rejected(tmp_path):
|
|
write_safetensors(tmp_path / "model.safetensors", {
|
|
"__metadata__": {"format": "pt"},
|
|
"w": {"dtype": "F32", "shape": [2], "data_offsets": [8, 0]},
|
|
})
|
|
tensors = [("w", torch.empty(2, device="meta"))]
|
|
with pytest.raises(ValueError, match="invalid safetensors metadata for 'w'"):
|
|
_load_admission._checkpoint_layout(tmp_path, SimpleNamespace(), tensors, None)
|
|
|
|
|
|
def test_zip_checkpoint_must_be_a_tensor_mapping(tmp_path):
|
|
torch.save([torch.zeros(2)], tmp_path / "pytorch_model.bin")
|
|
tensors = [("w", torch.empty(2, device="meta"))]
|
|
with pytest.raises(ValueError, match="unsupported checkpoint metadata"):
|
|
_load_admission._checkpoint_layout(tmp_path, SimpleNamespace(), tensors, None)
|
|
|
|
|
|
def test_dequant_without_checkpoint_uses_fallback_bound():
|
|
assert _load_admission._dequant_bytes(
|
|
None, 100, 7, token=None, revision=None, local_files_only=True,
|
|
) == 2 * 100 + 4 * 7
|
|
|
|
|
|
def test_dequant_headers_count_scale_siblings_once(tmp_path):
|
|
write_safetensors(tmp_path / "model.safetensors", {
|
|
"__metadata__": {"format": "pt"},
|
|
"layer.weight": {"dtype": "F8_E4M3", "shape": [4, 4], "data_offsets": [0, 16]},
|
|
"layer.weight_scale_inv": {"dtype": "F32", "shape": [1], "data_offsets": [16, 20]},
|
|
})
|
|
size = _load_admission._dequant_bytes(
|
|
tmp_path, 0, 0, token=None, revision=None, local_files_only=True,
|
|
)
|
|
# Shard: weight (16 + 64 expanded) + scale (4 + 4); plus the scale bytes and
|
|
# four times the largest expanded float tensor (64).
|
|
assert size == (16 + 64 + 4 + 4) + 4 + 4 * 64
|
|
|
|
|
|
def test_dequant_rejects_negative_header_ranges(tmp_path):
|
|
write_safetensors(tmp_path / "model.safetensors", {
|
|
"w": {"dtype": "F32", "shape": [2], "data_offsets": [8, 4]},
|
|
})
|
|
with pytest.raises(ValueError, match="invalid safetensors metadata for 'w'"):
|
|
_load_admission._dequant_bytes(
|
|
tmp_path, 0, 0, token=None, revision=None, local_files_only=True,
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("entry", [admit_model_load, _load_admission.admit_model_load])
|
|
def test_off_mode_bypasses_load_modelling(config, entry):
|
|
report = entry(config, device="cpu", mode="off", dtype="int8")
|
|
assert report.bypassed and report.demands == {}
|
|
|
|
|
|
def test_invalid_explicit_placement_still_reports_valid_devices(config):
|
|
report = admit_model_load(config, device="cpu", device_map={"": "bogus-device"},
|
|
observer=roomy, mode="default")
|
|
unknown = [c for c in report.components if c.bytes is None]
|
|
assert {c.device for c in unknown} == {"cpu"}
|
|
assert "cannot model load metadata/placement" in unknown[0].reason
|
|
with pytest.raises(PhaseAdmissionError, match="cannot model load metadata"):
|
|
admit_model_load(config, device="cpu", device_map={"": "bogus-device"},
|
|
observer=roomy, mode="strict")
|
|
|
|
|
|
def test_auto_device_map_bounds_memory_when_quantized_model_does_not_fit(config, monkeypatch):
|
|
from obliteratus import device as dev
|
|
from obliteratus.models import loader
|
|
|
|
calls = []
|
|
monkeypatch.setattr(dev, "supports_device_map_auto", lambda resolved: True)
|
|
monkeypatch.setattr(loader, "_available_gpu_memory_gb", lambda: 0.0)
|
|
monkeypatch.setattr(loader, "quantized_model_fits_gpu",
|
|
lambda estimate, quantization, free: calls.append(free) and False)
|
|
monkeypatch.setattr(loader, "_bounded_max_memory",
|
|
lambda utilization, **kwargs: calls.append(utilization) or {"cpu": "64GiB"})
|
|
import transformers.modeling_utils
|
|
|
|
planned = []
|
|
|
|
def planner(model, device_map, max_memory, hf_quantizer, **kwargs):
|
|
planned.append((device_map, max_memory))
|
|
return {"": "cpu"}
|
|
|
|
monkeypatch.setattr(transformers.modeling_utils, "_get_device_map", planner)
|
|
report = admit_model_load(config, device="auto", observer=roomy)
|
|
assert planned == [("auto", {"cpu": "64GiB"})]
|
|
assert calls == [0.0, None]
|
|
assert report.demands["cpu"].weight_bytes > 0
|