Files
OBLITERATUS/tests/test_excise_failures.py
Joseph Magly 2e9a815d1b fix(excise): propagate nested recipe failures
Keep allocation failures terminal through quantized experts, adapter selection, Bayesian evaluation and refinement helpers. Route folded-writer errors and warnings promoted to errors through public cleanup before verification or export.

Exercise exact exception causes, failed-operation counts, rollback, cleanup and no-export behavior across recipe variants. Fixes #389.
2026-10-28 23:15:14 -04:00

275 lines
11 KiB
Python

"""EXCISE failures cannot become successful partial edits or exported checkpoints."""
from unittest.mock import MagicMock, Mock
import warnings
import pytest
import torch
from transformers import Qwen2Config, Qwen2ForCausalLM
from obliteratus import abliterate
from obliteratus.abliterate import AbliterationPipeline, PipelineCancelledError, PipelineFailure
from obliteratus.concepts.gate import GateFailed
from obliteratus.execution_budget import SearchBudgetExhausted
from obliteratus.models.loader import ModelHandle
from obliteratus.phase_admission import PhaseAdmissionError
TARGETS = ("attention", "ffn", "sae-attention", "sae-ffn")
def prepare_public_run(pipeline, monkeypatch):
"""Use the real public failure boundary without generation or model loading."""
for name in (
"_summon", "_capture_stock_baseline", "_probe", "_distill",
"_validate_concept_gate", "_prepare_concept_preservation",
"_capture_concept_evaluation", "_capture_concept_repair",
"_capture_baseline_kl_logits",
):
monkeypatch.setattr(pipeline, name, lambda: None)
monkeypatch.setattr(pipeline, "_admit_pipeline_phase", lambda *_a, **_k: None)
verify, export = Mock(), Mock()
monkeypatch.setattr(pipeline, "_verify", verify)
monkeypatch.setattr(pipeline, "_rebirth", export)
return verify, export
@pytest.fixture
def editing_pipeline(tmp_path, monkeypatch):
config = Qwen2Config(
vocab_size=128, hidden_size=32, intermediate_size=64,
num_hidden_layers=2, num_attention_heads=4, num_key_value_heads=2,
max_position_embeddings=64,
)
model = Qwen2ForCausalLM(config)
pipeline = AbliterationPipeline(
model_name="fixture/qwen2", output_dir=str(tmp_path / "checkpoint"),
method="advanced", refinement_passes=1, norm_preserve=False,
project_biases=False, layer_adaptive_strength=False,
safety_neuron_masking=False, attention_head_surgery=False,
use_sae_features=True, use_kl_optimization=False,
projection_target="all", harmful_prompts=["fixture-a"],
harmless_prompts=["fixture-b"], on_log=lambda _: None,
)
pipeline.handle = ModelHandle(model, MagicMock(), config, "fixture/qwen2", "causal_lm")
pipeline._strong_layers = [0]
direction = torch.zeros(32)
direction[0] = 1
pipeline.refusal_directions = {0: direction.clone()}
pipeline.refusal_subspaces = {0: direction.unsqueeze(0)}
sae = torch.zeros(1, 32)
sae[0, 1] = 1
pipeline._sae_directions = {0: sae}
# Capacity observation is independent of injected operation failures.
monkeypatch.setattr(pipeline, "_admit_pipeline_phase", lambda *_a, **_k: None)
events = []
pipeline._on_stage = events.append
return pipeline, events
def inject_projection_failure(monkeypatch, pipeline, target, error):
layer = pipeline.handle.model.model.layers[0]
original = pipeline._project_out_advanced
injected = []
def project(module, direction, names, **kwargs):
is_sae = abs(direction.flatten()[1].item()) > 0.99
kind = "attention" if module is layer.self_attn else "ffn" if module is layer.mlp else "other"
actual = ("sae-" if is_sae else "") + kind
if actual == target and not injected:
injected.append(actual)
raise error
return original(module, direction, names, **kwargs)
monkeypatch.setattr(pipeline, "_project_out_advanced", project)
return injected
@pytest.mark.parametrize("target", TARGETS)
def test_projection_oom_propagates_without_success(editing_pipeline, monkeypatch, target):
pipeline, events = editing_pipeline
error = torch.OutOfMemoryError("test allocation exhausted")
injected = inject_projection_failure(monkeypatch, pipeline, target, error)
gradients_enabled = torch.is_grad_enabled()
with pytest.raises(torch.OutOfMemoryError) as caught:
pipeline._excise()
assert caught.value is error
assert injected == [target]
assert not any(event.status == "done" for event in events)
assert pipeline._excise_modified_count is None
assert torch.is_grad_enabled() == gradients_enabled
@pytest.mark.parametrize("target", TARGETS)
def test_public_run_cleans_up_oom_and_never_exports(editing_pipeline, monkeypatch, target):
pipeline, events = editing_pipeline
handle = pipeline.handle
error = torch.OutOfMemoryError("test allocation exhausted")
injected = inject_projection_failure(monkeypatch, pipeline, target, error)
for name in (
"_summon", "_capture_stock_baseline", "_probe", "_distill",
"_validate_concept_gate", "_prepare_concept_preservation",
"_capture_concept_evaluation", "_capture_concept_repair",
"_capture_baseline_kl_logits",
):
monkeypatch.setattr(pipeline, name, lambda: None)
verify, export = Mock(), Mock()
monkeypatch.setattr(pipeline, "_verify", verify)
monkeypatch.setattr(pipeline, "_rebirth", export)
with pytest.raises(PipelineFailure, match="Memory exhausted during EXCISE") as caught:
pipeline.run()
assert caught.value.__cause__ is error
assert caught.value.stage == "excise"
assert caught.value.metric == "memory_exhaustion"
assert injected == [target]
assert [event.status for event in events if event.stage == "excise"] == ["running", "error"]
assert "module names" not in events[-1].message
assert handle.model is None and handle.tokenizer is None
assert pipeline._excise_modified_count is None
verify.assert_not_called()
export.assert_not_called()
assert not pipeline.output_dir.exists()
@pytest.mark.parametrize("target", TARGETS)
@pytest.mark.parametrize("error_type", [AttributeError, RuntimeError])
def test_other_projection_failures_are_counted_and_terminal(
editing_pipeline, monkeypatch, target, error_type,
):
pipeline, events = editing_pipeline
injected = inject_projection_failure(monkeypatch, pipeline, target, error_type("test projection failed"))
with pytest.warns(UserWarning, match="projection failed"):
with pytest.raises(PipelineFailure, match="1 projection operation") as caught:
pipeline._excise()
assert injected == [target]
assert caught.value.metric == "projection"
assert events[-1].details["failed_projection_count"] == 1
assert not any(event.status == "done" for event in events)
assert pipeline._excise_modified_count is None
@pytest.mark.parametrize("kind", ["attention", "ffn"])
@pytest.mark.parametrize("error_type", [AttributeError, torch.OutOfMemoryError])
def test_sae_lookup_failure_is_not_silent(editing_pipeline, monkeypatch, kind, error_type):
pipeline, events = editing_pipeline
name = "get_attention_module" if kind == "attention" else "get_ffn_module"
original = getattr(abliterate, name)
calls = []
error = error_type("test SAE lookup failed")
def lookup(*args):
calls.append(args)
if len(calls) == 2: # Ordinary projection succeeds; SAE lookup fails.
raise error
return original(*args)
monkeypatch.setattr(abliterate, name, lookup)
if error_type is torch.OutOfMemoryError:
with pytest.raises(torch.OutOfMemoryError) as caught:
pipeline._excise()
assert caught.value is error
else:
with pytest.warns(UserWarning, match="SAE .* lookup failed"):
with pytest.raises(PipelineFailure, match="1 projection operation"):
pipeline._excise()
assert len(calls) == 2
assert not any(event.status == "done" for event in events)
def test_complete_sae_edit_still_succeeds(editing_pipeline):
pipeline, events = editing_pipeline
pipeline._excise()
assert events[-1].status == "done"
assert pipeline._excise_modified_count == 10
@pytest.mark.parametrize("target", TARGETS)
@pytest.mark.parametrize("policy", ["always", "ignore", "error"])
def test_public_projection_failure_cleans_up_under_every_warning_filter(
editing_pipeline, monkeypatch, target, policy,
):
pipeline, events = editing_pipeline
handle = pipeline.handle
error = RuntimeError("injected incomplete projection")
injected = inject_projection_failure(monkeypatch, pipeline, target, error)
verify, export = prepare_public_run(pipeline, monkeypatch)
with warnings.catch_warnings(record=True) as observed:
warnings.simplefilter(policy)
with pytest.raises(PipelineFailure) as caught:
pipeline.run()
assert injected == [target]
assert caught.value.stage == "excise" and caught.value.metric == "projection"
if policy == "error":
assert isinstance(caught.value.__cause__, UserWarning)
assert caught.value.__cause__.__context__ is error
if policy == "always":
assert len(observed) == 1 and "injected incomplete projection" in str(observed[0].message)
assert handle.model is None and handle.tokenizer is None
assert [event.status for event in events if event.stage == "excise"] == ["running", "error"]
assert pipeline._excise_modified_count is None
verify.assert_not_called()
export.assert_not_called()
@pytest.mark.parametrize("failed_calls", [{1}, {1, 2, 3}])
def test_multipass_projection_failures_cannot_be_erased_by_later_success(
editing_pipeline, monkeypatch, failed_calls,
):
pipeline, events = editing_pipeline
pipeline.refinement_passes = 2
pipeline.true_iterative_refinement = False
pipeline.use_sae_features = False
original = pipeline._project_out_advanced
calls = []
def project(*args, **kwargs):
calls.append(args)
if len(calls) in failed_calls:
raise RuntimeError("injected multipass failure")
return original(*args, **kwargs)
monkeypatch.setattr(pipeline, "_project_out_advanced", project)
verify, export = prepare_public_run(pipeline, monkeypatch)
with pytest.warns(UserWarning, match="projection failed"):
with pytest.raises(PipelineFailure, match=f"{len(failed_calls)} projection operation"):
pipeline.run()
assert len(calls) > max(failed_calls)
assert pipeline._effective_refinement_passes == 2
assert events[-1].details["failed_projection_count"] == len(failed_calls)
assert not any(event.status == "done" for event in events)
assert pipeline.handle.model is None
verify.assert_not_called()
export.assert_not_called()
@pytest.mark.parametrize("error", [
PipelineFailure("existing terminal failure", stage="excise", metric="original"),
PipelineCancelledError("cancelled", stage="excise"),
GateFailed("gate rejected"),
SearchBudgetExhausted("search budget exhausted"),
PhaseAdmissionError("excise", "cpu", 100, 10),
])
def test_public_excise_boundary_preserves_existing_failure_contracts(editing_pipeline, monkeypatch, error):
pipeline, events = editing_pipeline
verify, export = prepare_public_run(pipeline, monkeypatch)
monkeypatch.setattr(pipeline, "_excise", Mock(side_effect=error))
expected = PipelineFailure if isinstance(error, PhaseAdmissionError) else type(error)
with pytest.raises(expected) as caught:
pipeline.run()
if isinstance(error, PhaseAdmissionError):
assert caught.value.__cause__ is error
assert caught.value.metric == "memory_admission"
assert events[-1].details["shortfall_bytes"] == 90
else:
assert caught.value is error
assert not events # The owning failure path, not this boundary, emits its event.
assert pipeline.handle.model is None and pipeline.handle.tokenizer is None
verify.assert_not_called()
export.assert_not_called()