"""Exact KL rollback groups cover final global writes and failure cleanup.""" from __future__ import annotations import inspect from types import SimpleNamespace import pytest import torch from torch import nn from obliteratus.abliterate import AbliterationPipeline def _pipeline(model, strong=(0,)): pipeline = AbliterationPipeline.__new__(AbliterationPipeline) pipeline.handle = SimpleNamespace(model=model) pipeline.method = "basic" pipeline._bayesian_trials = 0 pipeline.use_kl_optimization = True pipeline.kl_budget = 0.0 pipeline.layers = None pipeline._strong_layers = list(strong) pipeline._layer_excise_weights = {} pipeline._refusal_strengths = {} pipeline._preserved_directions = [] pipeline.concept_subspaces = {index: torch.ones(1, 2) for index in strong} pipeline._quality_metrics = {} pipeline._kl_contributions = {} pipeline._kl_restored_groups = [] pipeline._free_gpu_memory = lambda: None pipeline.log = lambda _message: None return pipeline class _Model(nn.Module): def __init__(self, layers=1): super().__init__() self.layers = nn.ModuleList([ nn.Linear(2, 2, bias=False) for _ in range(layers) ]) self.lm_head = nn.Linear(2, 2, bias=False) def _bytes(module): return sum(value.numel() * value.element_size() for value in module.state_dict().values()) def test_rollback_inventory_adds_global_head_but_not_model_level_tables(): model = _Model(layers=3) model.engram_tables = nn.Embedding(1_000_000, 2, device="meta") pipeline = _pipeline(model, strong=(0, 1, 2)) groups = pipeline._kl_rollback_groups(model.layers) assert list(groups) == [0, "lm_head.weight"] sizes = [_bytes(groups[key]) for key in groups] expected = sum(sizes) + max( size + max(sizes[:index] + sizes[index + 1:] + [sizes[-1]]) for index, size in enumerate(sizes) ) assert pipeline._kl_rollback_host_bytes(model.layers) == expected def test_optimizer_can_restore_layer_and_final_global_head(): model = _Model() pipeline = _pipeline(model) with torch.no_grad(): model.layers[0].weight.zero_() model.lm_head.weight.zero_() pristine = { 0: pipeline._snapshot_layer_state(model.layers[0]), "lm_head.weight": pipeline._snapshot_layer_state(model.lm_head), } with torch.no_grad(): model.layers[0].weight.fill_(2) model.lm_head.weight.fill_(1) pipeline._measure_sequence_token_kl = lambda: ( model.layers[0].weight.abs().sum().item() + model.lm_head.weight.abs().sum().item() ) pipeline._kl_optimize_corrections(model.layers, 2, pristine) assert not model.layers[0].weight.count_nonzero() assert not model.lm_head.weight.count_nonzero() assert pipeline._strong_layers == [] assert pipeline._kl_restored_groups == [0, "lm_head.weight"] assert pipeline._kl_corrected_layers == [0] assert pipeline._kl_corrected_globals == ["lm_head.weight"] assert set(pipeline._kl_contributions) == {0, "lm_head.weight"} def test_marginal_measurement_failure_restores_modified_group(): model = _Model() pipeline = _pipeline(model) with torch.no_grad(): model.layers[0].weight.zero_() pristine = {0: pipeline._snapshot_layer_state(model.layers[0])} with torch.no_grad(): model.layers[0].weight.fill_(3) expected = model.layers[0].weight.detach().clone() calls = 0 def measure(): nonlocal calls calls += 1 if calls == 1: return 1.0 raise ValueError("measurement failed") pipeline._measure_sequence_token_kl = measure with pytest.raises(ValueError, match="measurement failed"): pipeline._kl_optimize_corrections(model.layers, 1, pristine) assert torch.equal(model.layers[0].weight, expected) assert not getattr(model, "_obliteratus_surgery_invalid", False) def test_unsatisfied_budget_retains_effective_restoration_evidence(): model = _Model() pipeline = _pipeline(model) with torch.no_grad(): model.layers[0].weight.zero_() model.lm_head.weight.zero_() pristine = { 0: pipeline._snapshot_layer_state(model.layers[0]), "lm_head.weight": pipeline._snapshot_layer_state(model.lm_head), } with torch.no_grad(): model.layers[0].weight.fill_(2) model.lm_head.weight.fill_(1) pipeline._measure_sequence_token_kl = lambda: 10.0 + ( model.layers[0].weight.abs().sum().item() + model.lm_head.weight.abs().sum().item() ) with pytest.raises(RuntimeError, match="could not satisfy"): pipeline._kl_optimize_corrections(model.layers, 2, pristine) assert pipeline._kl_corrected_layers == [0] assert pipeline._kl_corrected_globals == ["lm_head.weight"] assert pipeline._kl_restored_groups == [0, "lm_head.weight"] assert pipeline._strong_layers == [] def test_failed_temporary_restore_marks_model_invalid(): model = _Model() pipeline = _pipeline(model) with torch.no_grad(): model.layers[0].weight.zero_() pristine = {0: pipeline._snapshot_layer_state(model.layers[0])} with torch.no_grad(): model.layers[0].weight.fill_(3) calls = 0 restore = pipeline._restore_layer_state def fail_second(module, snapshot): nonlocal calls calls += 1 if calls == 2: raise OSError("restore failed") restore(module, snapshot) pipeline._restore_layer_state = fail_second pipeline._measure_sequence_token_kl = lambda: 1.0 with pytest.raises(RuntimeError, match="discard and reload"): pipeline._kl_optimize_corrections(model.layers, 1, pristine) assert model._obliteratus_surgery_invalid is True def test_correction_runs_after_global_and_auxiliary_weight_edits(): source = inspect.getsource(AbliterationPipeline._excise_inner) correction = source.index("self._kl_optimize_corrections(") assert source.index("Expert weight transplant") < correction assert correction < source.index("Activation steering hooks")