mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
1181 lines
52 KiB
Python
1181 lines
52 KiB
Python
"""Offline phase admission contracts; every accelerator observation is mocked."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import replace
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
import re
|
|
import torch
|
|
from accelerate.hooks import AlignDevicesHook, add_hook_to_module
|
|
from transformers import GPT2Config, GPT2LMHeadModel
|
|
|
|
from obliteratus import phase_admission as admission
|
|
from obliteratus.abliterate import AbliterationPipeline, PipelineFailure
|
|
from obliteratus.models import loader
|
|
from obliteratus.models.loader import ModelHandle
|
|
from obliteratus.phase_admission import (
|
|
DeviceDemand,
|
|
GIB,
|
|
MemoryObservation,
|
|
PhaseAdmissionError,
|
|
PhaseDemand,
|
|
admit_model_load,
|
|
admit_model_phase,
|
|
admit_phase,
|
|
model_phase_demand,
|
|
transformer_demands,
|
|
)
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def enforced_admission(monkeypatch):
|
|
# These original numeric/peak contracts exercise the opt-in total bound.
|
|
# Unknown-path regressions explicitly exercise both policy modes below.
|
|
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "strict")
|
|
monkeypatch.setattr("obliteratus.gpu_lifecycle.current_process_grant", lambda: None)
|
|
|
|
|
|
@pytest.fixture
|
|
def tiny_config():
|
|
return GPT2Config(vocab_size=64, n_positions=32, n_embd=16, n_layer=2, n_head=2, n_inner=32)
|
|
|
|
|
|
@pytest.fixture
|
|
def tiny_handle(tiny_config):
|
|
model = GPT2LMHeadModel(tiny_config).eval()
|
|
tokenizer = Mock(return_value={"input_ids": torch.ones(1, 8, dtype=torch.long)})
|
|
tokenizer.apply_chat_template.side_effect = lambda messages, **kwargs: messages[-1]["content"]
|
|
return ModelHandle(model, tokenizer, tiny_config, "local-random-gpt2", "causal_lm")
|
|
|
|
|
|
def ample_memory(device):
|
|
return MemoryObservation(device, 100 * GIB, 100 * GIB)
|
|
|
|
|
|
def assert_unknown_warns_by_default(handle, phase, message, **kwargs):
|
|
report = admit_model_phase(
|
|
handle, phase, observer=ample_memory, mode="default", **kwargs
|
|
)
|
|
assert any(component.bytes is None for component in report.components)
|
|
assert re.search(message, "\n".join(report.warnings))
|
|
with pytest.raises(PhaseAdmissionError, match=message):
|
|
admit_model_phase(handle, phase, observer=ample_memory, mode="strict", **kwargs)
|
|
return report
|
|
|
|
|
|
@pytest.mark.parametrize("phase", ["summon", "probe", "distill", "excise", "verify", "rebirth"])
|
|
@pytest.mark.parametrize("device", ["cpu", "cuda:0"])
|
|
def test_over_budget_error_has_structured_bytes_and_gib(phase, device):
|
|
with pytest.raises(PhaseAdmissionError) as caught:
|
|
admit_phase(
|
|
phase,
|
|
{device: DeviceDemand(activation_bytes=9 * GIB)},
|
|
observer=lambda name: MemoryObservation(name, 6 * GIB, 20 * GIB),
|
|
safety_reserve_bytes=0,
|
|
)
|
|
error = caught.value
|
|
assert (error.phase, error.device) == (phase, device)
|
|
assert error.required_bytes == 9 * GIB
|
|
assert error.available_bytes == 6 * GIB
|
|
assert error.shortfall_bytes == 3 * GIB
|
|
assert f"Phase '{phase}'" in str(error)
|
|
assert device in str(error)
|
|
assert f"{9 * GIB} bytes (9.00 GiB)" in str(error)
|
|
assert f"{6 * GIB} bytes (6.00 GiB)" in str(error)
|
|
assert f"shortfall {3 * GIB} bytes (3.00 GiB)" in str(error)
|
|
assert "batch size / max sequence length" in str(error)
|
|
|
|
|
|
def test_modelling_failure_reports_phase_and_cause_without_fake_shortfall():
|
|
failure = LookupError("unsupported execution metadata")
|
|
with pytest.raises(PhaseAdmissionError) as caught:
|
|
with admission._modelling_demand("baseline"):
|
|
raise failure
|
|
|
|
error = caught.value
|
|
message = str(error)
|
|
assert message.startswith(
|
|
"Phase 'baseline': Cannot model phase demand: LookupError: unsupported execution metadata."
|
|
)
|
|
assert "0 bytes" not in message
|
|
assert "shortfall" not in message
|
|
assert "Check model configuration, checkpoint metadata and execution placement" in message
|
|
assert "OBLITERATUS_PHASE_ADMISSION=off" in message
|
|
assert error.__cause__ is failure
|
|
assert (error.phase, error.device) == ("baseline", "cpu")
|
|
assert (error.required_bytes, error.available_bytes, error.shortfall_bytes) == (0, 0, 0)
|
|
assert error.reason.startswith("Cannot model phase demand: LookupError:")
|
|
|
|
|
|
def test_busy_gpu_cannot_borrow_another_devices_free_memory():
|
|
states = {
|
|
"cuda:0": MemoryObservation("cuda:0", GIB, 16 * GIB),
|
|
"cuda:1": MemoryObservation("cuda:1", 15 * GIB, 16 * GIB),
|
|
}
|
|
demands = {name: DeviceDemand(activation_bytes=2 * GIB) for name in states}
|
|
with pytest.raises(PhaseAdmissionError) as caught:
|
|
admit_phase("probe", demands, observer=states.get, safety_reserve_bytes=0)
|
|
assert caught.value.device == "cuda:0"
|
|
assert caught.value.shortfall_bytes == GIB
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"observation",
|
|
[
|
|
None,
|
|
MemoryObservation("cuda:1", 100, 100),
|
|
MemoryObservation("cuda:0", -1, 100),
|
|
MemoryObservation("cuda:0", 101, 100),
|
|
MemoryObservation("cuda:0", 100, 100, 101),
|
|
MemoryObservation("cuda:0", float("nan"), 100),
|
|
MemoryObservation("cuda:0", True, 100),
|
|
],
|
|
)
|
|
def test_missing_or_invalid_observation_fails_closed(observation):
|
|
with pytest.raises(PhaseAdmissionError, match="observation failed") as caught:
|
|
admit_phase(
|
|
"verify", {"cuda:0": DeviceDemand(output_bytes=10)}, observer=lambda _: observation
|
|
)
|
|
assert caught.value.available_bytes == 0
|
|
assert caught.value.shortfall_bytes == 10
|
|
|
|
|
|
def test_observer_failure_is_not_replaced_with_total_capacity():
|
|
observer = Mock(side_effect=RuntimeError("driver query failed"))
|
|
with pytest.raises(PhaseAdmissionError, match="driver query failed"):
|
|
admit_phase("probe", {"cuda:0": DeviceDemand(activation_bytes=1)}, observer=observer)
|
|
|
|
|
|
def test_default_observer_measures_each_cuda_device_and_host(monkeypatch):
|
|
mem_get_info = Mock(return_value=(6 * GIB, 16 * GIB))
|
|
memory_reserved = Mock(return_value=3 * GIB)
|
|
monkeypatch.setattr(torch.cuda, "mem_get_info", mem_get_info)
|
|
monkeypatch.setattr(torch.cuda, "memory_reserved", memory_reserved)
|
|
monkeypatch.setattr("obliteratus.models.host_resources.available_host_bytes", lambda: 7 * GIB)
|
|
assert admission.observe_memory("cuda:1") == MemoryObservation(
|
|
"cuda:1", 6 * GIB, 16 * GIB, 3 * GIB
|
|
)
|
|
mem_get_info.assert_called_once_with(torch.device("cuda:1"))
|
|
memory_reserved.assert_called_once_with(torch.device("cuda:1"))
|
|
assert admission.observe_memory("cpu").free_bytes == 7 * GIB
|
|
|
|
|
|
def test_resident_weights_are_not_charged_again_and_reserve_is_subtracted():
|
|
report = admit_phase(
|
|
"probe",
|
|
PhaseDemand("probe", {"cpu": DeviceDemand(resident_bytes=50 * GIB, activation_bytes=10)}),
|
|
observer=lambda name: MemoryObservation(name, 110, 110),
|
|
safety_reserve_bytes=100,
|
|
)
|
|
assert report.available_bytes == {"cpu": 10}
|
|
assert report.demands["cpu"].additional_bytes == 10
|
|
|
|
|
|
def test_utilization_ceiling_accounts_for_current_allocator_reservations():
|
|
with pytest.raises(PhaseAdmissionError) as caught:
|
|
admit_phase(
|
|
"excise",
|
|
{"cuda:0": DeviceDemand(edit_bytes=2 * GIB)},
|
|
observer=lambda name: MemoryObservation(name, 12 * GIB, 16 * GIB, 7 * GIB),
|
|
gpu_memory_utilization=0.5,
|
|
safety_reserve_bytes=0,
|
|
)
|
|
assert caught.value.available_bytes == GIB
|
|
assert caught.value.shortfall_bytes == GIB
|
|
|
|
|
|
def test_existing_reservations_above_ceiling_reject_even_zero_additional_demand():
|
|
with pytest.raises(PhaseAdmissionError, match="reservations exceed utilization ceiling"):
|
|
admit_phase(
|
|
"distill",
|
|
{"cuda:0": DeviceDemand(resident_bytes=9 * GIB)},
|
|
observer=lambda name: MemoryObservation(name, 7 * GIB, 16 * GIB, 9 * GIB),
|
|
gpu_memory_utilization=0.5,
|
|
safety_reserve_bytes=0,
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"active_grant,explicit_grant", [(5 * GIB, None), (8 * GIB, 5 * GIB), (0, None)]
|
|
)
|
|
def test_broker_grant_is_aggregate_and_explicit_limit_can_only_tighten(
|
|
monkeypatch,
|
|
active_grant,
|
|
explicit_grant,
|
|
):
|
|
monkeypatch.setattr("obliteratus.gpu_lifecycle.current_process_grant", lambda: active_grant)
|
|
with pytest.raises(PhaseAdmissionError, match="GPU lifecycle broker grant"):
|
|
admit_phase(
|
|
"probe",
|
|
{name: DeviceDemand(activation_bytes=2 * GIB) for name in ("cuda:0", "cuda:1")},
|
|
observer=lambda name: MemoryObservation(name, 15 * GIB, 16 * GIB, GIB),
|
|
safety_reserve_bytes=0,
|
|
broker_grant_bytes=explicit_grant,
|
|
)
|
|
|
|
|
|
def test_cpu_only_demand_does_not_consume_gpu_broker_grant(monkeypatch):
|
|
read_grant = Mock(side_effect=AssertionError("CPU-only admission must not read GPU grants"))
|
|
monkeypatch.setattr("obliteratus.gpu_lifecycle.current_process_grant", read_grant)
|
|
report = admit_phase("probe", {"cpu": DeviceDemand(activation_bytes=1)}, observer=ample_memory)
|
|
assert list(report.observations) == ["cpu"]
|
|
read_grant.assert_not_called()
|
|
|
|
|
|
@pytest.mark.parametrize("phase", ["probe", "distill", "excise", "verify", "rebirth"])
|
|
def test_fitting_cpu_model_is_silent_and_preserves_weights_outputs_rng(tiny_handle, phase, caplog):
|
|
inputs = torch.tensor([[1, 2, 3]])
|
|
with torch.no_grad():
|
|
before = tiny_handle.model(inputs).logits.clone()
|
|
weights = {name: value.clone() for name, value in tiny_handle.model.state_dict().items()}
|
|
rng = torch.random.get_rng_state().clone()
|
|
report = admit_model_phase(tiny_handle, phase, observer=ample_memory, batch_size=1, tokens=8)
|
|
assert not report.bypassed
|
|
assert set(report.observations) == {"cpu"}
|
|
assert torch.equal(rng, torch.random.get_rng_state())
|
|
for name, value in tiny_handle.model.state_dict().items():
|
|
assert torch.equal(weights[name], value)
|
|
with torch.no_grad():
|
|
assert torch.equal(before, tiny_handle.model(inputs).logits)
|
|
assert not [record for record in caplog.records if record.name == admission.__name__]
|
|
|
|
|
|
def test_explicit_escape_hatch_skips_observation_and_unmodelled_geometry(monkeypatch, caplog):
|
|
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "off")
|
|
observer = Mock(side_effect=AssertionError("bypass must not measure memory"))
|
|
reports = [
|
|
admit_phase(
|
|
"probe", {"cuda:0": DeviceDemand(activation_bytes=100 * GIB)}, observer=observer
|
|
),
|
|
admit_model_phase(object(), "verify", observer=observer),
|
|
admit_model_load(object(), device="cuda:0", observer=observer),
|
|
]
|
|
assert all(report.bypassed for report in reports)
|
|
observer.assert_not_called()
|
|
assert caplog.text.count("PHASE MEMORY ADMISSION DISABLED") == 3
|
|
|
|
|
|
def test_standalone_model_load_needs_no_pipeline_or_weights(tiny_config, monkeypatch):
|
|
weights_loader = Mock(side_effect=AssertionError("admission must not load checkpoint weights"))
|
|
monkeypatch.setattr(GPT2LMHeadModel, "from_pretrained", weights_loader)
|
|
rng = torch.random.get_rng_state().clone()
|
|
report = admit_model_load(tiny_config, device="cpu", dtype="float32", observer=ample_memory)
|
|
assert report.phase == "summon"
|
|
assert report.demands["cpu"].weight_bytes > 0
|
|
assert report.demands["cpu"].staging_bytes > 0
|
|
assert torch.equal(rng, torch.random.get_rng_state())
|
|
weights_loader.assert_not_called()
|
|
with pytest.raises(PhaseAdmissionError) as caught:
|
|
admit_model_load(
|
|
tiny_config,
|
|
device="cpu",
|
|
dtype="float32",
|
|
observer=lambda name: MemoryObservation(name, 0, GIB),
|
|
)
|
|
assert caught.value.phase == "summon"
|
|
assert caught.value.device == "cpu"
|
|
|
|
|
|
def test_standalone_load_checks_explicit_gpu_placement_without_cuda(tiny_config):
|
|
report = admit_model_load(
|
|
tiny_config,
|
|
device="cuda:1",
|
|
dtype="float16",
|
|
observer=ample_memory,
|
|
device_map={"": "cuda:1"},
|
|
)
|
|
assert set(report.demands) == {"cuda:1", "cpu"}
|
|
assert report.demands["cuda:1"].weight_bytes > 0
|
|
assert report.demands["cpu"].staging_bytes > 0
|
|
|
|
|
|
def _geometry(**overrides):
|
|
values = dict(
|
|
hidden_size=16,
|
|
num_hidden_layers=2,
|
|
num_attention_heads=2,
|
|
num_key_value_heads=1,
|
|
vocab_size=64,
|
|
intermediate_size=32,
|
|
model_type="llama",
|
|
)
|
|
values.update(overrides)
|
|
return SimpleNamespace(**values)
|
|
|
|
|
|
def _geometry_demands(config, **overrides):
|
|
options = dict(
|
|
batch_size=2,
|
|
tokens=8,
|
|
dtype_bytes=2,
|
|
resident_bytes={},
|
|
largest_tensor_bytes={},
|
|
output_device="cuda:1",
|
|
input_device="cuda:0",
|
|
)
|
|
options.update(overrides)
|
|
return transformer_demands(config, ["cuda:0", "cuda:1"], **options)
|
|
|
|
|
|
def test_moe_charges_router_logits_top_k_and_shared_experts():
|
|
base = dict(num_experts=8, num_experts_per_tok=2, moe_intermediate_size=128)
|
|
original = _geometry_demands(_geometry(**base))["cuda:0"].activation_bytes
|
|
for addition in (
|
|
{"num_experts": 64},
|
|
{"num_experts_per_tok": 4},
|
|
{"shared_expert_intermediate_size": 128},
|
|
):
|
|
larger = _geometry_demands(_geometry(**(base | addition)))["cuda:0"].activation_bytes
|
|
assert larger > original
|
|
|
|
|
|
def test_fused_expert_tensor_staging_and_edit_workspace_cover_all_experts(tiny_handle):
|
|
fused = torch.nn.Parameter(torch.zeros(8, 128, 64))
|
|
tiny_handle.model.transformer.h[0].register_parameter("fused_experts", fused)
|
|
tiny_handle.config.num_experts = 8
|
|
tiny_handle.config.num_experts_per_tok = 2
|
|
tiny_handle.config.moe_intermediate_size = 128
|
|
demand = model_phase_demand(tiny_handle, "excise", tokens=8).devices["cpu"]
|
|
full_tensor_bytes = fused.numel() * fused.element_size()
|
|
assert demand.staging_bytes == 0 # resident floating weights need no transfer
|
|
assert demand.edit_bytes >= 3 * full_tensor_bytes // fused.shape[0]
|
|
assert demand.edit_bytes < full_tensor_bytes # live fused experts are edited slice by slice
|
|
|
|
|
|
@pytest.mark.parametrize("nested", [False, True])
|
|
def test_qwen_hybrid_includes_recurrent_convolution_state_and_attention_kv(nested):
|
|
config = _geometry(
|
|
model_type="qwen3_5_text",
|
|
layer_types=["linear_attention", "full_attention"],
|
|
linear_num_key_heads=2,
|
|
linear_num_value_heads=4,
|
|
linear_key_head_dim=8,
|
|
linear_value_head_dim=8,
|
|
linear_conv_kernel_dim=4,
|
|
)
|
|
demands = _geometry_demands(SimpleNamespace(text_config=config) if nested else config)
|
|
# Batch two: float32 recurrent matrix plus history for all Q/K/V channels.
|
|
recurrent_state = 2 * (4 * 8 * 8 + (2 * 2 * 8 + 4 * 8) * 4) * 4
|
|
assert demands["cuda:0"].kv_bytes >= recurrent_state
|
|
assert demands["cuda:1"].kv_bytes > 0
|
|
assert demands["cuda:0"].activation_bytes > recurrent_state
|
|
|
|
|
|
def test_mamba_state_has_an_explicit_geometry_bound():
|
|
demands = _geometry_demands(
|
|
_geometry(model_type="mamba", state_size=16, conv_kernel=4, expand=2)
|
|
)
|
|
assert all(demand.kv_bytes > 0 for demand in demands.values())
|
|
|
|
|
|
@pytest.mark.parametrize("kind", ["linear_attention", "mamba"])
|
|
def test_recurrent_chunk_workspace_grows_when_sequence_needs_another_chunk(kind):
|
|
config = _geometry(
|
|
layer_types=[kind, "full_attention"],
|
|
linear_num_key_heads=2,
|
|
linear_num_value_heads=4,
|
|
linear_key_head_dim=8,
|
|
linear_value_head_dim=8,
|
|
linear_conv_kernel_dim=4,
|
|
state_size=16,
|
|
conv_kernel=4,
|
|
expand=2,
|
|
)
|
|
before, boundary, after = [
|
|
_geometry_demands(config, tokens=tokens)["cuda:0"] for tokens in (63, 64, 65)
|
|
]
|
|
assert before.kv_bytes == boundary.kv_bytes == after.kv_bytes
|
|
assert after.activation_bytes - boundary.activation_bytes > (
|
|
boundary.activation_bytes - before.activation_bytes
|
|
)
|
|
|
|
|
|
def test_unmodelled_recurrent_kind_warns_by_default_and_rejects_strict(tiny_handle):
|
|
tiny_handle.config.layer_types = ["unknown_recurrent_kernel", "full_attention"]
|
|
assert_unknown_warns_by_default(
|
|
tiny_handle, "probe", "unmodelled recurrent layer types.*unknown_recurrent_kernel"
|
|
)
|
|
|
|
|
|
def test_missing_hybrid_geometry_warns_by_default_and_rejects_strict(tiny_handle):
|
|
tiny_handle.config.layer_types = ["linear_attention", "full_attention"]
|
|
assert_unknown_warns_by_default(tiny_handle, "probe", "linear_num_key_heads")
|
|
|
|
|
|
def test_embedding_only_device_is_admitted_using_accelerate_execution_hooks(tiny_handle):
|
|
model = tiny_handle.model
|
|
model.hf_device_map = {
|
|
"transformer.wte": "cuda:0",
|
|
"transformer.h": "cuda:1",
|
|
"lm_head": "cuda:1",
|
|
}
|
|
placements = [(model.transformer.wte, "cuda:0"), (model.lm_head, "cuda:1")]
|
|
placements.extend((layer, "cuda:1") for layer in model.transformer.h)
|
|
for owner, target in placements:
|
|
add_hook_to_module(owner, AlignDevicesHook(
|
|
execution_device="cpu", offload=True, place_submodules=True,
|
|
))
|
|
owner._hf_hook.execution_device = target
|
|
demand = model_phase_demand(tiny_handle, "probe", tokens=8)
|
|
embedding = demand.devices["cuda:0"]
|
|
assert embedding.kv_bytes == 0
|
|
assert embedding.staging_bytes > 0
|
|
assert embedding.activation_bytes > 0
|
|
assert embedding.output_bytes > 0
|
|
|
|
def observer(device):
|
|
state = ample_memory(device)
|
|
return replace(state, free_bytes=0) if device == "cuda:0" else state
|
|
|
|
with pytest.raises(PhaseAdmissionError) as caught:
|
|
admit_phase("probe", demand, observer=observer)
|
|
assert caught.value.device == "cuda:0"
|
|
|
|
|
|
def test_export_estimates_host_materialization_without_building_state_dict(
|
|
tiny_handle, monkeypatch
|
|
):
|
|
state_dict = Mock(side_effect=AssertionError("admission must not gather tensors"))
|
|
monkeypatch.setattr(tiny_handle.model, "state_dict", state_dict)
|
|
demand = model_phase_demand(tiny_handle, "export").devices["cpu"]
|
|
from obliteratus.checkpoint_writer import estimate_model_export_memory
|
|
assert demand.additional_bytes == (
|
|
estimate_model_export_memory(tiny_handle.model).estimated_peak_ram_bytes
|
|
)
|
|
state_dict.assert_not_called()
|
|
|
|
|
|
def _stub_pipeline(tiny_handle, tmp_path, monkeypatch):
|
|
events, logs = [], []
|
|
pipeline = AbliterationPipeline(
|
|
model_name="local-random-gpt2",
|
|
output_dir=str(tmp_path),
|
|
device="cpu",
|
|
dtype="float32",
|
|
harmful_prompts=["harmful"],
|
|
harmless_prompts=["harmless"],
|
|
max_seq_length=8,
|
|
verify_sample_size=1,
|
|
on_stage=events.append,
|
|
on_log=logs.append,
|
|
)
|
|
pipeline.handle = tiny_handle
|
|
pipeline._strong_layers = [0]
|
|
# Give validation real host demand so a zero budget rejects this boundary too.
|
|
pipeline._gate_mode = "report"
|
|
pipeline.concept_directions = {0: torch.ones(tiny_handle.hidden_size)}
|
|
pipeline._harmful_acts = {0: [torch.ones(1, tiny_handle.hidden_size)]}
|
|
pipeline._harmless_acts = {0: [torch.zeros(1, tiny_handle.hidden_size)]}
|
|
phase_methods = {
|
|
"summon": ("_summon",),
|
|
"baseline": ("_capture_stock_baseline", "_validate_architecture_surgery_support"),
|
|
"probe": ("_probe",),
|
|
"distill": ("_distill",),
|
|
"validate": ("_validate_concept_gate", "_capture_concept_evaluation",
|
|
"_capture_concept_repair"),
|
|
"excise": ("_capture_baseline_kl_logits", "_excise"),
|
|
"verify": ("_verify",),
|
|
"rebirth": ("_rebirth",),
|
|
}
|
|
work = {}
|
|
for phase, methods in phase_methods.items():
|
|
work[phase] = {}
|
|
for method in methods:
|
|
operation = Mock(return_value=tmp_path if method == "_rebirth" else None)
|
|
monkeypatch.setattr(pipeline, method, operation)
|
|
work[phase][method] = operation
|
|
monkeypatch.setattr(pipeline, "_free_gpu_memory", Mock())
|
|
return pipeline, work, events, logs
|
|
|
|
|
|
@pytest.mark.parametrize("phase", ["baseline", "probe", "distill", "validate",
|
|
"excise", "verify", "rebirth"])
|
|
def test_pipeline_rejects_before_phase_work_and_uses_failure_cleanup(
|
|
tiny_handle,
|
|
tmp_path,
|
|
monkeypatch,
|
|
phase,
|
|
):
|
|
pipeline, work, events, _ = _stub_pipeline(tiny_handle, tmp_path, monkeypatch)
|
|
cleanup = Mock()
|
|
monkeypatch.setattr(pipeline, "cleanup_failed_run", cleanup)
|
|
|
|
def observer(device):
|
|
if pipeline._active_stage == phase:
|
|
return MemoryObservation(device, 0, 100 * GIB)
|
|
return ample_memory(device)
|
|
|
|
monkeypatch.setattr(admission, "observe_memory", observer)
|
|
with pytest.raises(PipelineFailure) as caught:
|
|
pipeline.run()
|
|
assert caught.value.stage == phase
|
|
assert isinstance(caught.value.__cause__, PhaseAdmissionError)
|
|
assert caught.value.__cause__.device == "cpu"
|
|
for operation in work[phase].values():
|
|
operation.assert_not_called()
|
|
cleanup.assert_called_once_with()
|
|
assert events[-1].stage == phase
|
|
assert events[-1].status == "error"
|
|
assert "shortfall" in events[-1].message
|
|
|
|
|
|
def test_fitting_pipeline_retains_stage_work_order_events_and_logs(
|
|
tiny_handle, tmp_path, monkeypatch
|
|
):
|
|
pipeline, work, events, logs = _stub_pipeline(tiny_handle, tmp_path, monkeypatch)
|
|
monkeypatch.setattr(admission, "observe_memory", ample_memory)
|
|
sequence = []
|
|
for phase, methods in work.items():
|
|
names = list(methods)
|
|
for method, operation in methods.items():
|
|
|
|
def record(phase=phase, method=method, first=names[0], last=names[-1]):
|
|
sequence.append((phase, method))
|
|
if method == first:
|
|
pipeline._emit(phase, "running")
|
|
pipeline.log(f"{method} work")
|
|
if method == last:
|
|
pipeline._emit(phase, "done")
|
|
return tmp_path if phase == "rebirth" else None
|
|
|
|
operation.side_effect = record
|
|
assert pipeline.run() == tmp_path
|
|
expected = [(phase, method) for phase, methods in work.items() for method in methods]
|
|
assert sequence == expected
|
|
for methods in work.values():
|
|
for operation in methods.values():
|
|
operation.assert_called_once_with()
|
|
assert [(event.stage, event.status) for event in events] == [
|
|
(phase, status) for phase in work for status in ("running", "done")
|
|
]
|
|
assert logs == [f"{method} work" for _, method in expected]
|
|
|
|
|
|
def test_loader_rejects_summon_before_checkpoint_weight_load(tiny_config, monkeypatch):
|
|
from obliteratus.models.quant_dequant import QuantDetection, QuantScheme
|
|
|
|
monkeypatch.setattr(loader.AutoConfig, "from_pretrained", Mock(return_value=tiny_config))
|
|
monkeypatch.setattr(
|
|
loader.qd, "detect_quant_scheme", Mock(return_value=QuantDetection(QuantScheme.NONE))
|
|
)
|
|
monkeypatch.setattr(loader, "_select_model_class", Mock(return_value=GPT2LMHeadModel))
|
|
monkeypatch.setattr(admission, "observe_memory", lambda name: MemoryObservation(name, 0, GIB))
|
|
load_weights = Mock(side_effect=AssertionError("rejected SUMMON must not start weight loading"))
|
|
monkeypatch.setattr(GPT2LMHeadModel, "from_pretrained", load_weights)
|
|
with pytest.raises(PhaseAdmissionError) as caught:
|
|
loader.load_model(
|
|
"local-random-gpt2",
|
|
device="cpu",
|
|
local_files_only=True,
|
|
skip_snapshot=True,
|
|
phase_admission=True,
|
|
)
|
|
assert caught.value.phase == "summon"
|
|
load_weights.assert_not_called()
|
|
|
|
|
|
def test_output_head_inherits_recursive_offload_scope(tiny_handle, monkeypatch):
|
|
model = tiny_handle.model
|
|
head = model.lm_head
|
|
del model.lm_head
|
|
model.output_holder = torch.nn.Module()
|
|
model.output_holder.head = head
|
|
add_hook_to_module(model.output_holder, AlignDevicesHook(
|
|
execution_device="cpu", offload=True, place_submodules=True,
|
|
))
|
|
model.output_holder._hf_hook.execution_device = "cuda:1"
|
|
monkeypatch.setattr(model, "get_output_embeddings", lambda: head)
|
|
add_hook_to_module(model.transformer.wte, AlignDevicesHook(
|
|
execution_device="cpu", offload=True,
|
|
))
|
|
model.transformer.wte._hf_hook.execution_device = "cuda:0"
|
|
demand = model_phase_demand(tiny_handle, "verify", tokens=8)
|
|
assert demand.devices["cuda:0"].output_bytes > 0
|
|
assert demand.devices["cuda:1"].output_bytes > 0
|
|
|
|
|
|
def test_packed_four_bit_logical_shape_bounds_staging_and_edit_workspace(tiny_handle):
|
|
packed = torch.nn.Parameter(torch.zeros(128, dtype=torch.uint8), requires_grad=False)
|
|
packed.quant_state = SimpleNamespace(shape=(8, 128, 64))
|
|
tiny_handle.model.transformer.h[0].register_parameter("packed_experts", packed)
|
|
logical_bytes = 8 * 128 * 64 * 4
|
|
demand = model_phase_demand(tiny_handle, "excise", tokens=8).devices["cpu"]
|
|
assert demand.edit_bytes >= 2 * logical_bytes # full dequantization and requantization
|
|
probe = model_phase_demand(tiny_handle, "probe", tokens=8).devices["cpu"]
|
|
assert probe.staging_bytes >= logical_bytes
|
|
|
|
|
|
def test_unmodelled_accelerator_warns_by_default_and_rejects_strict(tiny_handle):
|
|
tiny_handle.model.transformer.wte._hf_hook = AlignDevicesHook(execution_device="xpu:0")
|
|
assert_unknown_warns_by_default(tiny_handle, "probe", "unmodelled execution device xpu")
|
|
|
|
|
|
def test_edit_workspace_excludes_unselected_embeddings_and_head(tiny_handle):
|
|
model = tiny_handle.model
|
|
model.transformer.wte = torch.nn.Embedding(8192, 16)
|
|
model.lm_head = torch.nn.Linear(16, 8192, bias=False)
|
|
names = ["transformer.h.0.attn.c_proj.weight"]
|
|
selected = model_phase_demand(tiny_handle, "excise", tokens=8, edit_parameter_names=names)
|
|
with_head = model_phase_demand(
|
|
tiny_handle, "excise", tokens=8, edit_parameter_names=names + ["lm_head.weight"]
|
|
)
|
|
assert selected.devices["cpu"].edit_bytes < model.lm_head.weight.numel() * 4
|
|
assert with_head.devices["cpu"].edit_bytes > selected.devices["cpu"].edit_bytes
|
|
no_edits = model_phase_demand(tiny_handle, "excise", tokens=8, edit_parameter_names=[])
|
|
assert no_edits.devices["cpu"].edit_bytes == 0
|
|
assert_unknown_warns_by_default(
|
|
tiny_handle, "excise", "unknown surgery parameter", edit_parameter_names=["missing.weight"]
|
|
)
|
|
|
|
|
|
def test_realized_attention_backend_overrides_unresolved_loader_config(tiny_handle, monkeypatch):
|
|
from copy import deepcopy
|
|
|
|
monkeypatch.setattr("obliteratus.phase_admission._sdpa_fused_eligible", lambda *a: True)
|
|
tiny_handle.config = deepcopy(tiny_handle.config)
|
|
tiny_handle.config._attn_implementation = None
|
|
tiny_handle.model.config._attn_implementation = "sdpa"
|
|
# At longer sequences, eager quadratic scores exceed CPU Flash's bounded
|
|
# per-thread tiles; very short sequences need not favor the fused kernel.
|
|
fused = model_phase_demand(tiny_handle, "probe", tokens=1024).devices["cpu"]
|
|
tiny_handle.model.config._attn_implementation = "eager"
|
|
eager = model_phase_demand(tiny_handle, "probe", tokens=1024).devices["cpu"]
|
|
assert eager.activation_bytes > fused.activation_bytes
|
|
|
|
|
|
def test_pipeline_edit_targets_follow_output_lora_and_embedding_methods(tiny_handle, tmp_path):
|
|
pipeline = AbliterationPipeline("local", output_dir=str(tmp_path), device="cpu", method="basic")
|
|
pipeline.handle = tiny_handle
|
|
# Untie embeddings to distinguish the two edit paths.
|
|
tiny_handle.model.lm_head.weight = torch.nn.Parameter(tiny_handle.model.lm_head.weight.clone())
|
|
pipeline._strong_layers = [0]
|
|
pipeline.concept_subspaces[0] = torch.ones(1, 16)
|
|
pipeline.concept_directions[0] = torch.ones(16)
|
|
pipeline.projection_target = "output"
|
|
pipeline.project_embeddings = False
|
|
names = pipeline._excise_parameter_names()
|
|
assert "transformer.h.0.attn.c_proj.weight" in names
|
|
assert "transformer.h.0.attn.c_attn.weight" not in names
|
|
assert "lm_head.weight" in names # Standard excise really does project this head.
|
|
assert "transformer.wte.weight" not in names
|
|
pipeline.project_embeddings = True
|
|
assert "transformer.wte.weight" in pipeline._excise_parameter_names()
|
|
pipeline.use_lora_ablation = True
|
|
lora_names = pipeline._excise_parameter_names()
|
|
assert "lm_head.weight" not in lora_names
|
|
assert "transformer.wte.weight" not in lora_names
|
|
assert "transformer.h.0.attn.c_attn.weight" in lora_names
|
|
|
|
|
|
def test_sae_norm_restoration_and_refinement_expand_actual_edit_targets(tiny_handle, tmp_path):
|
|
pipeline = AbliterationPipeline("local", output_dir=str(tmp_path), device="cpu", method="basic")
|
|
pipeline.handle = tiny_handle
|
|
pipeline._strong_layers = [0]
|
|
pipeline.projection_target = "attention"
|
|
pipeline.concept_subspaces[0] = torch.ones(1, 16)
|
|
assert "transformer.h.0.mlp.c_proj.weight" not in pipeline._excise_parameter_names()
|
|
pipeline.use_sae_features = True
|
|
pipeline._sae_directions[0] = torch.ones(1, 16)
|
|
assert "transformer.h.0.mlp.c_proj.weight" in pipeline._excise_parameter_names()
|
|
assert "transformer.h.0.mlp.c_fc.weight" not in pipeline._excise_parameter_names()
|
|
pipeline.use_sae_features = False
|
|
pipeline.norm_preserve = True
|
|
pipeline.concept_subspaces[0] = torch.ones(2, 16)
|
|
assert "transformer.h.0.mlp.c_fc.weight" in pipeline._excise_parameter_names()
|
|
pipeline.true_iterative_refinement = True
|
|
pipeline.refinement_passes = 2
|
|
assert "transformer.h.1.attn.c_attn.weight" in pipeline._excise_parameter_names()
|
|
|
|
|
|
def test_lora_adapter_storage_accumulates_across_selected_layers(tiny_handle):
|
|
names = ["transformer.h.0.attn.c_proj.weight", "transformer.h.1.attn.c_proj.weight"]
|
|
rank_one = model_phase_demand(
|
|
tiny_handle, "excise", tokens=8, edit_parameter_names=names, edit_adapter_rank=1,
|
|
)
|
|
lora = model_phase_demand(
|
|
tiny_handle, "excise", tokens=8, edit_parameter_names=names, edit_adapter_rank=4,
|
|
)
|
|
# Cache-line rounding makes tiny rank-one pairs larger than their payload,
|
|
# so rank growth need not equal the raw FP16 payload delta. It must remain
|
|
# monotone, and adding a layer must retain at least its additional pair.
|
|
assert lora.devices["cpu"].edit_bytes > rank_one.devices["cpu"].edit_bytes
|
|
one_layer = model_phase_demand(
|
|
tiny_handle, "excise", tokens=8, edit_parameter_names=names[:1], edit_adapter_rank=4,
|
|
)
|
|
assert lora.devices["cpu"].edit_bytes - one_layer.devices["cpu"].edit_bytes >= 2 * 4 * (16 + 16)
|
|
# Exact retained payload is checked against actual FP16 adapters by
|
|
# test_lora_construction_conversion_and_merge_peaks_fit_bound.
|
|
|
|
|
|
def test_expert_transplant_admits_host_running_mean_before_work(tiny_handle, tmp_path, monkeypatch):
|
|
pipeline, _, _, _ = _stub_pipeline(tiny_handle, tmp_path, monkeypatch)
|
|
pipeline.expert_transplant = True
|
|
pipeline._strong_layers = [0]
|
|
pipeline._expert_safety_scores[0] = [(0, 0.9), (1, 0.1)]
|
|
ffn = tiny_handle.model.transformer.h[0].mlp
|
|
expert = torch.nn.Module()
|
|
expert.down_proj = torch.nn.Linear(32, 16, bias=False)
|
|
ffn.experts = torch.nn.ModuleList([expert, expert])
|
|
admit = Mock()
|
|
monkeypatch.setattr(admission, "admit_model_phase", admit)
|
|
pipeline._admit_pipeline_phase("excise")
|
|
assert admit.call_args.kwargs["host_workspace_bytes"] == 0
|
|
assert admit.call_args.kwargs["sequential_workspace_bytes"]["cpu"] == 3 * 32 * 16 * 4 + 32
|
|
pipeline._expert_safety_scores.clear()
|
|
pipeline._admit_pipeline_phase("excise")
|
|
assert admit.call_args.kwargs["host_workspace_bytes"] == 0
|
|
|
|
|
|
def test_qwen_manifest_edits_only_declared_outputs(tiny_handle, tmp_path):
|
|
from obliteratus.models.qwen35_contracts import Qwen35LayerTargets
|
|
|
|
pipeline = AbliterationPipeline("local", output_dir=str(tmp_path), device="cpu", method="basic")
|
|
pipeline.handle = tiny_handle
|
|
pipeline._strong_layers = [0]
|
|
pipeline.project_embeddings = True
|
|
pipeline.concept_subspaces[0] = torch.ones(2, 16)
|
|
pipeline._qwen35_projection_manifest = (
|
|
Qwen35LayerTargets("attn", "c_proj", "mlp", "c_proj"),
|
|
) * 2
|
|
assert set(pipeline._excise_parameter_names()) == {
|
|
"transformer.h.0.attn.c_proj.weight", "transformer.h.0.mlp.c_proj.weight",
|
|
}
|
|
|
|
|
|
def test_moe_target_discovery_includes_packed_fused_logical_shapes(tiny_handle, tmp_path):
|
|
pipeline = AbliterationPipeline("local", output_dir=str(tmp_path), device="cpu", method="basic")
|
|
pipeline.handle = tiny_handle
|
|
pipeline._strong_layers = [0]
|
|
ffn = torch.nn.Module()
|
|
ffn.experts = torch.nn.Module()
|
|
packed = torch.nn.Parameter(torch.zeros(128, dtype=torch.uint8), requires_grad=False)
|
|
packed.quant_state = SimpleNamespace(shape=(2, 32, 16))
|
|
ffn.experts.register_parameter("gate_up_proj", packed)
|
|
tiny_handle.model.transformer.h[0].mlp = ffn
|
|
assert "transformer.h.0.mlp.experts.gate_up_proj" in pipeline._excise_parameter_names()
|
|
|
|
|
|
@pytest.mark.parametrize("free_gb,expected", [(0.1, (1, 64)), (0.3, (8, 128)), (0.6, (16, 256))])
|
|
def test_shared_probe_limits_preserve_existing_thresholds(
|
|
tiny_handle, tmp_path, monkeypatch, free_gb, expected,
|
|
):
|
|
from obliteratus import device
|
|
|
|
pipeline = AbliterationPipeline("local", output_dir=str(tmp_path), device="cpu", method="basic")
|
|
pipeline.handle = tiny_handle
|
|
monkeypatch.setattr(device, "get_total_free_gb", lambda: free_gb)
|
|
monkeypatch.setattr(device, "is_gpu_available", lambda: True)
|
|
assert pipeline._activation_collection_limits(2)[:2] == expected
|
|
pipeline.max_seq_length = 17
|
|
assert pipeline._activation_collection_limits(2)[:2] == (expected[0], 17)
|
|
|
|
|
|
def test_probe_admission_and_collection_share_limits_and_materialize_logits(
|
|
tiny_handle, tmp_path, monkeypatch,
|
|
):
|
|
pipeline, _, _, _ = _stub_pipeline(tiny_handle, tmp_path, monkeypatch)
|
|
pipeline.cot_aware = False
|
|
limits = Mock(return_value=(2, 8, None))
|
|
monkeypatch.setattr(pipeline, "_activation_collection_limits", limits)
|
|
tiny_handle.num_layers = 0 # Legacy metadata must not override realized collection layers.
|
|
admit = Mock()
|
|
monkeypatch.setattr(admission, "admit_model_phase", admit)
|
|
pipeline._admit_pipeline_phase("probe")
|
|
assert admit.call_args.kwargs["batch_size"] == 2
|
|
assert admit.call_args.kwargs["tokens"] == 8
|
|
batches = []
|
|
|
|
def tokenize(prompts, **kwargs):
|
|
batches.append((len(prompts), kwargs["max_length"]))
|
|
return {"input_ids": torch.ones(len(prompts), 8, dtype=torch.long),
|
|
"attention_mask": torch.ones(len(prompts), 8, dtype=torch.long)}
|
|
|
|
tiny_handle.tokenizer.side_effect = tokenize
|
|
logits = []
|
|
hook = tiny_handle.model.lm_head.register_forward_hook(
|
|
lambda m, args, out: logits.append(out.shape)
|
|
)
|
|
try:
|
|
pipeline._collect_activations(tiny_handle.model.transformer.h, ["x"] * 5, "harmful")
|
|
finally:
|
|
hook.remove()
|
|
assert limits.call_count == 1
|
|
assert all(call.args == (2,) for call in limits.call_args_list)
|
|
assert batches == [(2, 8), (2, 8), (1, 8)]
|
|
assert logits == [(2, 8, 64), (2, 8, 64), (1, 8, 64)]
|
|
|
|
|
|
def test_cpu_last_token_hook_retains_sequence_but_pooled_readout_does_not(
|
|
tiny_handle, tmp_path, monkeypatch,
|
|
):
|
|
pipeline, _, _, _ = _stub_pipeline(tiny_handle, tmp_path, monkeypatch)
|
|
tiny_handle.tokenizer.return_value = {
|
|
"input_ids": torch.ones(1, 8, dtype=torch.long),
|
|
"attention_mask": torch.ones(1, 8, dtype=torch.long),
|
|
}
|
|
pipeline.winsorize_activations = False
|
|
pipeline.cot_aware = False
|
|
rows = pipeline._collect_activations(tiny_handle.model.transformer.h, ["x"], "harmful")
|
|
assert rows[0][0].untyped_storage().nbytes() == 8 * 16 * 4
|
|
pipeline.cot_aware = True
|
|
pooled = pipeline._collect_activations(tiny_handle.model.transformer.h, ["x"], "harmful")
|
|
assert pooled[0][0].untyped_storage().nbytes() == 16 * 4
|
|
classic = model_phase_demand(tiny_handle, "probe", tokens=8, retained_rows=5)
|
|
compact = model_phase_demand(
|
|
tiny_handle, "probe", tokens=8, retained_rows=5, retain_full_sequence=False
|
|
)
|
|
extra = classic.devices["cpu"].activation_bytes - compact.devices["cpu"].activation_bytes
|
|
assert extra == 2 * 5 * 16 * 4 * 7
|
|
# Distillation sees the rows in measured resident RAM; it only charges new workspace.
|
|
first = model_phase_demand(tiny_handle, "distill", tokens=8, retained_rows=5)
|
|
second = model_phase_demand(tiny_handle, "distill", tokens=256, retained_rows=5)
|
|
assert first == second
|
|
|
|
|
|
def test_summon_pipeline_failure_precedes_weight_loading_and_cleans_up(
|
|
tiny_config,
|
|
tmp_path,
|
|
monkeypatch,
|
|
):
|
|
from obliteratus.models.quant_dequant import QuantDetection, QuantScheme
|
|
|
|
events = []
|
|
pipeline = AbliterationPipeline(
|
|
model_name="local-random-gpt2",
|
|
output_dir=str(tmp_path),
|
|
device="cpu",
|
|
dtype="float32",
|
|
on_stage=events.append,
|
|
)
|
|
monkeypatch.setattr(loader.AutoConfig, "from_pretrained", Mock(return_value=tiny_config))
|
|
monkeypatch.setattr(
|
|
loader.qd,
|
|
"detect_quant_scheme",
|
|
Mock(return_value=QuantDetection(QuantScheme.NONE)),
|
|
)
|
|
monkeypatch.setattr(loader, "_select_model_class", Mock(return_value=GPT2LMHeadModel))
|
|
monkeypatch.setattr(admission, "observe_memory", lambda name: MemoryObservation(name, 0, GIB))
|
|
load_weights = Mock(side_effect=AssertionError("rejected SUMMON started weight loading"))
|
|
cleanup = Mock()
|
|
monkeypatch.setattr(GPT2LMHeadModel, "from_pretrained", load_weights)
|
|
monkeypatch.setattr(pipeline, "cleanup_failed_run", cleanup)
|
|
with pytest.raises(PipelineFailure) as caught:
|
|
pipeline.run()
|
|
assert caught.value.stage == "summon"
|
|
assert isinstance(caught.value.__cause__, PhaseAdmissionError)
|
|
load_weights.assert_not_called()
|
|
cleanup.assert_called_once_with()
|
|
assert events[-1].stage == "summon"
|
|
assert events[-1].status == "error"
|
|
|
|
|
|
@pytest.mark.parametrize("shape", [(128,), (8, 16)])
|
|
@pytest.mark.parametrize("evidence", ["quant_state", "Params4bit", "Int8Params", "quantizer"])
|
|
def test_packed_parameter_without_geometry_warns_by_default_rejects_strict(
|
|
tiny_handle, shape, evidence,
|
|
):
|
|
packed = torch.nn.Parameter(torch.zeros(shape, dtype=torch.uint8), requires_grad=False)
|
|
if evidence == "quant_state":
|
|
packed.quant_state = SimpleNamespace(dtype=torch.bfloat16)
|
|
elif evidence == "quantizer":
|
|
tiny_handle.model.hf_quantizer = object()
|
|
else:
|
|
packed_type = type(evidence, (torch.nn.Parameter,), {})
|
|
packed = packed_type(packed, requires_grad=False)
|
|
tiny_handle.model.transformer.h[0].register_parameter("packed_experts", packed)
|
|
assert_unknown_warns_by_default(
|
|
tiny_handle, "excise", "packed tensor lacks logical quantization shape"
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("phase", ["probe", "distill", "excise", "verify", "rebirth"])
|
|
def test_ordinary_integer_buffer_does_not_reject_a_working_model(tiny_handle, phase):
|
|
model = tiny_handle.model
|
|
inputs = torch.ones(1, 8, dtype=torch.long)
|
|
with torch.no_grad():
|
|
expected = model(inputs).logits
|
|
model.register_buffer("counts", torch.zeros(4, 4, dtype=torch.int32))
|
|
report = admit_model_phase(tiny_handle, phase, observer=ample_memory)
|
|
assert report.phase == phase
|
|
assert admission._expanded_tensor_bytes(model, model.counts, model) == 4 * 4 * 4
|
|
with torch.no_grad():
|
|
torch.testing.assert_close(model(inputs).logits, expected, rtol=0, atol=0)
|
|
|
|
|
|
@pytest.mark.parametrize("dtype", [torch.uint8, torch.int8, torch.int16, torch.int32])
|
|
@pytest.mark.parametrize("shape,parameter", [((4,), True), ((4, 4), False), ((2, 4, 4), False)])
|
|
def test_unrecognised_integer_storage_uses_float_workspace_bound(
|
|
tiny_handle, dtype, shape, parameter,
|
|
):
|
|
model = tiny_handle.model
|
|
value = torch.zeros(shape, dtype=dtype)
|
|
if parameter:
|
|
value = torch.nn.Parameter(value, requires_grad=False)
|
|
model.register_parameter("counts", value)
|
|
else:
|
|
model.register_buffer("counts", value)
|
|
# A global quantizer alone does not turn unrelated buffers into packed weights.
|
|
model.hf_quantizer = object()
|
|
assert admission._expanded_tensor_bytes(model, value, model) == value.numel() * 4
|
|
demand = model_phase_demand(tiny_handle, "probe")
|
|
assert demand.devices["cpu"].staging_bytes == 0
|
|
admit_model_phase(tiny_handle, "probe", observer=ample_memory)
|
|
|
|
|
|
def test_known_packed_linear_can_supply_logical_dimensions(tiny_handle):
|
|
owner = torch.nn.Module()
|
|
owner.in_features, owner.out_features = 16, 32
|
|
packed_type = type("Params4bit", (torch.nn.Parameter,), {})
|
|
owner.register_parameter("weight", packed_type(
|
|
torch.zeros(128, dtype=torch.uint8), requires_grad=False,
|
|
))
|
|
tiny_handle.model.add_module("packed_linear", owner)
|
|
assert admission._expanded_tensor_bytes(owner, owner.weight) == 16 * 32 * 4
|
|
admit_model_phase(tiny_handle, "probe", observer=ample_memory)
|
|
|
|
|
|
def test_default_broker_check_counts_reservations_outside_phase_devices(monkeypatch):
|
|
monkeypatch.setattr("obliteratus.gpu_lifecycle.current_process_grant", lambda: 4 * GIB)
|
|
monkeypatch.setattr(admission, "observe_memory", ample_memory)
|
|
monkeypatch.setattr(torch.cuda, "device_count", lambda: 2)
|
|
monkeypatch.setattr(torch.cuda, "memory_reserved", lambda index: 3 * GIB if index == 1 else 0)
|
|
with pytest.raises(PhaseAdmissionError, match="GPU lifecycle broker grant"):
|
|
admit_phase("probe", {"cuda:0": DeviceDemand(activation_bytes=2 * GIB)})
|
|
|
|
|
|
@pytest.mark.parametrize("model_cache,expected", [(True, True), (False, False), (None, True)])
|
|
def test_generation_cache_none_inherits_model_default(tiny_handle, model_cache, expected,
|
|
tmp_path, monkeypatch):
|
|
pipeline, _, _, _ = _stub_pipeline(tiny_handle, tmp_path, monkeypatch)
|
|
tiny_handle.model.generation_config.use_cache = None
|
|
# Also model older/remote configs that permit None; current GPT2 is strict.
|
|
monkeypatch.setitem(tiny_handle.model.config.__dict__, "use_cache", model_cache)
|
|
workloads, _ = pipeline._phase_forward_workloads("baseline", 1, 8, diagnostic="coherence")
|
|
generation = [workload for workload in workloads if workload.cache_tokens is not None]
|
|
assert generation
|
|
assert all(workload.use_cache is expected for workload in generation)
|
|
assert tiny_handle.model.generation_config.use_cache is None
|
|
assert tiny_handle.model.config.use_cache is model_cache
|
|
monkeypatch.setattr(admission, "observe_memory", ample_memory)
|
|
assert pipeline.run() == tmp_path
|
|
|
|
|
|
def test_generation_cache_none_defaults_true_when_model_has_no_cache_setting(
|
|
tiny_handle, tmp_path, monkeypatch,
|
|
):
|
|
pipeline, _, _, _ = _stub_pipeline(tiny_handle, tmp_path, monkeypatch)
|
|
tiny_handle.model.generation_config.use_cache = None
|
|
del tiny_handle.model.config.use_cache
|
|
workloads, _ = pipeline._phase_forward_workloads("baseline", 1, 8, diagnostic="coherence")
|
|
assert all(workload.use_cache for workload in workloads)
|
|
|
|
|
|
@pytest.mark.parametrize("phase", ["baseline", "probe", "distill", "validate",
|
|
"excise", "verify", "rebirth"])
|
|
def test_pipeline_modelling_exception_uses_stage_failure_contract(
|
|
tiny_handle, tmp_path, monkeypatch, phase,
|
|
):
|
|
pipeline, work, events, _ = _stub_pipeline(tiny_handle, tmp_path, monkeypatch)
|
|
original = pipeline._phase_forward_workloads
|
|
failure = LookupError("unsupported execution metadata")
|
|
|
|
def plan(stage, *args, **kwargs):
|
|
if stage == phase:
|
|
raise failure
|
|
return original(stage, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(pipeline, "_phase_forward_workloads", plan)
|
|
monkeypatch.setattr(admission, "observe_memory", ample_memory)
|
|
cleanup = Mock()
|
|
monkeypatch.setattr(pipeline, "cleanup_failed_run", cleanup)
|
|
with pytest.raises(PipelineFailure) as caught:
|
|
pipeline.run()
|
|
assert caught.value.stage == phase
|
|
cause = caught.value.__cause__
|
|
assert isinstance(cause, PhaseAdmissionError)
|
|
assert cause.phase == phase
|
|
assert cause.__cause__ is failure
|
|
assert "Check model configuration" in str(cause)
|
|
for operation in work[phase].values():
|
|
operation.assert_not_called()
|
|
assert events[-1].stage == phase and events[-1].status == "error"
|
|
cleanup.assert_called_once_with()
|
|
|
|
|
|
@pytest.mark.parametrize("entry", [model_phase_demand, admit_model_phase])
|
|
def test_phase_estimator_wraps_validation_and_unexpected_errors(tiny_handle, monkeypatch, entry):
|
|
with pytest.raises(PhaseAdmissionError, match="Phase 'probe'"):
|
|
entry(tiny_handle, "probe", retained_rows=-1)
|
|
default = entry(tiny_handle, "probe", retained_rows=-1, mode="default")
|
|
if isinstance(default, PhaseDemand):
|
|
default = admit_phase("probe", default, observer=ample_memory, mode="default")
|
|
assert default.warnings
|
|
failure = KeyError("missing execution metadata")
|
|
monkeypatch.setattr(admission, "_model_inventory", Mock(side_effect=failure))
|
|
with pytest.raises(PhaseAdmissionError, match="Phase 'verify'") as caught:
|
|
entry(tiny_handle, "verify")
|
|
assert caught.value.__cause__ is failure
|
|
default = entry(tiny_handle, "verify", mode="default")
|
|
if isinstance(default, PhaseDemand):
|
|
default = admit_phase("verify", default, observer=ample_memory, mode="default")
|
|
assert "missing execution metadata" in "\n".join(default.warnings)
|
|
|
|
|
|
def test_admit_model_phase_wraps_invalid_estimator_keyword(tiny_handle):
|
|
with pytest.raises(PhaseAdmissionError, match="Phase 'probe'") as caught:
|
|
admit_model_phase(tiny_handle, "probe", invalid_geometry=True)
|
|
assert isinstance(caught.value.__cause__, TypeError)
|
|
report = admit_model_phase(tiny_handle, "probe", invalid_geometry=True, mode="default")
|
|
assert "invalid_geometry" in "\n".join(report.warnings)
|
|
|
|
|
|
@pytest.mark.parametrize("phase", ["baseline", "probe", "distill", "validate",
|
|
"excise", "verify", "rebirth"])
|
|
def test_pipeline_modelling_exception_warns_and_continues_default(
|
|
tiny_handle, tmp_path, monkeypatch, phase, caplog,
|
|
):
|
|
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "default")
|
|
pipeline, work, events, _ = _stub_pipeline(tiny_handle, tmp_path, monkeypatch)
|
|
original = pipeline._phase_forward_workloads
|
|
|
|
def plan(stage, *args, **kwargs):
|
|
if stage == phase:
|
|
raise LookupError("unsupported execution metadata")
|
|
return original(stage, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(pipeline, "_phase_forward_workloads", plan)
|
|
monkeypatch.setattr(admission, "observe_memory", ample_memory)
|
|
assert pipeline.run() == tmp_path
|
|
for operation in work[phase].values():
|
|
operation.assert_called_once_with()
|
|
assert not any(event.status == "error" for event in events)
|
|
warnings = [
|
|
record.message for record in caplog.records if "ADMISSION WARNING" in record.message
|
|
]
|
|
assert any(f"phase '{phase}'" in message and "unsupported execution metadata" in message
|
|
for message in warnings)
|
|
|
|
|
|
def test_unknown_attention_backend_warns_by_default_and_rejects_strict(tiny_handle):
|
|
tiny_handle.model.config._attn_implementation = "custom_unverified_kernel"
|
|
message = "unmodelled attention backend.*OBLITERATUS_PHASE_ADMISSION=off"
|
|
assert_unknown_warns_by_default(tiny_handle, "probe", message)
|
|
|
|
|
|
def test_export_rejection_precedes_any_checkpoint_file(tiny_handle, tmp_path, monkeypatch):
|
|
from obliteratus.models import checkpoint_export
|
|
|
|
pipeline, _, events, _ = _stub_pipeline(tiny_handle, tmp_path, monkeypatch)
|
|
pipeline.output_dir = tmp_path / "checkpoint"
|
|
monkeypatch.setattr(pipeline, "_rebirth", AbliterationPipeline._rebirth.__get__(pipeline))
|
|
writer = Mock(wraps=pipeline._write_local_checkpoint)
|
|
monkeypatch.setattr(pipeline, "_write_local_checkpoint", writer)
|
|
native_writer = Mock(wraps=checkpoint_export.write_native_checkpoint)
|
|
monkeypatch.setattr(checkpoint_export, "write_native_checkpoint", native_writer)
|
|
assert checkpoint_export.plan_native_export(tiny_handle.model) is not None
|
|
|
|
def observer(device):
|
|
return (MemoryObservation(device, 0, 100 * GIB)
|
|
if pipeline._active_stage == "rebirth" else ample_memory(device))
|
|
|
|
monkeypatch.setattr(admission, "observe_memory", observer)
|
|
with pytest.raises(PipelineFailure) as caught:
|
|
pipeline.run()
|
|
assert caught.value.stage == "rebirth"
|
|
assert isinstance(caught.value.__cause__, PhaseAdmissionError)
|
|
assert caught.value.__cause__.required_bytes > 0
|
|
writer.assert_not_called()
|
|
native_writer.assert_not_called()
|
|
assert not list(tmp_path.rglob("*"))
|
|
assert events[-1].stage == "rebirth" and events[-1].status == "error"
|
|
assert pipeline._checkpoint_prepared_model is None
|
|
assert pipeline._checkpoint_preparation_logs == []
|
|
|
|
|
|
@pytest.mark.parametrize("failure", ["budget", "preparation", "estimator"])
|
|
@pytest.mark.parametrize("had_quantized_flag", [False, True])
|
|
def test_rejected_export_restores_quantization_metadata(
|
|
tiny_handle, tmp_path, monkeypatch, failure, had_quantized_flag,
|
|
):
|
|
from transformers.quantizers.base import HfQuantizer
|
|
|
|
pipeline, work, _, logs = _stub_pipeline(tiny_handle, tmp_path, monkeypatch)
|
|
model = tiny_handle.model
|
|
config = model.config
|
|
config.quantization_config = {"quant_method": "fp8"}
|
|
model.quantization_method = "fp8"
|
|
model._obliteratus_dequantized_scheme = "fp8"
|
|
model._weight_conversions = [object()]
|
|
if had_quantized_flag:
|
|
model.is_quantized = True
|
|
else:
|
|
vars(model).pop("is_quantized", None)
|
|
|
|
def strip(model):
|
|
HfQuantizer.remove_quantization_config(None, model)
|
|
if failure == "preparation":
|
|
raise RuntimeError("failed after stripping quantization metadata")
|
|
|
|
model.hf_quantizer = SimpleNamespace(remove_quantization_config=Mock(side_effect=strip))
|
|
snapshots = [
|
|
(owner, names, {name: vars(owner)[name] for name in names if name in vars(owner)})
|
|
for owner, names in (
|
|
(model, ("hf_quantizer", "quantization_method", "is_quantized", "_weight_conversions")),
|
|
(config, ("quantization_config",)),
|
|
(pipeline, ("_checkpoint_prepared_model", "_checkpoint_preparation_logs")),
|
|
)
|
|
]
|
|
monkeypatch.setattr(admission, "observe_memory", lambda name: MemoryObservation(name, 0, GIB))
|
|
if failure == "estimator":
|
|
monkeypatch.setattr(
|
|
admission, "model_phase_demand", Mock(side_effect=ValueError("bad layout")),
|
|
)
|
|
with pytest.raises(PhaseAdmissionError, match="Phase 'rebirth'"):
|
|
pipeline._admit_pipeline_phase("rebirth")
|
|
for owner, names, original in snapshots:
|
|
for name in names:
|
|
assert (name in vars(owner)) == (name in original)
|
|
if name in original:
|
|
assert vars(owner)[name] is original[name]
|
|
model.hf_quantizer.remove_quantization_config.assert_called_once_with(model)
|
|
assert model.config is config
|
|
assert model._obliteratus_dequantized_scheme == "fp8"
|
|
assert logs == []
|
|
work["rebirth"]["_rebirth"].assert_not_called()
|
|
assert not list(tmp_path.rglob("*"))
|
|
|
|
|
|
def test_export_admission_plans_normalized_model_and_defers_existing_logs(
|
|
tiny_handle, tmp_path, monkeypatch,
|
|
):
|
|
from obliteratus.models import checkpoint_export
|
|
|
|
pipeline, _, _, logs = _stub_pipeline(tiny_handle, tmp_path, monkeypatch)
|
|
model = tiny_handle.model
|
|
model.config.quantization_config = {"quant_method": "fp8"}
|
|
model._obliteratus_dequantized_scheme = "fp8"
|
|
strip = Mock(side_effect=lambda model: setattr(model, "hf_quantizer", None))
|
|
model.hf_quantizer = SimpleNamespace(remove_quantization_config=strip)
|
|
assert checkpoint_export.plan_native_export(model) is None
|
|
monkeypatch.setattr(admission, "observe_memory", ample_memory)
|
|
admit = Mock(wraps=admission.admit_model_phase)
|
|
monkeypatch.setattr(admission, "admit_model_phase", admit)
|
|
planner = Mock(wraps=checkpoint_export.plan_native_export)
|
|
monkeypatch.setattr(checkpoint_export, "plan_native_export", planner)
|
|
pipeline._admit_pipeline_phase("rebirth", max_shard_size=1024)
|
|
planner.assert_called_once_with(model, max_shard_size=1024)
|
|
assert checkpoint_export.plan_native_export(model, max_shard_size=1024) is not None
|
|
assert admit.call_args.kwargs["max_shard_size"] == 1024
|
|
strip.assert_called_once_with(model)
|
|
assert logs == []
|
|
pipeline._prepare_native_checkpoint_model()
|
|
assert len(logs) == 2
|
|
assert logs[0].startswith("Stripping native quantization config")
|
|
assert logs[1].startswith("Input checkpoint was fp8")
|
|
pipeline._prepare_native_checkpoint_model()
|
|
assert len(logs) == 2
|
|
strip.assert_called_once_with(model)
|