mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 18:44:41 +01:00
503 lines
26 KiB
Python
503 lines
26 KiB
Python
"""Capacity-only commands stop before data, direction or model acquisition."""
|
|
|
|
import json
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock
|
|
import sys
|
|
|
|
import pytest
|
|
|
|
from obliteratus import cli
|
|
from obliteratus.concepts import cli as concepts
|
|
from scripts import pain_axis_appendix_d as appendix
|
|
from scripts import pain_axis_refusal_recovery as recovery
|
|
from scripts import pain_axis_replication as replication
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
|
|
|
|
@pytest.fixture
|
|
def planning_only(monkeypatch):
|
|
def forbidden(*args, **kwargs):
|
|
raise AssertionError("plan-only must not acquire a model, direction or dataset")
|
|
|
|
monkeypatch.setattr(concepts, "_resolve", forbidden)
|
|
monkeypatch.setattr(concepts, "_steer", forbidden)
|
|
monkeypatch.setattr(replication, "load", forbidden)
|
|
monkeypatch.setattr(replication.torch, "manual_seed", forbidden)
|
|
monkeypatch.setattr(replication.AutoModelForCausalLM, "from_pretrained", forbidden)
|
|
monkeypatch.setattr(replication.AutoTokenizer, "from_pretrained", forbidden)
|
|
monkeypatch.setattr(replication.ConceptDirection, "load", forbidden)
|
|
monkeypatch.setattr(recovery, "_model_directory", forbidden)
|
|
monkeypatch.setitem(sys.modules, "obliteratus.concepts.baseline",
|
|
SimpleNamespace(run_baseline=forbidden))
|
|
return forbidden
|
|
|
|
|
|
@pytest.mark.parametrize("command", ["obliterate", "abliterate"])
|
|
def test_primary_plan_only_precedes_gpu_and_remote_dispatch(tmp_path, monkeypatch, planning_only, command):
|
|
from obliteratus import capacity_planning
|
|
|
|
planned = Mock(return_value={"status": "planned"})
|
|
monkeypatch.setattr(capacity_planning, "maybe_plan", planned)
|
|
monkeypatch.setattr(cli, "_apply_gpu_selection", planning_only)
|
|
monkeypatch.setattr(cli, "_cmd_abliterate", planning_only)
|
|
monkeypatch.setattr(cli, "_cmd_remote_abliterate", planning_only)
|
|
output = tmp_path / command
|
|
cli.main([command, "fixture/model", "--concept", "missing/pack", "--plan-only",
|
|
"--output-dir", str(output), "--max-seq-length", "512",
|
|
"--refusal-max-tokens", "192"])
|
|
args, model = planned.call_args.args
|
|
assert model == "fixture/model" and args.plan_only
|
|
assert args.plan_output == output.with_name(output.name + ".capacity-plan.json")
|
|
assert planned.call_args.kwargs["batch_size"] == 16
|
|
assert planned.call_args.kwargs["sequence_length"] == 512
|
|
assert planned.call_args.kwargs["generation_tokens"] == 200
|
|
|
|
|
|
@pytest.mark.parametrize("command", ["obliterate", "abliterate"])
|
|
@pytest.mark.parametrize("explicit_output", [False, True])
|
|
def test_primary_default_plan_survives_checkpoint_publication(
|
|
tmp_path, monkeypatch, planning_only, command, explicit_output,
|
|
):
|
|
from obliteratus import capacity_planning
|
|
from obliteratus.persistence_contracts import atomic_checkpoint_directory
|
|
|
|
monkeypatch.chdir(tmp_path)
|
|
output = tmp_path / "checkpoint" if explicit_output else Path("abliterated/fixture_model")
|
|
evidence = output.with_name(output.name + ".capacity-plan.json")
|
|
payload = '{"status": "feasible"}\n'
|
|
|
|
def write_plan(args, *unused, **kwargs):
|
|
args.plan_output.parent.mkdir(parents=True, exist_ok=True)
|
|
args.plan_output.write_text(payload)
|
|
|
|
monkeypatch.setattr(capacity_planning, "maybe_plan", write_plan)
|
|
options = ["--output-dir", str(output)] if explicit_output else []
|
|
cli.main([command, "fixture/model", "--plan-only", *options])
|
|
assert evidence.read_text() == payload
|
|
assert not output.exists()
|
|
|
|
output.mkdir()
|
|
(output / "previous.json").write_text("{}")
|
|
with atomic_checkpoint_directory(output) as staging:
|
|
(staging / "config.json").write_text("{}")
|
|
|
|
assert evidence.read_text() == payload
|
|
assert (output / "config.json").is_file()
|
|
assert not (output / "previous.json").exists()
|
|
|
|
|
|
@pytest.mark.parametrize("command", ["obliterate", "abliterate"])
|
|
def test_primary_plan_preserves_explicit_evidence_path(tmp_path, monkeypatch, planning_only, command):
|
|
from obliteratus import capacity_planning
|
|
|
|
planned = Mock(return_value={"status": "planned"})
|
|
monkeypatch.setattr(capacity_planning, "maybe_plan", planned)
|
|
evidence = tmp_path / "selected-plan.json"
|
|
cli.main([command, "fixture/model", "--plan-only", "--plan-output", str(evidence)])
|
|
assert planned.call_args.args[0].plan_output == evidence
|
|
|
|
|
|
@pytest.mark.parametrize("command", ["obliterate", "abliterate"])
|
|
@pytest.mark.parametrize("device", ["auto", "cuda", "cuda:0", "cuda:1", "cpu"])
|
|
def test_primary_plan_respects_execution_device(tmp_path, monkeypatch, planning_only, command, device):
|
|
monkeypatch.setattr(cli, "_apply_gpu_selection", planning_only)
|
|
monkeypatch.setattr(cli, "_cmd_abliterate", planning_only)
|
|
source = Path(__file__).parent / "fixtures" / "capacity_configs" / "qwen2.5-72b-instruct.json"
|
|
destination = tmp_path / "plan.json"
|
|
arguments = [
|
|
command, "offline", "--method", "basic", "--dtype", "bfloat16", "--device", device, "--plan-only",
|
|
"--plan-config", str(source), "--plan-output", str(destination),
|
|
"--plan-free-memory", "96", "96", "--plan-host-memory", "64", "--plan-disk-memory", "1024",
|
|
"--plan-batch-size", "1", "--plan-sequence-length", "128", "--plan-generation-tokens", "16",
|
|
]
|
|
if device == "auto":
|
|
cli.main(arguments)
|
|
else:
|
|
with pytest.raises(SystemExit) as error:
|
|
cli.main(arguments)
|
|
assert error.value.code == 2
|
|
report = json.loads(destination.read_text())
|
|
expected = "cuda:0" if device == "cuda" else device
|
|
assert report["execution_device"] == expected
|
|
if device == "auto":
|
|
assert report["status"] == "feasible", report["refusals"]
|
|
assert set(report["bytes_by_device"]) == {"cuda:0", "cuda:1"}
|
|
else:
|
|
assert report["status"] == "refused"
|
|
assert set(report["bytes_by_device"]) == {expected}
|
|
assert any(reason.startswith(f"load/{expected} shortfall") for reason in report["refusals"])
|
|
|
|
|
|
@pytest.mark.parametrize("command", ["obliterate", "abliterate"])
|
|
@pytest.mark.parametrize("limit", [None, 64, 1024])
|
|
def test_primary_plan_bounds_probe_and_refusal_workloads(monkeypatch, planning_only, command, limit):
|
|
from obliteratus import capacity_planning
|
|
|
|
planned = Mock(return_value={"status": "planned"})
|
|
monkeypatch.setattr(capacity_planning, "maybe_plan", planned)
|
|
options = [] if limit is None else ["--max-seq-length", str(limit)]
|
|
cli.main([command, "fixture/model", "--method", "basic", "--plan-only", *options])
|
|
assert planned.call_args.kwargs["batch_size"] == 16
|
|
assert planned.call_args.kwargs["sequence_length"] == (512 if limit is None else limit)
|
|
assert planned.call_args.kwargs["generation_tokens"] == 200
|
|
|
|
|
|
@pytest.mark.parametrize("command", ["baseline", "steer"])
|
|
@pytest.mark.parametrize("plan_flag", ["--plan-only", "--dry-run"])
|
|
def test_concept_plan_only_precedes_gpu_and_pack_direction_io(
|
|
tmp_path, monkeypatch, planning_only, command, plan_flag,
|
|
):
|
|
planned = Mock(return_value={"status": "planned"})
|
|
monkeypatch.setattr(concepts, "maybe_plan", planned)
|
|
monkeypatch.setattr(cli, "_apply_gpu_selection", planning_only)
|
|
output = tmp_path / command
|
|
options = (["missing/pack", "fixture/model", "--max-seq-length", "512"]
|
|
if command == "baseline" else
|
|
["fixture/model", "missing.safetensors", "--prompt", "fixture",
|
|
"--max-new-tokens", "240"])
|
|
cli.main(["concepts", command, *options, plan_flag, "--output-dir", str(output),
|
|
"--plan-config", "qwen2.5-72b", "--plan-free-memory", "80", "80"])
|
|
args, model = planned.call_args.args
|
|
assert model == "fixture/model" and args.plan_only
|
|
assert args.plan_output == output / "capacity-plan.json"
|
|
assert args.plan_free_memory == [80, 80]
|
|
assert planned.call_args.kwargs["batch_size"] == (16 if command == "baseline" else 1)
|
|
assert planned.call_args.kwargs["sequence_length"] == (512 if command == "baseline" else None)
|
|
assert planned.call_args.kwargs["generation_tokens"] == (240 if command == "steer" else None)
|
|
|
|
|
|
def test_concept_baseline_plan_bounds_default_probe(monkeypatch, planning_only):
|
|
planned = Mock(return_value={"status": "planned"})
|
|
monkeypatch.setattr(concepts, "maybe_plan", planned)
|
|
cli.main(["concepts", "baseline", "missing/pack", "fixture/model", "--plan-only",
|
|
"--output-dir", "fixture-output"])
|
|
assert planned.call_args.kwargs["batch_size"] == 16
|
|
assert planned.call_args.kwargs["sequence_length"] == 256
|
|
|
|
|
|
@pytest.mark.parametrize("command", ["baseline", "steer"])
|
|
@pytest.mark.parametrize("device", [None, "cuda:1"])
|
|
def test_concept_plan_uses_cpu_default_or_selected_gpu(tmp_path, planning_only, command, device):
|
|
source = tmp_path / "config.json"
|
|
source.write_text(json.dumps({
|
|
"model_type": "qwen2", "vocab_size": 64, "hidden_size": 32, "intermediate_size": 64,
|
|
"num_hidden_layers": 2, "num_attention_heads": 4, "num_key_value_heads": 2,
|
|
"max_position_embeddings": 256, "tie_word_embeddings": False,
|
|
}))
|
|
destination = tmp_path / "plan.json"
|
|
options = (["missing/pack", "fixture/model", "--extraction-layer", "0"] if command == "baseline"
|
|
else ["fixture/model", "missing.safetensors", "--prompt", "fixture"])
|
|
selection = [] if device is None else ["--device", device, "--plan-free-memory", "0", "8"]
|
|
cli.main([
|
|
"concepts", command, *options, "--plan-only", "--output-dir", str(tmp_path / "out"),
|
|
"--plan-config", str(source), "--plan-output", str(destination), "--plan-samples", "32",
|
|
"--plan-prompt-characters", "2048",
|
|
"--plan-host-memory", "32", "--plan-disk-memory", "64", *selection,
|
|
])
|
|
report = json.loads(destination.read_text())
|
|
assert report["status"] == "feasible", report["refusals"]
|
|
assert report["execution_device"] == (device or "cpu")
|
|
assert set(report["bytes_by_device"]) == {device or "cpu"}
|
|
for rows in report["phases"].values():
|
|
assert "cuda:0" not in rows
|
|
assert rows[device or "cpu"]["budget_bytes"] == (8 if device else 32) * 1024**3
|
|
|
|
|
|
@pytest.mark.parametrize("neighbour_samples", [None, 0, 1000])
|
|
def test_concept_baseline_plan_retains_report_readouts_without_writer_rollback(tmp_path, planning_only, neighbour_samples):
|
|
source = tmp_path / "config.json"
|
|
source.write_text(json.dumps({
|
|
"model_type": "qwen2", "vocab_size": 64, "hidden_size": 32, "intermediate_size": 64,
|
|
"num_hidden_layers": 2, "num_attention_heads": 4, "num_key_value_heads": 2,
|
|
"max_position_embeddings": 256, "tie_word_embeddings": False,
|
|
}))
|
|
destination = tmp_path / "plan.json"
|
|
options = [] if neighbour_samples is None else ["--plan-neighbour-samples", str(neighbour_samples)]
|
|
cli.main([
|
|
"concepts", "baseline", "missing/pack", "fixture/model", "--plan-only",
|
|
"--output-dir", str(tmp_path / "out"), "--extraction-layer", "0",
|
|
"--plan-config", str(source), "--plan-output", str(destination),
|
|
"--plan-samples", "32",
|
|
"--plan-prompt-characters", "2048",
|
|
"--plan-free-memory", "8", "8", "--plan-host-memory", "32", "--plan-disk-memory", "64",
|
|
*options,
|
|
])
|
|
report = json.loads(destination.read_text())
|
|
assert report["status"] == "feasible", report["refusals"]
|
|
assert report["edit_contract"] == "basic_sequential_projection"
|
|
assert report["phases"]["edit"]["cpu"]["components"]["concept_transaction_rollback"] == 0
|
|
neighbours = 96 if neighbour_samples is None else neighbour_samples
|
|
assert report["workload"]["auxiliary_readouts"] == 3
|
|
assert report["workload"]["neighbour_samples"] == neighbours
|
|
expected = (2 * 32 * 4 + neighbours) * 2 * 32 * 4
|
|
for phase in ("activations", "direction", "edit", "verify", "save"):
|
|
assert report["phases"][phase]["cpu"]["components"]["retained_activations"] == expected
|
|
assert report["workload"]["concept_extraction"] is True
|
|
assert report["workload"]["validation_gate"] is True
|
|
assert report["phases"]["direction"]["cpu"]["components"]["activation_materialization"] == 3 * expected
|
|
|
|
|
|
@pytest.mark.parametrize("command", ["baseline", "obliterate"])
|
|
@pytest.mark.parametrize("bounded", [False, True])
|
|
def test_custom_gated_plan_requires_text_bound_without_acquiring_pack(
|
|
tmp_path, monkeypatch, planning_only, command, bounded,
|
|
):
|
|
monkeypatch.setattr(cli, "_apply_gpu_selection", planning_only)
|
|
source = tmp_path / "config.json"
|
|
source.write_text(json.dumps({
|
|
"model_type": "qwen2", "vocab_size": 64, "hidden_size": 32, "intermediate_size": 64,
|
|
"num_hidden_layers": 2, "num_attention_heads": 4, "num_key_value_heads": 2,
|
|
"max_position_embeddings": 256, "tie_word_embeddings": False,
|
|
}))
|
|
destination = tmp_path / "plan.json"
|
|
options = (["concepts", "baseline", "missing/pack", "fixture/model"] if command == "baseline"
|
|
else [command, "fixture/model", "--concept", "missing/pack", "--method", "basic", "--skip-repair"])
|
|
arguments = [
|
|
*options, "--plan-only", "--output-dir", str(tmp_path / "out"),
|
|
"--plan-config", str(source), "--plan-output", str(destination), "--plan-samples", "32",
|
|
"--plan-free-memory", "8", "8", "--plan-host-memory", "32", "--plan-disk-memory", "64",
|
|
]
|
|
if bounded:
|
|
cli.main([*arguments, "--plan-prompt-characters", "2048"])
|
|
else:
|
|
with pytest.raises(SystemExit) as error:
|
|
cli.main(arguments)
|
|
assert error.value.code == (4 if command == "baseline" else 2)
|
|
report = json.loads(destination.read_text())
|
|
assert report["status"] == ("feasible" if bounded else "refused"), report["refusals"]
|
|
assert report["workload"]["prompt_characters"] == (2048 if bounded else None)
|
|
if not bounded:
|
|
assert len(report["refusals"]) == 1 and "--plan-prompt-characters" in report["refusals"][0]
|
|
assert not (tmp_path / "out").exists()
|
|
|
|
|
|
@pytest.mark.parametrize("command,gate_mode", [
|
|
("baseline", None), ("obliterate", None), ("obliterate", "off"),
|
|
("obliterate", "report"), ("obliterate", "enforce"), ("abliterate", "report"),
|
|
])
|
|
@pytest.mark.parametrize("concept", ["refusal", "core/refusal"])
|
|
@pytest.mark.parametrize("dtype", ["float32", "bfloat16"])
|
|
def test_named_refusal_plans_retain_classic_readout_storage(
|
|
tmp_path, planning_only, command, gate_mode, concept, dtype,
|
|
):
|
|
source = tmp_path / "config.json"
|
|
source.write_text(json.dumps({
|
|
"model_type": "qwen2", "vocab_size": 64, "hidden_size": 32, "intermediate_size": 64,
|
|
"num_hidden_layers": 2, "num_attention_heads": 4, "num_key_value_heads": 2,
|
|
"max_position_embeddings": 512, "tie_word_embeddings": False,
|
|
}))
|
|
destination = tmp_path / "plan.json"
|
|
options = (["concepts", "baseline", concept, "fixture/model"] if command == "baseline"
|
|
else [command, "fixture/model", "--concept", concept, "--method", "basic"])
|
|
arguments = [
|
|
*options, "--plan-only", "--device", "cpu", "--dtype", dtype,
|
|
"--output-dir", str(tmp_path / "out"), "--max-seq-length", "256",
|
|
"--plan-config", str(source), "--plan-output", str(destination),
|
|
"--plan-samples", "131072", "--plan-host-memory", "16.5", "--plan-disk-memory", "64",
|
|
]
|
|
# Include lexical Python metadata while preserving the FP32 sequence-backing
|
|
# shortfall and BF16 admission that this storage regression distinguishes.
|
|
if gate_mode is not None:
|
|
arguments += ["--gate-mode", gate_mode]
|
|
if dtype == "float32":
|
|
with pytest.raises(SystemExit) as error:
|
|
cli.main(arguments)
|
|
assert error.value.code == (4 if command == "baseline" else 2)
|
|
else:
|
|
cli.main(arguments)
|
|
report = json.loads(destination.read_text())
|
|
assert report["status"] == ("refused" if dtype == "float32" else "feasible"), report["refusals"]
|
|
assert report["workload"]["concept_extraction"] is False
|
|
assert report["workload"]["auxiliary_readouts"] == report["workload"]["neighbour_samples"] == 0
|
|
gated = command == "baseline" or gate_mode in {"report", "enforce"}
|
|
assert report["workload"]["validation_gate"] is gated
|
|
assert report["edit_contract"] == "basic_sequential_projection"
|
|
rows = 2 * 131072 * 2 * 32 * 4
|
|
expected = rows * (256 if dtype == "float32" else 1)
|
|
for phase in ("activations", "direction", "edit", "verify", "save"):
|
|
assert report["phases"][phase]["cpu"]["components"]["retained_activations"] == expected
|
|
assert report["phases"]["direction"]["cpu"]["components"]["activation_materialization"] == (3 * rows if gated else 0)
|
|
lexical = report["phases"]["direction"]["cpu"]["components"].get("lexical_probe", 0)
|
|
assert (lexical > 2 * 131072 * 1024 * 8) if gated else lexical == 0
|
|
assert report["phases"]["load"]["cpu"]["headroom_bytes"] > 0
|
|
if dtype == "float32":
|
|
assert any(reason.startswith("activations/cpu shortfall") for reason in report["refusals"])
|
|
|
|
|
|
@pytest.mark.parametrize("stage,tokens", [("vectors", 60), ("ablate", 60), ("resteer", 120),
|
|
("battery", 60)])
|
|
def test_replication_dry_run_precedes_stage_and_seed(tmp_path, monkeypatch, planning_only, stage, tokens):
|
|
planned = Mock(return_value={"status": "planned"})
|
|
monkeypatch.setattr(replication, "maybe_plan", planned)
|
|
monkeypatch.setattr(replication, "stage_" + stage, planning_only)
|
|
output = tmp_path / stage
|
|
replication.main([stage, "--model", "fixture/model", "--name", "Qwen_2.5_7B_instruct",
|
|
"--pin", "missing", "--out", str(output), "--dry-run"])
|
|
args, model = planned.call_args.args
|
|
assert model == "fixture/model" and args.plan_only
|
|
assert args.plan_output == output / "capacity-plan.json"
|
|
assert planned.call_args.kwargs == {"dtype": "bfloat16", "placement": {"device_map": "cuda"},
|
|
"batch_size": 8 if stage == "ablate" else 1,
|
|
"generation_tokens": tokens,
|
|
"activation_datasets": 7 if stage == "vectors" else 2,
|
|
"collection_copies": 2 if stage == "vectors" else 0,
|
|
"concept_edit": stage in {"ablate", "resteer", "battery"}}
|
|
|
|
|
|
@pytest.mark.parametrize("host_gib,refused", [(32, True), (128, False)])
|
|
def test_vector_plan_retains_seven_datasets_and_collection_copies(tmp_path, planning_only, host_gib, refused):
|
|
source = Path(__file__).parent / "fixtures" / "capacity_configs" / "qwen2.5-32b-instruct.json"
|
|
destination = tmp_path / "plan.json"
|
|
arguments = [
|
|
"vectors", "--model", "offline", "--name", "Qwen_2.5_7B_instruct", "--pin", "missing",
|
|
"--out", str(tmp_path / "out"), "--plan-only", "--plan-config", str(source),
|
|
"--plan-output", str(destination), "--plan-free-memory", "96", "96",
|
|
"--plan-host-memory", str(host_gib), "--plan-disk-memory", "256",
|
|
"--plan-samples", "8192", "--plan-sequence-length", "32",
|
|
]
|
|
if refused:
|
|
with pytest.raises(SystemExit) as error:
|
|
replication.main(arguments)
|
|
assert error.value.code == 2
|
|
else:
|
|
replication.main(arguments)
|
|
report = json.loads(destination.read_text())
|
|
assert report["status"] == ("refused" if refused else "feasible"), report["refusals"]
|
|
assert report["workload"]["activation_datasets"] == 7
|
|
assert report["workload"]["samples"] == 8192
|
|
for phase in ("activations", "direction", "edit", "verify", "save"):
|
|
assert report["phases"][phase]["cpu"]["components"]["retained_activations"] == 70 * 1024**3
|
|
assert report["phases"]["activations"]["cpu"]["components"]["collection_materialization"] == 20 * 1024**3
|
|
if refused:
|
|
assert any(reason.startswith("activations/cpu shortfall") for reason in report["refusals"])
|
|
|
|
|
|
def test_appendix_plan_uses_configured_batch_and_longest_completion(tmp_path, monkeypatch, planning_only):
|
|
planned = Mock(return_value={"status": "planned"})
|
|
monkeypatch.setattr(appendix, "maybe_plan", planned)
|
|
evidence = tmp_path / "selected-plan.json"
|
|
appendix.main(["--model", "fixture/model", "--name", "Qwen_2.5_7B_instruct", "--pin", "missing",
|
|
"--out", str(tmp_path / "out"), "--direction", "missing.safetensors",
|
|
"--plan-only", "--plan-output", str(evidence), "--batch-size", "4",
|
|
"--max-new-tokens", "80", "--refusal-eval-max-new-tokens", "128"])
|
|
args, model = planned.call_args.args
|
|
assert model == "fixture/model" and Path(args.plan_output) == evidence
|
|
assert planned.call_args.kwargs == {"dtype": "bfloat16", "placement": {"device_map": "cuda"}, "batch_size": 4,
|
|
"generation_tokens": 128, "concept_edit": True}
|
|
|
|
|
|
@pytest.mark.parametrize("device_map,dtype", [("cuda", "bfloat16"), ("cpu", "float32"),
|
|
("auto", "bfloat16")])
|
|
def test_recovery_plan_does_not_require_peft_and_declares_training_limit(
|
|
tmp_path, monkeypatch, planning_only, device_map, dtype,
|
|
):
|
|
planned = Mock(return_value={"status": "planned"})
|
|
monkeypatch.setattr(recovery, "maybe_plan", planned)
|
|
monkeypatch.setitem(sys.modules, "peft", None)
|
|
recovery.main(["--model", "fixture/model", "--pin", "missing", "--direction", "missing",
|
|
"--prompts", "missing", "--license", "missing", "--out", str(tmp_path / "out"),
|
|
"--projection-threshold", "0.1", "--plan-only", "--max-length", "512",
|
|
"--distill-tokens", "160", "--device-map", device_map])
|
|
args, model = planned.call_args.args
|
|
assert model == "fixture/model" and args.plan_only
|
|
assert args.plan_output == tmp_path / "out.capacity-plan.json"
|
|
assert planned.call_args.kwargs["placement"] == {"device_map": device_map}
|
|
assert planned.call_args.kwargs["dtype"] == dtype
|
|
assert planned.call_args.kwargs["sequence_length"] == 512
|
|
assert planned.call_args.kwargs["generation_tokens"] == 160
|
|
assert planned.call_args.kwargs["concept_edit"] is True
|
|
assert "training memory is not modelled" in planned.call_args.kwargs["unsupported_reason"]
|
|
|
|
|
|
@pytest.mark.parametrize("command", ["replication", "appendix", "recovery"])
|
|
@pytest.mark.parametrize("device_map,max_memory,expected_budgets,refused", [
|
|
("cuda", None, {"cuda:0": 8}, False),
|
|
("cpu", {"cpu": "3GiB"}, {"cpu": 3}, False),
|
|
("auto", {"1": "6GiB", "2": 7 * 1024**3, "cpu": "3GiB"},
|
|
{"cuda:1": 6, "cuda:2": 7}, False),
|
|
({"": "cuda:2", "model.layers.0": "cpu", "model.layers.1": "disk"},
|
|
{"2": "6GiB", "cpu": "3GiB"}, {"cuda:2": 6}, False),
|
|
({"": "cuda:1", "model.layers.0": 0}, {"0": "4GiB", "1": "6GiB"},
|
|
{"cuda:0": 4, "cuda:1": 6}, False),
|
|
("cuda:1", {"1": "1GiB"}, {"cuda:1": 1}, True),
|
|
("balanced_low_0", {"0": "6GiB", "1": "6GiB"}, {"cuda:0": 6, "cuda:1": 6}, True),
|
|
])
|
|
def test_pain_axis_plan_respects_loader_placement_and_records_limits(
|
|
tmp_path, planning_only, capsys, command, device_map, max_memory, expected_budgets, refused,
|
|
):
|
|
source = tmp_path / "config.json"
|
|
source.write_text(json.dumps({
|
|
"model_type": "qwen2", "vocab_size": 64, "hidden_size": 32, "intermediate_size": 64,
|
|
"num_hidden_layers": 2, "num_attention_heads": 4, "num_key_value_heads": 2,
|
|
"max_position_embeddings": 256, "tie_word_embeddings": False,
|
|
}))
|
|
destination = tmp_path / "plan.json"
|
|
backing = tmp_path / "offload"
|
|
options = ["--model", "offline", "--pin", "missing", "--out", str(tmp_path / "out")]
|
|
if command == "replication":
|
|
main = replication.main
|
|
options = ["vectors", *options, "--name", "Qwen_2.5_7B_instruct"]
|
|
elif command == "appendix":
|
|
main = appendix.main
|
|
options += ["--name", "Qwen_2.5_7B_instruct", "--direction", "missing"]
|
|
else:
|
|
main = recovery.main
|
|
options += ["--direction", "missing", "--prompts", "missing", "--license", "missing",
|
|
"--projection-threshold", "0.1"]
|
|
options += [
|
|
"--dry-run", "--plan-config", str(source), "--plan-output", str(destination),
|
|
"--plan-host-memory", "8", "--plan-disk-memory", "64", "--plan-source-shard-gib", "0.001",
|
|
"--plan-samples", "8", "--plan-sequence-length", "32", "--plan-generation-tokens", "16",
|
|
"--device-map", json.dumps(device_map) if isinstance(device_map, dict) else device_map,
|
|
"--offload-folder", str(backing),
|
|
]
|
|
if device_map != "cpu":
|
|
options += ["--plan-free-memory", "8", "12", "16"]
|
|
if max_memory is not None:
|
|
options += ["--max-memory", json.dumps(max_memory)]
|
|
refused = refused or command == "recovery"
|
|
if refused:
|
|
with pytest.raises(SystemExit) as error:
|
|
main(options)
|
|
assert error.value.code == 2
|
|
else:
|
|
main(options)
|
|
report = json.loads(destination.read_text())
|
|
assert report["status"] == ("refused" if refused else "feasible"), report["refusals"]
|
|
assert report["metadata_source"]["weights_loaded"] is False
|
|
requested = {"device_map": device_map, "offload_folder": str(backing)}
|
|
if max_memory is not None:
|
|
requested["max_memory"] = max_memory
|
|
assert report["placement"]["requested"] == requested
|
|
if isinstance(device_map, dict):
|
|
normalized = {name: f"cuda:{value}" if isinstance(value, int) else value
|
|
for name, value in device_map.items()}
|
|
assert report["device_map"] == normalized
|
|
gpus = {device for device in report["bytes_by_device"] if device.startswith("cuda:")}
|
|
assert gpus == {device for device in expected_budgets if device.startswith("cuda:")}
|
|
for phase in report["phases"].values():
|
|
for device, budget in expected_budgets.items():
|
|
assert phase[device]["budget_bytes"] == budget * 1024**3
|
|
assert phase["cpu"]["budget_bytes"] == (3 if max_memory and "cpu" in max_memory else 8) * 1024**3
|
|
if command == "recovery":
|
|
assert any("training memory is not modelled" in reason for reason in report["refusals"])
|
|
if device_map == "balanced_low_0":
|
|
assert any("does not model this strategy" in reason for reason in report["refusals"])
|
|
if expected_budgets == {"cuda:1": 1}:
|
|
assert any("shortfall" in reason for reason in report["refusals"])
|
|
assert "phase device peak GiB" in capsys.readouterr().out
|
|
assert not backing.exists() and not (tmp_path / "out").exists()
|
|
|
|
|
|
def test_plan_arguments_do_not_change_normal_command_defaults():
|
|
args = appendix.parser().parse_args([
|
|
"--model", "fixture/model", "--name", "Qwen_2.5_7B_instruct", "--pin", "pin",
|
|
"--out", "out", "--direction", "direction",
|
|
])
|
|
assert not args.plan_only and args.plan_output is None
|
|
assert args.batch_size == 8 and args.max_new_tokens == 60
|
|
assert args.override_gate == appendix.REASON
|