Files
OBLITERATUS/tests/test_excise_recipe_oom.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

164 lines
7.8 KiB
Python

"""LoRA and Bayesian recipes preserve allocation failures at the public boundary."""
from unittest.mock import Mock
import optuna
import pytest
import torch
from obliteratus import bayesian_optimizer as bo, lora_ablation as lora
from obliteratus.abliterate import PipelineFailure
from obliteratus.strategies import utils
from tests.test_excise_failures import editing_pipeline as editing_pipeline, prepare_public_run
def _assert_public_oom(pipeline, events, error, verify, export):
model = pipeline.handle.model
pristine = {name: value.clone() for name, value in model.state_dict().items()}
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"
assert [event.status for event in events] == ["running", "error"]
assert pipeline.handle.model is None and pipeline.handle.tokenizer is None
assert pipeline._excise_modified_count is None
assert not pipeline.output_dir.exists()
for name, value in model.state_dict().items():
assert torch.equal(value, pristine[name]), name
verify.assert_not_called()
export.assert_not_called()
@pytest.mark.parametrize("getter", ["get_attention_module", "get_ffn_module"])
def test_public_lora_target_lookup_oom_cannot_export_partial_adapters(editing_pipeline, monkeypatch, getter):
pipeline, events = editing_pipeline
pipeline.use_lora_ablation = True
error = torch.OutOfMemoryError("LoRA target lookup exhausted memory")
lookup = Mock(side_effect=error)
monkeypatch.setattr(utils, getter, lookup)
verify, export = prepare_public_run(pipeline, monkeypatch)
_assert_public_oom(pipeline, events, error, verify, export)
lookup.assert_called_once()
def _legacy_adapters(pipeline, getter):
adapters = lora.compute_lora_adapters(pipeline)
# Keep named targets ahead of the legacy target: all must resolve before
# any commit, even when the failed target is late in the selection.
kind, name = ("attn", "o_proj") if getter == "get_attention_module" else ("ffn", "down_proj")
parent = "self_attn" if kind == "attn" else "mlp"
pair = adapters.pop(f"model.layers.0.{parent}.{name}")
assert adapters
adapters[f"layer.0.{kind}.{name}"] = pair
return adapters
@pytest.mark.parametrize("getter", ["get_layer_modules", "get_attention_module", "get_ffn_module"])
def test_public_lora_legacy_merge_lookup_oom_is_terminal(editing_pipeline, monkeypatch, getter):
pipeline, events = editing_pipeline
pipeline.use_lora_ablation = True
adapters = _legacy_adapters(pipeline, getter)
monkeypatch.setattr(lora, "compute_lora_adapters", lambda *_a, **_kw: adapters)
error = torch.OutOfMemoryError("legacy LoRA merge exhausted memory")
lookup = Mock(side_effect=error)
monkeypatch.setattr(utils, getter, lookup)
verify, export = prepare_public_run(pipeline, monkeypatch)
_assert_public_oom(pipeline, events, error, verify, export)
lookup.assert_called_once()
@pytest.mark.parametrize("getter", ["get_layer_modules", "get_attention_module", "get_ffn_module"])
def test_lora_legacy_unmerge_lookup_oom_preserves_all_merged_weights(editing_pipeline, monkeypatch, getter):
pipeline, _ = editing_pipeline
adapters = _legacy_adapters(pipeline, getter)
originals = lora.apply_lora_adapters(pipeline, adapters, keep_originals=True)
model = pipeline.handle.model
merged = {name: value.clone() for name, value in model.state_dict().items()}
error = torch.OutOfMemoryError("legacy LoRA unmerge exhausted memory")
lookup = Mock(side_effect=error)
monkeypatch.setattr(utils, getter, lookup)
with pytest.raises(torch.OutOfMemoryError) as caught:
lora.unmerge_lora_adapters(pipeline, adapters, original_weights=originals)
assert caught.value is error
lookup.assert_called_once()
assert pipeline._lora_adapters is adapters
for name, value in model.state_dict().items():
assert torch.equal(value, merged[name]), name
@pytest.mark.parametrize("operation", ["tokenize", "forward"])
def test_public_bayesian_reference_oom_keeps_memory_classification(editing_pipeline, monkeypatch, operation):
pipeline, events = editing_pipeline
pipeline._bayesian_trials = 1
error = torch.OutOfMemoryError("Bayesian reference capture exhausted memory")
tokenizer = Mock(return_value={"input_ids": torch.tensor([[1, 2]])})
pipeline.handle.tokenizer = tokenizer
failing = tokenizer if operation == "tokenize" else Mock()
failing.side_effect = error
if operation == "forward":
monkeypatch.setattr(pipeline.handle.model, "forward", failing)
monkeypatch.setattr(pipeline, "_maybe_apply_chat_template", lambda prompts: prompts)
search = Mock(side_effect=AssertionError("search must not start without reference logits"))
monkeypatch.setattr(optuna, "create_study", search)
verify, export = prepare_public_run(pipeline, monkeypatch)
_assert_public_oom(pipeline, events, error, verify, export)
failing.assert_called_once()
search.assert_not_called()
assert pipeline._bayesian_skipped is None
def test_public_bayesian_trial_oom_restores_weights_without_completing_or_exporting(
editing_pipeline, monkeypatch, tmp_path,
):
pipeline, events = editing_pipeline
pipeline._bayesian_trials = 1
pipeline._bayesian_study_path = tmp_path / "study"
pipeline.use_sae_features = False
model = pipeline.handle.model
pristine = {name: value.clone() for name, value in model.state_dict().items()}
pipeline.handle.tokenizer = Mock(return_value={"input_ids": torch.tensor([[1, 2]])})
monkeypatch.setattr(pipeline, "_maybe_apply_chat_template", lambda prompts: prompts)
error = torch.OutOfMemoryError("Bayesian trial generation exhausted memory")
def generate(**_kwargs):
assert any(not torch.equal(value, pristine[name]) for name, value in model.state_dict().items())
raise error
failing = Mock(side_effect=generate)
monkeypatch.setattr(model, "generate", failing)
study_factory = Mock(wraps=optuna.create_study)
monkeypatch.setattr(optuna, "create_study", study_factory)
verify, export = prepare_public_run(pipeline, monkeypatch)
_assert_public_oom(pipeline, events, error, verify, export)
failing.assert_called_once()
study_factory.assert_called_once()
assert pipeline._bayesian_execution is None
assert not pipeline._bayesian_execution_captured
assert pipeline._bayesian_plan is None
assert not list(pipeline._bayesian_study_path.glob("*.trial-*.json"))
@pytest.mark.parametrize("error_type", [RuntimeError, torch.OutOfMemoryError])
def test_bayesian_generation_only_counts_non_oom_failures_as_invalid(editing_pipeline, monkeypatch, error_type):
pipeline, _ = editing_pipeline
pipeline.harmful_prompts = ["fixture-a", "fixture-b"]
error = error_type("generation failed")
tokenizer = Mock(return_value={"input_ids": torch.tensor([[1, 2]])})
tokenizer.decode.return_value = "Here is a useful response."
pipeline.handle.tokenizer = tokenizer
generate = Mock(side_effect=[error, torch.tensor([[1, 2, 3]])])
monkeypatch.setattr(pipeline.handle.model, "generate", generate)
monkeypatch.setattr(pipeline, "_maybe_apply_chat_template", lambda prompts: prompts)
if error_type is torch.OutOfMemoryError:
with pytest.raises(torch.OutOfMemoryError) as caught:
bo._measure_refusal_rate(pipeline)
assert caught.value is error
generate.assert_called_once()
tokenizer.decode.assert_not_called()
else:
assert bo._measure_refusal_rate(pipeline) == 0.5
assert generate.call_count == 2
measurement = pipeline._bayesian_refusal_measurement
assert measurement.total == 2 and measurement.invalid == 1 and measurement.refused == 0
assert measurement.invalid_rate == 0.5