Files
OBLITERATUS/tests/test_phase_rollback_admission.py

218 lines
9.6 KiB
Python

"""KL rollback admission follows actual candidates, dtypes and snapshot lifetimes."""
from types import SimpleNamespace
from unittest.mock import Mock, call
import weakref
import pytest
import torch
from torch import nn
from torch._subclasses.fake_tensor import FakeTensorMode
from obliteratus import abliterate, phase_admission
from obliteratus.abliterate import AbliterationPipeline
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
def _pipeline(selected):
pipeline = AbliterationPipeline.__new__(AbliterationPipeline)
pipeline.method = "basic"
pipeline._bayesian_trials = 0
pipeline.use_kl_optimization = True
pipeline._strong_layers = selected
pipeline._layer_excise_weights = {}
pipeline._refusal_strengths = {}
pipeline._preserved_directions = []
return pipeline
def _layer(rows=8, columns=8, *, dtype=torch.bfloat16, device="cpu"):
layer = nn.Module()
layer.weight = nn.Parameter(torch.empty(rows, columns, device=device, dtype=dtype))
return layer
def _bytes(layer):
return sum(value.numel() * value.element_size() for value in layer.state_dict().values())
def test_rollback_candidates_share_existing_weakest_third_order():
pipeline = _pipeline([5, 4, 3, 2, 1, 0])
pipeline._layer_excise_weights = {0: 0.2, 1: 0.2, 2: 0.2, 3: 0.8}
pipeline._refusal_strengths = {0: 5.0, 1: 1.0, 2: 1.0}
assert pipeline._kl_rollback_candidates() == [1, 2]
pipeline._strong_layers = [5]
assert pipeline._kl_rollback_candidates() == [5]
pipeline.use_kl_optimization = False
assert pipeline._kl_rollback_candidates() == []
def test_unrelated_giant_layers_are_never_inspected_or_charged(monkeypatch):
layers = nn.ModuleList([_layer(), _layer(16), _layer(), _layer(), _layer()])
# Metadata only: this unrelated tensor would be 2 TB if allocated.
layers.append(_layer(1_000_000, 1_000_000, device="meta"))
pipeline = _pipeline([0, 1, 2, 3, 4, 5])
pipeline._layer_excise_weights = {0: 0.0, 1: 0.1}
forbidden = Mock(side_effect=AssertionError("unselected layer was inspected"))
for layer in layers[2:]:
monkeypatch.setattr(layer, "state_dict", forbidden)
size0, size1 = _bytes(layers[0]), _bytes(layers[1])
assert pipeline._kl_rollback_host_bytes(layers) == size0 + size1 + 2 * size1
forbidden.assert_not_called()
@pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32, torch.float64])
def test_snapshot_demand_preserves_original_dtype_and_persistent_buffers(dtype):
layer = _layer(dtype=dtype)
layer.register_buffer("counter", torch.ones(3, dtype=torch.int64))
layer.register_buffer("transient", torch.ones(1024), persistent=False)
pipeline = _pipeline([0])
expected = 64 * torch.empty((), dtype=dtype).element_size() + 3 * 8
assert pipeline._kl_rollback_host_bytes(nn.ModuleList([layer])) == 3 * expected
snapshot = pipeline._snapshot_layer_state(layer)
assert snapshot["weight"].dtype == dtype
assert "transient" not in snapshot
def test_state_dict_aliases_are_charged_as_the_actual_snapshot_clones_them():
layer = _layer()
layer.alias = layer.weight
pipeline = _pipeline([0])
bound = pipeline._kl_rollback_host_bytes(nn.ModuleList([layer]))
assert bound == 3 * 2 * layer.weight.numel() * layer.weight.element_size()
snapshot = pipeline._snapshot_layer_state(layer)
assert snapshot["alias"].data_ptr() != snapshot["weight"].data_ptr()
def test_cuda_snapshot_reserves_cpu_transfer_copy_without_any_cuda_allocation():
with FakeTensorMode():
host = _layer(128, 64)
cuda = _layer(128, 64, device="cuda:0")
pipeline = _pipeline([0])
host_bound = pipeline._kl_rollback_host_bytes(nn.ModuleList([host]))
cuda_bound = pipeline._kl_rollback_host_bytes(nn.ModuleList([cuda]))
assert cuda_bound - host_bound == 128 * 64 * 2
def test_largest_candidates_overlap_only_where_loop_order_allows():
# Three candidates, with the largest first and the smallest last. The
# greedy boundary cannot duplicate the largest candidate's snapshot.
layers = nn.ModuleList([_layer(32), _layer(16), _layer(8)] + [_layer()] * 6)
pipeline = _pipeline(list(range(9)))
sizes = [_bytes(layer) for layer in layers[:3]]
assert pipeline._kl_rollback_host_bytes(layers) == sum(sizes) + sizes[0] + sizes[1]
@pytest.mark.parametrize("enabled,selected", [(False, [0]), (True, [])])
def test_disabled_or_empty_candidates_require_no_snapshot_memory(enabled, selected):
pipeline = _pipeline(selected)
pipeline.use_kl_optimization = enabled
layer = _layer(device="meta")
assert pipeline._kl_rollback_host_bytes(nn.ModuleList([layer])) == 0
def test_selected_meta_candidate_fails_before_snapshot_work():
pipeline = _pipeline([0])
with pytest.raises(RuntimeError, match="cannot snapshot meta tensor.*KL rollback"):
pipeline._kl_rollback_host_bytes(nn.ModuleList([_layer(device="meta")]))
def test_empty_or_non_tensor_candidate_state_fails_explicitly(monkeypatch):
pipeline = _pipeline([0])
layer = nn.Module()
with pytest.raises(RuntimeError, match="non-empty layer snapshot"):
pipeline._kl_rollback_host_bytes(nn.ModuleList([layer]))
monkeypatch.setattr(layer, "state_dict", lambda: {"extra_state": {"metadata": 1}})
with pytest.raises(RuntimeError, match="non-tensor state.*KL rollback"):
pipeline._kl_rollback_host_bytes(nn.ModuleList([layer]))
def test_excise_and_admission_use_the_same_candidate_selector(monkeypatch):
layers = nn.ModuleList([_layer(), _layer(16), _layer(), _layer(24)])
pipeline = _pipeline([0, 1, 2, 3])
pipeline.handle = SimpleNamespace(architecture="llama", config=SimpleNamespace())
pipeline.log = Mock()
pipeline._emit = Mock()
pipeline._excise_inner = Mock()
selected = Mock(return_value=[1, 3])
monkeypatch.setattr(pipeline, "_kl_rollback_candidates", selected)
monkeypatch.setattr(abliterate, "get_layer_modules", lambda _: layers)
capture = Mock(wraps=pipeline._snapshot_layer_state)
monkeypatch.setattr(pipeline, "_snapshot_layer_state", capture)
assert pipeline._kl_rollback_host_bytes(layers) > 0
pipeline._excise()
assert selected.call_count == 2
assert capture.call_args_list == [call(layers[1]), call(layers[3])]
assert list(pipeline._excise_inner.call_args.args[-1]) == [1, 3]
@pytest.mark.parametrize("stage", ["excise", "probe", "verify"])
def test_admission_passes_exact_rollback_host_bytes_only_for_excise(
stage, tmp_path, monkeypatch,
):
pipeline = AbliterationPipeline(
model_name="local/config", output_dir=str(tmp_path), device="cpu", method="basic",
harmful_prompts=["harmful"], harmless_prompts=["harmless"],
use_kl_optimization=True,
)
layers = nn.ModuleList([_layer()])
pipeline.handle = SimpleNamespace(config=SimpleNamespace(), hidden_size=8)
pipeline._strong_layers = [0]
monkeypatch.setattr(abliterate, "get_layer_modules", lambda _: layers)
monkeypatch.setattr(pipeline, "_activation_collection_limits", lambda _: (1, 8, None))
monkeypatch.setattr(pipeline, "_phase_forward_workloads", lambda *_, **__: ([], 37))
monkeypatch.setattr(pipeline, "_excise_parameter_names", lambda **_: [])
admit = Mock()
monkeypatch.setattr(phase_admission, "admit_model_phase", admit)
pipeline._admit_pipeline_phase_impl(stage, max_shard_size=1024**3)
options = admit.call_args.kwargs
rollback_bytes = 3 * _bytes(layers[0]) if stage == "excise" else 0
assert options["host_workspace_bytes"] == 37 + rollback_bytes
assert "rollback_layers" not in options
def test_host_bound_covers_real_old_and_new_modified_snapshot_lifetimes(monkeypatch, cpu_threads):
layers = nn.ModuleList([_layer(), _layer(16)] + [_layer()] * 4)
pipeline = _pipeline(list(range(6)))
pipeline.kl_budget = 0.0
pipeline._quality_metrics = {}
pipeline._kl_contributions = {}
pipeline.log = lambda _: None
pipeline._free_gpu_memory = lambda: None
bound = pipeline._kl_rollback_host_bytes(layers)
weak_tensors, peaks = [], []
capture = pipeline._snapshot_layer_state
def tracked_snapshot(layer):
snapshot = capture(layer)
weak_tensors.extend(weakref.ref(value) for value in snapshot.values())
peaks.append(sum(
value.numel() * value.element_size()
for reference in weak_tensors if (value := reference()) is not None
))
return snapshot
monkeypatch.setattr(pipeline, "_snapshot_layer_state", tracked_snapshot)
with cpu_memory_profile() as profile:
pristine = {}
for index in pipeline._kl_rollback_candidates():
with torch.no_grad():
layers[index].weight.zero_()
pristine[index] = tracked_snapshot(layers[index])
with torch.no_grad():
layers[index].weight.fill_(1.0)
# This regression inventories the snapshot contract. KL forwards have
# their own separately admitted work; a Python-only score avoids adding
# an artificial abs/reduction tensor to that snapshot-only allowance.
pipeline._measure_sequence_token_kl = lambda: sum(
abs(value) for index in pristine
for row in layers[index].weight.tolist() for value in row
)
pipeline._kl_optimize_corrections(layers, 2, pristine)
# At the greedy-loop boundary both modified snapshots of the largest
# candidate coexist with every pristine candidate snapshot.
assert max(peaks) == bound
assert 0 < cpu_peak_bytes(profile) <= bound
assert pipeline._quality_metrics["kl_divergence"] == 0.0