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>
299 lines
13 KiB
Python
299 lines
13 KiB
Python
"""Metadata-only export bounds follow gathering and safetensors shard lifetimes."""
|
|
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from obliteratus.checkpoint_writer import (
|
|
ExportTensorMetadata,
|
|
WriterLimits,
|
|
_TensorSpec,
|
|
_admit,
|
|
estimate_export_memory,
|
|
estimate_model_export_memory,
|
|
estimated_peak_ram_bytes,
|
|
model_export_metadata,
|
|
)
|
|
|
|
|
|
MIB = 1024 ** 2
|
|
|
|
|
|
def record(name, size, device="cuda:0", **kwargs):
|
|
return ExportTensorMetadata(name, size, device, **kwargs)
|
|
|
|
|
|
def test_contiguous_cuda_export_only_materializes_current_host_shard():
|
|
estimate = estimate_export_memory([
|
|
record("a", 900_000_000), record("b", 800_000_000), record("c", 700_000_000),
|
|
], zero_copy_cpu_serialization=True)
|
|
assert estimate.device_workspace_bytes == {}
|
|
assert estimate.retained_host_bytes == 0
|
|
assert estimate.largest_shard_bytes == 1_700_000_000
|
|
assert estimate.estimated_peak_ram_bytes == 1_700_000_000 + MIB
|
|
|
|
|
|
def test_qwen_1_5b_bfloat16_export_fits_measured_host_headroom_without_cuda():
|
|
from transformers import AutoModelForCausalLM, Qwen2Config
|
|
from obliteratus.models._load_admission import _make_meta_model
|
|
from obliteratus.phase_admission import DeviceDemand, MemoryObservation, admit_phase
|
|
|
|
config = Qwen2Config(
|
|
hidden_size=1536, intermediate_size=8960, num_hidden_layers=28,
|
|
num_attention_heads=12, num_key_value_heads=2, vocab_size=151936,
|
|
tie_word_embeddings=True, dtype="bfloat16",
|
|
)
|
|
model = _make_meta_model(config, AutoModelForCausalLM, torch.bfloat16, False)
|
|
metadata = [
|
|
record(name, value.numel() * value.element_size(), storage_id=id(value))
|
|
for name, value in model.named_parameters(remove_duplicate=False)
|
|
]
|
|
estimate = estimate_export_memory(metadata, zero_copy_cpu_serialization=True)
|
|
assert estimate.device_workspace_bytes == {}
|
|
assert estimate.estimated_peak_ram_bytes <= 2_000_000_000 + MIB
|
|
|
|
def observer(device):
|
|
free = int(8.14 * 1024 ** 3) if device == "cpu" else 0
|
|
return MemoryObservation(device, free, max(free, 24 * 1024 ** 3))
|
|
|
|
report = admit_phase("export", {
|
|
"cpu": DeviceDemand(activation_bytes=estimate.estimated_peak_ram_bytes),
|
|
"cuda:0": DeviceDemand(),
|
|
}, observer=observer)
|
|
assert not report.bypassed
|
|
|
|
|
|
def test_contiguous_cpu_export_reuses_existing_storage():
|
|
estimate = estimate_export_memory([
|
|
record("a", 1_500_000_000, "cpu"), record("b", 1_500_000_000, "cpu"),
|
|
], zero_copy_cpu_serialization=True)
|
|
assert estimate.device_workspace_bytes == {}
|
|
assert estimate.estimated_peak_ram_bytes == MIB
|
|
|
|
|
|
def test_legacy_serializer_charges_bytes_and_one_transfer_tensor():
|
|
estimate = estimate_export_memory([
|
|
record("a", 900_000_000), record("b", 800_000_000), record("c", 700_000_000),
|
|
], zero_copy_cpu_serialization=False)
|
|
assert estimate.shard_host_bytes == 1_700_000_000
|
|
assert estimate.tensor_host_bytes == 900_000_000
|
|
assert estimate.estimated_peak_ram_bytes == 2_600_000_000 + MIB
|
|
|
|
|
|
def test_tied_aliases_are_not_charged_or_sharded_twice():
|
|
estimate = estimate_export_memory([
|
|
record("embedding", 400, storage_id="shared"),
|
|
record("layer", 900, storage_id="layer"),
|
|
record("head", 400, storage_id="shared"),
|
|
], zero_copy_cpu_serialization=True)
|
|
assert estimate.largest_shard_bytes == 1300
|
|
assert estimate.estimated_peak_ram_bytes == 1300 + MIB
|
|
|
|
|
|
def test_noncontiguous_tensors_reserve_only_per_shard_device_copies():
|
|
estimate = estimate_export_memory([
|
|
record("a", 400, contiguous=False), record("b", 300, contiguous=False),
|
|
record("c", 200, "cuda:1", contiguous=True),
|
|
], zero_copy_cpu_serialization=True)
|
|
assert estimate.device_workspace_bytes == {"cuda:0": 700}
|
|
assert estimate.estimated_peak_ram_bytes == 900 + MIB
|
|
|
|
|
|
def test_offload_gather_counts_new_host_storage_once_and_excludes_cpu_backing():
|
|
estimate = estimate_export_memory([
|
|
record("resident_cpu", 100, "cpu"), record("migrated_cuda", 200),
|
|
record("disk", 300, "meta"),
|
|
record("cpu_backing", 400, "meta", host_storage_resident=True),
|
|
], zero_copy_cpu_serialization=True)
|
|
assert estimate.retained_host_bytes == 500
|
|
assert estimate.tensor_host_bytes == 300
|
|
assert estimate.shard_host_bytes == 0
|
|
assert estimate.device_workspace_bytes == {}
|
|
assert estimate.estimated_peak_ram_bytes == 800 + MIB
|
|
|
|
|
|
def test_mps_offloaded_gather_includes_accelerate_restore_workspace():
|
|
estimate = estimate_export_memory([
|
|
record("mps", 200, "mps"), record("disk", 300, "meta"),
|
|
], zero_copy_cpu_serialization=True)
|
|
assert estimate.device_workspace_bytes == {"mps": 200}
|
|
assert estimate.retained_host_bytes == 500
|
|
|
|
|
|
def test_model_metadata_does_not_call_state_dict_and_ignores_nonpersistent_buffers(monkeypatch):
|
|
model = torch.nn.Module()
|
|
model.weight = torch.nn.Parameter(torch.ones(8))
|
|
model.alias = model.weight
|
|
model.register_buffer("persistent", torch.zeros(2))
|
|
model.register_buffer("temporary", torch.zeros(100), persistent=False)
|
|
forbidden = Mock(side_effect=AssertionError("must not gather or materialize tensors"))
|
|
monkeypatch.setattr(model, "state_dict", forbidden)
|
|
metadata = model_export_metadata(model)
|
|
assert {item.name for item in metadata} == {"weight", "alias", "persistent"}
|
|
estimate = estimate_model_export_memory(model)
|
|
assert estimate.largest_shard_bytes == 40
|
|
assert estimate.estimated_peak_ram_bytes == MIB
|
|
forbidden.assert_not_called()
|
|
|
|
|
|
def test_disjoint_views_include_transformers_pre_shard_clones():
|
|
base = torch.arange(16, dtype=torch.float32)
|
|
model = torch.nn.Module()
|
|
model.first = torch.nn.Parameter(base[:8])
|
|
model.second = torch.nn.Parameter(base[8:])
|
|
estimate = estimate_model_export_memory(model)
|
|
assert estimate.retained_host_bytes == 64
|
|
assert estimate.estimated_peak_ram_bytes == 64 + MIB
|
|
|
|
|
|
def test_cpu_offload_backing_is_reused_by_actual_pipeline_gather(monkeypatch):
|
|
from accelerate import cpu_offload
|
|
from obliteratus.abliterate import AbliterationPipeline
|
|
|
|
model = torch.nn.Sequential(torch.nn.Linear(4, 4), torch.nn.Linear(4, 2))
|
|
cpu_offload(model, execution_device="cpu")
|
|
assert all(value.is_meta for value in model.parameters())
|
|
original_state_dict = model.state_dict
|
|
forbidden = Mock(side_effect=AssertionError("estimation must not gather"))
|
|
monkeypatch.setattr(model, "state_dict", forbidden)
|
|
pointers = {item.storage_id[0][1] for item in model_export_metadata(model)}
|
|
estimate = estimate_model_export_memory(model)
|
|
assert estimate.retained_host_bytes == 0
|
|
assert estimate.estimated_peak_ram_bytes == MIB
|
|
forbidden.assert_not_called()
|
|
monkeypatch.setattr(model, "state_dict", original_state_dict)
|
|
pipeline = object.__new__(AbliterationPipeline)
|
|
pipeline.handle = SimpleNamespace(model=model)
|
|
pipeline.log = Mock()
|
|
gathered = pipeline._gather_state_dict()
|
|
assert gathered
|
|
assert all(value.device.type == "cpu" for value in gathered.values())
|
|
assert {value.untyped_storage().data_ptr() for value in gathered.values()} <= pointers
|
|
|
|
|
|
def test_disk_offload_metadata_never_reads_lazy_weights(monkeypatch):
|
|
from accelerate.hooks import AlignDevicesHook
|
|
from accelerate.utils.offload import OffloadedWeightsLoader
|
|
|
|
model = torch.nn.Sequential(torch.nn.Linear(4, 4, bias=False, device="meta"))
|
|
backing = OffloadedWeightsLoader(index={"weight": {"dtype": "float32", "shape": [4, 4]}})
|
|
model[0]._hf_hook = AlignDevicesHook(
|
|
execution_device="cpu", offload=True, weights_map=backing,
|
|
)
|
|
forbidden = Mock(side_effect=AssertionError("must not read disk weights"))
|
|
monkeypatch.setattr(OffloadedWeightsLoader, "__getitem__", forbidden)
|
|
estimate = estimate_model_export_memory(model)
|
|
assert estimate.retained_host_bytes == 64
|
|
assert estimate.estimated_peak_ram_bytes == 128 + MIB
|
|
forbidden.assert_not_called()
|
|
|
|
|
|
def test_cpu_offload_uses_backing_strides_for_serialization_copy():
|
|
from accelerate import cpu_offload
|
|
|
|
model = torch.nn.Sequential(torch.nn.Linear(4, 4, bias=False))
|
|
model[0].weight = torch.nn.Parameter(torch.arange(16, dtype=torch.float32).reshape(4, 4).T)
|
|
cpu_offload(model, execution_device="cpu")
|
|
estimate = estimate_model_export_memory(model)
|
|
assert estimate.retained_host_bytes == 0
|
|
assert estimate.shard_host_bytes == 64
|
|
assert estimate.estimated_peak_ram_bytes == 64 + MIB
|
|
|
|
|
|
def test_canonical_writer_uses_shared_peak_accounting(tmp_path):
|
|
plan = ((_TensorSpec("a", "a", 8), _TensorSpec("b", "b", 16)),)
|
|
admission = _admit(tmp_path / "output", plan, (), WriterLimits())
|
|
assert admission.estimated_peak_ram_bytes == estimated_peak_ram_bytes(
|
|
shard_bytes=24, tensor_bytes=16,
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("phase", ["rebirth", "export"])
|
|
def test_phase_export_uses_writer_estimate_for_actual_cpu_model(phase, monkeypatch):
|
|
from transformers import GPT2Config, GPT2LMHeadModel
|
|
from obliteratus.models.loader import ModelHandle
|
|
from obliteratus.phase_admission import model_phase_demand
|
|
|
|
config = GPT2Config(
|
|
n_embd=8, n_head=2, n_layer=1, n_positions=16, vocab_size=32,
|
|
bos_token_id=0, eos_token_id=1,
|
|
)
|
|
model = GPT2LMHeadModel(config)
|
|
handle = ModelHandle(model, None, config, "local/random", "causal_lm")
|
|
expected = estimate_model_export_memory(model)
|
|
forbidden = Mock(side_effect=AssertionError("phase admission must not gather"))
|
|
monkeypatch.setattr(model, "state_dict", forbidden)
|
|
demands = model_phase_demand(handle, phase).devices
|
|
assert demands["cpu"].additional_bytes == expected.estimated_peak_ram_bytes
|
|
forbidden.assert_not_called()
|
|
|
|
|
|
@pytest.fixture
|
|
def native_handle():
|
|
from transformers import GPT2Config, GPT2LMHeadModel
|
|
from obliteratus.models.loader import ModelHandle
|
|
|
|
config = GPT2Config(n_embd=8, n_head=2, n_layer=2, n_positions=16,
|
|
vocab_size=32, tie_word_embeddings=False)
|
|
return ModelHandle(GPT2LMHeadModel(config), None, config, "local/random", "causal_lm")
|
|
|
|
|
|
@pytest.mark.parametrize("phase", ["rebirth", "export"])
|
|
@pytest.mark.parametrize("shard_size", [1024, "2GB"])
|
|
def test_phase_uses_native_plan_host_demand_exactly(native_handle, phase, shard_size, monkeypatch):
|
|
from obliteratus.models import checkpoint_export
|
|
from obliteratus.phase_admission import model_phase_demand
|
|
|
|
plan = checkpoint_export.plan_native_export(native_handle.model, max_shard_size=shard_size)
|
|
assert plan is not None
|
|
planner = Mock(wraps=checkpoint_export.plan_native_export)
|
|
monkeypatch.setattr(checkpoint_export, "plan_native_export", planner)
|
|
fallback = Mock(side_effect=AssertionError("native export must not budget a full gather"))
|
|
monkeypatch.setattr("obliteratus.checkpoint_writer.estimate_model_export_memory", fallback)
|
|
demands = model_phase_demand(native_handle, phase, max_shard_size=shard_size).devices
|
|
assert demands["cpu"].additional_bytes == checkpoint_export.native_export_host_demand(plan)
|
|
planner.assert_called_once_with(native_handle.model, max_shard_size=shard_size)
|
|
fallback.assert_not_called()
|
|
if shard_size == 1024:
|
|
assert len(plan.shards) > 1
|
|
assert demands["cpu"].additional_bytes < 2 * plan.total_bytes + MIB
|
|
|
|
|
|
@pytest.mark.parametrize("phase", ["rebirth", "export"])
|
|
def test_declined_native_plan_preserves_full_offload_gather_accounting(
|
|
native_handle, phase, tmp_path, monkeypatch,
|
|
):
|
|
from accelerate import disk_offload
|
|
from obliteratus.models import checkpoint_export
|
|
from obliteratus.phase_admission import model_phase_demand
|
|
|
|
full_size = sum(p.numel() * p.element_size() for p in native_handle.model.parameters())
|
|
disk_offload(native_handle.model, tmp_path / "offload", execution_device="cpu")
|
|
assert all(p.is_meta for p in native_handle.model.parameters())
|
|
planner = Mock(return_value=None)
|
|
monkeypatch.setattr(checkpoint_export, "plan_native_export", planner)
|
|
expected = estimate_model_export_memory(native_handle.model, max_shard_size=1024)
|
|
assert expected.retained_host_bytes >= full_size
|
|
demands = model_phase_demand(native_handle, phase, max_shard_size=1024).devices
|
|
assert demands["cpu"].additional_bytes == expected.estimated_peak_ram_bytes
|
|
planner.assert_called_once_with(native_handle.model, max_shard_size=1024)
|
|
|
|
|
|
@pytest.mark.parametrize("phase", ["rebirth", "export"])
|
|
def test_export_planner_exception_is_a_phase_rejection(native_handle, phase, monkeypatch):
|
|
from obliteratus.phase_admission import (
|
|
PhaseAdmissionError, admit_model_phase, model_phase_demand,
|
|
)
|
|
|
|
failure = OSError("invalid checkpoint metadata")
|
|
monkeypatch.setattr("obliteratus.models.checkpoint_export.plan_native_export",
|
|
Mock(side_effect=failure))
|
|
report = admit_model_phase(native_handle, phase, observer=lambda d: None)
|
|
assert report.warnings and any(c.bytes is None for c in report.components)
|
|
with pytest.raises(PhaseAdmissionError, match=f"Phase '{phase}'") as caught:
|
|
model_phase_demand(native_handle, phase, mode="strict")
|
|
assert caught.value.__cause__ is failure
|
|
assert "Check model configuration" in str(caught.value)
|