Files
OBLITERATUS/tests/conditional/test_phase_admission_gpu.py
Joseph Magly 50cc0eca8e fix(abliterate): fail closed on unknown ownership; release interrupted results
Codex review r5 for #605 (3 x Medium):
- Ownership covers sparse model components (COO indices/values and the
  compressed layouts' index and value tensors). When the storage of a
  result or a model tensor cannot be identified, the result is cleared
  (fail closed) instead of copied.
- The sweep holds no per-field locals across iterations, and traceback
  frames along the original interrupt's cause/context chain are cleared
  before re-raising and before resource release, so interrupted results
  are collectable before accelerator memory is released. The original
  exception object is still raised.
- A further interrupt during the fail-closed clearing pass retries the
  field instead of aborting the pass; the first interrupt propagates.
Fourteen regressions fail on the previous head; GPU-marked tests cover
CUDA sparse buffer components and CUDA result lifetime at release.
2026-11-23 19:24:19 -05:00

399 lines
16 KiB
Python

"""Optional measured CUDA bounds using random local weights and real phase operations."""
from __future__ import annotations
import logging
from unittest.mock import Mock
import pytest
import torch
from transformers import Qwen2Config, Qwen2ForCausalLM
from obliteratus import phase_admission as admission
from obliteratus.abliterate import AbliterationPipeline
from obliteratus.checkpoint_writer import PIPELINE_EXPORT_MAX_SHARD_SIZE
from obliteratus.models.loader import ModelHandle
from obliteratus.phase_admission import MemoryObservation, PhaseAdmissionError, admit_model_phase
@pytest.fixture
def tiny_cuda_handle(monkeypatch):
if not torch.cuda.is_available():
pytest.skip("requires a CUDA runner; all weights are initialized locally")
monkeypatch.delenv("OBLITERATUS_PHASE_ADMISSION", raising=False)
config = Qwen2Config(
vocab_size=256, hidden_size=128, intermediate_size=384,
num_hidden_layers=2, num_attention_heads=4, num_key_value_heads=2,
max_position_embeddings=128, pad_token_id=0, eos_token_id=255,
)
config._attn_implementation = "sdpa"
with torch.random.fork_rng(devices=[0]):
torch.manual_seed(7)
model = Qwen2ForCausalLM(config).eval().to(device="cuda:0", dtype=torch.float32)
handle = ModelHandle(model, None, config, "local-random-qwen2", "causal_lm")
yield handle
del handle, model
torch.cuda.empty_cache()
@pytest.mark.gpu
def test_direction_extraction_uses_cuda_float32_and_matches_cpu(tiny_cuda_handle, monkeypatch):
from tests.fixtures.refusal_golden import PROMPT_ENDINGS
from tests.fixtures.tiny_offline_model import _fixture_tokenizer
tiny_cuda_handle.tokenizer = _fixture_tokenizer()
calls = []
svd = torch.linalg.svd
def record_svd(matrix, *args, **kwargs):
calls.append((matrix.device.type, matrix.dtype))
return svd(matrix, *args, **kwargs)
monkeypatch.setattr(torch.linalg, "svd", record_svd)
results = []
for policy, expected in (("cpu", "cpu"), ("auto", "cuda")):
pipeline = AbliterationPipeline(
"local", method="advanced", analysis_device=policy,
harmful_prompts=[f"harmful {end}" for end in PROMPT_ENDINGS],
harmless_prompts=[f"harmless {end}" for end in PROMPT_ENDINGS],
)
pipeline.handle = tiny_cuda_handle
pipeline._admitted_collection_limits = (1, 32, None)
pipeline._probe()
pipeline._distill()
assert {row.device.type for rows in pipeline._harmful_acts.values() for row in rows} == {expected}
assert calls and all(call == (expected, torch.float32) for call in calls)
calls.clear()
results.append({
idx: basis.cpu().T @ basis.cpu() for idx, basis in pipeline.refusal_subspaces.items()
})
assert results[0].keys() == results[1].keys() and results[0]
for layer in results[0]:
# Compare projectors because SVD basis-vector signs are arbitrary.
torch.testing.assert_close(results[0][layer], results[1][layer], rtol=2e-3, atol=2e-3)
def _phase_operation(handle, phase, request):
"""Return the unchanged operation and metadata describing its forward calls."""
model = handle.model
batch, tokens, generated = 2, 16, 8
inputs = torch.arange(batch * tokens, device="cuda:0").reshape(batch, tokens) + 1
mask = torch.ones_like(inputs)
workload = admission.ForwardWorkload
options = {"forward_workloads": []}
if phase == "baseline":
options["forward_workloads"] = [workload(
batch, tokens, logits_copies=3, logits_dtype_bytes=4,
)]
def operation():
return model(input_ids=inputs, attention_mask=mask, labels=inputs)
elif phase == "probe":
options["forward_workloads"] = [workload(
batch, tokens, logits_copies=1, logits_dtype_bytes=4,
)]
def operation():
return model(input_ids=inputs, attention_mask=mask)
elif phase == "distill":
# Distillation uses retained CPU activations and must not reserve a
# fictitious GPU forward. Exercise an actual host covariance/SVD.
activations = torch.arange(16 * 128, dtype=torch.float32).reshape(16, 128)
def operation():
centered = activations.double() - activations.double().mean(0)
return torch.linalg.svd(centered, full_matrices=False)
elif phase == "excise":
direction = torch.ones((128, 1), device="cuda:0") / 128**0.5
options["edit_parameter_names"] = (
"model.layers.0.mlp.down_proj.weight", "lm_head.weight",
)
def operation():
projected = AbliterationPipeline._project_out_advanced(
model.model.layers[0].mlp, direction, ["down_proj"], norm_preserve=True,
)
projected += AbliterationPipeline._project_out_advanced(
model, direction, ["lm_head"], norm_preserve=True,
)
return projected
elif phase == "verify":
options["forward_workloads"] = [
workload(batch, tokens, cache_tokens=tokens, logits_tokens=1, logits_copies=3),
workload(batch, 1, cache_tokens=tokens + generated, logits_tokens=1, logits_copies=3),
]
def operation():
return model.generate(
input_ids=inputs, attention_mask=mask, max_new_tokens=generated,
do_sample=False, use_cache=True,
)
else:
# Request storage only after the CUDA fixture has skipped CPU runners.
destination = request.getfixturevalue("tmp_path") / "local-checkpoint"
from obliteratus.models.checkpoint_export import plan_native_export, write_native_checkpoint
plan = plan_native_export(model, max_shard_size=PIPELINE_EXPORT_MAX_SHARD_SIZE)
assert plan is not None
def operation():
destination.mkdir(exist_ok=True)
write_native_checkpoint(model, destination, plan)
return destination
return operation, options
@pytest.mark.gpu
@pytest.mark.parametrize("phase", ["baseline", "probe", "distill", "excise", "verify", "rebirth"])
def test_tiny_cuda_phase_bounds_cover_measured_peak_without_large_overestimate(
tiny_cuda_handle, phase, request,
):
operation, options = _phase_operation(tiny_cuda_handle, phase, request)
# Admit before both the warmup and measured operation. Warming the same
# shapes excludes lazy library initialization from the phase comparison.
admit_model_phase(tiny_cuda_handle, phase, **options)
with torch.no_grad():
warmup = operation()
del warmup
torch.cuda.synchronize(0)
torch.cuda.empty_cache()
report = admit_model_phase(tiny_cuda_handle, phase, **options)
if phase == "rebirth":
# Native export streams tensors to host buffers: its demand is CPU-only,
# so the report has no CUDA entry and the CUDA growth must stay tiny.
assert "cuda:0" not in report.demands
assert report.available_bytes["cpu"] >= report.demands["cpu"].additional_bytes
else:
assert "cuda:0" in report.observations
assert report.available_bytes["cuda:0"] >= report.demands["cuda:0"].additional_bytes
torch.cuda.reset_peak_memory_stats(0)
before = torch.cuda.memory_allocated(0)
with torch.no_grad():
result = operation()
torch.cuda.synchronize(0)
measured = torch.cuda.max_memory_allocated(0) - before
if phase == "rebirth":
assert measured <= 64 * 1024**2, (phase, measured)
assert (result / "model.safetensors").is_file()
assert report.demands["cpu"].additional_bytes > 0
return
estimated = report.demands["cuda:0"].additional_bytes
logging.getLogger("obliteratus.phase_admission").debug(
"CUDA phase=%s required_bytes=%d measured_peak_additional_bytes=%d",
phase, estimated, measured,
)
assert estimated >= measured, (phase, estimated, measured)
# A 64 MiB allowance covers small-tensor allocator rounding and backend
# workspace granularity; the multiplicative term still constrains growth.
assert estimated <= 4 * measured + 64 * 1024**2, (phase, estimated, measured)
if phase in {"baseline", "probe"}:
assert torch.isfinite(result.logits).all()
elif phase == "excise":
assert result == 2
elif phase == "verify":
assert 16 < result.shape[1] <= 24
@pytest.mark.gpu
def test_random_tiny_cuda_model_fits_and_tiny_observation_rejects(tiny_cuda_handle):
def tiny_gpu_memory(device):
if device == "cpu":
return MemoryObservation(device, 1024**3, 1024**3)
return MemoryObservation(device, 1, 1024**3)
with pytest.raises(PhaseAdmissionError) as caught:
admit_model_phase(
tiny_cuda_handle, "probe", batch_size=1, tokens=8, observer=tiny_gpu_memory,
)
assert caught.value.device == "cuda:0"
assert caught.value.shortfall_bytes > 0
@pytest.mark.gpu
def test_failed_run_results_move_off_the_accelerator():
from obliteratus.abliterate import _tensors_to_cpu
from obliteratus.concepts.direction import ConceptDirection
if not torch.cuda.is_available():
pytest.skip("CUDA unavailable")
direction = ConceptDirection({0: torch.ones(8, device="cuda")},
subspaces={0: torch.ones(2, 8, device="cuda")})
moved = _tensors_to_cpu({"d": direction, "s": {0: torch.zeros(4, device="cuda")}})
assert moved["d"].directions[0].device.type == "cpu"
assert moved["d"].subspaces[0].device.type == "cpu"
assert moved["s"][0].device.type == "cpu"
assert torch.equal(moved["d"].directions[0], torch.ones(8))
@pytest.mark.gpu
def test_failed_run_release_moves_lora_and_keeps_aliases():
from collections import namedtuple
from obliteratus.abliterate import AbliterationPipeline
if not torch.cuda.is_available():
pytest.skip("CUDA unavailable")
Pair = namedtuple("Pair", "a b")
pipeline = AbliterationPipeline("local", method="basic", harmful_prompts=["h"],
harmless_prompts=["s"])
shared = {0: torch.ones(8, device="cuda")}
pipeline.concept_directions = shared
pipeline._lora_adapters = {"layer.0": Pair(torch.ones(4, 2, device="cuda"), torch.ones(2, 4, device="cuda"))}
pipeline._extra_alias = shared
pipeline._release_accelerator_results()
assert pipeline.concept_directions[0].device.type == "cpu"
assert pipeline._extra_alias is pipeline.concept_directions
lora = pipeline._lora_adapters["layer.0"]
assert isinstance(lora, Pair) and lora.a.device.type == "cpu" and lora.b.device.type == "cpu"
@pytest.mark.gpu
def test_failed_run_drops_cuda_model_weights_and_unsupported_results(
tiny_cuda_handle, monkeypatch,
):
class Plain:
pass
class Slots:
__slots__ = ("tensor",)
pipeline = AbliterationPipeline("local", method="basic")
pipeline.handle = tiny_cuda_handle
head = tiny_cuda_handle.model.get_output_embeddings().weight
buffer = torch.ones(2, device="cuda")
tiny_cuda_handle.model.register_buffer("cleanup_probe", buffer)
pipeline._preserved_global_writer_weights = frozenset({head})
pipeline._extra_buffer = [buffer]
parameter_view, buffer_view, parameter_data = head[1:, :2], buffer[1:], head.data
pipeline._extra_parameter_view = [parameter_view]
pipeline._extra_buffer_view = [buffer_view]
pipeline._extra_parameter_data = [parameter_data]
detach_head = Mock(wraps=head.detach)
detach_buffer = Mock(wraps=buffer.detach)
monkeypatch.setattr(head, "detach", detach_head)
monkeypatch.setattr(buffer, "detach", detach_buffer)
alias_detaches = []
for alias in (parameter_view, buffer_view, parameter_data):
detach = Mock(wraps=alias.detach)
monkeypatch.setattr(alias, "detach", detach)
alias_detaches.append(detach)
direction = torch.ones(8, device="cuda")
pipeline._extra_plain, pipeline._extra_slots = Plain(), Slots()
pipeline._extra_plain.tensor = pipeline._extra_slots.tensor = direction
pipeline._extra_key = {direction: "value"}
children = [direction]
pipeline._extra_cycle = (children,)
children.append(pipeline._extra_cycle)
pipeline.concept_directions = {0: direction}
pipeline.cleanup_failed_run()
assert not pipeline._preserved_global_writer_weights
assert pipeline._extra_buffer is None
assert pipeline._extra_parameter_view is None
assert pipeline._extra_buffer_view is None
assert pipeline._extra_parameter_data is None
assert pipeline._extra_plain is pipeline._extra_slots is None
assert pipeline._extra_key is pipeline._extra_cycle is None
assert tiny_cuda_handle.model is None
assert pipeline.concept_directions[0].device.type == "cpu"
assert torch.equal(pipeline.concept_directions[0], torch.ones(8))
detach_head.assert_not_called()
detach_buffer.assert_not_called()
for detach in alias_detaches:
detach.assert_not_called()
@pytest.mark.gpu
@pytest.mark.filterwarnings("ignore:Sparse CSR tensor support is in beta state")
def test_failed_run_drops_cuda_sparse_buffer_components(tiny_cuda_handle, monkeypatch):
pipeline = AbliterationPipeline("local", method="basic")
pipeline.handle = tiny_cuda_handle
indices = torch.tensor([[0, 1, 2], [2, 0, 1]], device="cuda")
values = torch.tensor([1.0, 2.0, 3.0], device="cuda")
coo = torch.sparse_coo_tensor(indices, values, (3, 3), check_invariants=True).coalesce()
csr = coo.to_sparse_csr()
tiny_cuda_handle.model.register_buffer("cleanup_sparse_coo", coo)
tiny_cuda_handle.model.register_buffer("cleanup_sparse_csr", csr)
aliases = {
"_extra_coo_values": coo._values(),
"_extra_coo_indices": coo._indices(),
"_extra_coo_rewrapped": torch.sparse_coo_tensor(
coo._indices(), coo._values(), (3, 3), check_invariants=True,
),
"_extra_csr_values": csr.values(),
}
detaches = []
for name, alias in aliases.items():
detach = Mock(wraps=alias.detach)
monkeypatch.setattr(alias, "detach", detach)
detaches.append(detach)
setattr(pipeline, name, [alias])
direction = torch.ones(8, device="cuda")
pipeline.concept_directions = {0: direction}
pipeline.cleanup_failed_run()
for name in aliases:
assert getattr(pipeline, name) is None
for detach in detaches:
detach.assert_not_called()
assert pipeline.concept_directions[0].device.type == "cpu"
assert torch.equal(pipeline.concept_directions[0], torch.ones(8))
@pytest.mark.gpu
@pytest.mark.parametrize("source", ["transfer", "logging"])
def test_failed_run_interrupt_releases_cuda_results_before_memory_release(
source, tiny_cuda_handle, monkeypatch,
):
import weakref
pipeline = AbliterationPipeline("local", method="basic")
pipeline.handle = tiny_cuda_handle
refs, alive_at_release = [], []
release = pipeline._free_gpu_memory
def free_memory():
# No gc.collect() first: the CUDA results must already be unreferenced.
alive_at_release.append([ref() is not None for ref in refs])
release()
monkeypatch.setattr(pipeline, "_free_gpu_memory", Mock(side_effect=free_memory))
# Fields cleanup adds itself go first, so the CUDA result is the last field swept.
probe = AbliterationPipeline("local", method="basic")
probe._free_gpu_memory = lambda: None
before = set(vars(probe))
probe.cleanup_failed_run()
for name in vars(probe):
if name not in before and name not in vars(pipeline):
setattr(pipeline, name, getattr(probe, name))
first = torch.zeros(1024, device="cuda")
later = torch.ones(1024, device="cuda")
pipeline._extra_result = [first]
pipeline._extra_later = {"later": later}
original = KeyboardInterrupt("interrupt cleanup")
if source == "transfer":
first.detach = Mock(side_effect=original)
else:
first.detach = Mock(side_effect=RuntimeError("transfer failed"))
monkeypatch.setattr(pipeline, "_on_log", Mock(side_effect=[original, None]))
refs.extend((weakref.ref(first), weakref.ref(later)))
del first, later
with pytest.raises(KeyboardInterrupt) as caught:
pipeline.cleanup_failed_run()
assert caught.value is original
assert alive_at_release == [[False, False]]
assert [ref() is not None for ref in refs] == [False, False]
assert pipeline._extra_result is None and pipeline._extra_later is None