mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
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.
157 lines
6.5 KiB
Python
157 lines
6.5 KiB
Python
"""Allocation failures in EXCISE setup/refinement must not select fallbacks."""
|
|
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from obliteratus import abliterate
|
|
from obliteratus.abliterate import PipelineFailure
|
|
from tests.test_excise_failures import editing_pipeline as editing_pipeline, prepare_public_run
|
|
|
|
|
|
@pytest.mark.parametrize("getter", ["get_attention_module", "get_ffn_module"])
|
|
@pytest.mark.parametrize("failure_call", [1, 2])
|
|
def test_target_enumeration_propagates_ordinary_and_sae_lookup_oom(
|
|
editing_pipeline, monkeypatch, getter, failure_call,
|
|
):
|
|
pipeline, _ = editing_pipeline
|
|
error = torch.OutOfMemoryError("target enumeration exhausted memory")
|
|
original = getattr(abliterate, getter)
|
|
calls = []
|
|
|
|
def lookup(*args):
|
|
calls.append(args)
|
|
if len(calls) == failure_call:
|
|
raise error
|
|
return original(*args)
|
|
|
|
monkeypatch.setattr(abliterate, getter, lookup)
|
|
with pytest.raises(torch.OutOfMemoryError) as caught:
|
|
pipeline._excise_parameter_names()
|
|
assert caught.value is error
|
|
assert len(calls) == failure_call
|
|
|
|
|
|
@pytest.mark.parametrize("method,getter", [
|
|
("_identify_refusal_heads", "get_attention_module"),
|
|
("_identify_safety_experts", "get_ffn_module"),
|
|
])
|
|
def test_refinement_classification_does_not_skip_lookup_oom(
|
|
editing_pipeline, monkeypatch, method, getter,
|
|
):
|
|
pipeline, _ = editing_pipeline
|
|
error = torch.OutOfMemoryError("classification exhausted memory")
|
|
lookup = Mock(side_effect=error)
|
|
monkeypatch.setattr(abliterate, getter, lookup)
|
|
with pytest.raises(torch.OutOfMemoryError) as caught:
|
|
getattr(pipeline, method)()
|
|
assert caught.value is error
|
|
lookup.assert_called_once()
|
|
|
|
|
|
def test_public_kl_capture_oom_is_not_an_optional_diagnostic_skip(editing_pipeline, monkeypatch):
|
|
pipeline, events = editing_pipeline
|
|
capture = pipeline._capture_baseline_kl_logits
|
|
verify, export = prepare_public_run(pipeline, monkeypatch)
|
|
monkeypatch.setattr(pipeline, "_capture_baseline_kl_logits", capture)
|
|
monkeypatch.setattr(pipeline, "_kl_capture_prompts", lambda: ["fixture"])
|
|
monkeypatch.setattr(pipeline, "_maybe_apply_chat_template", lambda prompts: prompts)
|
|
error = torch.OutOfMemoryError("baseline logits exhausted memory")
|
|
tokenizer = Mock(side_effect=error)
|
|
pipeline.handle.tokenizer = tokenizer
|
|
with pytest.raises(PipelineFailure) as caught:
|
|
pipeline.run()
|
|
assert caught.value.__cause__ is error
|
|
assert caught.value.stage == "excise" and caught.value.metric == "memory_exhaustion"
|
|
tokenizer.assert_called_once()
|
|
assert [event.status for event in events] == ["error"]
|
|
assert pipeline.handle.model is None and pipeline.handle.tokenizer is None
|
|
verify.assert_not_called()
|
|
export.assert_not_called()
|
|
|
|
|
|
@pytest.mark.parametrize("failure", ["wasserstein-init", "wasserstein-extract", "leace-extract"])
|
|
def test_public_iterative_extraction_oom_does_not_fall_back_to_another_recipe(
|
|
editing_pipeline, monkeypatch, failure,
|
|
):
|
|
from obliteratus.analysis import wasserstein_optimal
|
|
|
|
pipeline, events = editing_pipeline
|
|
pipeline.refinement_passes = 2
|
|
pipeline.true_iterative_refinement = True
|
|
pipeline.use_sae_features = False
|
|
pipeline.n_directions = 1
|
|
pipeline.use_wasserstein_optimal = failure.startswith("wasserstein")
|
|
error = torch.OutOfMemoryError("iterative extraction exhausted memory")
|
|
failing_operation = Mock(side_effect=error)
|
|
if failure == "wasserstein-init":
|
|
monkeypatch.setattr(wasserstein_optimal, "WassersteinOptimalExtractor", failing_operation)
|
|
elif failure == "wasserstein-extract":
|
|
monkeypatch.setattr(wasserstein_optimal.WassersteinOptimalExtractor, "extract", failing_operation)
|
|
else:
|
|
monkeypatch.setattr(pipeline, "_make_direction_extractor", lambda **_kw: Mock(extract=failing_operation))
|
|
verify, export = prepare_public_run(pipeline, monkeypatch)
|
|
probes = []
|
|
|
|
def probe():
|
|
probes.append(None)
|
|
pipeline._harmful_means = {0: torch.ones(1, 32)}
|
|
pipeline._harmless_means = {0: torch.zeros(1, 32)}
|
|
pipeline._harmful_acts = {0: [torch.ones(1, 32)]}
|
|
pipeline._harmless_acts = {0: [torch.zeros(1, 32)]}
|
|
|
|
monkeypatch.setattr(pipeline, "_probe", probe)
|
|
weight = pipeline.handle.model.model.layers[0].self_attn.o_proj.weight
|
|
pristine = weight.clone()
|
|
direction = pipeline.concept_directions[0].clone()
|
|
with pytest.raises(PipelineFailure) as caught:
|
|
pipeline.run()
|
|
assert len(probes) == 2 # Initial PROBE and actual EXCISE re-extraction.
|
|
assert not torch.equal(weight, pristine) # The first surgery pass completed.
|
|
assert torch.equal(pipeline.concept_directions[0], direction)
|
|
failing_operation.assert_called_once()
|
|
assert caught.value.__cause__ is error
|
|
assert caught.value.stage == "excise" and caught.value.metric == "memory_exhaustion"
|
|
assert [event.status for event in events] == ["running", "error"]
|
|
assert pipeline.handle.model is None
|
|
verify.assert_not_called()
|
|
export.assert_not_called()
|
|
|
|
|
|
@pytest.mark.parametrize("error_type", [AttributeError, RuntimeError, torch.OutOfMemoryError])
|
|
def test_public_selected_expert_transplant_lookup_failure_is_terminal(
|
|
editing_pipeline, monkeypatch, error_type,
|
|
):
|
|
pipeline, events = editing_pipeline
|
|
pipeline.use_sae_features = False
|
|
pipeline.expert_transplant = True
|
|
pipeline._expert_safety_scores = {0: [(0, 1.0), (1, 0.0)]}
|
|
original = abliterate.get_ffn_module
|
|
calls = []
|
|
error = error_type("selected expert group unavailable")
|
|
|
|
def lookup(*args):
|
|
calls.append(args)
|
|
if len(calls) == 2: # Ordinary FFN succeeds; the selected transplant fails.
|
|
raise error
|
|
return original(*args)
|
|
|
|
monkeypatch.setattr(abliterate, "get_ffn_module", lookup)
|
|
verify, export = prepare_public_run(pipeline, monkeypatch)
|
|
with pytest.raises(PipelineFailure) as caught:
|
|
pipeline.run()
|
|
assert len(calls) == 2
|
|
assert caught.value.stage == "excise"
|
|
if error_type is torch.OutOfMemoryError:
|
|
assert caught.value.metric == "memory_exhaustion"
|
|
assert caught.value.__cause__ is error
|
|
else:
|
|
assert caught.value.metric == "projection"
|
|
assert "expert transplant lookup failed" in str(caught.value)
|
|
assert caught.value.__cause__.__cause__ is error
|
|
assert [event.status for event in events] == ["running", "error"]
|
|
assert pipeline.handle.model is None
|
|
verify.assert_not_called()
|
|
export.assert_not_called()
|