Files
OBLITERATUS/tests/test_phase_optional_admission.py

300 lines
14 KiB
Python

"""Surgery options follow their real allocation lifetimes or reject explicitly."""
from unittest.mock import Mock
import pytest
import torch
from transformers import LlamaConfig, LlamaForCausalLM
from obliteratus import phase_admission as admission
from obliteratus.abliterate import AbliterationPipeline
from obliteratus.models.loader import ModelHandle
from tests.test_phase_collections_admission import LengthTokenizer
from tests.test_phase_memory_helpers import cpu_memory_profile, cpu_peak_bytes
from tests.test_phase_memory_helpers import cpu_threads as cpu_threads
pytestmark = pytest.mark.cpu
MIB = 1024**2
def make_pipeline(*, hidden=64, intermediate=96, layers=4, dtype=torch.float32, **options):
config = LlamaConfig(
hidden_size=hidden, intermediate_size=intermediate, num_hidden_layers=layers,
num_attention_heads=8, num_key_value_heads=8, vocab_size=32,
use_cache=False, attn_implementation="sdpa",
)
model = LlamaForCausalLM(config).to(dtype=dtype).eval()
pipeline = AbliterationPipeline(
"local", method="basic", harmful_prompts=["8"] * 8, harmless_prompts=["8"] * 8,
use_kl_optimization=False, **options,
)
pipeline.handle = ModelHandle(model, LengthTokenizer(), config, "local", "causal_lm")
pipeline._strong_layers = list(range(layers))
pipeline.concept_directions = {i: torch.nn.functional.normalize(torch.randn(hidden), dim=0)
for i in range(layers)}
pipeline.concept_subspaces = {i: d.unsqueeze(0) for i, d in pipeline.concept_directions.items()}
pipeline._free_gpu_memory = lambda: None
pipeline.log = Mock()
return pipeline
def test_bayesian_rejects_before_reference_or_snapshot_work_and_reproduces_real_peak(
monkeypatch, caplog, cpu_threads, tmp_path,
):
import optuna
from obliteratus.bayesian_optimizer import (
bayesian_admission_options,
run_bayesian_optimization,
)
pipeline = make_pipeline(hidden=64, intermediate=256, layers=8)
pipeline.output_dir = tmp_path / "candidate"
usable_budget = MIB // 2
free_bytes = usable_budget + admission.DEFAULT_RESERVE_BYTES
monkeypatch.setattr(admission, "observe_memory",
lambda d: admission.MemoryObservation(d, free_bytes, 512 * MIB))
pipeline._bayesian_trials = 0
pipeline._admit_pipeline_phase("excise") # ordinary edit fits the same headroom
assert "Proceeding in default mode" not in caplog.text
pipeline._bayesian_trials = 50
pipeline._admit_pipeline_phase("excise")
assert "estimated shortfall" in caplog.text and "Proceeding in default mode" in caplog.text
demand = bayesian_admission_options(pipeline, n_refusal_prompts=8, n_kl_prompts=1)
execution = pipeline._bayesian_execution
snapshot_bytes = sum(
parameter.numel() * parameter.element_size()
for _name, parameter in execution._selected
)
assert demand["snapshot_host_bytes"] == snapshot_bytes
assert execution.identity()["targets"][-1] == "global.lm_head.weight"
capture = Mock(wraps=execution.capture)
monkeypatch.setattr(execution, "capture", capture)
forward = Mock(wraps=pipeline.handle.model.forward)
monkeypatch.setattr(pipeline.handle.model, "forward", forward)
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "strict")
work = Mock()
monkeypatch.setattr(pipeline, "_excise", work)
with pytest.raises(admission.PhaseAdmissionError,
match="Phase 'excise'.*shortfall"):
pipeline._admit_pipeline_phase("excise")
pipeline._excise()
work.assert_not_called()
capture.assert_not_called()
forward.assert_not_called()
class SnapshotsCaptured(RuntimeError):
pass
monkeypatch.setattr(optuna, "create_study", Mock(side_effect=SnapshotsCaptured))
# Run the real reference forwards and snapshot construction, stopping before
# trials. No monkeypatch substitutes the model or the saved tensor payloads.
with cpu_memory_profile() as profile:
with pytest.raises(SnapshotsCaptured):
run_bayesian_optimization(pipeline, n_trials=50, n_kl_prompts=1)
peak = cpu_peak_bytes(profile)
# The executor inventories every reversible attention/FFN target plus the
# global head. One real reference forward overlaps those retained snapshots.
host_bound = demand["snapshot_host_bytes"] + demand["forward_host_bytes"]
assert snapshot_bytes <= peak < host_bound + 4096
assert peak > usable_budget
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "off")
pipeline._admit_pipeline_phase("excise")
@pytest.mark.parametrize("override,method,expected", [(0, "basic", 0), (50, "basic", 50),
(0, "heretic", 50)])
def test_bayesian_trial_selection_is_shared_with_execution(monkeypatch, override, method, expected):
pipeline = make_pipeline(layers=1)
pipeline.method = method
pipeline._bayesian_trials = override
assert pipeline._effective_bayesian_trials() == expected
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "strict")
selector = Mock(side_effect=LookupError("shared trial selector"))
monkeypatch.setattr(pipeline, "_effective_bayesian_trials", selector)
with pytest.raises(admission.PhaseAdmissionError, match="shared trial selector"):
pipeline._admit_pipeline_phase("excise")
with pytest.raises(LookupError, match="shared trial selector"):
pipeline._excise()
assert selector.call_count == 2
def test_spectral_cascade_has_its_own_sequential_peak(cpu_threads):
pipeline = make_pipeline(spectral_cascade=True)
_, sequential, _ = pipeline._optional_phase_workspaces("excise", [])
with cpu_memory_profile() as profile:
pipeline._apply_spectral_cascade_weights()
peak = cpu_peak_bytes(profile)
assert 0 < peak <= sequential["cpu"] <= 4 * peak
@pytest.mark.parametrize("dtype", [torch.float32, torch.bfloat16, torch.float64])
def test_expert_transplant_counts_mean_and_blend_as_sequential_workspace(dtype, cpu_threads):
pipeline = make_pipeline(layers=1, expert_transplant=True)
layer = pipeline.handle.model.model.layers[0]
layer.mlp.experts = torch.nn.ModuleList([torch.nn.Module() for _ in range(3)])
for expert in layer.mlp.experts:
expert.down_proj = torch.nn.Linear(96, 64, bias=False, dtype=dtype)
pipeline._expert_safety_scores = {0: [(0, 3.0), (1, 2.0), (2, 1.0)]}
_, sequential, _ = pipeline._optional_phase_workspaces("excise", [])
with torch.no_grad(), cpu_memory_profile() as profile:
assert pipeline._transplant_expert_weights([layer]) == 1
peak = cpu_peak_bytes(profile)
assert 0 < peak <= sequential["cpu"] <= 3 * peak
def test_activation_steering_accounts_for_installation_and_forward_hook_peak(cpu_threads):
pipeline = make_pipeline(layers=1, activation_steering=True)
workloads = [admission.ForwardWorkload(2, 128)]
host, _, forward = pipeline._optional_phase_workspaces("excise", workloads)
with cpu_memory_profile() as profile:
pipeline._install_activation_steering(pipeline.handle.model.model.layers)
assert 0 < cpu_peak_bytes(profile) <= host
# Isolate hook allocations from the already-budgeted decoder forward.
module = pipeline.handle.model.model.layers[0]
hidden = torch.ones(2, 128, 64)
hook = next(iter(module._forward_hooks.values()))
with cpu_memory_profile() as profile:
output = hook(module, (), hidden)
assert torch.isfinite(output).all()
peak = cpu_peak_bytes(profile)
assert 0 < peak <= forward["cpu"] <= 2 * peak
@pytest.mark.parametrize("option", ["attention_head_surgery", "safety_neuron_masking",
"project_embeddings", "invert_refusal"])
def test_ordinary_edit_bound_covers_enabled_optional_surgery_peak(option, monkeypatch, cpu_threads):
pipeline = make_pipeline(layers=1, **{option: True})
if option == "safety_neuron_masking":
pipeline.projection_target = "all"
mask = Mock(wraps=pipeline._mask_safety_neurons)
monkeypatch.setattr(pipeline, "_mask_safety_neurons", mask)
if option == "attention_head_surgery":
pipeline._refusal_heads = {0: [(i, 1.0) for i in range(8)]}
report = admission.model_phase_demand(
pipeline.handle, "excise", edit_parameter_names=pipeline._excise_parameter_names(),
forward_workloads=[],
)
with cpu_memory_profile() as profile:
pipeline._excise()
peak = cpu_peak_bytes(profile)
assert 0 < peak <= report.devices["cpu"].additional_bytes
if option == "safety_neuron_masking":
mask.assert_called()
@pytest.mark.parametrize("option", ["rdo_refinement", "use_wasserstein_optimal",
"float_layer_interpolation", "attention_head_surgery"])
def test_distill_optional_algorithms_fit_actual_peak(option, cpu_threads, monkeypatch):
optimizer_steps = 0
if option == "rdo_refinement":
# Adam allocates its state on the first step; later steps reuse it while
# new gradients/projections overlap previous loop locals. Three real
# updates cover both lifetimes without profiling 500 identical steps.
monkeypatch.setattr("obliteratus.abliterate._RDO_REFINEMENT_STEPS", 3)
adam_step = torch.optim.Adam.step
def count_step(optimizer, *args, **kwargs):
nonlocal optimizer_steps
result = adam_step(optimizer, *args, **kwargs)
optimizer_steps += 1
return result
monkeypatch.setattr(torch.optim.Adam, "step", count_step)
pipeline = make_pipeline(hidden=32, intermediate=48, layers=1, **{option: True})
# Four rows per class are the minimum that enters the real RDO optimizer.
pipeline._harmful_acts = {0: [torch.randn(1, 32) + 1 for _ in range(4)]}
pipeline._harmless_acts = {0: [torch.randn(1, 32) for _ in range(4)]}
pipeline._harmful_means = {0: torch.cat(pipeline._harmful_acts[0]).mean(0, keepdim=True)}
pipeline._harmless_means = {0: torch.cat(pipeline._harmless_acts[0]).mean(0, keepdim=True)}
options = pipeline._distillation_workload()
report = admission.model_phase_demand(
pipeline.handle, "distill", retained_rows=8, distill_workload=options,
)
with cpu_memory_profile() as profile:
pipeline._distill()
peak = cpu_peak_bytes(profile)
assert 0 < peak <= report.devices["cpu"].additional_bytes
if option == "rdo_refinement":
assert optimizer_steps == 3
assert any("RDO: refined" in str(call) for call in pipeline.log.call_args_list)
elif option == "use_wasserstein_optimal":
assert any("W2 cost=" in str(call) for call in pipeline.log.call_args_list)
elif option == "attention_head_surgery":
assert pipeline._refusal_heads
else:
assert pipeline._float_layer_weights
def test_successful_lora_skips_later_optional_surgery_budgets():
pipeline = make_pipeline(use_lora_ablation=True, expert_transplant=True,
spectral_cascade=True, activation_steering=True)
assert pipeline._lora_adapter_ranks()
assert pipeline._optional_phase_workspaces("excise", []) == (0, {}, {})
def test_unsupported_steering_storage_rejects_explicitly_before_work(monkeypatch, caplog):
pipeline = make_pipeline(layers=1, activation_steering=True)
pipeline.concept_directions[0] = torch.empty(64, device="meta")
pipeline._admit_pipeline_phase("excise")
assert "activation steering" in caplog.text and "Proceeding in default mode" in caplog.text
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "strict")
work = Mock()
monkeypatch.setattr(pipeline, "_excise", work)
with pytest.raises(admission.PhaseAdmissionError,
match="excise.*activation steering.*OBLITERATUS_PHASE_ADMISSION=off"):
pipeline._admit_pipeline_phase("excise")
pipeline._excise()
work.assert_not_called()
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "off")
pipeline._admit_pipeline_phase("excise")
@pytest.mark.parametrize("option", ["expert_transplant", "activation_steering"])
def test_refinement_with_future_auxiliary_targets_rejects_explicitly(option, monkeypatch, caplog):
pipeline = make_pipeline(true_iterative_refinement=True, refinement_passes=2,
**{option: True})
pipeline._admit_pipeline_phase("excise")
assert "future direction/expert selections" in caplog.text
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "strict")
with pytest.raises(admission.PhaseAdmissionError,
match="excise.*iterative refinement.*OBLITERATUS_PHASE_ADMISSION=off"):
pipeline._admit_pipeline_phase("excise")
def test_zero_strength_steering_keeps_installation_but_allocates_no_forward_workspace(cpu_threads):
pipeline = make_pipeline(layers=1, activation_steering=True, steering_strength=0)
host, _, forward = pipeline._optional_phase_workspaces(
"excise", [admission.ForwardWorkload(1, 16)],
)
assert host == 64 * 4 and forward == {}
pipeline._install_activation_steering(pipeline.handle.model.model.layers)
module = pipeline.handle.model.model.layers[0]
hook = next(iter(module._forward_hooks.values()))
hidden = torch.ones(1, 16, 64)
with cpu_memory_profile() as profile:
assert hook(module, (), hidden) is hidden
assert cpu_peak_bytes(profile) == 0
def test_transplant_only_ffns_do_not_enter_ordinary_projection_transactions(monkeypatch, cpu_threads):
pipeline = make_pipeline(layers=1, expert_transplant=True)
pipeline.projection_target = "attention"
layer = pipeline.handle.model.model.layers[0]
layer.mlp.experts = torch.nn.ModuleList([torch.nn.Module() for _ in range(3)])
for expert in layer.mlp.experts:
expert.down_proj = torch.nn.Linear(96, 64, bias=False)
expert.up_proj = torch.nn.Linear(64, 128, bias=False)
pipeline._expert_safety_scores = {0: [(0, 3.0), (1, 2.0), (2, 1.0)]}
assert all(".mlp." not in name for name in pipeline._excise_parameter_names())
captured = Mock()
monkeypatch.setattr(admission, "admit_model_phase", captured)
pipeline._admit_pipeline_phase("excise")
options = captured.call_args.kwargs.copy()
options.pop("gpu_memory_utilization")
report = admission.model_phase_demand(pipeline.handle, "excise", **options)
with cpu_memory_profile() as profile:
pipeline._excise()
peak = cpu_peak_bytes(profile)
assert 0 < peak <= report.devices["cpu"].additional_bytes <= 2 * peak