Files
OBLITERATUS/tests/test_phase_lora_admission.py
Joseph MaglyandClaude Opus 5.5 41389fda24 Remove transient allocations added by storage-dtype validation
Finiteness checks reduce to scalars, the LoRA delta buffer is reused, and
norm restoration validates scaled extrema before rescaling in place, so the
admitted memory bounds hold without changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 00:20:50 -04:00

240 lines
12 KiB
Python

"""LoRA admission follows its construct/merge path, including measured CPU peaks."""
from unittest.mock import Mock
import pytest
import torch
from torch._subclasses.fake_tensor import FakeTensorMode
from transformers import LlamaConfig, LlamaForCausalLM
from obliteratus import lora_ablation, phase_admission
from obliteratus.abliterate import AbliterationPipeline
from obliteratus.models.loader import ModelHandle
from obliteratus.phase_admission import MemoryObservation, PhaseAdmissionError, model_phase_demand
from tests.test_phase_edit_admission import _LiveTensorBytes
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
pytestmark = pytest.mark.cpu
MIB = 1024**2
@pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32, torch.float64])
@pytest.mark.parametrize("undo", [False, True])
def test_lora_merge_validation_reuses_delta_without_mutating_inputs(dtype, undo, cpu_threads):
weight = torch.ones(64, 128, dtype=dtype)
b = torch.full((64, 1), 0.5, dtype=torch.float16)
a = torch.full((1, 128), 0.25, dtype=torch.float16)
tracker = _LiveTensorBytes([weight, b, a])
with torch.no_grad(), tracker:
updated = lora_ablation._validate_lora_merge(weight, b, a, "writer", undo=undo)
# The half product and storage-dtype cast may overlap. The checked merged
# value must reuse that workspace, even for a double-precision destination.
assert tracker.peak <= weight.numel() * (2 + weight.element_size()) + 8
assert torch.equal(updated, torch.full_like(weight, 0.875 if undo else 1.125))
assert torch.equal(weight, torch.ones_like(weight))
assert torch.equal(b, torch.full_like(b, 0.5))
assert torch.equal(a, torch.full_like(a, 0.25))
def _pipeline(tmp_path, *, hidden=32, dtype=torch.bfloat16, rank=1, layers=1):
config = LlamaConfig(
vocab_size=32, hidden_size=hidden, intermediate_size=hidden * 4,
num_hidden_layers=layers, num_attention_heads=8, num_key_value_heads=8,
attn_implementation="sdpa",
)
model = LlamaForCausalLM(config).eval().to(dtype)
pipeline = AbliterationPipeline(
model_name="local-random-llama", output_dir=str(tmp_path), method="basic",
device="cpu", use_lora_ablation=True, lora_rank=rank, use_kl_optimization=False,
harmful_prompts=["harmful"], harmless_prompts=["harmless"],
on_log=lambda _: None, on_stage=lambda _: None,
)
pipeline.handle = ModelHandle(model, Mock(), config, "local-random-llama", "causal_lm")
pipeline._strong_layers = list(range(layers))
direction = torch.arange(1, hidden + 1, dtype=torch.float32)
direction /= direction.norm()
pipeline.refusal_subspaces = {idx: direction[None] for idx in range(layers)}
return pipeline
def _demand(pipeline, *, mode=None):
return model_phase_demand(
pipeline.handle, "excise", forward_workloads=[],
edit_parameter_names=pipeline._excise_parameter_names(),
edit_adapter_ranks=pipeline._lora_adapter_ranks(), mode=mode,
).devices["cpu"].additional_bytes
def test_real_rank_one_lora_peak_fits_110_mib_headroom(tmp_path, monkeypatch, cpu_threads):
# Keep the original 110 MiB admission regression as weight-free arithmetic.
with torch.device("meta"):
original = _pipeline(tmp_path, hidden=1024)
# Cast on meta first: Module.to cannot swap FakeTensors with live weakrefs.
with FakeTensorMode():
for module in original.handle.model.modules():
for name, parameter in list(module._parameters.items()):
if parameter is not None:
module._parameters[name] = torch.nn.Parameter(
torch.empty(parameter.shape, dtype=parameter.dtype, device="cpu"),
requires_grad=parameter.requires_grad,
)
for name, buffer in list(module._buffers.items()):
if buffer is not None:
module._buffers[name] = torch.empty(buffer.shape, dtype=buffer.dtype, device="cpu")
original_bound = _demand(original)
assert 32 * MIB <= original_bound <= 46 * MIB
assert original_bound <= 32 * MIB * 1.5
pipeline = _pipeline(tmp_path, hidden=256)
assert len(pipeline._lora_adapter_ranks()) == 7
bound = _demand(pipeline)
monkeypatch.setattr(phase_admission, "observe_memory",
lambda d: MemoryObservation(d, 110 * MIB, 110 * MIB))
original._admit_pipeline_phase("excise") # 46 MiB after the normal 64 MiB reserve.
pipeline._admit_pipeline_phase("excise")
ordinary_projection = Mock(side_effect=AssertionError("ordinary projection ran"))
monkeypatch.setattr(pipeline, "_project_out_advanced", ordinary_projection)
with cpu_memory_profile() as profile:
pipeline._excise()
peak = cpu_peak_bytes(profile)
assert 2 * MIB <= peak <= bound <= 46 * MIB
assert bound <= peak * 1.5
assert len(pipeline._lora_adapters) == 7
ordinary_projection.assert_not_called()
@pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16, torch.float32, torch.float64])
def test_lora_construction_conversion_and_merge_peaks_fit_bound(tmp_path, dtype, cpu_threads):
pipeline = _pipeline(tmp_path, hidden=64, dtype=dtype, rank=8, layers=2)
# Actual subspaces have rank one despite the configured upper limit of eight.
assert set(pipeline._lora_adapter_ranks().values()) == {1}
bound = _demand(pipeline)
with cpu_memory_profile() as profile:
pipeline._excise()
assert 0 < cpu_peak_bytes(profile) <= bound
# The GitHub runner's low-precision GEMM used more scratch than this
# workstation's AVX2 path. Cover that observed peak on every host, too.
if dtype in {torch.float16, torch.bfloat16, torch.float32}:
assert bound >= (196096 if dtype == torch.float32 else 163328)
retained = sum(t.numel() * t.element_size() for pair in pipeline._lora_adapters.values()
for t in pair)
assert retained == sum(2 * sum(target.module.weight.shape) * target.rank
for target in lora_ablation.lora_projection_targets(pipeline, 8))
def test_lora_admission_and_execution_share_target_selection(tmp_path, monkeypatch):
pipeline = _pipeline(tmp_path)
selector = Mock(wraps=lora_ablation.lora_projection_targets)
monkeypatch.setattr(lora_ablation, "lora_projection_targets", selector)
names = pipeline._excise_parameter_names()
ranks = pipeline._lora_adapter_ranks()
adapters = lora_ablation.compute_lora_adapters(pipeline)
assert selector.call_count == 3
assert set(names) == set(ranks) == {name + ".weight" for name in adapters}
assert all(call.args == (pipeline, pipeline.lora_rank) for call in selector.call_args_list)
def test_empty_adapter_selection_models_and_executes_ordinary_fallback(tmp_path, monkeypatch):
pipeline = _pipeline(tmp_path)
monkeypatch.setattr(lora_ablation, "lora_projection_targets", lambda *_: ())
assert pipeline._lora_adapter_ranks() == {}
selected = pipeline._excise_parameter_names()
assert "lm_head.weight" in selected
fallback_bound = _demand(pipeline)
pipeline.use_lora_ablation = False
assert pipeline._excise_parameter_names() == selected
assert _demand(pipeline) == fallback_bound
pipeline.use_lora_ablation = True
ordinary = Mock(wraps=pipeline._project_out_advanced)
monkeypatch.setattr(pipeline, "_project_out_advanced", ordinary)
pipeline._excise()
assert ordinary.called
assert pipeline._lora_adapters == {}
def test_lora_rejection_precedes_any_construction_or_merge(tmp_path, monkeypatch):
pipeline = _pipeline(tmp_path)
construction = Mock(side_effect=AssertionError("LoRA started before admission"))
monkeypatch.setattr(lora_ablation, "compute_lora_adapters", construction)
monkeypatch.setattr(phase_admission, "observe_memory", lambda d: MemoryObservation(d, 0, 0))
pipeline._admit_pipeline_phase("excise") # Default warns about estimate-only LoRA.
construction.assert_not_called()
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "strict")
with pytest.raises(PhaseAdmissionError, match="excise.*cpu"):
pipeline._admit_pipeline_phase("excise")
pipeline._excise()
construction.assert_not_called()
def test_unmodelled_lora_storage_is_explicitly_rejected(tmp_path):
pipeline = _pipeline(tmp_path)
pipeline.handle.model.model.layers[0].self_attn.q_proj.weight = torch.nn.Parameter(
torch.ones(32, 32, dtype=torch.uint8), requires_grad=False,
)
report = phase_admission.admit_model_phase(
pipeline.handle, "excise", forward_workloads=[],
edit_parameter_names=pipeline._excise_parameter_names(),
edit_adapter_ranks=pipeline._lora_adapter_ranks(), observer=lambda d: None,
)
assert report.warnings and any(c.bytes is None for c in report.components)
with pytest.raises(
PhaseAdmissionError, match="LoRA construct/merge.*OBLITERATUS_PHASE_ADMISSION=off",
):
_demand(pipeline, mode="strict")
def test_lora_shared_weights_keep_distinct_adapter_destinations(tmp_path, cpu_threads):
pipeline = _pipeline(tmp_path)
attention = pipeline.handle.model.model.layers[0].self_attn
attention.k_proj.weight = attention.q_proj.weight
selected = pipeline._lora_adapter_ranks()
assert "model.layers.0.self_attn.k_proj.weight" in selected
assert "model.layers.0.self_attn.q_proj.weight" in selected
bound = _demand(pipeline)
with cpu_memory_profile() as profile:
pipeline._excise()
assert len(pipeline._lora_adapters) == 7
assert 0 < cpu_peak_bytes(profile) <= bound
def test_lora_conversions_follow_execution_order_instead_of_sum_of_largest(tmp_path, cpu_threads):
from types import SimpleNamespace
model = torch.nn.Module()
model.first = torch.nn.Linear(64, 64, bias=False, dtype=torch.bfloat16)
model.middle = torch.nn.Linear(8, 8, bias=False, dtype=torch.bfloat16)
model.last = torch.nn.Linear(64, 64, bias=False, dtype=torch.bfloat16)
names = tuple(dict(model.named_parameters()))
bound = phase_admission._edit_workspace(SimpleNamespace(model=model), names, adapter_rank=1)
# Each CPU allocation is rounded to a 64-byte cache line. The small pair
# therefore occupies two lines instead of its nominal 32 bytes.
retained = 2 * (128 + 64 + 128)
# The two large FP32 conversions never overlap: the middle weight breaks
# their lifetime. The large merge similarly follows the small delta.
construction = 4 * (64 * 64 + 8 * 8) + 16 * 128
merge = 2 * 8 * 8 + (2 + 2 + 4) * 64 * 64 + cpu_threads * 2 * 64 * 64 * 4
assert bound["cpu"] == retained + max(construction, merge)
def test_cpu_lora_accounts_for_small_allocation_granularity(cpu_threads):
from types import SimpleNamespace
model = torch.nn.Linear(9, 9, bias=False, dtype=torch.bfloat16)
bound = phase_admission._edit_workspace(SimpleNamespace(model=model), ("weight",), adapter_rank=1)
# Two 18-byte adapters round to 64 each; the FP16 result and cast each
# round 162 to 192, and the FP32 accumulator rounds 324 to 384 bytes.
assert bound["cpu"] == 2 * 64 + 2 * 192 + 384 + cpu_threads * 32768
@pytest.mark.parametrize("rank", [2, 8])
def test_actual_multidirection_lora_vectors_and_pairs_fit_measured_peak(tmp_path, rank, cpu_threads):
pipeline = _pipeline(tmp_path, hidden=64, rank=rank)
pipeline.refusal_subspaces = {0: torch.eye(64)[:rank]}
assert set(pipeline._lora_adapter_ranks().values()) == {rank}
bound = _demand(pipeline)
with cpu_memory_profile() as profile:
pipeline._excise()
assert 0 < cpu_peak_bytes(profile) <= bound
assert bound >= 163328 # Observed rank-two GitHub CPU runner peak.
assert all(b.shape[1] == a.shape[0] == rank for b, a in pipeline._lora_adapters.values())