Files
OBLITERATUS/tests/test_capacity_planning.py

2819 lines
156 KiB
Python

"""Offline capacity maths and config-only metadata contracts; no weights fetched."""
from __future__ import annotations
import copy
import argparse
import hashlib
import json
from pathlib import Path
from unittest.mock import MagicMock, Mock
import pytest
import torch
from transformers import AutoConfig, AutoModelForCausalLM, AutoModelForImageTextToText, GenerationConfig
from obliteratus.capacity_planning import (
CapacityPlanningError, Workload, add_capacity_arguments, maybe_plan, meta_inventory, plan_capacity,
)
from obliteratus.qualification import parameter_device
pytestmark = pytest.mark.cpu
GIB = 1024**3
CONFIGS = Path(__file__).parent / "fixtures" / "capacity_configs"
QWEN2_CASES = [
("qwen2.5-72b-instruct.json", 145_412_407_296, 1_755_369_472),
("qwen2.5-32b-instruct.json", 65_527_752_704, 975_210_496),
]
UNSUPPORTED_GENERATION_MODES = [
({"penalty_alpha": 0.6, "top_k": 4, "num_beams": 1}, "contrastive_search"),
({"penalty_alpha": 0.6}, "contrastive_search"),
({"penalty_alpha": 0.6, "top_k": None, "do_sample": True}, "contrastive_search"),
({"num_beams": 4, "num_beam_groups": 2}, "group_beam_search"),
({"num_beams": 4, "constraints": []}, "constrained_beam_search"),
({"num_beams": 4, "force_words_ids": [[1]]}, "constrained_beam_search"),
({"dola_layers": "low"}, "dola_generation"),
]
def read_config(name):
return json.loads((CONFIGS / name).read_text())
def test_forward_demand_reuse_is_local_and_keeps_output_changes(tiny_inventory, monkeypatch):
from obliteratus import capacity_planning, phase_admission
inventory = copy.deepcopy(tiny_inventory)
inventory["text_config"].update(use_cache=True, output_hidden_states=False, output_attentions=False)
inventory["generation_config"] = {}
demands = Mock(wraps=phase_admission.transformer_demands)
monkeypatch.setattr(phase_admission, "transformer_demands", demands)
workload = Workload(batch_size=2, sequence_length=128, generation_tokens=16)
mapping = {"": "cuda:0"}
ordinary = capacity_planning._forward(inventory, mapping, workload, generation=True, use_cache=True)
# Identical prefill bounds are evaluated once; decode remains distinct.
assert demands.call_count == 2
assert {call.kwargs["tokens"] for call in demands.call_args_list} == {1, 128}
assert capacity_planning._forward(inventory, mapping, workload, generation=True, use_cache=True) == ordinary
assert demands.call_count == 4
inventory["generation_config"].update(output_hidden_states=True, return_dict_in_generate=True)
retained = capacity_planning._forward(inventory, mapping, workload, generation=True, use_cache=True)
assert retained["cuda:0"]["activations"] > ordinary["cuda:0"]["activations"]
assert any(call.kwargs.get("retained_hidden_tokens", 0) > 0 for call in demands.call_args_list)
mapping[""] = "cuda:1"
moved = capacity_planning._forward(inventory, mapping, workload, generation=True, use_cache=True)
assert moved == {"cuda:1": retained["cuda:0"]}
@pytest.mark.parametrize("invalid", [None, 0, 1, "false", [], [False]])
def test_demand_reuse_keeps_strict_config_output_validation(tiny_inventory, invalid):
from obliteratus.capacity_planning import _forward
inventory = copy.deepcopy(tiny_inventory)
mapping = {"": "cuda:0"}
_forward(inventory, mapping, Workload(), generation=True, use_cache=True)
inventory["text_config"]["output_hidden_states"] = invalid
with pytest.raises(ValueError, match="^output_hidden_states must be boolean or None$"):
_forward(inventory, mapping, Workload(), generation=True, use_cache=True)
@pytest.mark.parametrize("model_cache,generation_cache", [(True, False), (False, True)])
def test_demand_reuse_keeps_distinct_prefill_cache_policies(tiny_inventory, monkeypatch, model_cache, generation_cache):
from obliteratus import capacity_planning, phase_admission
inventory = copy.deepcopy(tiny_inventory)
inventory["text_config"]["use_cache"] = model_cache
real_demands = phase_admission.transformer_demands
demands = Mock(wraps=real_demands)
monkeypatch.setattr(phase_admission, "transformer_demands", demands)
report = capacity_planning._forward(inventory, {"": "cuda:0"}, Workload(),
generation=True, use_cache=generation_cache)
prefills = [call.kwargs for call in demands.call_args_list if call.kwargs["tokens"] == 128]
assert len(prefills) == 2
assert {row["use_cache"] for row in prefills} == {True, False}
candidates = []
for call in demands.call_args_list:
demand = real_demands(*call.args, **call.kwargs)["cuda:0"]
candidates.append({"activations": demand.activation_bytes, "kv_cache": demand.kv_bytes,
"logits": demand.output_bytes, "onload": demand.staging_bytes})
assert any(row["kv_cache"] > 0 for row in candidates)
assert report["cuda:0"] == max(candidates, key=lambda row: sum(row.values()))
@pytest.mark.parametrize("attention", [None, 0, 1, [], [False], 0.5])
def test_demand_reuse_does_not_narrow_attention_config_values(tiny_inventory, attention):
from obliteratus.capacity_planning import _forward
# Unlike hidden-state output, the public helper uses this config field's
# truth value. Unhashable/noncanonical scalar keys must still reach it.
inventory = copy.deepcopy(tiny_inventory)
inventory["text_config"]["output_attentions"] = bool(attention)
expected = _forward(inventory, {"": "cuda:0"}, Workload(), generation=True, use_cache=True)
inventory["text_config"]["output_attentions"] = attention
actual = _forward(inventory, {"": "cuda:0"}, Workload(), generation=True, use_cache=True)
assert actual == expected
def test_final_placement_reuse_observes_prefixes_sizes_and_map_mutations(large_inventory):
inventory = copy.deepcopy(large_inventory)
budgets = {"cuda:0": 512 * GIB, "cuda:1": 512 * GIB}
mapping = {"": 0, "model.layers.1": "1", "model.layers.10": 1, "lm_head": "cuda:1"}
original = copy.deepcopy(inventory)
report = plan(inventory, budgets, device_map=mapping)
assert inventory == original
assert report["status"] == "feasible", report["refusals"]
assert report["block_devices"]["model.layers.1"] == "cuda:1"
assert report["block_devices"]["model.layers.10"] == "cuda:1"
assert report["block_devices"]["model.layers.11"] == "cuda:0"
for device in budgets:
sizes = [size for name, size in inventory["tensor_sizes"].items()
if ((name.startswith(("model.layers.1.", "model.layers.10.")) or name == "lm_head.weight")
== (device == "cuda:1"))]
assert report["bytes_by_device"][device] == sum(sizes)
for phase in ("load", "reload"):
assert report["phases"][phase][device]["components"]["checkpoint_device_staging"] == max(sizes)
mapping["model.layers.1"] = 0
mapping["lm_head"] = 0
moved = plan(inventory, budgets, device_map=mapping)
assert moved["block_devices"]["model.layers.1"] == "cuda:0"
assert moved["block_devices"]["model.layers.10"] == "cuda:1"
assert moved["bytes_by_device"] != report["bytes_by_device"]
old = inventory["tensor_sizes"]["lm_head.weight"]
size = 2**64 + 7
inventory["tensor_sizes"]["lm_head.weight"] = size
inventory["snapshot_tensor_sizes"]["lm_head.weight"] = size
inventory["module_sizes"]["lm_head"] = size
inventory["total_weight_bytes"] += size - old
changed = plan(inventory, budgets, device_map=mapping)
assert changed["status"] == "refused"
for phase in ("load", "reload"):
assert changed["phases"][phase]["cuda:0"]["components"]["checkpoint_device_staging"] == size
assert changed["phases"]["edit"]["cuda:0"]["components"]["edit_workspace"] == 8 * size
@pytest.mark.parametrize("explicit", [False, True])
def test_reuse_preserves_probe_vs_placement_first_error(tiny_inventory, explicit):
inventory = copy.deepcopy(tiny_inventory)
inventory["text_config"]["intermediate_size"] = 0
inventory["tensor_sizes"]["uncovered.weight"] = 1
mapping = {"model": "cuda:0", "model.layers.3": "cuda:1", "lm_head": "cuda:1"}
expected = ("^placement does not cover uncovered.weight$" if explicit
else "^missing or invalid decoder geometry: intermediate_size=0$")
with pytest.raises(ValueError, match=expected):
plan(inventory, device_map=mapping if explicit else None)
@pytest.mark.parametrize("mutation,expected", [
({"model.layers.0.mlp.up_proj.weight": 1}, "decoder block model.layers.0 must remain complete"),
({"": "invalid"}, "unresolved placement device"),
({"model.layers.3": 0}, "placement leaves requested device cuda:1 empty"),
])
def test_final_resolution_revalidates_mutated_maps(tiny_inventory, mutation, expected):
mapping = {"": 0, "model.layers.3": 1}
assert plan(tiny_inventory, device_map=mapping)["status"] == "feasible"
mapping.update(mutation)
with pytest.raises(ValueError, match=expected):
plan(tiny_inventory, device_map=mapping)
def test_automatic_classification_observes_added_nonblock_storage(tiny_inventory):
inventory = copy.deepcopy(tiny_inventory)
before = plan(inventory)
size = 2**64 + 1
inventory["tensor_sizes"]["lm_head.extra"] = size
inventory["snapshot_tensor_sizes"]["lm_head.extra"] = size
inventory["module_sizes"]["lm_head"] += size
inventory["total_weight_bytes"] += size
after = plan(inventory)
assert before["status"] == "feasible"
assert after["status"] == "refused"
assert sum(after["bytes_by_device"].values()) - sum(before["bytes_by_device"].values()) == size
head = after["device_map"]["lm_head"]
assert after["phases"]["load"][head]["components"]["checkpoint_device_staging"] == size
@pytest.mark.parametrize("output", ["output_hidden_states", "output_attentions"])
@pytest.mark.parametrize("outer,inner", [(False, False), (True, True), (True, False), (False, True)])
@pytest.mark.parametrize("explicit", [None, False, True])
def test_native_nested_output_defaults_keep_all_forward_bounds(tiny_inventory, output, outer, inner, explicit):
from obliteratus.phase_admission import transformer_demands
nested = dict(tiny_inventory["config"], **{output: inner})
inventory = meta_inventory(dict(tiny_inventory["config"], llm_config=nested, **{output: outer}),
generation_config={output: explicit})
assert inventory["text_config"][output] is outer
assert inventory["text_config"]["llm_config"][output] is inner
workload = Workload(samples=1, sequence_length=128, generation_tokens=1)
report = plan(inventory, {"cpu": 64 * GIB}, workload=workload, device_map={"": "cpu"})
cfg = dict(inventory["text_config"], _attn_implementation="eager")
options = dict(batch_size=1, dtype_bytes=2, resident_bytes={}, largest_tensor_bytes={},
input_device="cpu", output_device="cpu", attention_mask=True,
mamba_scan_backend="torch", use_cache=True, logits_copies=3)
placement = ["cpu"] * len(inventory["blocks"])
# The baseline uses nested defaults. Generation explicitly retains outputs
# enabled by the outer model config or generation flag. Neither may replace
# the other before the existing largest-complete-demand selection.
flags = {"output_hidden_states": False, "output_attentions": False,
output: outer or bool(explicit)}
demands = [
transformer_demands(cfg, placement, tokens=128, **options)["cpu"],
transformer_demands(cfg, placement, tokens=128, cache_tokens=128, **flags, **options)["cpu"],
transformer_demands(cfg, placement, tokens=1, cache_tokens=129, logits_tokens=1,
**flags, **options)["cpu"],
]
expected = max(demands, key=lambda demand: demand.activation_bytes + demand.kv_bytes
+ demand.output_bytes + demand.staging_bytes)
assert report["status"] == "feasible", report["refusals"]
for phase in ("baseline", "verify", "reload"):
components = report["phases"][phase]["cpu"]["components"]
assert {key: components[key] for key in ("activations", "kv_cache", "logits", "onload")} == {
"activations": expected.activation_bytes, "kv_cache": expected.kv_bytes,
"logits": expected.output_bytes, "onload": expected.staging_bytes,
}
@pytest.mark.parametrize("invalid", [None, 0, [], [False]])
def test_native_nested_default_error_precedes_valid_generation_override(tiny_inventory, invalid):
nested = dict(tiny_inventory["config"], output_hidden_states=invalid)
inventory = meta_inventory(dict(tiny_inventory["config"], llm_config=nested),
generation_config={"output_hidden_states": True})
with pytest.raises(ValueError, match="^output_hidden_states must be boolean or None$"):
plan(inventory, {"cpu": 64 * GIB}, device_map={"": "cpu"})
@pytest.mark.parametrize("output", ["output_hidden_states", "output_attentions"])
def test_native_nested_config_keeps_generation_flag_validation_first(tiny_inventory, output):
nested = dict(tiny_inventory["config"], hidden_size=0)
inventory = meta_inventory(dict(tiny_inventory["config"], llm_config=nested),
generation_config={output: 0})
with pytest.raises(ValueError, match=f"^{output} must be boolean or None$"):
plan(inventory, {"cpu": 64 * GIB}, device_map={"": "cpu"})
@pytest.mark.parametrize("output", ["output_hidden_states", "output_attentions"])
def test_nested_text_config_priority_and_none_fallback_preserve_reports(tiny_inventory, output):
nested = dict(tiny_inventory["config"], **{output: False})
inventory = meta_inventory(dict(tiny_inventory["config"], llm_config=nested, **{output: True}),
generation_config={})
options = dict(workload=Workload(samples=1, sequence_length=128, generation_tokens=1),
device_map={"": "cpu"})
expected = plan(inventory, {"cpu": 64 * GIB}, **options)
inventory["text_config"]["text_config"] = copy.deepcopy(nested)
inventory["text_config"]["llm_config"]["hidden_size"] = 0
assert plan(inventory, {"cpu": 64 * GIB}, **options) == expected
inventory["text_config"]["text_config"] = None
with pytest.raises(ValueError, match="^missing or invalid decoder geometry: hidden_size=0$"):
plan(inventory, {"cpu": 64 * GIB}, **options)
@pytest.fixture(scope="module")
def large_inventory():
return meta_inventory(read_config("qwen2.5-72b-instruct.json"))
@pytest.fixture(scope="module")
def tiny_inventory():
return meta_inventory({
"model_type": "qwen2", "architectures": ["Qwen2ForCausalLM"],
"vocab_size": 64, "hidden_size": 32, "intermediate_size": 64,
"num_hidden_layers": 6, "num_attention_heads": 4,
"num_key_value_heads": 2, "max_position_embeddings": 65536,
"tie_word_embeddings": False,
})
def plan(inventory, free_memory=None, **kwargs):
return plan_capacity(
inventory, free_memory or {"cuda:0": 80 * GIB, "cuda:1": 80 * GIB},
host_memory_bytes=kwargs.pop("host_memory_bytes", 512 * GIB),
disk_memory_bytes=kwargs.pop("disk_memory_bytes", 2048 * GIB),
**kwargs,
)
def test_config_fixtures_have_public_sources_and_content_digests():
provenance = read_config("provenance.json")
assert provenance["schema_version"] == 1
assert len(provenance["configs"]) == 3
for entry in provenance["configs"]:
payload = (CONFIGS / entry["file"]).read_bytes()
assert hashlib.sha256(payload).hexdigest() == entry["sha256"]
assert entry["source_url"].startswith("https://huggingface.co/Qwen/")
assert len(entry["config_revision"]) == 40
assert entry["construction"] == "published_config"
assert read_config("qwen3.8-27b.json") == json.loads(
(Path(__file__).parent / "fixtures" / "qwen38" / "config.json").read_text()
)
@pytest.mark.parametrize("filename,total_bytes,block_bytes", QWEN2_CASES)
def test_qwen2_exact_per_module_maths(filename, total_bytes, block_bytes):
config = read_config(filename)
inventory = meta_inventory(config)
hidden = config["hidden_size"]
kv_width = hidden // config["num_attention_heads"] * config["num_key_value_heads"]
# Q/K/V biases, attention/MLP matrices and two RMSNorms are all resident.
parameters_per_block = (
2 * hidden * hidden + 2 * hidden * kv_width
+ 3 * hidden * config["intermediate_size"] + 3 * hidden + 2 * kv_width
)
assert parameters_per_block * 2 == block_bytes
rotary_bytes = hidden // config["num_attention_heads"] // 2 * 4
rotary_sizes = {name: size for name, size in inventory["tensor_sizes"].items()
if name.startswith("model.rotary_emb.")}
assert rotary_sizes["model.rotary_emb.inv_freq"] == rotary_bytes
assert all(size == rotary_bytes for size in rotary_sizes.values())
assert inventory["total_weight_bytes"] == total_bytes + sum(rotary_sizes.values())
assert inventory["source_weight_bytes"] == total_bytes
assert inventory["module_sizes"]["model.rotary_emb"] == sum(rotary_sizes.values())
assert inventory["module_sizes"]["model.layers.0"] == block_bytes
assert inventory["module_sizes"]["model.embed_tokens"] == config["vocab_size"] * hidden * 2
assert inventory["module_sizes"]["lm_head"] == config["vocab_size"] * hidden * 2
assert len(inventory["blocks"]) == config["num_hidden_layers"]
@pytest.mark.parametrize("filename", [case[0] for case in QWEN2_CASES] + ["qwen3.8-27b.json"])
def test_published_configs_match_independent_meta_instantiation(filename, monkeypatch):
def forbidden(*args, **kwargs):
raise AssertionError("config-only planning must never fetch weights or inspect CUDA")
monkeypatch.setattr(torch.cuda, "is_available", forbidden)
monkeypatch.setattr(torch.cuda, "mem_get_info", forbidden)
monkeypatch.setattr(AutoModelForCausalLM, "from_pretrained", forbidden)
monkeypatch.setattr(AutoModelForImageTextToText, "from_pretrained", forbidden)
raw = read_config(filename)
config = AutoConfig.for_model(**raw)
factory = AutoModelForImageTextToText if raw["model_type"] == "qwen3_5" else AutoModelForCausalLM
with torch.device("meta"):
model = factory.from_config(config, dtype=torch.bfloat16, attn_implementation="eager")
model.tie_weights()
expected = {name: value.numel() * value.element_size() for name, value in model.state_dict().items()}
assert all(value.is_meta for value in model.state_dict().values())
expected.update((name, value.numel() * value.element_size())
for name, value in model.named_buffers(remove_duplicate=False))
inventory = meta_inventory(raw)
assert inventory["tensor_sizes"] == expected
assert inventory["total_weight_bytes"] == sum(expected.values())
if raw["model_type"] == "qwen3_5":
assert sum(value.numel() for value in model.parameters()) == 27_356_728_560
assert len(inventory["blocks"]) == 64
assert any(name.startswith("model.visual.") for name in inventory["tensor_sizes"])
assert inventory["config"]["text_config"]["layer_types"].count("linear_attention") == 48
@pytest.mark.parametrize("capacity_gib,offload", [(96, "none"), (80, "cpu")])
def test_72b_fits_small_workload_with_admitted_edit_placement(large_inventory, capacity_gib, offload):
budgets = {"cuda:0": capacity_gib * GIB, "cuda:1": capacity_gib * GIB}
report = plan(large_inventory, budgets, offload=offload)
assert report["status"] == "feasible", report.get("refusals")
assert not report["refusals"]
assert report["export"]["host_peak_with_offload_bytes"] < large_inventory["total_weight_bytes"] // 10
assert sum(report["bytes_by_device"].values()) == large_inventory["total_weight_bytes"]
assert all(report["bytes_by_device"][device] > 0 for device in budgets)
for block in large_inventory["blocks"]:
placements = {
parameter_device(name, report["device_map"])
for name in large_inventory["tensor_sizes"] if name.startswith(block + ".")
}
assert len(placements) == 1
for phase in report["phases"].values():
for device, capacity in budgets.items():
assert type(phase[device]["peak_bytes"]) is int
assert phase[device]["peak_bytes"] <= capacity
if offload == "cpu":
assert parameter_device("lm_head.weight", report["device_map"]) == "cpu"
assert report["phases"]["edit"]["cpu"]["components"]["edit_workspace"] > 17 * GIB
assert report["phases"]["verify"]["cuda:0"]["components"]["onload"] >= 2_491_416_576
def test_72b_two_80gib_devices_refuse_unoffloaded_head_edit(large_inventory):
report = plan(large_inventory)
assert report["status"] == "refused"
assert any(reason.startswith("edit/cuda:") for reason in report["refusals"])
assert parameter_device("lm_head.weight", report["device_map"]).startswith("cuda:")
def test_large_batch_and_sequence_refuse_even_when_weights_fit(large_inventory):
report = plan(large_inventory, {"cuda:0": 96 * GIB, "cuda:1": 96 * GIB},
workload=Workload(batch_size=16, sequence_length=32768))
assert report["status"] == "refused"
assert report["refusals"]
assert report["phases"]
def test_per_device_free_memory_cannot_be_replaced_with_aggregate(large_inventory):
report = plan(large_inventory, {"cuda:0": GIB, "cuda:1": 191 * GIB})
assert report["status"] == "refused"
assert report["refusals"]
def test_unequal_budgets_assign_more_blocks_to_larger_device(large_inventory):
report = plan(large_inventory, {"cuda:0": 48 * GIB, "cuda:1": 144 * GIB})
assert report["status"] == "feasible", report.get("refusals")
assert report["bytes_by_device"]["cuda:1"] > report["bytes_by_device"]["cuda:0"]
@pytest.mark.parametrize("capacity_gib", [(96, 96, 8), (12, 176, 8)])
def test_head_placement_search_fits_asymmetric_devices(large_inventory, capacity_gib):
budgets = {f"cuda:{index}": capacity * GIB for index, capacity in enumerate(capacity_gib)}
report = plan(large_inventory, budgets)
assert report["status"] == "feasible", report["refusals"]
assert parameter_device("lm_head.weight", report["device_map"]) != "cuda:2"
assert set(report["block_devices"].values()) == set(budgets)
for phase in report["phases"].values():
for device, budget in budgets.items():
assert phase[device]["peak_bytes"] <= budget
fixed = dict(report["device_map"], lm_head="cuda:2")
refused = plan(large_inventory, budgets, device_map=fixed)
assert refused["device_map"] == fixed
assert any(reason.startswith("edit/cuda:2 shortfall") for reason in refused["refusals"])
@pytest.mark.parametrize("offload", ["cpu", "disk"])
def test_head_can_remain_resident_when_offload_host_workspace_is_tight(large_inventory, offload):
budgets = {"cuda:0": 96 * GIB, "cuda:1": 96 * GIB, "cuda:2": 8 * GIB}
report = plan(large_inventory, budgets, offload=offload, host_memory_bytes=22 * GIB)
assert report["status"] == "feasible", report["refusals"]
assert parameter_device("lm_head.weight", report["device_map"]).startswith("cuda:")
assert offload in report["block_devices"].values()
assert all(rows["cpu"]["headroom_bytes"] >= 0 for rows in report["phases"].values())
@pytest.mark.parametrize("devices", [3, 4])
def test_every_requested_device_has_whole_block_ownership(tiny_inventory, devices):
budgets = {f"cuda:{index}": 8 * GIB for index in range(devices)}
report = plan(tiny_inventory, budgets)
assert report["status"] == "feasible", report.get("refusals")
owners = {parameter_device(block, report["device_map"]) for block in tiny_inventory["blocks"]}
assert owners == set(budgets)
def test_more_devices_than_decoder_blocks_refuses(tiny_inventory):
with pytest.raises(ValueError, match="decoder blocks"):
plan(tiny_inventory, {f"cuda:{index}": 8 * GIB for index in range(7)})
def test_explicit_map_cannot_split_a_decoder_block(tiny_inventory):
mapping = {"": "cuda:0", "model.layers.0.self_attn.q_proj": "cuda:1"}
with pytest.raises(ValueError, match="decoder block"):
plan(tiny_inventory, device_map=mapping)
def test_index_inventory_mismatch_refuses_before_download():
config = read_config("qwen2.5-32b-instruct.json")
inventory = meta_inventory(config, index={
"metadata": {"total_size": 65_527_752_704},
"weight_map": {"unexpected.weight": "model-00001-of-00001.safetensors"},
})
report = plan(inventory)
assert report["status"] == "refused"
assert any("index" in str(reason).lower() for reason in report["refusals"])
def test_complete_index_confirms_inventory(tiny_inventory):
checkpoint_sizes = tiny_inventory["snapshot_tensor_sizes"]
index = {
"metadata": {"total_size": sum(checkpoint_sizes.values())},
"weight_map": {name: "model.safetensors" for name in checkpoint_sizes},
}
inventory = meta_inventory(tiny_inventory["config"], index=index)
report = plan(inventory)
assert report["status"] == "feasible", report.get("refusals")
@pytest.mark.parametrize("model_type", ["llama", "qwen2", "gpt2"])
@pytest.mark.parametrize("execution,offload", [("cuda", "none"), ("cpu", "cpu"), ("cpu", "disk")])
def test_generated_maps_cover_nonpersistent_buffers_for_strict_load(model_type, execution, offload, monkeypatch):
from obliteratus.phase_admission import MemoryObservation, admit_model_load
config = {"model_type": model_type, "vocab_size": 64, "tie_word_embeddings": False}
if model_type == "gpt2":
config.update(n_layer=6, n_embd=32, n_head=4, n_positions=128)
else:
config.update(num_hidden_layers=6, hidden_size=32, intermediate_size=64,
num_attention_heads=4, num_key_value_heads=2, max_position_embeddings=128)
inventory = meta_inventory(config)
buffers = inventory["tensor_sizes"].keys() - inventory["snapshot_tensor_sizes"].keys()
if model_type != "gpt2":
assert "model.rotary_emb.inv_freq" in buffers
assert all(name.startswith("model.rotary_emb.") for name in buffers)
assert inventory["tensor_sizes"]["model.rotary_emb.inv_freq"] == 16
budgets = {"cuda:0": 64 * GIB, "cuda:1": 64 * GIB} if execution == "cuda" else {"cpu": 64 * GIB}
report = plan(inventory, budgets, offload=offload)
assert report["status"] == "feasible", report["refusals"]
assert all(parameter_device(name, report["device_map"]) in {*budgets, "disk"} for name in buffers)
monkeypatch.setattr("obliteratus.gpu_lifecycle.current_process_grant", lambda: None)
admitted = admit_model_load(
AutoConfig.for_model(**config), device="cuda:0" if execution == "cuda" else "cpu",
dtype="bfloat16", device_map=report["device_map"], mode="strict",
observer=lambda device: MemoryObservation(device, 512 * GIB, 512 * GIB),
)
assert admitted.demands
if model_type != "gpt2":
incomplete = dict(report["device_map"])
del incomplete["model.rotary_emb"]
with pytest.raises(ValueError, match="placement does not cover model.rotary_emb"):
plan(inventory, budgets, offload=offload, device_map=incomplete)
def test_tied_embedding_storage_is_counted_once_and_coplaced(tiny_inventory):
config = copy.deepcopy(tiny_inventory["config"])
config["tie_word_embeddings"] = True
tied = meta_inventory(config)
embedding_bytes = config["vocab_size"] * config["hidden_size"] * 2
assert tied["total_weight_bytes"] == tiny_inventory["total_weight_bytes"] - embedding_bytes
assert tied["tied_parameters"]
report = plan(tied)
assert report["status"] == "feasible", report["refusals"]
assert report["export"]["status"] == "capacity_estimate_only"
assert report["export"]["runtime_check_required"] is True
for group in tied["tied_parameters"]:
assert len({parameter_device(name, report["device_map"]) for name in group}) == 1
assert sum(report["bytes_by_device"].values()) == tied["total_weight_bytes"]
@pytest.mark.parametrize("model_type", ["gpt2", "llama", "qwen2"])
@pytest.mark.parametrize("tied", [False, True])
def test_snapshot_capacity_matches_runtime_logical_entries(tmp_path, model_type, tied):
from obliteratus.models.snapshot import DiskSnapshot, SnapshotError
config = {"model_type": model_type, "vocab_size": 64, "tie_word_embeddings": tied}
if model_type == "gpt2":
config.update(n_layer=1, n_embd=32, n_head=4, n_positions=128)
else:
config.update(num_hidden_layers=1, hidden_size=32, intermediate_size=64,
num_attention_heads=4, num_key_value_heads=2, max_position_embeddings=128)
inventory = meta_inventory(config)
model = AutoModelForCausalLM.from_config(AutoConfig.for_model(**config), torch_dtype=torch.bfloat16)
expected = {name: value.numel() * value.element_size() for name, value in model.state_dict().items()}
assert inventory["snapshot_tensor_sizes"] == expected
required = sum(expected.values()) + len(expected) * 12288
if model_type == "qwen2":
assert required == 211_264
cap = 200_000 if model_type == "qwen2" else required - 1
refused = plan(inventory, {"cpu": 64 * GIB}, snapshot_enabled=True, snapshot_max_bytes=cap)
assert refused["status"] == refused["snapshot"]["status"] == "refused"
assert refused["snapshot"]["required_disk_bytes"] == required
with pytest.raises(SnapshotError, match="disk storage"):
DiskSnapshot(model, directory=tmp_path, max_bytes=cap)
admitted = plan(inventory, {"cpu": 64 * GIB}, snapshot_enabled=True, snapshot_max_bytes=required)
assert admitted["status"] == "feasible", admitted["refusals"]
with DiskSnapshot(model, directory=tmp_path, max_bytes=required) as snapshot:
assert admitted["snapshot"]["required_disk_bytes"] == snapshot.policy["required_disk_bytes"]
assert admitted["snapshot"]["host_peak_bytes"] == snapshot.policy["host_peak_bytes"]
assert len(snapshot.records) == len(expected)
assert not list(tmp_path.iterdir())
@pytest.mark.parametrize("offload", ["cpu", "disk"])
def test_tied_snapshot_offload_counts_both_embedding_entries(tiny_inventory, offload):
config = dict(tiny_inventory["config"], tie_word_embeddings=True)
inventory = meta_inventory(config)
embedding_bytes = config["vocab_size"] * config["hidden_size"] * 2
mapping = {"": "cuda:0", "model.layers.0": "cuda:1", "model.embed_tokens": offload, "lm_head": offload}
report = plan(inventory, device_map=mapping, offload=offload, snapshot_enabled=True,
allow_unbounded_export=True)
assert report["status"] == "feasible", report["refusals"]
snapshot = report["snapshot"]
assert snapshot["overlay_bytes"] == 0
assert snapshot["disk_restore_bytes"] == 2 * embedding_bytes
logical_bytes = sum(inventory["snapshot_tensor_sizes"].values())
logical_count = len(inventory["snapshot_tensor_sizes"])
assert snapshot["required_disk_bytes"] == logical_bytes + logical_count * 12288 + 2 * snapshot["disk_restore_bytes"]
@pytest.mark.parametrize("backing,offload", [("cpu", "none"), ("cpu", "cpu"), ("disk", "none"), ("disk", "disk")])
def test_72b_offloaded_snapshot_reserves_repeated_restore_backing(large_inventory, backing, offload):
blocks = large_inventory["blocks"]
mapping = {"": "cuda:0", **{block: "cuda:1" for block in blocks[40:-1]},
blocks[-1]: backing, "lm_head": backing}
budgets = {"cuda:0": 96 * GIB, "cuda:1": 96 * GIB}
options = dict(device_map=mapping, offload=offload, snapshot_enabled=True)
refused = plan(large_inventory, budgets, snapshot_max_bytes=140 * GIB, **options)
snapshot = refused["snapshot"]
assert refused["status"] == snapshot["status"] == "refused"
assert snapshot["overlay_bytes"] == 0
assert snapshot["disk_restore_bytes"] == 4_246_786_048
assert snapshot["required_disk_bytes"] == 153_917_812_736
assert any("Snapshot policy refused" in reason for reason in refused["refusals"])
required = snapshot["required_disk_bytes"]
admitted = plan(large_inventory, budgets, snapshot_max_bytes=required, **options)
assert admitted["status"] == "feasible", admitted["refusals"]
disk = admitted["phases"]["save"]["disk"]
assert disk["components"]["snapshot"] == required
shortfall = plan(large_inventory, budgets, snapshot_max_bytes=required,
disk_memory_bytes=disk["peak_bytes"] - 1, **options)
assert shortfall["snapshot"]["status"] == "disk_backed"
assert shortfall["status"] == "refused"
assert shortfall["phases"]["save"]["disk"]["headroom_bytes"] == -1
@pytest.mark.parametrize("backing,explicit", [("cpu", False), ("cpu", True), ("disk", False),
("disk", True), ("mixed", True)])
def test_dispatched_snapshot_capacity_matches_runtime_loader(tmp_path, tiny_inventory, backing, explicit):
from accelerate.hooks import attach_align_device_hook
from accelerate.utils.offload import OffloadedWeightsLoader
from obliteratus.models.snapshot import DiskSnapshot, SnapshotError
options = dict(snapshot_enabled=True)
if explicit:
target = "cpu" if backing == "mixed" else backing
options["device_map"] = {"": "cuda:0", "model.layers.0": "cuda:1", "model.layers.4": target,
"model.layers.5": "disk" if backing == "mixed" else target, "lm_head": target}
else:
options["offload"] = backing
report = plan(tiny_inventory, **options)
assert report["status"] == "feasible", report["refusals"]
model = AutoModelForCausalLM.from_config(AutoConfig.for_model(**tiny_inventory["config"]), torch_dtype=torch.bfloat16)
# Dispatch's loader and prefixed hooks can exercise restore admission on CPU.
loader = OffloadedWeightsLoader(state_dict=model.state_dict())
for module, device in report["device_map"].items():
if device in {"cpu", "disk"}:
attach_align_device_hook(model.get_submodule(module), execution_device="cpu", offload=True,
weights_map=loader, module_name=module)
sizes = tiny_inventory["snapshot_tensor_sizes"]
offloaded = {name for name in sizes if parameter_device(name, report["device_map"]) in {"cpu", "disk"}}
assert offloaded
assert {name for name, value in model.state_dict().items() if value.is_meta} == offloaded
assert report["snapshot"]["disk_restore_bytes"] == sum(sizes[name] for name in offloaded)
old_cap = sum(sizes.values()) + len(sizes) * 12288
refused = plan(tiny_inventory, snapshot_max_bytes=old_cap, **options)
assert refused["status"] == refused["snapshot"]["status"] == "refused"
with pytest.raises(SnapshotError, match="disk storage"):
DiskSnapshot(model, directory=tmp_path, max_bytes=old_cap)
with DiskSnapshot(model, directory=tmp_path, max_bytes=report["snapshot"]["required_disk_bytes"]) as snapshot:
for key in ("required_disk_bytes", "disk_restore_bytes", "overlay_bytes", "host_peak_bytes"):
assert report["snapshot"][key] == snapshot.policy[key]
assert not list(tmp_path.iterdir())
@pytest.mark.parametrize("offload", ["none", "cpu", "disk"])
def test_cpu_snapshot_backing_matches_runtime(tmp_path, tiny_inventory, offload):
from accelerate import cpu_offload, dispatch_model
from obliteratus.models.snapshot import DiskSnapshot
report = plan(tiny_inventory, {"cpu": 64 * GIB}, offload=offload, snapshot_enabled=True)
assert report["status"] == "feasible", report["refusals"]
model = AutoModelForCausalLM.from_config(AutoConfig.for_model(**tiny_inventory["config"]), torch_dtype=torch.bfloat16)
if offload == "cpu":
cpu_offload(model, execution_device=torch.device("cpu"))
elif offload == "disk":
dispatch_model(model, report["device_map"], offload_dir=tmp_path / "offload")
with DiskSnapshot(model, directory=tmp_path, max_bytes=report["snapshot"]["required_disk_bytes"]) as snapshot:
for key in ("required_disk_bytes", "disk_restore_bytes", "overlay_bytes", "host_peak_bytes"):
assert report["snapshot"][key] == snapshot.policy[key]
logical_bytes = sum(tiny_inventory["snapshot_tensor_sizes"].values())
assert snapshot.policy["overlay_bytes"] == (logical_bytes if offload == "cpu" else 0)
@pytest.mark.parametrize("model_type", ["gpt2", "llama", "qwen2"])
def test_resident_tied_configs_admit_provisional_bounded_export(model_type):
config = {"model_type": model_type, "vocab_size": 64, "tie_word_embeddings": True}
if model_type == "gpt2":
config.update(n_layer=2, n_embd=32, n_head=4, n_positions=128)
else:
config.update(num_hidden_layers=2, hidden_size=32, intermediate_size=64,
num_attention_heads=4, num_key_value_heads=2, max_position_embeddings=128)
inventory = meta_inventory(config)
assert inventory["text_config"]["max_position_embeddings"] == 128
report = plan(inventory, {"cpu": 64 * GIB})
assert report["status"] == "feasible", report["refusals"]
assert inventory["tied_parameters"]
assert report["export"]["eligible"] and report["export"]["runtime_check_required"]
@pytest.mark.parametrize("memory", ["host_memory_bytes", "disk_memory_bytes"])
def test_host_and_disk_are_independent_hard_budgets(large_inventory, memory):
report = plan(large_inventory, {"cuda:0": 96 * GIB, "cuda:1": 96 * GIB}, **{memory: 1})
assert report["status"] == "refused"
assert report["refusals"]
@pytest.mark.parametrize("devices", [1, 2])
def test_hybrid_config_refuses_unqualified_kernel_workspace(devices):
inventory = meta_inventory(read_config("qwen3.8-27b.json"))
report = plan(inventory, {f"cuda:{index}": 96 * GIB for index in range(devices)})
assert report["status"] == "refused"
assert any("single" in str(reason).lower() or "hybrid" in str(reason).lower()
for reason in report["refusals"])
def test_phase_components_sum_to_peak_and_headroom(tiny_inventory):
report = plan(tiny_inventory)
assert set(report["phases"]) == {
"load", "baseline", "activations", "direction", "edit", "verify", "save", "reload",
}
for rows in report["phases"].values():
for row in rows.values():
assert all(type(value) is int and value >= 0 for value in row["components"].values())
assert row["peak_bytes"] == sum(row["components"].values())
assert row["headroom_bytes"] == row["budget_bytes"] - row["peak_bytes"]
assert report["snapshot"]["status"] == "disabled_with_reason"
assert report["snapshot"]["restore_available"] is False
assert report["snapshot"]["reason"]
assert report["phases"]["save"]["disk"]["components"]["snapshot"] == 0
assert report["runtime_check_required"] is True
assert report["export"]["runtime_check_required"] is True
def test_concept_transactions_charge_retained_host_rollback(tiny_inventory):
ordinary = plan(tiny_inventory)
concept = plan(tiny_inventory, concept_edit=True)
expected = sum(size for name, size in tiny_inventory["tensor_sizes"].items()
if name.endswith((".o_proj.weight", ".down_proj.weight")))
assert concept["edit_contract"] == "concept_output_transactions"
assert concept["phases"]["edit"]["cpu"]["components"]["concept_transaction_rollback"] == expected
assert concept["phases"]["edit"]["cpu"]["peak_bytes"] == ordinary["phases"]["edit"]["cpu"]["peak_bytes"] + expected
@pytest.mark.parametrize("offload", ["none", "disk"])
@pytest.mark.parametrize("snapshot", [False, True])
def test_reload_disk_budget_counts_only_new_offload_and_reserve(large_inventory, offload, snapshot):
initial = (300 if offload == "none" and not snapshot else 512) * GIB
mapping = {"": "cuda:0", "lm_head": "cuda:1" if offload == "none" else offload}
mapping.update({block: "cuda:0" if index < 40 else "cuda:1"
for index, block in enumerate(large_inventory["blocks"])})
if offload != "none":
mapping[large_inventory["blocks"][-1]] = offload
report = plan(large_inventory, {"cuda:0": 96 * GIB, "cuda:1": 96 * GIB},
source_staged=True, disk_memory_bytes=initial,
device_map=mapping, offload=offload, snapshot_enabled=snapshot,
snapshot_max_bytes=256 * GIB)
assert report["status"] == "feasible", report["refusals"]
rows = report["phases"]
assert rows["save"]["disk"]["components"]["output_and_staging"] == 2 * large_inventory["total_weight_bytes"]
assert rows["reload"]["disk"]["components"] == {
"source_download": 0, "output_and_staging": 0,
"offload": report["bytes_by_device"].get("disk", 0), "edit_backing": 0,
"reload_reservation": 0, "snapshot": 0, "reserve": GIB,
}
remaining = initial - large_inventory["total_weight_bytes"] - rows["save"]["disk"]["components"]["snapshot"]
assert rows["reload"]["disk"]["peak_bytes"] <= remaining
def test_fallback_requires_explicit_control_and_host_capacity(tiny_inventory):
inventory = copy.deepcopy(tiny_inventory)
inventory["export"] = {"eligible": False, "bounded": False}
assert plan(inventory)["status"] == "refused"
allowed = plan(inventory, allow_unbounded_export=True)
assert allowed["status"] == "feasible"
assert allowed["export"]["bounded"] is False
assert allowed["export"]["operator_allowed_fallback"] is True
assert allowed["export"]["host_peak_bytes"] == 4 * inventory["total_weight_bytes"]
assert plan(inventory, allow_unbounded_export=True, host_memory_bytes=1)["status"] == "refused"
@pytest.mark.parametrize("offload,rehearsal", [("cpu", False), ("disk", False), ("cpu", True)])
def test_offloaded_ties_admit_fallback_before_freezing_capacity(tiny_inventory, offload, rehearsal):
inventory = meta_inventory(dict(tiny_inventory["config"], tie_word_embeddings=True))
mapping = {"": "cpu"} if rehearsal else {
"": "cuda:0", "model.layers.0": "cuda:1", "model.embed_tokens": offload, "lm_head": offload,
}
budgets = {"cpu": 64 * GIB} if rehearsal else {"cuda:0": 8 * GIB, "cuda:1": 8 * GIB}
options = dict(device_map=mapping, offload=offload, source_shard_bytes=0, host_reserve_bytes=0)
refused = plan(inventory, budgets, **options)
assert refused["status"] == "refused"
assert not refused["export"]["eligible"] and not refused["export"]["bounded"]
assert any("whole-state fallback is not admitted" in reason for reason in refused["refusals"])
admitted = plan(inventory, budgets, allow_unbounded_export=True, **options)
assert admitted["status"] == "feasible", admitted["refusals"]
assert admitted["export"]["operator_allowed_fallback"] is True
assert admitted["export"]["status"] == "operator_fallback"
assert admitted["export"]["host_peak_bytes"] == 4 * inventory["total_weight_bytes"]
save = admitted["phases"]["save"]["cpu"]
assert save["components"]["bounded_export"] >= 4 * inventory["total_weight_bytes"]
shortfall = plan(inventory, budgets, allow_unbounded_export=True,
host_memory_bytes=save["peak_bytes"] - 1, **options)
assert any(reason.startswith("save/cpu shortfall") for reason in shortfall["refusals"])
def test_source_shard_staging_accounts_for_source_dtype(tiny_inventory):
config = copy.deepcopy(tiny_inventory["config"])
config["dtype"] = "float32"
config.pop("torch_dtype", None)
checkpoint_sizes = tiny_inventory["snapshot_tensor_sizes"]
index = {"metadata": {"total_size": 2 * sum(checkpoint_sizes.values())},
"weight_map": {name: "model.safetensors" for name in checkpoint_sizes}}
inventory = meta_inventory(config, index=index)
assert inventory["source_shards"]["model.safetensors"] == 2 * sum(checkpoint_sizes.values())
@pytest.mark.parametrize("source_key", ["dtype", "torch_dtype"])
@pytest.mark.parametrize("source_dtype,scale", [("float32", 2), ("bfloat16", 1)])
@pytest.mark.parametrize("target_dtype", ["bfloat16", "float16", "float32"])
@pytest.mark.parametrize("indexed", [False, True])
def test_source_bytes_without_index_total_use_checkpoint_dtype(
tiny_inventory, source_key, source_dtype, scale, target_dtype, indexed,
):
config = copy.deepcopy(tiny_inventory["config"])
config.pop("dtype", None)
config.pop("torch_dtype", None)
config[source_key] = source_dtype
index = {"weight_map": {name: "model.safetensors" for name in tiny_inventory["snapshot_tensor_sizes"]}}
inventory = meta_inventory(config, dtype=target_dtype, index=index if indexed else None)
expected = scale * sum(tiny_inventory["snapshot_tensor_sizes"].values())
assert inventory["source_weight_bytes"] == expected
if indexed:
assert inventory["source_shards"] == {"model.safetensors": expected}
def test_missing_source_dtype_conservatively_reserves_fp32(tiny_inventory):
config = copy.deepcopy(tiny_inventory["config"])
config.pop("dtype", None)
config.pop("torch_dtype", None)
inventory = meta_inventory(config)
assert inventory["source_weight_bytes"] == 2 * sum(tiny_inventory["snapshot_tensor_sizes"].values())
def test_index_total_remains_authoritative_for_source_disk_bytes(tiny_inventory):
config = dict(tiny_inventory["config"], dtype="float32")
sizes = tiny_inventory["snapshot_tensor_sizes"]
index = {"metadata": {"total_size": sum(sizes.values())},
"weight_map": {name: "model.safetensors" for name in sizes}}
inventory = meta_inventory(config, index=index)
assert inventory["source_weight_bytes"] == sum(sizes.values())
@pytest.mark.parametrize("indexed", [False, True])
def test_32b_fp32_source_download_refuses_200gib_disk(indexed):
config = read_config("qwen2.5-32b-instruct.json")
ordinary = meta_inventory(config)
index = {"weight_map": {name: "model.safetensors" for name in ordinary["snapshot_tensor_sizes"]}}
inventory = meta_inventory(dict(config, torch_dtype="float32"), index=index if indexed else None)
report = plan(inventory, disk_memory_bytes=200 * GIB)
assert plan(ordinary, disk_memory_bytes=200 * GIB)["status"] == "feasible"
assert inventory["source_weight_bytes"] == 2 * ordinary["source_weight_bytes"]
assert inventory["total_weight_bytes"] == ordinary["total_weight_bytes"]
assert report["status"] == "refused"
assert any(reason.startswith("save/disk shortfall") for reason in report["refusals"])
disk = report["phases"]["save"]["disk"]
assert disk["components"]["source_download"] == inventory["source_weight_bytes"]
assert 245 * GIB < disk["peak_bytes"] < 246 * GIB
staged = plan(inventory, disk_memory_bytes=200 * GIB, source_staged=True)
assert staged["status"] == "feasible", staged["refusals"]
assert staged["phases"]["save"]["disk"]["components"]["source_download"] == 0
def test_dtype_changes_storage_bytes_without_changing_parameter_shapes(tiny_inventory):
fp32 = meta_inventory(tiny_inventory["config"], dtype="float32")
assert fp32["dtype_bytes"] == 4
expected = {name: size * (2 if name in tiny_inventory["snapshot_tensor_sizes"] else 1)
for name, size in tiny_inventory["tensor_sizes"].items()}
assert fp32["tensor_sizes"] == expected
assert fp32["total_weight_bytes"] == sum(expected.values())
@pytest.mark.parametrize("offload", ["cpu", "disk"])
def test_offload_keeps_a_complete_block_and_admits_onload_staging(tiny_inventory, offload):
report = plan(tiny_inventory, offload=offload)
assert report["status"] == "feasible", report.get("refusals")
assert report["bytes_by_device"][offload] > 0
assert any(parameter_device(block, report["device_map"]) == offload
for block in tiny_inventory["blocks"])
assert report["phases"]["verify"]["cuda:0"]["components"]["onload"] > 0
assert report["export"]["host_peak_with_offload_bytes"] > report["export"]["host_peak_bytes"]
@pytest.mark.parametrize("offload", ["cpu", "disk"])
@pytest.mark.parametrize("owner", [None, "missing.module", "lm_head"])
def test_explicit_map_requires_weights_on_requested_offload(tiny_inventory, offload, owner):
mapping = plan(tiny_inventory)["device_map"]
if owner is not None:
mapping[owner] = offload
report = plan(tiny_inventory, device_map=mapping, offload=offload)
if owner == "lm_head":
assert report["status"] == "feasible", report["refusals"]
assert report["bytes_by_device"][offload] > 0
else:
assert report["status"] == "refused"
assert report["refusals"] == [f"requested {offload} offload has no assigned weights"]
def test_workload_scales_kv_cache_and_retained_observations(tiny_inventory):
ordinary = plan(tiny_inventory)
doubled = plan(tiny_inventory, workload=Workload(batch_size=2, samples=64))
for device in ("cuda:0", "cuda:1"):
base_kv = ordinary["phases"]["verify"][device]["components"]["kv_cache"]
actual_kv = doubled["phases"]["verify"][device]["components"]["kv_cache"]
assert actual_kv == 2 * base_kv
base_retained = ordinary["phases"]["activations"]["cpu"]["components"]["retained_activations"]
actual_retained = doubled["phases"]["activations"]["cpu"]["components"]["retained_activations"]
assert actual_retained == 2 * base_retained
@pytest.mark.parametrize("explicit_map", [False, True])
def test_32b_uncached_generation_refuses_before_loading_weights(explicit_map):
config = read_config("qwen2.5-32b-instruct.json")
cached = meta_inventory(dict(config, use_cache=True))
uncached = meta_inventory(dict(config, use_cache=False))
budgets = {"cuda:0": 48 * GIB, "cuda:1": 48 * GIB}
workload = Workload(sequence_length=128, generation_tokens=8192)
ordinary = plan(cached, budgets, workload=workload)
assert ordinary["status"] == "feasible", ordinary["refusals"]
report = plan(uncached, budgets, workload=workload,
device_map=ordinary["device_map"] if explicit_map else None)
assert report["status"] == "refused"
assert report["cache_policy"]["generation_use_cache"] is False
for phase in ("baseline", "verify", "reload"):
assert any(reason.startswith(f"{phase}/cuda:") for reason in report["refusals"])
for device in budgets:
components = report["phases"][phase][device]["components"]
assert components["kv_cache"] == 0
assert components["activations"] > ordinary["phases"][phase][device]["components"]["activations"]
@pytest.mark.parametrize("use_cache", [True, False])
@pytest.mark.parametrize("num_beams,num_return_sequences", [(8, 1), (8, 4)])
def test_generation_expands_batches_without_expanding_activation_collection(
tiny_inventory, monkeypatch, use_cache, num_beams, num_return_sequences,
):
from obliteratus import phase_admission
inventory = copy.deepcopy(tiny_inventory)
inventory["generation_config"].update(use_cache=use_cache, num_beams=1, num_return_sequences=1)
workload = Workload(batch_size=2, sequence_length=16, generation_tokens=512)
ordinary = plan(inventory, workload=workload)
inventory["generation_config"].update(num_beams=num_beams, num_return_sequences=num_return_sequences)
demands = Mock(wraps=phase_admission.transformer_demands)
monkeypatch.setattr(phase_admission, "transformer_demands", demands)
report = plan(inventory, workload=workload, device_map=ordinary["device_map"])
assert report["status"] == "feasible", report["refusals"]
assert report["generation_batch_size"] == 16
decodes = [call.kwargs for call in demands.call_args_list if call.kwargs.get("cache_tokens") == 528]
assert decodes and all(row["batch_size"] == 16 for row in decodes)
assert report["phases"]["activations"] == ordinary["phases"]["activations"]
for phase in ("baseline", "verify", "reload"):
for device in ("cuda:0", "cuda:1"):
before = ordinary["phases"][phase][device]["components"]
after = report["phases"][phase][device]["components"]
assert after["weights"] == before["weights"]
assert after["kv_cache"] == 8 * before["kv_cache"]
assert after["logits"] == 8 * before["logits"]
assert after["activations"] > before["activations"]
@pytest.mark.parametrize("explicit_map", [False, True])
def test_32b_inherited_beams_refuse_long_generation_when_single_beam_fits(explicit_map):
inventory = meta_inventory(read_config("qwen2.5-32b-instruct.json"), generation_config={"num_beams": 1})
budgets = {"cuda:0": 48 * GIB, "cuda:1": 48 * GIB}
workload = Workload(sequence_length=128, generation_tokens=32768)
ordinary = plan(inventory, budgets, workload=workload)
assert ordinary["status"] == "feasible", ordinary["refusals"]
inventory["generation_config"]["num_beams"] = 8
report = plan(inventory, budgets, workload=workload,
device_map=ordinary["device_map"] if explicit_map else None)
assert report["status"] == "refused"
assert report["generation_batch_size"] == 8
for phase in ("baseline", "verify", "reload"):
assert any(reason.startswith(f"{phase}/cuda:") for reason in report["refusals"])
@pytest.mark.parametrize("field", ["num_beams", "num_return_sequences"])
@pytest.mark.parametrize("value", [None, 1, 0, -1, True, 1.5])
def test_generation_batch_counts_require_positive_integers(tiny_inventory, field, value):
inventory = copy.deepcopy(tiny_inventory)
inventory["generation_config"][field] = value
if value is None or type(value) is int and value > 0:
assert plan(inventory)["generation_batch_size"] == 1
else:
with pytest.raises(ValueError, match=field):
plan(inventory)
@pytest.mark.parametrize("generation_config,expected", [(None, 8), ({"num_beams": 4}, 4)])
def test_generation_beams_inherit_model_defaults_or_local_override(tiny_inventory, generation_config, expected):
inventory = meta_inventory(dict(tiny_inventory["config"], num_beams=8), generation_config=generation_config)
report = plan(inventory, workload=Workload(batch_size=2))
assert report["generation_batch_size"] == 2 * expected
@pytest.mark.parametrize("generation_config", [None, {}, {"num_beams": 4, "output_scores": False}])
def test_generation_defaults_follow_checkpoint_loading(tmp_path, tiny_inventory, generation_config):
raw = dict(tiny_inventory["config"], num_beams=8, output_scores=True)
original = copy.deepcopy(raw)
(tmp_path / "config.json").write_text(json.dumps(raw))
if generation_config is None:
expected = GenerationConfig.from_pretrained(
tmp_path, config_file_name="config.json", _from_model_config=True, local_files_only=True,
)
else:
(tmp_path / "generation_config.json").write_text(json.dumps(generation_config))
expected = GenerationConfig.from_pretrained(tmp_path, local_files_only=True)
inventory = meta_inventory(raw, generation_config=generation_config)
for name in ("num_beams", "output_scores", "return_dict_in_generate"):
assert inventory["generation_config"][name] == getattr(expected, name)
assert raw == original
@pytest.mark.parametrize("use_cache", [True, False])
@pytest.mark.parametrize("scale,branches", [(None, 1), (1.0, 1), (1.5, 2), (0.5, 2), (0.0, 2)])
@pytest.mark.parametrize("execution", ["split", "cpu", "disk_offload"])
def test_generation_guidance_bounds_both_branches(tiny_inventory, use_cache, scale, branches, execution):
inventory = copy.deepcopy(tiny_inventory)
inventory["generation_config"].update(use_cache=use_cache, num_beams=4)
budgets = {"cpu": 64 * GIB} if execution == "cpu" else {"cuda:0": 80 * GIB, "cuda:1": 80 * GIB}
options = dict(workload=Workload(batch_size=2, sequence_length=16, generation_tokens=512),
offload="disk" if execution == "disk_offload" else "none")
ordinary = plan(inventory, budgets, **options)
inventory["generation_config"]["guidance_scale"] = scale
report = plan(inventory, budgets, device_map=ordinary["device_map"], **options)
assert report["status"] == "feasible", report["refusals"]
assert report["generation_batch_size"] == 8
assert report["cache_policy"]["generation_guidance_scale"] == scale
assert report["cache_policy"]["generation_cache_copies"] == (branches if use_cache else 0)
assert report["phases"]["activations"] == ordinary["phases"]["activations"]
for phase in ("baseline", "verify", "reload"):
for device in budgets:
before = ordinary["phases"][phase][device]["components"]
after = report["phases"][phase][device]["components"]
for component in ("activations", "kv_cache", "logits"):
assert after[component] == branches * before[component]
assert after["onload"] == before["onload"]
assert after["weights"] == before["weights"]
@pytest.mark.parametrize("generation_config,copies", [(None, 2), ({"guidance_scale": 1.0}, 1)])
def test_generation_guidance_inherits_model_defaults_or_local_override(tiny_inventory, generation_config, copies):
inventory = meta_inventory(dict(tiny_inventory["config"], guidance_scale=1.5), generation_config=generation_config)
assert plan(inventory)["cache_policy"]["generation_cache_copies"] == copies
@pytest.mark.parametrize("scale", [True, "1.5", float("nan"), float("inf"), float("-inf")])
def test_generation_guidance_requires_a_finite_number(tiny_inventory, scale):
inventory = copy.deepcopy(tiny_inventory)
inventory["generation_config"]["guidance_scale"] = scale
with pytest.raises(ValueError, match="guidance_scale"):
plan(inventory)
@pytest.mark.parametrize("explicit_map", [False, True])
def test_32b_guidance_refuses_long_generation_when_one_cache_fits(explicit_map):
inventory = meta_inventory(read_config("qwen2.5-32b-instruct.json"))
budgets = {"cuda:0": 48 * GIB, "cuda:1": 48 * GIB}
workload = Workload(batch_size=2, sequence_length=128, generation_tokens=32640)
ordinary = plan(inventory, budgets, workload=workload)
assert ordinary["status"] == "feasible", ordinary["refusals"]
inventory["generation_config"]["guidance_scale"] = 1.5
report = plan(inventory, budgets, workload=workload,
device_map=ordinary["device_map"] if explicit_map else None)
assert report["status"] == "refused"
for phase in ("baseline", "verify", "reload"):
assert any(reason.startswith(f"{phase}/cuda:") for reason in report["refusals"])
def test_guidance_cache_cost_changes_automatic_placement(tiny_inventory, monkeypatch):
monkeypatch.setattr("obliteratus.capacity_planning._forward", lambda *args, **kwargs: {})
inventory = copy.deepcopy(tiny_inventory)
options = dict(workload=Workload(sequence_length=16, generation_tokens=512), offload="cpu",
host_memory_bytes=300_000, reserve_bytes=0, host_reserve_bytes=0)
ordinary = plan(inventory, {"cuda:0": 500_000}, **options)
inventory["generation_config"]["guidance_scale"] = 1.5
guided = plan(inventory, {"cuda:0": 500_000}, **options)
ordinary_gpu = sum(parameter_device(block, ordinary["device_map"]) == "cuda:0" for block in inventory["blocks"])
guided_gpu = sum(parameter_device(block, guided["device_map"]) == "cuda:0" for block in inventory["blocks"])
assert guided_gpu < ordinary_gpu
@pytest.mark.parametrize("model_cache", [True, False])
@pytest.mark.parametrize("generation_cache", [None, True, False])
def test_generation_cache_override_preserves_activation_policy(
tiny_inventory, monkeypatch, model_cache, generation_cache,
):
from obliteratus import phase_admission
inventory = meta_inventory(dict(tiny_inventory["config"], use_cache=model_cache),
generation_config={"use_cache": generation_cache})
demands = Mock(wraps=phase_admission.transformer_demands)
monkeypatch.setattr(phase_admission, "transformer_demands", demands)
workload = Workload(sequence_length=16, generation_tokens=512)
report = plan(inventory, workload=workload)
effective = model_cache if generation_cache is None else generation_cache
assert report["status"] == "feasible", report["refusals"]
assert report["cache_policy"]["forward_use_cache"] is model_cache
assert report["cache_policy"]["generation_use_cache"] is effective
decodes = [call.kwargs for call in demands.call_args_list if call.kwargs.get("cache_tokens") == 528]
assert decodes
assert all(row["use_cache"] is effective for row in decodes)
assert all(row["tokens"] == (1 if effective else 528) for row in decodes)
for device in ("cuda:0", "cuda:1"):
assert bool(report["phases"]["activations"][device]["components"]["kv_cache"]) is model_cache
assert inventory["text_config"]["use_cache"] is model_cache
assert inventory["generation_config"]["use_cache"] is generation_cache
def test_uncached_qualification_plan_also_bounds_cached_comparisons(tiny_inventory, monkeypatch):
from obliteratus import phase_admission
inventory = meta_inventory(dict(tiny_inventory["config"], use_cache=False))
demands = Mock(wraps=phase_admission.transformer_demands)
monkeypatch.setattr(phase_admission, "transformer_demands", demands)
report = plan(inventory, workload=Workload(sequence_length=32, generation_tokens=16, comparison_prompts=2))
assert report["status"] == "feasible", report["refusals"]
assert report["cache_policy"]["generation_use_cache"] is False
assert report["cache_policy"]["comparison_use_cache"] is True
decodes = {(call.kwargs["tokens"], call.kwargs["use_cache"])
for call in demands.call_args_list if call.kwargs.get("cache_tokens") == 48}
assert decodes == {(1, True), (48, False)}
def test_qualification_comparisons_keep_greedy_batches_when_diagnostics_inherit_beams(tiny_inventory, monkeypatch):
from obliteratus import phase_admission
inventory = copy.deepcopy(tiny_inventory)
inventory["generation_config"].update(num_beams=8, use_cache=False)
demands = Mock(wraps=phase_admission.transformer_demands)
monkeypatch.setattr(phase_admission, "transformer_demands", demands)
report = plan(inventory, workload=Workload(batch_size=2, sequence_length=32, generation_tokens=16,
comparison_prompts=2))
assert report["status"] == "feasible", report["refusals"]
decodes = {(call.kwargs["tokens"], call.kwargs["use_cache"], call.kwargs["batch_size"])
for call in demands.call_args_list if call.kwargs.get("cache_tokens") == 48}
assert decodes == {(1, True, 2), (48, False, 16)}
def test_qualification_comparisons_remain_unguided(tiny_inventory, monkeypatch):
from obliteratus.phase_admission import DeviceDemand
def demands(config, placement, *, use_cache, **kwargs):
return {device: DeviceDemand(kv_bytes=100 if use_cache else 0) for device in set(placement)}
monkeypatch.setattr("obliteratus.phase_admission.transformer_demands", demands)
inventory = copy.deepcopy(tiny_inventory)
inventory["text_config"]["use_cache"] = False
inventory["generation_config"].update(use_cache=False, guidance_scale=1.5)
report = plan(inventory, workload=Workload(batch_size=2, sequence_length=32,
generation_tokens=16, comparison_prompts=2))
assert report["status"] == "feasible", report["refusals"]
assert report["cache_policy"]["comparison_use_cache"] is True
for device in ("cuda:0", "cuda:1"):
assert report["phases"]["verify"][device]["components"]["kv_cache"] == 100
@pytest.mark.parametrize("use_cache", [True, False])
@pytest.mark.parametrize("implementation", [None, "dynamic", "static", "offloaded"])
def test_generation_cache_without_workspace_contract_refuses(tiny_inventory, use_cache, implementation):
inventory = copy.deepcopy(tiny_inventory)
inventory["generation_config"].update(use_cache=use_cache, cache_implementation=implementation)
report = plan(inventory)
refused = use_cache and implementation not in {None, "dynamic"}
assert report["status"] == ("refused" if refused else "feasible")
if refused:
assert any("cache implementation" in reason for reason in report["refusals"])
@pytest.mark.parametrize("generation_config,mode", UNSUPPORTED_GENERATION_MODES)
@pytest.mark.parametrize("use_cache", [True, False])
@pytest.mark.parametrize("explicit_map", [False, True])
def test_unsupported_generation_modes_refuse_before_admission(
tiny_inventory, generation_config, mode, use_cache, explicit_map,
):
inventory = copy.deepcopy(tiny_inventory)
inventory["generation_config"]["use_cache"] = use_cache
ordinary = plan(inventory)
assert ordinary["status"] == "feasible", ordinary["refusals"]
inventory["generation_config"].update(generation_config)
original = copy.deepcopy(inventory["generation_config"])
report = plan(inventory, device_map=ordinary["device_map"] if explicit_map else None)
assert report["status"] == "refused"
assert report["generation_mode"] == mode
assert any(f"Generation mode {mode!r}" in reason for reason in report["refusals"])
assert inventory["generation_config"] == original
@pytest.mark.parametrize("generation_config,mode", [
({"penalty_alpha": 0.6, "top_k": 1}, "greedy_search"),
({"penalty_alpha": 0.0, "top_k": 4}, "greedy_search"),
({"penalty_alpha": None, "top_k": 4}, "greedy_search"),
({"penalty_alpha": 0.6, "top_k": 4, "num_beams": 4}, "beam_search"),
({"num_beam_groups": 2, "num_beams": 1}, "greedy_search"),
({"do_sample": True}, "greedy_search"),
({"num_beams": 4, "do_sample": True}, "beam_search"),
])
def test_generation_mode_uses_effective_selectors(tiny_inventory, generation_config, mode):
inventory = copy.deepcopy(tiny_inventory)
inventory["generation_config"].update(generation_config)
report = plan(inventory)
assert report["status"] == "feasible", report["refusals"]
assert report["generation_mode"] == mode
@pytest.mark.parametrize("generation_config,mode", UNSUPPORTED_GENERATION_MODES)
def test_local_generation_config_can_disable_inherited_unsupported_mode(tiny_inventory, generation_config, mode):
config = dict(tiny_inventory["config"], **generation_config)
inherited = plan(meta_inventory(config))
assert inherited["status"] == "refused"
assert inherited["generation_mode"] == mode
overridden = plan(meta_inventory(config, generation_config={}))
assert overridden["status"] == "feasible", overridden["refusals"]
assert overridden["generation_mode"] == "greedy_search"
@pytest.mark.parametrize("field,value", [("prompt_lookup_num_tokens", 8192), ("assistant_early_exit", 2), ("use_mtp", True)])
@pytest.mark.parametrize("use_cache", [True, False])
@pytest.mark.parametrize("explicit_map", [False, True])
def test_assisted_generation_requires_workspace_contract(tiny_inventory, field, value, use_cache, explicit_map):
inventory = copy.deepcopy(tiny_inventory)
inventory["generation_config"]["use_cache"] = use_cache
ordinary = plan(inventory)
assert ordinary["status"] == "feasible", ordinary["refusals"]
inventory["generation_config"][field] = value
report = plan(inventory, device_map=ordinary["device_map"] if explicit_map else None)
assert report["status"] == "refused"
assert report["generation_assistance_policy"][field] == value
assert any("Assisted generation" in reason for reason in report["refusals"])
inventory["generation_config"][field] = False if field == "use_mtp" else None
assert plan(inventory)["status"] == "feasible"
@pytest.mark.parametrize("explicit_map", [False, True])
def test_32b_prompt_lookup_cannot_use_single_token_decode_estimate(explicit_map):
config = read_config("qwen2.5-32b-instruct.json")
ordinary = meta_inventory(config)
workload = Workload(sequence_length=8192, generation_tokens=8192)
base = plan(ordinary, workload=workload)
assert base["status"] == "feasible", base["refusals"]
assisted = meta_inventory(config, generation_config={"prompt_lookup_num_tokens": 8192})
report = plan(assisted, workload=workload, device_map=base["device_map"] if explicit_map else None)
assert report["status"] == "refused"
assert any("Assisted generation" in reason for reason in report["refusals"])
@pytest.mark.parametrize("field,value", [("prompt_lookup_num_tokens", 8192), ("assistant_early_exit", 2), ("use_mtp", True)])
def test_local_generation_config_can_disable_inherited_assistance(tiny_inventory, field, value):
config = dict(tiny_inventory["config"], **{field: value})
inherited = meta_inventory(config)
assert plan(inherited)["status"] == "refused"
disabled = meta_inventory(config, generation_config={field: False if field == "use_mtp" else None})
assert plan(disabled)["status"] == "feasible"
@pytest.mark.parametrize("neighbours", [0, 3 * 4096])
def test_32b_concept_readout_retention_refuses_32gib_host(neighbours):
inventory = meta_inventory(read_config("qwen2.5-32b-instruct.json"))
budgets = {"cuda:0": 96 * GIB, "cuda:1": 96 * GIB}
workload = Workload(samples=4096, auxiliary_readouts=3, neighbour_samples=neighbours,
prompt_characters=8192)
report = plan(inventory, budgets, workload=workload, host_memory_bytes=32 * GIB)
assert report["status"] == "refused"
assert any(reason.startswith("activations/cpu shortfall") for reason in report["refusals"])
expected = 40 * GIB + neighbours * 64 * 5120 * 4
for phase in ("activations", "direction", "edit", "verify", "save"):
assert report["phases"][phase]["cpu"]["components"]["retained_activations"] == expected
ordinary = plan(inventory, budgets, workload=Workload(samples=4096), host_memory_bytes=32 * GIB)
assert ordinary["status"] == "feasible", ordinary["refusals"]
admitted = plan(inventory, budgets, workload=workload, host_memory_bytes=256 * GIB)
assert admitted["status"] == "feasible", admitted["refusals"]
@pytest.mark.parametrize("gated,neighbours", [(False, 0), (True, 0), (True, 3 * 4096)])
def test_concept_direction_peak_covers_all_layer_materialization(gated, neighbours):
inventory = meta_inventory(read_config("qwen2.5-32b-instruct.json"))
workload = Workload(samples=4096, concept_extraction=True,
auxiliary_readouts=3 if gated else 0, neighbour_samples=neighbours,
prompt_characters=8192)
report = plan(inventory, workload=workload, host_memory_bytes=(48 if gated else 20) * GIB)
components = report["phases"]["direction"]["cpu"]["components"]
primary = 10 * GIB
retained = primary * (4 if gated else 1) + neighbours * 64 * 5120 * 4
assert components["retained_activations"] == retained
assert components["activation_materialization"] == (3 * retained if gated else primary)
assert report["status"] == "refused"
assert any(reason.startswith("direction/cpu shortfall") for reason in report["refusals"])
for phase in ("edit", "verify", "save"):
assert "activation_materialization" not in report["phases"][phase]["cpu"]["components"]
admitted = plan(inventory, workload=workload, host_memory_bytes=256 * GIB)
assert admitted["status"] == "feasible", admitted["refusals"]
def test_refusal_report_gate_refuses_host_that_only_fits_cached_rows():
inventory = meta_inventory(read_config("qwen2.5-32b-instruct.json"))
options = dict(host_memory_bytes=32 * GIB)
ordinary = plan(inventory, workload=Workload(samples=4096), **options)
workload = Workload(samples=4096, validation_gate=True, prompt_characters=8192)
report = plan(inventory, workload=workload, **options)
assert ordinary["status"] == "feasible", ordinary["refusals"]
assert report["status"] == "refused"
assert any(reason.startswith("direction/cpu shortfall") for reason in report["refusals"])
before = ordinary["phases"]["direction"]["cpu"]["components"]
after = report["phases"]["direction"]["cpu"]["components"]
assert after["retained_activations"] == before["retained_activations"] == 10 * GIB
assert after["activation_materialization"] == 30 * GIB
assert after["direction_extraction"] == 2 * before["direction_extraction"]
admitted = plan(inventory, workload=workload, host_memory_bytes=128 * GIB)
assert admitted["status"] == "feasible", admitted["refusals"]
@pytest.mark.parametrize("gate", [{"validation_gate": True}, {"auxiliary_readouts": 3}])
def test_gate_lexical_probe_refuses_host_that_fits_hidden_rows(tiny_inventory, gate):
config = dict(tiny_inventory["config"], num_hidden_layers=2)
# Serialized Transformers configs retain per-layer attention/MLP metadata.
for name in ("layer_types", "mlp_layer_types"):
if config.get(name) is not None:
config[name] = config[name][:2]
inventory = meta_inventory(config)
samples = 1024**2
ordinary = plan(inventory, workload=Workload(samples=samples), host_memory_bytes=16 * GIB)
workload = Workload(samples=samples, prompt_characters=0, **gate)
report = plan(inventory, workload=workload, host_memory_bytes=16 * GIB)
assert ordinary["status"] == "feasible", ordinary["refusals"]
assert report["status"] == "refused"
assert any(reason.startswith("direction/cpu shortfall") for reason in report["refusals"])
components = report["phases"]["direction"]["cpu"]["components"]
# Even an empty-text bound reserves the full column cap. Presence matrices
# alone fill the budget; training copies must also fit.
assert components["lexical_probe"] > 2 * samples * 1024 * 8 == 16 * GIB
without_lexical = report["phases"]["direction"]["cpu"]["peak_bytes"] - components["lexical_probe"]
assert without_lexical < 16 * GIB
assert all("lexical_probe" not in row["cpu"]["components"] for row in ordinary["phases"].values())
assert all("lexical_probe" not in row["cpu"]["components"]
for phase, row in report["phases"].items() if phase != "direction")
admitted = plan(inventory, workload=workload, host_memory_bytes=128 * GIB)
assert admitted["status"] == "feasible", admitted["refusals"]
@pytest.mark.parametrize("samples", [8, 768])
@pytest.mark.parametrize("datasets", [2, 4])
def test_gate_lexical_plan_bounds_runtime_workspace(tiny_inventory, samples, datasets):
from obliteratus.concepts.gate import _fold_indices
from obliteratus.concepts.gate_memory import _lexical_workspace
sizes = {"positive": samples, **{f"control:{index}": samples - index - 1
for index in range(datasets - 1)}}
prompts = {name: [f"{name} feature{index} sequence{index}" for index in range(count)]
for name, count in sizes.items()}
texts = (prompts["positive"], {str(index): prompts[f"control:{index}"]
for index in range(datasets - 1)})
runtime = _lexical_workspace(texts, sizes, _fold_indices(sizes, 2, 245))
report = plan(tiny_inventory, workload=Workload(samples=samples, activation_datasets=datasets,
validation_gate=True,
prompt_characters=sum(len(p) for group in prompts.values() for p in group)))
components = report["phases"]["direction"]["cpu"]["components"]
bound = components["lexical_probe"] + components["lexical_metadata"]
assert 0 < runtime <= bound
assert bound > sum(sizes.values()) * 1024 * 8
@pytest.mark.parametrize("gate", [{"validation_gate": True}, {"auxiliary_readouts": 3}])
def test_gated_capacity_requires_untruncated_text_metadata_bound(tiny_inventory, gate):
report = plan(tiny_inventory, workload=Workload(**gate))
assert report["status"] == "refused"
assert any("--plan-prompt-characters" in reason for reason in report["refusals"])
assert all(row["headroom_bytes"] >= 0 for rows in report["phases"].values() for row in rows.values())
# An explicit zero bound is valid for an all-empty lexical corpus.
admitted = plan(tiny_inventory, workload=Workload(prompt_characters=0, **gate))
assert admitted["status"] == "feasible", admitted["refusals"]
def test_long_lexical_prompts_refuse_independently_of_token_sequence_length(tiny_inventory):
options = dict(host_memory_bytes=GIB, source_shard_bytes=0, host_reserve_bytes=0)
short = plan(tiny_inventory, workload=Workload(samples=8, sequence_length=1,
validation_gate=True, prompt_characters=16), **options)
long = plan(tiny_inventory, workload=Workload(samples=8, sequence_length=1,
validation_gate=True, prompt_characters=1_000_000), **options)
assert short["status"] == "feasible", short["refusals"]
assert long["status"] == "refused"
assert any(reason.startswith("direction/cpu shortfall") for reason in long["refusals"])
components = long["phases"]["direction"]["cpu"]["components"]
assert components["lexical_metadata"] > GIB
assert long["phases"]["direction"]["cpu"]["peak_bytes"] - components["lexical_metadata"] < GIB
assert all("lexical_metadata" not in rows["cpu"]["components"]
for phase, rows in long["phases"].items() if phase != "direction")
def test_72b_concept_denoising_refuses_96gib_host_without_gating(large_inventory):
budgets = {"cuda:0": 96 * GIB, "cuda:1": 96 * GIB}
workload = Workload(samples=8192, n_directions=1, concept_extraction=True)
report = plan(large_inventory, budgets, workload=workload, host_memory_bytes=96 * GIB)
assert report["status"] == "refused"
assert any(reason.startswith("direction/cpu shortfall") for reason in report["refusals"])
for phase in ("direction", "edit", "verify", "save"):
assert report["phases"][phase]["cpu"]["components"]["retained_directions"] >= 20 * GIB
components = report["phases"]["direction"]["cpu"]["components"]
assert sum(components[name] for name in (
"retained_activations", "activation_materialization", "retained_directions",
)) >= 100 * GIB
admitted = plan(large_inventory, budgets, workload=workload, host_memory_bytes=128 * GIB)
assert admitted["status"] == "feasible", admitted["refusals"]
@pytest.mark.parametrize("samples", [8, 64])
@pytest.mark.parametrize("datasets", [2, 4])
@pytest.mark.parametrize("zero_variance", [False, True])
def test_concept_retention_covers_pooled_control_pca_backing(tiny_inventory, samples, datasets, zero_variance):
from obliteratus.concepts.extraction import extract_layer
from obliteratus.concepts.target import ExtractionRecipe
hidden = tiny_inventory["text_config"]["hidden_size"]
controls = {str(index): (torch.zeros(samples, hidden) if zero_variance else torch.eye(samples, hidden))
for index in range(datasets - 1)}
extraction = extract_layer(torch.ones(samples, hidden), controls,
ExtractionRecipe(denoise={"control_pc_variance": 0.01}))
pcs = extraction.removed_pcs
if zero_variance:
assert pcs.numel() == 0
assert pcs.untyped_storage().nbytes() > pcs.numel() * pcs.element_size()
options = dict(samples=samples, activation_datasets=datasets)
concept = plan(tiny_inventory, workload=Workload(concept_extraction=True, **options))
ordinary = plan(tiny_inventory, workload=Workload(**options))
for phase in ("direction", "edit", "verify", "save"):
extra = (concept["phases"][phase]["cpu"]["components"]["retained_directions"]
- ordinary["phases"][phase]["cpu"]["components"]["retained_directions"])
assert extra >= len(tiny_inventory["blocks"]) * pcs.untyped_storage().nbytes()
def test_qualification_comparison_retention_refuses_before_model_loading(large_inventory):
budgets = {"cuda:0": 96 * GIB, "cuda:1": 96 * GIB}
options = dict(batch_size=4, sequence_length=32, generation_tokens=16, samples=3)
small = plan(large_inventory, budgets, workload=Workload(comparison_prompts=2, **options),
host_memory_bytes=32 * GIB)
assert small["status"] == "feasible", small["refusals"]
large = plan(large_inventory, budgets, workload=Workload(comparison_prompts=100_000, **options),
host_memory_bytes=32 * GIB)
assert large["status"] == "refused"
assert any(reason.startswith("baseline/cpu shortfall") for reason in large["refusals"])
per_set = 100_000 * (large_inventory["text_config"]["vocab_size"] * 4 + 48 * 8)
assert per_set > 56 * GIB
for phase, copies in (("baseline", 1), ("activations", 1), ("direction", 1), ("edit", 1),
("verify", 2), ("save", 2), ("reload", 4)):
assert large["phases"][phase]["cpu"]["components"]["retained_comparisons"] == copies * per_set
assert "retained_comparisons" not in large["phases"]["load"]["cpu"]["components"]
admitted = plan(large_inventory, budgets, workload=Workload(comparison_prompts=100_000, **options),
host_memory_bytes=256 * GIB)
assert admitted["status"] == "feasible", admitted["refusals"]
@pytest.mark.parametrize("output", ["output_attentions", "output_hidden_states"])
@pytest.mark.parametrize("split", [False, True])
def test_forward_estimates_retain_config_enabled_outputs(tiny_inventory, output, split):
inventory = meta_inventory(dict(tiny_inventory["config"], **{output: True}))
budgets = {"cuda:0": 8 * GIB, **({"cuda:1": 8 * GIB} if split else {})}
mapping = {"": "cuda:0"}
if split:
mapping.update({block: "cuda:1" for block in inventory["blocks"][3:]})
mapping["lm_head"] = "cuda:1"
workload = Workload(batch_size=2, sequence_length=128)
ordinary = plan(tiny_inventory, budgets, device_map=mapping, workload=workload)
retained = plan(inventory, budgets, device_map=mapping, workload=workload)
assert inventory["text_config"][output] is True
for phase in ("baseline", "activations", "verify", "reload"):
for device in budgets:
layers = sum(parameter_device(block, mapping) == device for block in inventory["blocks"])
per_layer = (2 * 4 * 128**2 * 4 if output == "output_attentions" else 2 * 128 * 32 * 2)
extra = (retained["phases"][phase][device]["components"]["activations"]
- ordinary["phases"][phase][device]["components"]["activations"])
assert extra >= layers * per_layer
@pytest.mark.parametrize("output", ["output_attentions", "output_hidden_states"])
def test_config_enabled_outputs_can_refuse_an_otherwise_feasible_plan(tiny_inventory, output):
workload = Workload(batch_size=4, sequence_length=2048)
ordinary = plan(tiny_inventory, {"cuda:0": 8 * GIB}, workload=workload)
budget = max(rows["cuda:0"]["peak_bytes"] for rows in ordinary["phases"].values())
assert plan(tiny_inventory, {"cuda:0": budget}, workload=workload)["status"] == "feasible"
inventory = copy.deepcopy(tiny_inventory)
inventory["text_config"][output] = True
report = plan(inventory, {"cuda:0": budget}, workload=workload)
assert report["status"] == "refused"
assert any(reason.startswith("baseline/cuda:0 shortfall") for reason in report["refusals"])
def test_forward_attention_outputs_include_input_device_copies(tiny_inventory):
inventory = copy.deepcopy(tiny_inventory)
inventory["text_config"]["output_attentions"] = True
mapping = {"": "cuda:0", "lm_head": "cuda:1"}
mapping.update({block: "cuda:1" for block in inventory["blocks"][3:]})
workload = Workload(batch_size=2, sequence_length=128)
ordinary = plan(tiny_inventory, device_map=mapping, workload=workload)
returned = plan(inventory, device_map=mapping, workload=workload)
for device, copies in (("cuda:0", 6), ("cuda:1", 3)):
before = ordinary["phases"]["activations"][device]["components"]["activations"]
after = returned["phases"]["activations"][device]["components"]["activations"]
assert after - before == copies * 2 * 4 * 128**2 * 4
@pytest.mark.parametrize("output", ["output_hidden_states", "output_attentions"])
@pytest.mark.parametrize("use_cache", [True, False])
def test_generation_forward_outputs_without_structured_return(tiny_inventory, output, use_cache):
inventory = copy.deepcopy(tiny_inventory)
inventory["generation_config"].update(use_cache=use_cache, return_dict_in_generate=False)
workload = Workload(batch_size=2, sequence_length=16, generation_tokens=512)
ordinary = plan(inventory, {"cuda:0": 8 * GIB}, workload=workload)
inventory["generation_config"][output] = True
returned = plan(inventory, {"cuda:0": 8 * GIB}, workload=workload)
query = 1 if use_cache else 528
expected = 7 * 2 * query * 32 * 2 if output == "output_hidden_states" else 6 * 2 * 4 * query * 528 * 4
assert returned["phases"]["activations"] == ordinary["phases"]["activations"]
for phase in ("baseline", "verify", "reload"):
before = ordinary["phases"][phase]["cuda:0"]["components"]
after = returned["phases"][phase]["cuda:0"]["components"]
assert after["activations"] - before["activations"] == expected
assert after["logits"] == before["logits"]
@pytest.mark.parametrize("output", ["output_hidden_states", "output_attentions"])
@pytest.mark.parametrize("use_cache", [True, False])
@pytest.mark.parametrize("execution", ["single", "split", "cpu", "disk_offload"])
def test_generation_retains_layer_outputs_across_steps(tiny_inventory, output, use_cache, execution):
inventory = copy.deepcopy(tiny_inventory)
inventory["generation_config"].update(use_cache=use_cache, return_dict_in_generate=False, **{output: True})
budgets = {"cpu": 64 * GIB} if execution == "cpu" else {"cuda:0": 32 * GIB}
mapping = {"": next(iter(budgets))}
if execution in {"split", "disk_offload"}:
target = "cuda:1" if execution == "split" else "disk"
if execution == "split":
budgets[target] = 32 * GIB
mapping.update({block: target for block in inventory["blocks"][3:]})
mapping["lm_head"] = target
workload = Workload(batch_size=2, sequence_length=16, generation_tokens=512)
options = dict(workload=workload, device_map=mapping, offload="disk" if execution == "disk_offload" else "none")
ordinary = plan(inventory, budgets, **options)
inventory["generation_config"]["return_dict_in_generate"] = True
retained = plan(inventory, budgets, **options)
assert ordinary["status"] == retained["status"] == "feasible", retained["refusals"]
assert retained["phases"]["activations"] == ordinary["phases"]["activations"]
assert inventory["text_config"][output] is False
entry = next(iter(budgets))
owners = [parameter_device(block, mapping) for block in inventory["blocks"]]
owners = [entry if device == "disk" else device for device in owners]
# Enumerate the actual earlier forwards independently of the planner's sums.
previous = [(1 if use_cache and step else workload.sequence_length + step, workload.sequence_length + step)
for step in range(workload.generation_tokens - 1)]
for device in budgets:
layers = owners.count(device)
returned = sum(owner != entry for owner in owners) if device == entry else 0
if output == "output_hidden_states":
rows = sum(query for query, _ in previous)
expected = 2 * rows * 32 * 2 * (layers + returned + int(device == entry))
else:
cells = sum(query * key for query, key in previous)
expected = 2 * 4 * cells * 4 * (layers + returned)
for phase in ("baseline", "verify", "reload"):
before = ordinary["phases"][phase][device]["components"]
after = retained["phases"][phase][device]["components"]
assert after["activations"] - before["activations"] == expected
assert after["kv_cache"] == before["kv_cache"]
assert after["logits"] == before["logits"]
@pytest.mark.parametrize("scores,logits", [(True, False), (False, True), (True, True)])
@pytest.mark.parametrize("use_cache", [True, False])
@pytest.mark.parametrize("expansion", ["num_beams", "num_return_sequences"])
def test_generation_retains_score_and_logit_rows(tiny_inventory, scores, logits, use_cache, expansion):
inventory = copy.deepcopy(tiny_inventory)
inventory["generation_config"].update(use_cache=use_cache, output_scores=scores, output_logits=logits,
return_dict_in_generate=False, guidance_scale=1.5, num_beams=4)
inventory["generation_config"][expansion] = 4
workload = Workload(batch_size=2, sequence_length=16, generation_tokens=512)
ordinary = plan(inventory, workload=workload)
inventory["generation_config"]["return_dict_in_generate"] = True
retained = plan(inventory, workload=workload, device_map=ordinary["device_map"])
assert retained["status"] == "feasible", retained["refusals"]
assert retained["generation_batch_size"] == 8
assert retained["phases"]["activations"] == ordinary["phases"]["activations"]
for phase in ("baseline", "verify", "reload"):
for device in ("cuda:0", "cuda:1"):
before = ordinary["phases"][phase][device]["components"]
after = retained["phases"][phase][device]["components"]
logit_devices = {parameter_device(inventory[name], ordinary["device_map"])
for name in ("embedding_module", "output_module")}
expected = 2 * 512 * (int(scores) + int(logits)) * 8 * 64 * 4 if device in logit_devices else 0
assert after["logits"] - before["logits"] == expected
assert after["activations"] == before["activations"]
assert after["kv_cache"] == before["kv_cache"]
inventory["generation_config"].update(output_scores=False, output_logits=False, return_dict_in_generate=False)
without_outputs = plan(inventory, workload=workload, device_map=ordinary["device_map"])
assert without_outputs["phases"] == ordinary["phases"]
@pytest.mark.parametrize("output", ["output_hidden_states", "output_attentions", "output_scores", "output_logits"])
@pytest.mark.parametrize("use_cache", [True, False])
@pytest.mark.parametrize("explicit_map", [False, True])
def test_generation_outputs_refuse_an_otherwise_feasible_plan(tiny_inventory, output, use_cache, explicit_map):
inventory = copy.deepcopy(tiny_inventory)
inventory["generation_config"].update(use_cache=use_cache)
workload = Workload(batch_size=2, sequence_length=16, generation_tokens=512)
ordinary = plan(inventory, {"cuda:0": 8 * GIB}, workload=workload)
budget = max(rows["cuda:0"]["peak_bytes"] for rows in ordinary["phases"].values())
ordinary = plan(inventory, {"cuda:0": budget}, workload=workload)
assert ordinary["status"] == "feasible", ordinary["refusals"]
inventory["generation_config"].update(return_dict_in_generate=True, **{output: True})
report = plan(inventory, {"cuda:0": budget}, workload=workload,
device_map=ordinary["device_map"] if explicit_map else None)
assert report["status"] == "refused"
assert report["phases"]["activations"] == ordinary["phases"]["activations"]
for phase in ("baseline", "verify", "reload"):
assert any(reason.startswith(f"{phase}/cuda:0 shortfall") for reason in report["refusals"])
@pytest.mark.parametrize("output", ["output_hidden_states", "output_attentions", "output_scores", "output_logits"])
def test_generation_outputs_inherit_model_defaults_or_local_override(tiny_inventory, output):
config = dict(tiny_inventory["config"], **{output: True})
inherited = meta_inventory(config)
overridden = meta_inventory(config, generation_config={output: False, "return_dict_in_generate": False})
workload = Workload(sequence_length=16, generation_tokens=512)
inherited_plan = plan(inherited, workload=workload)
overridden_plan = plan(overridden, workload=workload, device_map=inherited_plan["device_map"])
assert inherited_plan["generation_output_policy"][output] is True
assert inherited_plan["generation_output_policy"]["return_dict_in_generate"] is True
assert overridden_plan["generation_output_policy"][output] is False
assert overridden_plan["generation_output_policy"]["return_dict_in_generate"] is False
assert inherited_plan["phases"]["activations"] == overridden_plan["phases"]["activations"]
for phase in ("baseline", "verify", "reload"):
assert inherited_plan["phases"][phase]["cuda:0"]["peak_bytes"] > overridden_plan["phases"][phase]["cuda:0"]["peak_bytes"]
@pytest.mark.parametrize("output", ["output_hidden_states", "output_attentions", "output_scores", "output_logits", "return_dict_in_generate"])
@pytest.mark.parametrize("value", [None, False, True, 0, 1, "true"])
def test_generation_output_flags_require_booleans(tiny_inventory, output, value):
inventory = copy.deepcopy(tiny_inventory)
inventory["generation_config"][output] = value
if value is None or type(value) is bool:
assert plan(inventory)["generation_output_policy"][output] is bool(value)
else:
with pytest.raises(ValueError, match=output):
plan(inventory)
def test_qualification_comparisons_disable_inherited_generation_outputs(tiny_inventory, monkeypatch):
from obliteratus import phase_admission
inventory = copy.deepcopy(tiny_inventory)
inventory["text_config"].update(output_hidden_states=True, output_attentions=True)
inventory["generation_config"].update(use_cache=False, num_beams=4, return_dict_in_generate=True,
output_hidden_states=True, output_attentions=True,
output_scores=True, output_logits=True)
demands = Mock(wraps=phase_admission.transformer_demands)
monkeypatch.setattr(phase_admission, "transformer_demands", demands)
report = plan(inventory, workload=Workload(sequence_length=16, generation_tokens=512, comparison_prompts=2))
assert report["status"] == "feasible", report["refusals"]
comparisons = [call.kwargs for call in demands.call_args_list
if call.kwargs.get("cache_tokens") == 528 and call.kwargs["use_cache"]]
assert comparisons
for row in comparisons:
assert row["batch_size"] == 1
assert row["output_hidden_states"] is row["output_attentions"] is False
assert row["retained_hidden_tokens"] == row["retained_logits_copies"] == 0
@pytest.mark.parametrize("staged", [False, True])
def test_qualification_disk_admission_includes_both_observation_files(staged):
inventory = meta_inventory(read_config("qwen2.5-32b-instruct.json"))
options = dict(batch_size=4, sequence_length=32, generation_tokens=16, samples=3)
budgets = {"cuda:0": 96 * GIB, "cuda:1": 96 * GIB}
disk_budget = (150 if staged else 220) * GIB
ordinary = plan(inventory, budgets, workload=Workload(comparison_prompts=2, **options),
source_staged=staged, host_memory_bytes=256 * GIB, disk_memory_bytes=disk_budget)
assert ordinary["status"] == "feasible", ordinary["refusals"]
workload = Workload(comparison_prompts=100_000, **options)
report = plan(inventory, budgets, workload=workload, source_staged=staged,
host_memory_bytes=256 * GIB, disk_memory_bytes=disk_budget)
assert report["status"] == "refused"
assert all(row["headroom_bytes"] >= 0 for rows in report["phases"].values()
for device, row in rows.items() if device != "disk")
assert any(reason.startswith("save/disk shortfall") for reason in report["refusals"])
per_set = 100_000 * (inventory["text_config"]["vocab_size"] * 4 + 48 * 8)
observation_bytes = 4 * per_set + 4 * 64 * 5120 * 4
for phase, rows in report["phases"].items():
if phase != "reload":
assert rows["disk"]["components"]["qualification_observations"] == observation_bytes
# Reload observes free space after the checkpoint and observation files exist.
assert "qualification_observations" not in report["phases"]["reload"]["disk"]["components"]
admitted = plan(inventory, budgets, workload=workload, source_staged=staged,
host_memory_bytes=256 * GIB, disk_memory_bytes=512 * GIB)
assert admitted["status"] == "feasible", admitted["refusals"]
@pytest.mark.parametrize("dtype", ["bfloat16", "float32"])
@pytest.mark.parametrize("concept", [False, True])
@pytest.mark.parametrize("backing", ["cpu", "disk"])
@pytest.mark.parametrize("validation_gate", [False, True])
def test_collection_retention_matches_readout_storage(tiny_inventory, dtype, concept, backing, validation_gate):
inventory = meta_inventory(tiny_inventory["config"], dtype=dtype)
workload = Workload(samples=32, sequence_length=128, concept_extraction=concept,
validation_gate=validation_gate, prompt_characters=64)
options = {} if backing == "cpu" else {"device_map": {"": "cpu", "model.layers.0": "disk"}}
report = plan(inventory, {"cpu": 64 * GIB}, workload=workload, **options)
rows = 2 * 32 * 6 * 32 * 4
components = report["phases"]["direction"]["cpu"]["components"]
assert components["retained_activations"] == rows * (128 if dtype == "float32" and not concept else 1)
assert components["activation_materialization"] == (3 * rows if validation_gate else rows if concept else 0)
@pytest.mark.parametrize("offload", ["cpu", "disk"])
@pytest.mark.parametrize("retention", [{"auxiliary_readouts": 3}, {"neighbour_samples": 192}])
def test_offload_placement_accounts_for_all_retained_readouts(tiny_inventory, monkeypatch, offload, retention):
monkeypatch.setattr("obliteratus.capacity_planning._forward", lambda *args, **kwargs: {})
options = dict(offload=offload, host_memory_bytes=300_000, reserve_bytes=0, host_reserve_bytes=0)
ordinary = plan(tiny_inventory, {"cuda:0": 200_000}, **options)
concept = plan(tiny_inventory, {"cuda:0": 200_000},
workload=Workload(prompt_characters=0, **retention), **options)
ordinary_gpu = sum(parameter_device(block, ordinary["device_map"]) == "cuda:0"
for block in tiny_inventory["blocks"])
concept_gpu = sum(parameter_device(block, concept["device_map"]) == "cuda:0"
for block in tiny_inventory["blocks"])
assert concept_gpu > ordinary_gpu
@pytest.mark.parametrize("samples", [8, 6000, 16384])
def test_exact_leace_workspace_covers_fp64_fit_and_previous_eraser(large_inventory, samples):
report = plan(large_inventory, {"cuda:0": 96 * GIB, "cuda:1": 96 * GIB},
workload=Workload(samples=samples, direction_method="leace_exact"))
assert report["status"] == "feasible", report["refusals"]
hidden = large_inventory["text_config"]["hidden_size"]
# H/B and the concatenated FP32 input coexist with FP64 X and centered X.
rows = 2 * samples
sample_storage = 2 * rows * hidden * 4 + 2 * rows * hidden * 8
# covariance, eigenvectors, W, W_plus, removal, eye and projection coexist
# while the caller still holds the previous layer's FP32 eraser.
matrices = 7 * hidden**2 * 8 + hidden**2 * 4
components = report["phases"]["direction"]["cpu"]["components"]
assert components["direction_extraction"] >= sample_storage + matrices
@pytest.mark.parametrize("dtype", ["bfloat16", "float32"])
@pytest.mark.parametrize("readouts", [0, 3])
def test_exact_leace_fp64_workspace_is_independent_of_model_dtype(tiny_inventory, dtype, readouts):
inventory = meta_inventory(tiny_inventory["config"], dtype=dtype)
options = dict(samples=8, auxiliary_readouts=readouts, prompt_characters=16)
exact = plan(inventory, workload=Workload(direction_method="leace_exact", **options))
ordinary = plan(inventory, workload=Workload(**options))
exact_workspace = exact["phases"]["direction"]["cpu"]["components"]["direction_extraction"]
ordinary_workspace = ordinary["phases"]["direction"]["cpu"]["components"]["direction_extraction"]
assert exact_workspace == (ordinary_workspace if readouts else 2 * ordinary_workspace)
assert exact["status"] == "feasible", exact["refusals"]
def test_72b_svd_retention_refuses_28gib_host_budget(large_inventory):
budgets = {f"cuda:{index}": 96 * GIB for index in range(4)}
workload = Workload(samples=4096, n_directions=2)
report = plan(large_inventory, budgets, workload=workload, host_memory_bytes=28 * GIB)
assert report["status"] == "refused"
assert any(reason.startswith("direction/cpu shortfall") for reason in report["refusals"])
for phase in ("direction", "edit", "verify", "save"):
components = report["phases"][phase]["cpu"]["components"]
assert components["retained_activations"] == 20 * GIB
assert components["retained_directions"] >= 10 * GIB
assert report["phases"][phase]["cpu"]["peak_bytes"] > 30 * GIB
admitted = plan(large_inventory, budgets, workload=workload, host_memory_bytes=64 * GIB)
assert admitted["status"] == "feasible", admitted["refusals"]
@pytest.mark.parametrize("samples", [8, 64])
def test_direction_retention_covers_svd_backing_storage(tiny_inventory, samples):
hidden = tiny_inventory["text_config"]["hidden_size"]
_, _, basis = torch.linalg.svd(torch.eye(samples, hidden), full_matrices=False)
subspace = basis[:2]
primary = subspace[0] / subspace[0].norm()
stored = subspace.untyped_storage().nbytes() + primary.untyped_storage().nbytes()
assert stored > subspace.numel() * subspace.element_size()
report = plan(tiny_inventory, workload=Workload(samples=samples, n_directions=2))
expected = len(tiny_inventory["blocks"]) * stored
for phase in ("direction", "edit", "verify", "save"):
assert report["phases"][phase]["cpu"]["components"]["retained_directions"] >= expected
ordinary = plan(tiny_inventory, workload=Workload(samples=samples, n_directions=1))
assert ordinary["phases"]["direction"]["cpu"]["components"]["retained_directions"] < expected
@pytest.mark.parametrize("quantization", ["4bit", "8bit"])
@pytest.mark.parametrize("allow_fallback", [False, True])
def test_requested_quantization_cannot_claim_dense_capacity(tiny_inventory, quantization, allow_fallback):
report = plan(tiny_inventory, quantization=quantization, allow_unbounded_export=allow_fallback)
assert report["status"] == "refused"
assert report["quantization"] == quantization
assert any("quantization requires a separate" in reason for reason in report["refusals"])
assert report["export"]["eligible"] is False
assert report["export"]["bounded"] is False
assert tiny_inventory["export"]["eligible"] is True
@pytest.mark.parametrize("command", ["obliterate", "abliterate"])
@pytest.mark.parametrize("quantization", ["4bit", "8bit"])
def test_primary_quantization_plan_records_refusal(tmp_path, tiny_inventory, monkeypatch, command, quantization):
from obliteratus import cli
monkeypatch.setattr("obliteratus.capacity_planning.meta_inventory", lambda *args: tiny_inventory)
destination = tmp_path / "plan.json"
with pytest.raises(SystemExit) as error:
cli.main([
command, "offline", "--method", "basic", "--quantization", quantization, "--plan-only",
"--plan-config", str(CONFIGS / "qwen2.5-72b-instruct.json"),
"--plan-free-memory", "96", "96", "--plan-host-memory", "32", "--plan-disk-memory", "64",
"--plan-output", str(destination),
])
assert error.value.code == 2
report = json.loads(destination.read_text())
assert report["status"] == "refused"
assert report["quantization"] == quantization
assert report["export"]["eligible"] is False
assert report["export"]["bounded"] is False
assert any("quantization requires a separate" in reason for reason in report["refusals"])
def test_72b_snapshot_requires_explicit_larger_cap_or_disabled_policy(large_inventory):
budgets = {"cuda:0": 96 * GIB, "cuda:1": 96 * GIB}
refused = plan(large_inventory, budgets, snapshot_enabled=True)
assert refused["status"] == "refused"
assert refused["snapshot"]["status"] == "refused"
assert any("snapshot" in reason.lower() for reason in refused["refusals"])
admitted = plan(large_inventory, budgets, snapshot_enabled=True, snapshot_max_bytes=256 * GIB)
assert admitted["status"] == "feasible", admitted["refusals"]
assert admitted["snapshot"]["status"] == "disk_backed"
assert admitted["snapshot"]["restore_available"] is True
def test_72b_cpu_offload_snapshot_uses_host_availability_after_loading(large_inventory):
mapping = {"": "cuda:0", "lm_head": "cpu"}
mapping.update({block: "cuda:0" if index < 40 else "cuda:1"
for index, block in enumerate(large_inventory["blocks"])})
options = dict(device_map=mapping, offload="cpu", snapshot_enabled=True,
snapshot_max_bytes=256 * GIB, source_shard_bytes=3 * GIB)
report = plan(large_inventory, host_memory_bytes=30 * GIB, **options)
snapshot = report["snapshot"]
resident_host = report["bytes_by_device"]["cpu"]
assert snapshot["host_peak_bytes"] == 6 * max(large_inventory["snapshot_tensor_sizes"].values())
assert snapshot["overlay_bytes"] == 0
assert snapshot["disk_restore_bytes"] == resident_host
assert snapshot["admitted_host_bytes"] == (30 * GIB - resident_host) // 2
assert snapshot["host_peak_bytes"] > snapshot["admitted_host_bytes"]
assert report["status"] == snapshot["status"] == "refused"
assert "Snapshot policy refused: " + snapshot["reason"] in report["refusals"]
assert any(reason.startswith("edit/cpu shortfall") for reason in report["refusals"])
assert report["phases"]["load"]["cpu"]["peak_bytes"] < 30 * GIB
minimum_host = resident_host + 2 * snapshot["host_peak_bytes"]
boundary = plan(large_inventory, host_memory_bytes=minimum_host - 1, **options)
assert boundary["snapshot"]["status"] == "refused"
snapshot_boundary = plan(large_inventory, host_memory_bytes=minimum_host, **options)
assert snapshot_boundary["snapshot"]["status"] == "disk_backed"
required_host = max(minimum_host, max(rows["cpu"]["peak_bytes"] for rows in report["phases"].values()))
admitted = plan(large_inventory, host_memory_bytes=required_host, **options)
assert admitted["status"] == "feasible", admitted["refusals"]
assert admitted["snapshot"]["status"] == "disk_backed"
@pytest.mark.parametrize("backing", ["cpu", "disk"])
@pytest.mark.parametrize("host_budget", [1, 64 * GIB])
def test_snapshot_remaining_host_budget_handles_resident_cpu_and_disk(tiny_inventory, backing, host_budget):
devices = {"cpu": 64 * GIB} if backing == "cpu" else {"cuda:0": 8 * GIB}
mapping = {"": "cpu"} if backing == "cpu" else {"": "cuda:0", "model.layers.0": "disk"}
report = plan(tiny_inventory, devices, device_map=mapping, host_memory_bytes=host_budget,
snapshot_enabled=True)
remaining = max(0, host_budget - report["bytes_by_device"].get("cpu", 0))
assert report["snapshot"]["admitted_host_bytes"] == remaining // 2
@pytest.mark.parametrize("command", ["obliterate", "abliterate"])
@pytest.mark.parametrize("snapshot_args", [[], ["--plan-snapshot", "disabled"],
["--snapshot", "disk", "--snapshot-max-gib", "256"]])
def test_primary_snapshot_policy_matches_plan_and_execution(
tmp_path, large_inventory, monkeypatch, command, snapshot_args,
):
from obliteratus import cli
monkeypatch.setattr("obliteratus.capacity_planning.meta_inventory", lambda *args: large_inventory)
monkeypatch.setattr(cli, "_apply_gpu_selection", lambda args: None)
constructor = Mock()
constructor.return_value.run.return_value = str(tmp_path / "output")
monkeypatch.setattr("obliteratus.abliterate.AbliterationPipeline", constructor)
monkeypatch.setattr("obliteratus.telemetry.maybe_send_pipeline_report", Mock())
monkeypatch.setattr("rich.live.Live", MagicMock())
destination = tmp_path / "plan.json"
options = [
command, "offline", "--method", "basic", "--dtype", "bfloat16",
"--plan-config", str(CONFIGS / "qwen2.5-72b-instruct.json"),
"--plan-free-memory", "96", "96", "96", "96", "--plan-host-memory", "128",
"--plan-disk-memory", "1024", "--plan-output", str(destination), *snapshot_args,
]
cli.main([*options, "--plan-only"])
constructor.assert_not_called()
report = json.loads(destination.read_text())
assert report["status"] == "feasible", report["refusals"]
cli.main(options)
execution = constructor.call_args.kwargs
assert execution["snapshot_enabled"] == report["snapshot"]["enabled"]
assert execution["snapshot_max_bytes"] == report["snapshot"]["max_bytes"]
assert execution["snapshot_enabled"] is ("disk" in snapshot_args)
assert execution["snapshot_max_bytes"] == (256 if snapshot_args and "disk" in snapshot_args else 64) * GIB
@pytest.mark.parametrize("cap", [0, -1, float("inf"), float("nan")])
def test_snapshot_cap_is_validated_before_execution(cap):
from obliteratus.capacity_planning import snapshot_options
parser = argparse.ArgumentParser()
add_capacity_arguments(parser, snapshot_execution=True)
args = parser.parse_args([f"--snapshot-max-gib={cap}"])
with pytest.raises(ValueError, match="positive finite"):
snapshot_options(args)
def test_plan_only_prints_table_and_writes_metadata_evidence(tmp_path, capsys, tiny_inventory):
source = tmp_path / "config.json"
source.write_text(json.dumps(tiny_inventory["config"]))
destination = tmp_path / "plan.json"
parser = argparse.ArgumentParser()
add_capacity_arguments(parser)
args = parser.parse_args([
"--plan-only", "--plan-config", str(source), "--plan-free-memory", "80", "80",
"--plan-offload", "cpu",
"--plan-samples", "32",
"--plan-sequence-length", "128",
"--plan-host-memory", "512", "--plan-disk-memory", "2048", "--plan-output", str(destination),
])
report = maybe_plan(args, "offline-config")
assert json.loads(destination.read_text()) == report
assert report["metadata_source"]["weights_loaded"] is False
assert report["bytes_by_device"]["cpu"] > 0
assert report["metadata_source"]["config_kind"] == "local_json"
assert report["config_sha256"] == hashlib.sha256(
json.dumps(tiny_inventory["config"], sort_keys=True, separators=(",", ":")).encode()
).hexdigest()
assert report["index_sha256"] is None
assert report["claim"] == "config_only_capacity_estimate"
printed = capsys.readouterr().out
assert "peak GiB" in printed
assert "headroom GiB" in printed
assert "direction" in printed
assert "cuda:1" in printed
@pytest.mark.parametrize("generation_cache", [True, False])
@pytest.mark.parametrize("num_beams", [1, 8])
@pytest.mark.parametrize("guidance_scale", [None, 1.5])
def test_plan_only_reads_local_generation_cache_policy(tmp_path, tiny_inventory, generation_cache, num_beams, guidance_scale):
source = tmp_path / "config.json"
source.write_text(json.dumps(dict(tiny_inventory["config"], use_cache=not generation_cache)))
generation = {"use_cache": generation_cache, "num_beams": num_beams, "guidance_scale": guidance_scale}
generation_path = tmp_path / "generation_config.json"
generation_path.write_text(json.dumps(generation))
parser = argparse.ArgumentParser()
add_capacity_arguments(parser)
args = parser.parse_args([
"--plan-only", "--plan-config", str(tmp_path), "--plan-free-memory", "8", "8",
"--plan-samples", "32", "--plan-sequence-length", "16", "--plan-generation-tokens", "512",
"--plan-host-memory", "64", "--plan-disk-memory", "64",
])
report = maybe_plan(args, "offline")
assert report["generation_batch_size"] == report["workload"]["batch_size"] * num_beams
assert report["cache_policy"]["generation_use_cache"] is generation_cache
assert report["cache_policy"]["generation_guidance_scale"] == guidance_scale
copies = 2 if guidance_scale is not None else 1
assert report["cache_policy"]["generation_cache_copies"] == (copies if generation_cache else 0)
assert report["cache_policy"]["forward_use_cache"] is not generation_cache
assert report["metadata_source"]["generation_config"] == str(generation_path)
assert report["generation_config_sha256"] == hashlib.sha256(
json.dumps(generation, sort_keys=True, separators=(",", ":")).encode()
).hexdigest()
@pytest.mark.parametrize("field,value", [("prompt_lookup_num_tokens", 8192), ("assistant_early_exit", 2), ("use_mtp", True)])
def test_plan_only_records_refused_local_assisted_generation(tmp_path, tiny_inventory, monkeypatch, field, value):
def forbidden(*args, **kwargs):
raise AssertionError("assisted planning must stop before loading weights or inspecting CUDA")
monkeypatch.setattr(AutoModelForCausalLM, "from_pretrained", forbidden)
monkeypatch.setattr(torch.cuda, "mem_get_info", forbidden)
(tmp_path / "config.json").write_text(json.dumps(tiny_inventory["config"]))
generation_path = tmp_path / "generation_config.json"
generation_path.write_text(json.dumps({field: value}))
destination = tmp_path / "plan.json"
parser = argparse.ArgumentParser()
add_capacity_arguments(parser)
args = parser.parse_args([
"--plan-only", "--plan-config", str(tmp_path), "--plan-free-memory", "8", "8",
"--plan-samples", "32", "--plan-sequence-length", "16", "--plan-generation-tokens", "16",
"--plan-host-memory", "64", "--plan-disk-memory", "64", "--plan-output", str(destination),
])
with pytest.raises(CapacityPlanningError, match="Assisted generation") as error:
maybe_plan(args, "offline")
report = json.loads(destination.read_text())
assert report == error.value.plan
assert report["generation_assistance_policy"][field] == value
assert report["metadata_source"]["weights_loaded"] is False
assert report["metadata_source"]["generation_config"] == str(generation_path)
@pytest.mark.parametrize("generation_config,mode", UNSUPPORTED_GENERATION_MODES)
def test_plan_only_records_refused_local_generation_mode(tmp_path, tiny_inventory, monkeypatch, generation_config, mode):
loader = Mock(side_effect=AssertionError("weights must remain unloaded"))
cuda = Mock(side_effect=AssertionError("offline planning must not inspect CUDA"))
monkeypatch.setattr(AutoModelForCausalLM, "from_pretrained", loader)
monkeypatch.setattr(torch.cuda, "mem_get_info", cuda)
(tmp_path / "config.json").write_text(json.dumps(tiny_inventory["config"]))
generation_path = tmp_path / "generation_config.json"
generation_path.write_text(json.dumps(generation_config))
destination = tmp_path / "plan.json"
parser = argparse.ArgumentParser()
add_capacity_arguments(parser)
args = parser.parse_args([
"--plan-only", "--plan-config", str(tmp_path), "--plan-free-memory", "8", "8",
"--plan-samples", "32", "--plan-sequence-length", "16", "--plan-generation-tokens", "16",
"--plan-host-memory", "64", "--plan-disk-memory", "64", "--plan-output", str(destination),
])
with pytest.raises(CapacityPlanningError, match=mode) as error:
maybe_plan(args, "offline")
report = json.loads(destination.read_text())
assert report == error.value.plan
assert report["generation_mode"] == mode
assert report["metadata_source"]["weights_loaded"] is False
assert report["metadata_source"]["generation_config"] == str(generation_path)
assert report["generation_config_sha256"] == hashlib.sha256(
json.dumps(generation_config, sort_keys=True, separators=(",", ":")).encode()
).hexdigest()
loader.assert_not_called()
cuda.assert_not_called()
@pytest.mark.parametrize("output", ["output_hidden_states", "output_attentions", "output_scores", "output_logits"])
def test_plan_only_reads_local_generation_output_policy(tmp_path, tiny_inventory, output):
(tmp_path / "config.json").write_text(json.dumps(tiny_inventory["config"]))
parser = argparse.ArgumentParser()
add_capacity_arguments(parser)
args = parser.parse_args([
"--plan-only", "--plan-config", str(tmp_path), "--plan-free-memory", "8",
"--plan-batch-size", "2", "--plan-samples", "32", "--plan-sequence-length", "16",
"--plan-generation-tokens", "512", "--plan-host-memory", "64", "--plan-disk-memory", "64",
])
ordinary = maybe_plan(args, "offline")
generation_path = tmp_path / "generation_config.json"
generation = {output: True, "return_dict_in_generate": True}
generation_path.write_text(json.dumps(generation))
report = maybe_plan(args, "offline")
assert report["status"] == "feasible", report["refusals"]
assert report["generation_output_policy"][output] is True
assert report["generation_output_policy"]["return_dict_in_generate"] is True
assert report["metadata_source"]["generation_config"] == str(generation_path)
assert report["generation_config_sha256"] == hashlib.sha256(
json.dumps(generation, sort_keys=True, separators=(",", ":")).encode()
).hexdigest()
assert report["phases"]["activations"] == ordinary["phases"]["activations"]
for phase in ("baseline", "verify", "reload"):
assert report["phases"][phase]["cuda:0"]["peak_bytes"] > ordinary["phases"][phase]["cuda:0"]["peak_bytes"]
@pytest.mark.parametrize("mapping", [None, [], 1, "auto"])
def test_plan_rejects_nonobject_device_map_json(tmp_path, tiny_inventory, monkeypatch, mapping):
monkeypatch.setattr("obliteratus.capacity_planning.meta_inventory", lambda *args: tiny_inventory)
source = tmp_path / "config.json"
source.write_text("{}")
map_path = tmp_path / "map.json"
map_path.write_text(json.dumps(mapping))
parser = argparse.ArgumentParser()
add_capacity_arguments(parser)
parser.add_argument("--device-map", type=Path)
args = parser.parse_args([
"--plan-only", "--plan-config", str(source), "--device-map", str(map_path),
"--plan-samples", "32",
"--plan-free-memory", "96", "96", "--plan-host-memory", "64", "--plan-disk-memory", "64",
])
with pytest.raises(ValueError, match="device map JSON must be an object"):
maybe_plan(args, "offline")
@pytest.mark.parametrize("command", ["obliterate", "abliterate"])
@pytest.mark.parametrize("options,concept_edit", [
([], False),
(["--concept", "refusal"], False),
(["--concept", "pain", "--skip-repair"], True),
(["--preserve", "protected.safetensors"], True),
(["--concept", "refusal", "--preserve", "protected.safetensors"], True),
])
def test_primary_basic_plan_charges_only_requested_concept_rollback(
tmp_path, large_inventory, monkeypatch, command, options, concept_edit,
):
from obliteratus import cli
def forbidden(*args, **kwargs):
raise AssertionError("plan-only must not select GPUs or execute the pipeline")
monkeypatch.setattr("obliteratus.capacity_planning.meta_inventory", lambda *args: large_inventory)
monkeypatch.setattr(cli, "_apply_gpu_selection", forbidden)
monkeypatch.setattr(cli, "_cmd_abliterate", forbidden)
destination = tmp_path / "plan.json"
arguments = [
command, "offline", "--method", "basic", "--plan-only",
"--plan-config", str(CONFIGS / "qwen2.5-72b-instruct.json"),
"--plan-free-memory", "96", "96", "--plan-host-memory", "32", "--plan-disk-memory", "1024",
"--plan-batch-size", "1", "--plan-sequence-length", "128", "--plan-generation-tokens", "16",
"--plan-output", str(destination), *options,
]
if "pain" in options:
arguments.extend(["--plan-samples", "32", "--plan-prompt-characters", "2048"])
if concept_edit:
with pytest.raises(SystemExit) as error:
cli.main(arguments)
assert error.value.code == 2
else:
cli.main(arguments)
report = json.loads(destination.read_text())
assert report["workload"]["auxiliary_readouts"] == (3 if "pain" in options else 0)
assert report["workload"]["neighbour_samples"] == (96 if "pain" in options else 0)
rollback = report["phases"]["edit"]["cpu"]["components"]["concept_transaction_rollback"]
if concept_edit:
assert report["edit_contract"] == "concept_output_transactions"
assert rollback > 46 * GIB
assert report["status"] == "refused"
assert any(reason.startswith("edit/cpu shortfall") for reason in report["refusals"])
else:
assert report["edit_contract"] == "basic_sequential_projection"
assert rollback == 0
assert report["status"] == "feasible", report["refusals"]
@pytest.mark.parametrize("command", ["obliterate", "abliterate"])
@pytest.mark.parametrize("options,copies,neighbours", [
(["--gate-mode", "enforce"], 3, 96),
(["--gate-mode", "report", "--override-gate", "operator decision"], 3, 96),
(["--gate-mode", "off"], 0, 0),
(["--plan-neighbour-samples", "0"], 3, 0),
(["--plan-neighbour-samples", "1000"], 3, 1000),
])
def test_primary_concept_plan_matches_gate_readout_policy(
tmp_path, tiny_inventory, monkeypatch, command, options, copies, neighbours,
):
from obliteratus import cli
def forbidden(*args, **kwargs):
raise AssertionError("plan-only must not select GPUs or execute the pipeline")
monkeypatch.setattr("obliteratus.capacity_planning.meta_inventory", lambda *args: tiny_inventory)
monkeypatch.setattr(cli, "_apply_gpu_selection", forbidden)
monkeypatch.setattr(cli, "_cmd_abliterate", forbidden)
destination = tmp_path / "plan.json"
cli.main([
command, "offline", "--method", "basic", "--concept", "missing/pack", "--skip-repair",
"--plan-only", "--plan-config", str(CONFIGS / "qwen2.5-32b-instruct.json"),
"--plan-free-memory", "8", "8", "--plan-host-memory", "64", "--plan-disk-memory", "64",
"--plan-samples", "32", "--plan-prompt-characters", "2048", "--plan-output", str(destination), *options,
])
report = json.loads(destination.read_text())
assert report["status"] == "feasible", report["refusals"]
assert report["workload"]["auxiliary_readouts"] == copies
assert report["workload"]["neighbour_samples"] == neighbours
assert report["workload"]["concept_extraction"] is True
assert report["workload"]["validation_gate"] is (copies > 0)
expected = (64 * (1 + copies) + neighbours) * 6 * 32 * 4
assert report["phases"]["activations"]["cpu"]["components"]["retained_activations"] == expected
materialization = 3 * expected if copies else 64 * 6 * 32 * 4
assert report["phases"]["direction"]["cpu"]["components"]["activation_materialization"] == materialization
@pytest.mark.parametrize("repair,concept,preserve,repair_preserve,refused", [
(True, "pain", None, ["protected.safetensors"], True),
(True, "pain", ["protected.safetensors"], None, True),
(True, "pain", None, None, True),
(None, "pain", None, ["protected.safetensors"], True),
(None, "pain", None, None, True),
(True, "refusal", None, None, True),
(False, "pain", None, ["protected.safetensors"], False),
(None, "refusal", None, None, False),
])
def test_plan_does_not_admit_unmodelled_repair_snapshots(
large_inventory, monkeypatch, repair, concept, preserve, repair_preserve, refused,
):
monkeypatch.setattr("obliteratus.capacity_planning.meta_inventory", lambda *args: large_inventory)
parser = argparse.ArgumentParser()
add_capacity_arguments(parser)
args = parser.parse_args([
"--plan-only", "--plan-config", str(CONFIGS / "qwen2.5-72b-instruct.json"),
"--plan-free-memory", "96", "96", "--plan-host-memory", "1024", "--plan-disk-memory", "1024",
"--plan-samples", "4096", "--plan-prompt-characters", "1000000",
])
args.command, args.method, args.concept = "obliterate", "basic", concept
args.repair, args.preserve, args.repair_preserve = repair, preserve, repair_preserve
if refused:
with pytest.raises(CapacityPlanningError, match="repair snapshots") as error:
maybe_plan(args, "offline", sequence_length=128)
report = error.value.plan
assert report["status"] == "refused"
assert len(report["refusals"]) == 1
assert report["snapshot"]["status"] == "disabled_with_reason"
else:
assert maybe_plan(args, "offline", sequence_length=128)["status"] == "feasible"
@pytest.mark.parametrize("explicit,inherited,expected", [(None, None, 1024), (256, None, 256),
(None, 512, 512), (256, 512, 256)])
def test_unbounded_entry_points_default_to_config_context(
tmp_path, tiny_inventory, monkeypatch, explicit, inherited, expected,
):
inventory = copy.deepcopy(tiny_inventory)
inventory["text_config"]["max_position_embeddings"] = 1024
source = tmp_path / "config.json"
source.write_text("{}")
monkeypatch.setattr("obliteratus.capacity_planning.meta_inventory", lambda *args: inventory)
parser = argparse.ArgumentParser()
add_capacity_arguments(parser)
options = [] if explicit is None else ["--plan-sequence-length", str(explicit)]
args = parser.parse_args([
"--plan-only", "--plan-config", str(source), "--plan-free-memory", "80", "80",
"--plan-host-memory", "128", "--plan-disk-memory", "1024", "--plan-samples", "32", *options,
])
report = maybe_plan(args, "offline", sequence_length=inherited)
assert report["workload"]["sequence_length"] == expected
def test_plan_only_is_opt_in_and_never_resolves_remote_metadata():
parser = argparse.ArgumentParser()
add_capacity_arguments(parser)
assert maybe_plan(parser.parse_args([]), "Qwen/Qwen2.5-72B-Instruct") is None
with pytest.raises(ValueError, match="local --plan-config"):
maybe_plan(parser.parse_args(["--plan-only"]), "Qwen/Qwen2.5-72B-Instruct")
@pytest.mark.parametrize("command", ["obliterate", "abliterate"])
def test_primary_plan_counts_full_local_corpus_and_retains_refusal(
tmp_path, large_inventory, monkeypatch, command,
):
from obliteratus import cli
def forbidden(*args, **kwargs):
raise AssertionError("plan-only must not inspect GPUs or load weights")
monkeypatch.setattr(torch.cuda, "mem_get_info", forbidden)
monkeypatch.setattr(cli, "_apply_gpu_selection", forbidden)
monkeypatch.setattr(cli, "_cmd_abliterate", forbidden)
monkeypatch.setattr("obliteratus.capacity_planning.meta_inventory", lambda *args: large_inventory)
pairs = tmp_path / "pairs.json"
pairs.write_text(json.dumps({"harmful": ["unsafe"] * 4096, "harmless": ["safe"] * 4096}))
destination = tmp_path / "plan.json"
with pytest.raises(SystemExit) as error:
cli.main([
command, "offline", "--method", "basic", "--n-directions", "2", "--plan-only",
"--prompt-pairs-file", str(pairs),
"--plan-config", str(CONFIGS / "qwen2.5-72b-instruct.json"),
"--plan-free-memory", "96", "96", "--plan-host-memory", "28", "--plan-disk-memory", "1024",
"--plan-batch-size", "1", "--plan-sequence-length", "128", "--plan-generation-tokens", "16",
"--plan-output", str(destination),
])
assert error.value.code == 2
report = json.loads(destination.read_text())
assert report["workload"]["samples"] == 4096
assert report["status"] == "refused"
assert report["phases"]["direction"]["cpu"]["peak_bytes"] > 35 * GIB
assert any(reason.startswith("direction/cpu shortfall") for reason in report["refusals"])
@pytest.mark.parametrize("command", ["obliterate", "abliterate"])
@pytest.mark.parametrize("direction_method,host_gib", [
(None, 35.2), ("diff_means", 35.2), ("svd", 35.2), ("fisher_lda", 35.2),
("leace", 35.2), ("leace_exact", 35.2), ("leace_exact", 64),
])
def test_primary_plan_budgets_selected_extractor(
tmp_path, large_inventory, monkeypatch, command, direction_method, host_gib,
):
from obliteratus import cli
def forbidden(*args, **kwargs):
raise AssertionError("plan-only must not inspect GPUs or load weights")
monkeypatch.setattr("obliteratus.capacity_planning.meta_inventory", lambda *args: large_inventory)
monkeypatch.setattr(torch.cuda, "mem_get_info", forbidden)
monkeypatch.setattr(cli, "_apply_gpu_selection", forbidden)
monkeypatch.setattr(cli, "_cmd_abliterate", forbidden)
destination = tmp_path / "plan.json"
arguments = [
command, "offline", "--method", "basic", "--dtype", "bfloat16", "--plan-only",
"--plan-config", str(CONFIGS / "qwen2.5-72b-instruct.json"),
"--plan-free-memory", "96", "96", "--plan-host-memory", str(host_gib), "--plan-disk-memory", "1024",
"--plan-samples", "6000", "--plan-batch-size", "1", "--plan-sequence-length", "16",
"--plan-generation-tokens", "16", "--plan-output", str(destination),
]
if direction_method is not None:
arguments.extend(["--direction-method", direction_method])
refused = direction_method == "leace_exact" and host_gib == 35.2
if refused:
with pytest.raises(SystemExit) as error:
cli.main(arguments)
assert error.value.code == 2
else:
cli.main(arguments)
report = json.loads(destination.read_text())
assert report["workload"]["direction_method"] == (direction_method or "diff_means")
if direction_method == "leace_exact":
assert report["phases"]["direction"]["cpu"]["peak_bytes"] > 35.244 * GIB
if refused:
assert report["status"] == "refused"
assert any(reason.startswith("direction/cpu shortfall") for reason in report["refusals"])
else:
assert report["status"] == "feasible", report["refusals"]
@pytest.mark.parametrize("source", ["builtin", "refusal", "core/refusal", "pairs"])
@pytest.mark.parametrize("explicit", [None, 4096, 1])
def test_sample_bound_covers_known_corpus(tmp_path, tiny_inventory, monkeypatch, source, explicit):
from obliteratus.prompts import BUILTIN_HARMFUL, BUILTIN_HARMLESS
monkeypatch.setattr("obliteratus.capacity_planning.meta_inventory", lambda *args: tiny_inventory)
parser = argparse.ArgumentParser()
add_capacity_arguments(parser)
args = parser.parse_args([
"--plan-only", "--plan-config", str(CONFIGS / "qwen2.5-72b-instruct.json"),
"--plan-free-memory", "8", "8", "--plan-host-memory", "64", "--plan-disk-memory", "64",
])
args.command, args.plan_samples = "obliterate", explicit
expected = max(len(BUILTIN_HARMFUL), len(BUILTIN_HARMLESS))
characters = sum(map(len, BUILTIN_HARMFUL)) + sum(map(len, BUILTIN_HARMLESS))
if source in {"refusal", "core/refusal"}:
args.concept = source
if source == "pairs":
args.prompt_pairs_file = tmp_path / "pairs.json"
args.prompt_pairs_file.write_text(json.dumps({"harmful": ["unsafe"] * 40, "harmless": ["safe"] * 40}))
expected = 40
characters = 40 * (len("unsafe") + len("safe"))
if explicit == 1:
with pytest.raises(ValueError, match="cannot be smaller than the selected corpus"):
maybe_plan(args, "offline", sequence_length=128)
else:
report = maybe_plan(args, "offline", sequence_length=128)
assert report["workload"]["samples"] == (expected if explicit is None else explicit)
assert report["workload"]["prompt_characters"] == characters
@pytest.mark.parametrize("selection", ["pairs", "builtin", "baseline"])
@pytest.mark.parametrize("explicit", [None, 0, 1_000_000])
def test_prompt_character_bound_covers_untruncated_local_text(tmp_path, selection, explicit):
from obliteratus.capacity_planning import _plan_prompt_counts
from obliteratus.prompts import BUILTIN_HARMFUL, BUILTIN_HARMLESS
args = argparse.Namespace(command="obliterate", plan_samples=None,
plan_prompt_characters=explicit)
expected = sum(map(len, BUILTIN_HARMFUL)) + sum(map(len, BUILTIN_HARMLESS))
if selection == "pairs":
args.prompt_pairs_file = tmp_path / "pairs.json"
args.prompt_pairs_file.write_text(json.dumps({"harmful": ["long" * 1000] * 5,
"harmless": ["\ufb03\U0001f642" * 1000] * 5}))
expected = 30_000
elif selection == "baseline":
args.command, args.concepts_command, args.source = "concepts", "baseline", "core/refusal"
if explicit == 0:
with pytest.raises(ValueError, match="cannot be smaller than the selected corpus"):
_plan_prompt_counts(args)
else:
_, _, characters = _plan_prompt_counts(args)
assert characters == (expected if explicit is None else explicit)
@pytest.mark.parametrize("value", [-1, True, 1.5])
def test_invalid_explicit_prompt_character_bound_is_rejected(value):
from obliteratus.capacity_planning import _plan_prompt_counts
args = argparse.Namespace(plan_samples=32, plan_prompt_characters=value)
with pytest.raises(ValueError, match="--plan-prompt-characters must be a nonnegative integer"):
_plan_prompt_counts(args)
@pytest.mark.parametrize("selection", [{}, {"command": "obliterate", "concept": "missing/pack"},
{"command": "abliterate", "residue_file": ["missing.json"]}])
def test_uncountable_corpus_requires_explicit_sample_bound(tmp_path, tiny_inventory, monkeypatch, selection):
monkeypatch.setattr("obliteratus.capacity_planning.meta_inventory", lambda *args: tiny_inventory)
parser = argparse.ArgumentParser()
add_capacity_arguments(parser)
args = parser.parse_args([
"--plan-only", "--plan-config", str(CONFIGS / "qwen2.5-72b-instruct.json"),
"--plan-free-memory", "8", "8", "--plan-host-memory", "64", "--plan-disk-memory", "64",
])
vars(args).update(selection)
args.repair = False
with pytest.raises(ValueError, match="requires --plan-samples"):
maybe_plan(args, "offline", sequence_length=128)
args.plan_samples = 64
args.plan_prompt_characters = 128
assert maybe_plan(args, "offline", sequence_length=128)["workload"]["samples"] == 64
@pytest.mark.parametrize("samples", [0, -1])
def test_invalid_explicit_sample_bound_is_rejected(tiny_inventory, monkeypatch, samples):
monkeypatch.setattr("obliteratus.capacity_planning.meta_inventory", lambda *args: tiny_inventory)
parser = argparse.ArgumentParser()
add_capacity_arguments(parser)
args = parser.parse_args([
"--plan-only", "--plan-config", str(CONFIGS / "qwen2.5-72b-instruct.json"),
"--plan-free-memory", "8", "8", "--plan-host-memory", "64", "--plan-disk-memory", "64",
f"--plan-samples={samples}",
])
with pytest.raises(ValueError, match="positive integer"):
maybe_plan(args, "offline", sequence_length=128)
@pytest.mark.parametrize("environment,explicit,utilization,gpu_budget,host_budget,status", [
({"cuda:0": 32 * GIB, "cuda:1": 32 * GIB}, None, None, 32, 64, "refused"),
({"cuda:0": 96 * GIB}, {"0": 32 * GIB, "1": 32 * GIB}, None, 32, 64, "refused"),
({}, None, 0.5, 48, 64, "refused"),
({"cuda:0": 32 * GIB, "cuda:1": 32 * GIB}, None, 0.5, 32, 64, "refused"),
({"cpu": GIB}, None, None, 96, 1, "refused"),
({"cuda:0": 0, "cpu": 0}, {}, None, 96, 64, "feasible"),
])
def test_driver_plan_applies_effective_memory_limits(
tmp_path, large_inventory, monkeypatch, environment, explicit, utilization, gpu_budget, host_budget, status,
):
from scripts import gpu_qualification as driver
def forbidden(*args, **kwargs):
raise AssertionError("dry-run must not inspect GPUs or launch workers")
monkeypatch.setattr("obliteratus.capacity_planning.meta_inventory", lambda *args: large_inventory)
monkeypatch.setattr(torch.cuda, "mem_get_info", forbidden)
monkeypatch.setattr(driver, "supervise", forbidden)
monkeypatch.setenv("OBLITERATUS_DEVICE_MEMORY_CEILINGS", json.dumps(environment))
destination = tmp_path / "plan.json"
options = []
if explicit is not None:
ceilings = tmp_path / "ceilings.json"
ceilings.write_text(json.dumps(explicit))
options.extend(["--device-ceilings", str(ceilings)])
if utilization is not None:
options.extend(["--gpu-memory-utilization", str(utilization)])
result = driver.main([
"--output", str(tmp_path / "qualification"), "--plan-only", "--dtype", "bfloat16",
"--plan-config", str(CONFIGS / "qwen2.5-72b-instruct.json"),
"--plan-free-memory", "96", "96", "--plan-host-memory", "64", "--plan-disk-memory", "1024",
"--plan-output", str(destination), *options,
])
assert result == (0 if status == "feasible" else 1)
report = json.loads(destination.read_text())
assert report["status"] == status
for rows in report["phases"].values():
assert rows["cuda:0"]["budget_bytes"] == gpu_budget * GIB
assert rows["cuda:1"]["budget_bytes"] == gpu_budget * GIB
assert rows["cpu"]["budget_bytes"] == host_budget * GIB
@pytest.mark.parametrize("utilization", [0, -1, 1.1, float("nan"), float("inf")])
def test_plan_rejects_invalid_utilization(tiny_inventory, monkeypatch, utilization):
from scripts import gpu_qualification as driver
monkeypatch.setattr("obliteratus.capacity_planning.meta_inventory", lambda *args: tiny_inventory)
args = driver.parser().parse_args([
"--output", "unused", "--plan-only", "--plan-config", str(CONFIGS / "qwen2.5-72b-instruct.json"),
"--plan-free-memory", "8", "8", "--plan-host-memory", "64", "--plan-disk-memory", "64",
f"--gpu-memory-utilization={utilization}",
])
with pytest.raises(ValueError, match="gpu_memory_utilization must be finite"):
maybe_plan(args, "offline", sequence_length=128)
@pytest.mark.parametrize("device,options,message", [
("cuda:2", [], "visible-order budget for cuda:2"),
("mps", [], "no capacity contract for execution device"),
("cuda:0", ["--plan-offload", "cpu"], "Explicit execution devices"),
])
def test_plan_refuses_incompatible_execution_placement(tiny_inventory, monkeypatch, device, options, message):
monkeypatch.setattr("obliteratus.capacity_planning.meta_inventory", lambda *args: tiny_inventory)
parser = argparse.ArgumentParser()
add_capacity_arguments(parser)
args = parser.parse_args([
"--plan-only", "--plan-config", str(CONFIGS / "qwen2.5-72b-instruct.json"),
"--plan-free-memory", "8", "8", "--plan-host-memory", "64", "--plan-disk-memory", "64",
"--plan-samples", "32", *options,
])
args.device = device
with pytest.raises(ValueError, match=message):
maybe_plan(args, "offline", sequence_length=128)
def test_explicit_execution_device_rejects_conflicting_map(tiny_inventory, monkeypatch):
from scripts import gpu_qualification as driver
monkeypatch.setattr("obliteratus.capacity_planning.meta_inventory", lambda *args: tiny_inventory)
args = driver.parser().parse_args([
"--output", "unused", "--plan-only", "--plan-config", str(CONFIGS / "qwen2.5-72b-instruct.json"),
"--plan-free-memory", "8", "8", "--plan-host-memory", "64", "--plan-disk-memory", "64",
])
args.device_map = {"": "cuda:0", "model.layers.0": "cpu"}
with pytest.raises(ValueError, match="map conflicts with the requested execution device"):
maybe_plan(args, "offline", device="cuda:0", sequence_length=128)
@pytest.mark.parametrize("budgets", [[8], [8, 8, 8]])
def test_driver_dry_run_requires_requested_gpu_count(tmp_path, monkeypatch, budgets):
from scripts import gpu_qualification as driver
monkeypatch.setattr(driver, "supervise", Mock(side_effect=AssertionError("dry-run launched a worker")))
with pytest.raises(ValueError, match="exactly --devices"):
driver.main([
"--output", str(tmp_path / "unused"), "--plan-only", "--devices", "2",
"--plan-free-memory", *map(str, budgets),
])
@pytest.mark.parametrize("offload", ["none", "cpu", "disk"])
def test_driver_rehearsal_dry_run_uses_host_budget(tmp_path, tiny_inventory, monkeypatch, offload):
from scripts import gpu_qualification as driver
monkeypatch.setattr("obliteratus.capacity_planning.meta_inventory", lambda *args: tiny_inventory)
monkeypatch.setattr(driver, "supervise", Mock(side_effect=AssertionError("dry-run launched a worker")))
destination = tmp_path / "plan.json"
assert driver.main([
"--output", str(tmp_path / "unused"), "--plan-only", "--mode", "rehearsal", "--devices", "3",
"--offload", offload,
"--plan-config", str(CONFIGS / "qwen2.5-72b-instruct.json"), "--plan-output", str(destination),
"--plan-host-memory", "64", "--plan-disk-memory", "64",
]) == 0
report = json.loads(destination.read_text())
assert report["execution_device"] == "cpu"
assert set(report["bytes_by_device"]) == ({"cpu", "disk"} if offload == "disk" else {"cpu"})
assert all(set(rows) == {"cpu", "disk"} for rows in report["phases"].values())
@pytest.mark.parametrize("samples", [None, 64])
def test_driver_plan_counts_selected_workload(tmp_path, tiny_inventory, monkeypatch, samples):
from scripts import gpu_qualification as driver
monkeypatch.setattr("obliteratus.capacity_planning.meta_inventory", lambda *args: tiny_inventory)
workload = tmp_path / "workload.json"
workload.write_text(json.dumps({"harmful_prompts": ["unsafe"] * 40, "harmless_prompts": ["safe"] * 48,
"comparison_prompts": ["compare"] * 7}))
args = driver.parser().parse_args([
"--output", str(tmp_path / "unused"), "--plan-only", "--workload", str(workload),
"--plan-config", str(CONFIGS / "qwen2.5-72b-instruct.json"),
"--plan-free-memory", "8", "8", "--plan-host-memory", "64", "--plan-disk-memory", "64",
])
args.plan_samples = samples
report = maybe_plan(args, "offline", sequence_length=128)
assert report["workload"]["samples"] == (samples or 48)
assert report["workload"]["comparison_prompts"] == 7
args.plan_samples = 32
with pytest.raises(ValueError, match="cannot be smaller than the selected corpus"):
maybe_plan(args, "offline", sequence_length=128)
@pytest.mark.parametrize("comparisons,refused", [(2, False), (100_000, True)])
def test_driver_dry_run_counts_retained_comparisons(tmp_path, large_inventory, monkeypatch, comparisons, refused):
from scripts import gpu_qualification as driver
monkeypatch.setattr("obliteratus.capacity_planning.meta_inventory", lambda *args: large_inventory)
workload = tmp_path / "workload.json"
workload.write_text(json.dumps({"harmful_prompts": ["unsafe"] * 3, "harmless_prompts": ["safe"] * 3,
"comparison_prompts": ["compare"] * comparisons}))
destination = tmp_path / "plan.json"
status = driver.main([
"--output", str(tmp_path / "unused"), "--plan-only", "--workload", str(workload),
"--plan-config", str(CONFIGS / "qwen2.5-72b-instruct.json"), "--plan-output", str(destination),
"--dtype", "bfloat16",
"--plan-free-memory", "96", "96", "--plan-host-memory", "32", "--plan-disk-memory", "1024",
])
report = json.loads(destination.read_text())
assert status == int(refused)
assert report["workload"]["samples"] == 3
assert report["workload"]["comparison_prompts"] == comparisons
assert report["status"] == ("refused" if refused else "feasible")
if refused:
assert any(reason.startswith("baseline/cpu shortfall") for reason in report["refusals"])
def test_driver_fixture_dry_run_includes_default_comparisons(tmp_path, tiny_inventory, monkeypatch):
from scripts import gpu_qualification as driver
monkeypatch.setattr("obliteratus.capacity_planning.meta_inventory", lambda *args: tiny_inventory)
args = driver.parser().parse_args([
"--output", str(tmp_path / "unused"), "--plan-only",
"--plan-config", str(CONFIGS / "qwen2.5-72b-instruct.json"),
"--plan-free-memory", "8", "8", "--plan-host-memory", "64", "--plan-disk-memory", "64",
])
report = maybe_plan(args, "offline", sequence_length=32)
assert report["workload"]["comparison_prompts"] == 2
assert report["phases"]["verify"]["cpu"]["components"]["retained_comparisons"] > 0
@pytest.mark.parametrize("entry", ["dry_run", "preparation"])
@pytest.mark.parametrize("reference,gpu_gib,host_gib,selected,refused", [
("cuda:0", 96, 64, "cuda:0", True),
("cuda:0", 192, 64, "cuda:0", False),
("cpu", 96, 64, "cpu", True),
("cpu", 96, 512, "cpu", False),
("none", 96, 64, "none", False),
("auto", 96, 512, "none", False),
])
def test_qualification_budgets_reference_before_admitting_run(
tmp_path, monkeypatch, large_inventory, entry, reference, gpu_gib, host_gib, selected, refused,
):
from types import SimpleNamespace
from obliteratus import qualification_runtime as runtime
from obliteratus.models import host_resources
from scripts import gpu_qualification as driver
def forbidden(*args, **kwargs):
raise AssertionError("reference preflight must not load weights or launch workers")
monkeypatch.setenv("OBLITERATUS_DEVICE_MEMORY_CEILINGS", "{}")
monkeypatch.setattr("obliteratus.capacity_planning.meta_inventory", lambda *args: large_inventory)
monkeypatch.setattr(AutoModelForCausalLM, "from_pretrained", forbidden)
monkeypatch.setattr(driver, "run_worker", forbidden)
monkeypatch.setattr(driver, "supervise", forbidden)
source = tmp_path / "source"
source.mkdir()
(source / "config.json").write_text(json.dumps(large_inventory["config"]))
if entry == "dry_run":
monkeypatch.setattr(torch.cuda, "mem_get_info", forbidden)
destination = tmp_path / "plan.json"
status = driver.main([
"--output", str(tmp_path / "unused"), "--dry-run", "--plan-config", str(source),
"--plan-output", str(destination), "--dtype", "bfloat16", "--reference", reference,
"--plan-free-memory", str(gpu_gib), str(gpu_gib),
"--plan-host-memory", str(host_gib), "--plan-disk-memory", "1024",
])
assert status == int(refused)
report = json.loads(destination.read_text())
else:
monkeypatch.setattr(host_resources, "available_host_bytes", lambda: host_gib * GIB)
monkeypatch.setattr(torch.cuda, "mem_get_info", lambda device: (gpu_gib * GIB, gpu_gib * GIB))
monkeypatch.setattr(torch.cuda, "memory_reserved", lambda device: 0)
monkeypatch.setattr(runtime.shutil, "disk_usage", lambda path: SimpleNamespace(free=1024 * GIB))
configuration = {"requested_devices": ["cuda:0", "cuda:1"], "dtype": "bfloat16",
"gpu_memory_utilization": None, "generation_tokens": 4, "offload": "none",
"reference": reference, "reference_max_bytes": GIB,
"workload": {"harmful_prompts": ["unsafe"] * 3, "harmless_prompts": ["safe"] * 3,
"comparison_prompts": ["compare"] * 2}}
if refused:
with pytest.raises(CapacityPlanningError) as error:
runtime.capacity_plan(source, configuration, tmp_path)
report = error.value.plan
else:
report = runtime.capacity_plan(source, configuration, tmp_path)
assert report["status"] == ("refused" if refused else "feasible")
assert report["reference_policy"]["selected"] == selected
assert all(row["headroom_bytes"] >= 0 for rows in report["phases"].values() for row in rows.values())
if selected == "none":
assert "reference_plan" not in report
else:
reference_plan = report["reference_plan"]
assert reference_plan["bytes_by_device"] == {selected: large_inventory["total_weight_bytes"]}
assert reference_plan["status"] == ("refused" if refused else "feasible")
if refused:
assert any(reason.startswith(f"reference/load/{selected} shortfall") for reason in report["refusals"])
@pytest.mark.parametrize("reference_limit,host_gib,selected", [(0, 64, "none"), (GIB, 0, "none"), (GIB, 64, "cpu")])
def test_automatic_reference_uses_planned_host_headroom(tiny_inventory, reference_limit, host_gib, selected):
from obliteratus.capacity_planning import plan_qualification_capacity
report = plan_qualification_capacity(tiny_inventory, {"cuda:0": 8 * GIB, "cuda:1": 8 * GIB},
host_memory_bytes=host_gib * GIB, disk_memory_bytes=64 * GIB,
reference="auto", reference_max_bytes=reference_limit)
assert report["reference_policy"]["selected"] == selected
if selected == "cpu":
assert report["reference_plan"]["status"] == "feasible"
assert report["reference_plan"]["phases"]["load"]["cpu"]["budget_bytes"] == host_gib * GIB
def test_intervention_reference_option_does_not_request_qualification_worker(tmp_path, tiny_inventory, monkeypatch):
monkeypatch.setattr("obliteratus.capacity_planning.meta_inventory", lambda *args: tiny_inventory)
parser = argparse.ArgumentParser()
add_capacity_arguments(parser)
parser.add_argument("--reference", choices=("zero", "mean"), default="zero")
args = parser.parse_args([
"--plan-only", "--plan-config", str(CONFIGS / "qwen2.5-72b-instruct.json"),
"--plan-free-memory", "8", "8", "--plan-host-memory", "64", "--plan-disk-memory", "64",
"--plan-samples", "32", "--reference", "mean",
])
report = maybe_plan(args, "offline", sequence_length=128)
assert report["status"] == "feasible"
assert "reference_policy" not in report
@pytest.mark.parametrize("setting,value", [
("batch_size", 0), ("sequence_length", -1), ("samples", True),
("generation_tokens", 0), ("n_directions", 0), ("attention_backend", "unknown"),
("auxiliary_readouts", -1), ("auxiliary_readouts", True),
("neighbour_samples", -1), ("neighbour_samples", 1.5),
("activation_datasets", 0), ("activation_datasets", True),
("collection_copies", -1), ("collection_copies", 1.5),
("concept_extraction", 1), ("concept_extraction", "true"),
("preserve_refusal", 1), ("preserve_refusal", "true"),
("validation_gate", 1), ("validation_gate", "true"),
("prompt_characters", -1), ("prompt_characters", True), ("prompt_characters", 1.5),
("comparison_prompts", -1), ("comparison_prompts", True), ("comparison_prompts", 1.5),
("direction_method", "unknown"), ("direction_method", "som"),
("direction_method", None), ("direction_method", 1),
])
def test_invalid_workload_is_rejected(setting, value):
with pytest.raises(ValueError):
Workload(**{setting: value})
@pytest.mark.parametrize("bad_budget", [-1, True, float("nan"), 1.5])
def test_invalid_memory_budget_is_rejected(tiny_inventory, bad_budget):
with pytest.raises(ValueError, match="memory budgets"):
plan(tiny_inventory, {"cuda:0": bad_budget})
@pytest.mark.parametrize("samples,expected_rows", [(9, 0), (10, 10), (32, 16)])
def test_sequence_wide_kl_logits_survive_edit_and_verification(tiny_inventory, samples, expected_rows):
workload = Workload(samples=samples, sequence_length=1024)
report = plan(tiny_inventory, workload=workload)
expected = expected_rows * 1025 * 64 * 4
for phase in ("edit", "verify"):
assert report["phases"][phase]["cpu"]["components"]["retained_baseline_kl_logits"] == expected
if expected:
refused = plan(tiny_inventory, workload=workload, host_memory_bytes=GIB + expected - 1)
assert any(reason.startswith("edit/cpu shortfall") for reason in refused["refusals"])
def test_uncached_preservation_probe_defaults_overlap_primary_caches(tiny_inventory, monkeypatch):
from obliteratus import phase_admission
from obliteratus.prompts import BUILTIN_HARMFUL, BUILTIN_HARMLESS
demands = Mock(wraps=phase_admission.transformer_demands)
monkeypatch.setattr(phase_admission, "transformer_demands", demands)
report = plan(tiny_inventory, workload=Workload(batch_size=1, sequence_length=8, samples=2,
concept_extraction=True, preserve_refusal=True))
assert any(call.kwargs["batch_size"] == 16 and call.kwargs["tokens"] == 256
for call in demands.call_args_list)
components = report["phases"]["direction"]["cpu"]["components"]
assert components["retained_activations"] == 2 * 2 * 6 * 32 * 4
assert components["preservation_activations"] == 2 * max(len(BUILTIN_HARMFUL), len(BUILTIN_HARMLESS)) * 6 * 32 * 4
assert components["preservation_distillation"] > 0
assert components["preserved_directions"] == 2 * 6 * 32 * 4
@pytest.mark.parametrize("cached", [False, True])
def test_preservation_selection_is_recorded_without_acquiring_directions(tiny_inventory, monkeypatch, cached):
monkeypatch.setattr("obliteratus.capacity_planning.meta_inventory", lambda *args: tiny_inventory)
parser = argparse.ArgumentParser()
add_capacity_arguments(parser)
args = parser.parse_args([
"--plan-only", "--plan-config", str(CONFIGS / "qwen2.5-72b-instruct.json"),
"--plan-free-memory", "8", "8", "--plan-host-memory", "64", "--plan-disk-memory", "64",
"--plan-samples", "32",
])
args.preserve = ["refusal"]
args.refusal_direction = "never-read.safetensors" if cached else None
assert maybe_plan(args, "offline", sequence_length=8)["workload"]["preserve_refusal"] is not cached
def test_cpu_reference_and_disk_offload_reserve_retained_files(tiny_inventory):
from obliteratus.capacity_planning import plan_qualification_capacity
mapping = {"": "cuda:0", tiny_inventory["blocks"][-1]: "disk", "lm_head": "disk"}
options = dict(host_memory_bytes=64 * GIB, disk_memory_bytes=64 * GIB,
reference="cpu", offload="disk", device_map=mapping, source_staged=True)
report = plan_qualification_capacity(tiny_inventory, {"cuda:0": 8 * GIB}, **options)
disk = report["phases"]["save"]["disk"]
weights = tiny_inventory["total_weight_bytes"]
offloaded = report["bytes_by_device"]["disk"]
assert disk["components"]["retained_reference_export"] == weights
assert disk["components"]["edit_backing"] == offloaded
assert disk["components"]["reload_reservation"] == offloaded
assert disk["peak_bytes"] == GIB + 3 * weights + 3 * offloaded
options["disk_memory_bytes"] = disk["peak_bytes"] - 1
refused = plan_qualification_capacity(tiny_inventory, {"cuda:0": 8 * GIB}, **options)
assert refused["status"] == "refused"
assert any(reason.startswith("save/disk shortfall") for reason in refused["refusals"])
assert report["phases"]["reload"]["disk"]["peak_bytes"] == GIB + offloaded
def test_72b_partition_places_logits_only_on_input_and_head_devices(large_inventory):
workload = Workload(batch_size=16, sequence_length=384, generation_tokens=200)
budgets = {"cuda:0": 85 * GIB, "cuda:1": 85 * GIB}
explicit = {"": "cuda:0", "model.norm": "cuda:1"}
explicit.update({block: "cuda:0" if index < 36 else "cuda:1"
for index, block in enumerate(large_inventory["blocks"])})
known = plan(large_inventory, budgets, workload=workload, device_map=explicit)
assert known["status"] == "feasible", known["refusals"]
automatic = plan(large_inventory, budgets, workload=workload)
assert automatic["status"] == "feasible", automatic["refusals"]
assert automatic["phases"]["verify"]["cuda:1"]["components"]["logits"] == 0
assert all(row["headroom_bytes"] >= 0 for rows in automatic["phases"].values() for row in rows.values())
@pytest.mark.parametrize("sampling", [True, False])
@pytest.mark.parametrize("beams,returns", [(1, 2), (2, 3)])
def test_deterministic_override_rejects_incompatible_returns_before_weight_loading(
tiny_inventory, monkeypatch, sampling, beams, returns,
):
inventory = copy.deepcopy(tiny_inventory)
inventory["generation_config"].update(do_sample=sampling, num_beams=beams, num_return_sequences=returns)
monkeypatch.setattr(AutoModelForCausalLM, "from_pretrained", Mock(side_effect=AssertionError("loaded weights")))
with pytest.raises(ValueError, match="num_return_sequences"):
plan(inventory)
def test_bin_preparation_refuses_missing_reference_export_space(tmp_path, tiny_inventory, monkeypatch):
from types import SimpleNamespace
from obliteratus import qualification_runtime as runtime
from obliteratus.models import host_resources
source = tmp_path / "bin-source"
source.mkdir()
(source / "config.json").write_text(json.dumps(tiny_inventory["config"]))
(source / "pytorch_model.bin").write_bytes(b"metadata-only fixture")
mapping = {"": "cuda:0", tiny_inventory["blocks"][-1]: "disk", "lm_head": "disk"}
weights = tiny_inventory["total_weight_bytes"]
offloaded = tiny_inventory["module_sizes"][tiny_inventory["blocks"][-1]] + tiny_inventory["module_sizes"]["lm_head"]
free_disk = GIB + 2 * weights + 3 * offloaded
monkeypatch.setattr("obliteratus.capacity_planning.meta_inventory", lambda *args: tiny_inventory)
monkeypatch.setattr(host_resources, "available_host_bytes", lambda: 64 * GIB)
monkeypatch.setattr(torch.cuda, "mem_get_info", lambda device: (8 * GIB, 8 * GIB))
monkeypatch.setattr(torch.cuda, "memory_reserved", lambda device: 0)
monkeypatch.setattr(runtime.shutil, "disk_usage", lambda path: SimpleNamespace(free=free_disk))
monkeypatch.setenv("OBLITERATUS_DEVICE_MEMORY_CEILINGS", "{}")
monkeypatch.setattr(AutoModelForCausalLM, "from_pretrained", Mock(side_effect=AssertionError("loaded weights")))
configuration = {"requested_devices": ["cuda:0"], "dtype": "bfloat16", "gpu_memory_utilization": None,
"generation_tokens": 4, "offload": "disk", "device_map": mapping, "reference": "cpu",
"workload": {"harmful_prompts": ["unsafe"] * 3, "harmless_prompts": ["safe"] * 3,
"comparison_prompts": ["compare"] * 2}}
with pytest.raises(CapacityPlanningError) as error:
runtime.capacity_plan(source, configuration, tmp_path)
assert any(reason.startswith("save/disk shortfall") for reason in error.value.plan["refusals"])