mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
397 lines
19 KiB
Python
397 lines
19 KiB
Python
"""VALIDATE admits concatenation, FP64 caches, calibration and recipe refits."""
|
|
|
|
from dataclasses import replace
|
|
import tracemalloc
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
import torch
|
|
from torch._subclasses.fake_tensor import FakeTensorMode
|
|
from transformers import LlamaConfig, LlamaForCausalLM
|
|
|
|
from obliteratus import phase_admission
|
|
from obliteratus.abliterate import AbliterationPipeline, PipelineFailure
|
|
from obliteratus.concepts import ConceptDirection, ExtractionRecipe
|
|
from obliteratus.concepts.gate import (
|
|
GateCacheInputs, select_gate_cache_inputs, validate_direction,
|
|
)
|
|
from obliteratus.concepts.gate_memory import estimate_gate_host_bytes
|
|
from obliteratus.models.loader import ModelHandle
|
|
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
|
|
|
|
|
|
MIB = 1024**2
|
|
GIB = 1024**3
|
|
pytestmark = pytest.mark.cpu
|
|
|
|
|
|
def _inputs(*, hidden=32, layers=2, count=16, dtype=torch.float32, auxiliary=False):
|
|
generator = torch.Generator().manual_seed(245)
|
|
|
|
def group(rows=count):
|
|
return {index: [torch.randn(rows, hidden, dtype=dtype, generator=generator)]
|
|
for index in range(layers)}
|
|
|
|
inputs = GateCacheInputs(group(), {"neutral": group()})
|
|
inputs.texts = (
|
|
[f"positive example {index}" for index in range(count)],
|
|
{"neutral": [f"neutral sample {index}" for index in range(count)]},
|
|
)
|
|
if auxiliary:
|
|
inputs.controls["other"] = group(count + 3)
|
|
inputs.texts[1]["other"] = [f"other text {index}" for index in range(count + 3)]
|
|
|
|
def pair():
|
|
return group(), {"neutral": group(), "other": group(count + 3)}
|
|
|
|
inputs.readouts = {"final_token": pair(), "mean_pool": pair()}
|
|
inputs.masked = pair()
|
|
inputs.neighbours = {f"neighbour-{i}": group(count + i) for i in range(4)}
|
|
direction = ConceptDirection(
|
|
{index: torch.randn(hidden, dtype=dtype, generator=generator) for index in range(layers)},
|
|
concept_id="user/test", seed=245,
|
|
)
|
|
return direction, inputs
|
|
|
|
|
|
def _resident(direction, inputs):
|
|
groups = [inputs.positive, *inputs.controls.values(), *inputs.neighbours.values()]
|
|
pairs = [*inputs.readouts.values(), *([inputs.masked] if inputs.masked else [])]
|
|
for positive, controls in pairs:
|
|
groups.extend([positive, *controls.values()])
|
|
return [*direction.directions.values(),
|
|
*(tensor for group in groups for chunks in group.values() for tensor in chunks)]
|
|
|
|
|
|
def _large_metadata_inputs():
|
|
with FakeTensorMode():
|
|
positive = {index: [torch.empty(100, 8192)] for index in range(80)}
|
|
controls = {"harmless": {index: [torch.empty(100, 8192)] for index in range(80)}}
|
|
direction = SimpleNamespace(
|
|
directions={index: torch.ones(8192) for index in range(80)},
|
|
recipe=ExtractionRecipe(), concept_id="refusal", seed=245,
|
|
)
|
|
texts = (["positive"] * 100, {"harmless": ["control"] * 100})
|
|
return direction, GateCacheInputs(positive, controls, texts=texts)
|
|
|
|
|
|
def test_large_gate_counts_1500_mib_concat_and_fp64_copies_before_workspace():
|
|
direction, inputs = _large_metadata_inputs()
|
|
bound = estimate_gate_host_bytes(direction, inputs)
|
|
assert 80 * 200 * 8192 * (4 + 8) == 1500 * MIB
|
|
assert 1500 * MIB < bound < 1700 * MIB
|
|
|
|
|
|
def test_one_gib_host_headroom_rejects_before_gate_materialization(tmp_path, monkeypatch, caplog):
|
|
direction, inputs = _large_metadata_inputs()
|
|
config = LlamaConfig(
|
|
vocab_size=32, hidden_size=8192, intermediate_size=16384,
|
|
num_hidden_layers=80, num_attention_heads=64, num_key_value_heads=8,
|
|
)
|
|
config._attn_implementation = "eager"
|
|
with FakeTensorMode():
|
|
model = LlamaForCausalLM(config)
|
|
pipeline = AbliterationPipeline(
|
|
"local-random-model", output_dir=str(tmp_path), device="cpu", method="basic",
|
|
gate_mode="report", harmful_prompts=inputs.texts[0],
|
|
harmless_prompts=inputs.texts[1]["harmless"],
|
|
)
|
|
pipeline.handle = ModelHandle(model, None, config, "local-random-model", "causal_lm")
|
|
pipeline._concept_direction = direction
|
|
pipeline.concept_directions = direction.directions
|
|
pipeline._harmful_acts = inputs.positive
|
|
pipeline._harmless_acts = inputs.controls["harmless"]
|
|
monkeypatch.delenv("OBLITERATUS_PHASE_ADMISSION", raising=False)
|
|
monkeypatch.setattr(pipeline, "_activation_collection_limits", lambda _: (1, 8, None))
|
|
monkeypatch.setattr(pipeline, "_phase_forward_workloads", lambda *_, **__: ([], 0))
|
|
for name in ("_summon", "_capture_stock_baseline", "_validate_architecture_surgery_support",
|
|
"_probe", "_distill", "_free_gpu_memory"):
|
|
monkeypatch.setattr(pipeline, name, Mock())
|
|
original_admit = pipeline._admit_pipeline_phase
|
|
monkeypatch.setattr(pipeline, "_admit_pipeline_phase",
|
|
lambda stage: original_admit(stage) if stage == "validate" else None)
|
|
work = Mock(side_effect=AssertionError("gate work started before admission"))
|
|
monkeypatch.setattr(pipeline, "_validate_concept_gate", work)
|
|
materialize = Mock(side_effect=AssertionError("cache concatenated before admission"))
|
|
monkeypatch.setattr(GateCacheInputs, "materialize", materialize)
|
|
events = []
|
|
monkeypatch.setattr(pipeline, "_emit", lambda *args, **kwargs: events.append((args, kwargs)))
|
|
monkeypatch.setattr(phase_admission, "observe_memory", lambda device:
|
|
phase_admission.MemoryObservation(
|
|
device, GIB + phase_admission.DEFAULT_RESERVE_BYTES, 64 * GIB,
|
|
))
|
|
original_admit("validate")
|
|
assert "estimated shortfall" in caplog.text and "Proceeding in default mode" in caplog.text
|
|
work.assert_not_called()
|
|
materialize.assert_not_called()
|
|
monkeypatch.setenv("OBLITERATUS_PHASE_ADMISSION", "strict")
|
|
with pytest.raises(PipelineFailure) as caught:
|
|
pipeline._run_pipeline()
|
|
assert caught.value.stage == "validate"
|
|
error = caught.value.__cause__
|
|
assert isinstance(error, phase_admission.PhaseAdmissionError)
|
|
assert error.device == "cpu" and error.available_bytes == GIB
|
|
assert error.required_bytes > 1500 * MIB
|
|
assert "shortfall" in str(error) and "GiB" in str(error)
|
|
assert events[-1][0][:2] == ("validate", "error")
|
|
work.assert_not_called()
|
|
materialize.assert_not_called()
|
|
|
|
|
|
@pytest.mark.parametrize("method", ["diff_means", "svd", "fisher_lda", "leace_exact"])
|
|
@pytest.mark.parametrize("auxiliary", [False, True])
|
|
def test_real_gate_tensor_peak_fits_method_bound(method, auxiliary, cpu_threads):
|
|
# Two layers and two folds retain cross-layer caches and refit lifetimes.
|
|
# Four samples still exercise indexed train/test rows and lexical ridge.
|
|
direction, inputs = _inputs(hidden=12, count=4, auxiliary=auxiliary)
|
|
direction.recipe = replace(
|
|
direction.recipe, method=method, layer_select={"strategy": "cv_auc", "folds": 2},
|
|
)
|
|
bound = estimate_gate_host_bytes(direction, inputs, random_directions=2)
|
|
tracker = _LiveTensorBytes(_resident(direction, inputs))
|
|
with cpu_memory_profile() as profile, torch.no_grad(), tracker:
|
|
cache = inputs.materialize()
|
|
report = validate_direction(direction, cache, random_directions=2)
|
|
assert report["random_directions"] == 2
|
|
assert report["folds"] == 2
|
|
assert tracker.peak <= bound
|
|
assert 0 < cpu_peak_bytes(profile) <= bound
|
|
# Extraction tightness excludes the independently bounded lexical probe and
|
|
# Python feature metadata, which are not measured by the tensor tracker.
|
|
tensor_bound = estimate_gate_host_bytes(direction, replace(inputs, texts=None), random_directions=2)
|
|
assert tensor_bound <= 5 * tracker.peak
|
|
|
|
|
|
@pytest.mark.parametrize("method", ["diff_means", "svd", "fisher_lda", "leace_exact"])
|
|
def test_denoised_refit_retains_pcs_and_fits_real_peak(method, cpu_threads):
|
|
direction, inputs = _inputs(hidden=12, count=4, layers=2, auxiliary=True)
|
|
direction.recipe = replace(
|
|
direction.recipe, method=method, denoise={"control_pc_variance": 0.5},
|
|
layer_select={"strategy": "cv_auc", "folds": 2},
|
|
)
|
|
bound = estimate_gate_host_bytes(direction, inputs, random_directions=2)
|
|
tracker = _LiveTensorBytes(_resident(direction, inputs))
|
|
with cpu_memory_profile() as profile, torch.no_grad(), tracker:
|
|
cache = inputs.materialize()
|
|
report = validate_direction(direction, cache, random_directions=2)
|
|
assert report["folds"] == 2
|
|
assert tracker.peak <= bound
|
|
tensor_bound = estimate_gate_host_bytes(direction, replace(inputs, texts=None), random_directions=2)
|
|
assert tensor_bound <= 5 * tracker.peak
|
|
assert 0 < cpu_peak_bytes(profile) <= bound
|
|
|
|
|
|
@pytest.mark.parametrize("method", ["diff_means", "svd", "fisher_lda", "leace_exact"])
|
|
def test_recipe_specific_workspace_covers_wide_hidden_refit_without_lexical_probe(method, cpu_threads):
|
|
hidden = 32 # Wider than either training group, including the thin SVD.
|
|
direction, inputs = _inputs(hidden=hidden, count=4, layers=2)
|
|
inputs.texts = None
|
|
direction.recipe = replace(
|
|
direction.recipe, layer_select={"strategy": "cv_auc", "folds": 2},
|
|
)
|
|
difference_bound = estimate_gate_host_bytes(direction, inputs, random_directions=2)
|
|
direction.recipe = replace(direction.recipe, method=method)
|
|
bound = estimate_gate_host_bytes(direction, inputs, random_directions=2)
|
|
tracker = _LiveTensorBytes(_resident(direction, inputs))
|
|
with cpu_memory_profile() as profile, torch.no_grad(), tracker:
|
|
validate_direction(direction, inputs.materialize(), random_directions=2)
|
|
peak = cpu_peak_bytes(profile)
|
|
assert tracker.peak <= bound <= 5 * tracker.peak
|
|
assert 0 < peak <= bound
|
|
if method in {"fisher_lda", "leace_exact"}:
|
|
assert bound >= difference_bound + 5 * hidden**2 * 8
|
|
# A generic difference-of-means allowance cannot cover dense refits.
|
|
assert peak > difference_bound
|
|
|
|
|
|
def test_shared_group_selection_preserves_control_slices_and_auxiliary_eligibility():
|
|
def chunks(count, offset=0):
|
|
return {0: [torch.full((1, 3), float(index + offset)) for index in range(count)]}
|
|
|
|
positive, harmless = chunks(3), chunks(5, 10)
|
|
controls = {"a": {0: harmless[0][:2]}, "b": {0: harmless[0][2:]}}
|
|
texts = (["p"] * 3, {"a": ["a"] * 2, "b": ["b"] * 3})
|
|
readouts = {"harmful": {"final_token": positive, "mean_pool": positive, "masked": positive},
|
|
"harmless": {"final_token": harmless, "masked": harmless}}
|
|
inputs = select_gate_cache_inputs(
|
|
positive, controls, harmless=harmless, refusal=False, readouts=readouts,
|
|
neighbours={"neighbour": chunks(7)}, texts=texts, top_tokens=[],
|
|
)
|
|
assert set(inputs.readouts) == {"final_token"}
|
|
assert inputs.masked is not None
|
|
cache = inputs.materialize()
|
|
assert cache.controls["a"][0].shape == (2, 3)
|
|
assert cache.controls["b"][0].shape == (3, 3)
|
|
assert torch.equal(cache.masked[1]["b"][0], torch.cat(harmless[0][2:]))
|
|
assert cache.neighbours["neighbour"][0].shape == (7, 3)
|
|
direction = ConceptDirection({0: torch.ones(3)}, concept_id="user/test")
|
|
assert estimate_gate_host_bytes(direction, inputs) > 0
|
|
|
|
|
|
def test_fourth_neighbour_only_charges_its_actual_concatenation():
|
|
direction, inputs = _inputs(auxiliary=True)
|
|
full = estimate_gate_host_bytes(direction, inputs)
|
|
unused = inputs.neighbours.pop("neighbour-3")
|
|
raw = sum(value.numel() * value.element_size()
|
|
for values in unused.values() for value in values)
|
|
assert full - estimate_gate_host_bytes(direction, inputs) == raw
|
|
|
|
|
|
@pytest.mark.parametrize("rank", [1, 2])
|
|
def test_metadata_estimator_does_not_run_extraction_scans_or_factorizations(monkeypatch, rank):
|
|
class MetadataText(str):
|
|
def casefold(self):
|
|
raise AssertionError("features constructed during gate admission")
|
|
|
|
direction, inputs = _inputs(auxiliary=True)
|
|
inputs.texts = ([MetadataText(prompt) for prompt in inputs.texts[0]],
|
|
{name: [MetadataText(prompt) for prompt in prompts]
|
|
for name, prompts in inputs.texts[1].items()})
|
|
direction.recipe = replace(direction.recipe, method="leace_exact", n_directions=rank)
|
|
forbidden = Mock(side_effect=AssertionError("matrix work during gate admission"))
|
|
for name in ("svd", "solve", "eigh"):
|
|
monkeypatch.setattr(torch.linalg, name, forbidden)
|
|
monkeypatch.setattr(torch, "isfinite", forbidden)
|
|
monkeypatch.setattr("obliteratus.concepts.gate.extract_directions", forbidden)
|
|
monkeypatch.setattr("obliteratus.concepts.gate.extract_layer", forbidden)
|
|
monkeypatch.setattr("obliteratus.concepts.gate._lexical_features", forbidden)
|
|
monkeypatch.setattr("obliteratus.concepts.gate._lexical_vocabulary", forbidden)
|
|
assert estimate_gate_host_bytes(direction, inputs) > 0
|
|
forbidden.assert_not_called()
|
|
|
|
|
|
@pytest.mark.parametrize("kind", ["words", "trigrams", "unicode"])
|
|
def test_lexical_metadata_bound_covers_full_feature_sets_and_vocabulary(kind):
|
|
from obliteratus.concepts.gate import _fold_indices, _lexical_features, _lexical_vocabulary
|
|
from obliteratus.concepts.gate_memory import _lexical_workspace, lexical_metadata_bytes
|
|
|
|
sizes = {"positive": 4, "control:neutral": 4}
|
|
|
|
def prompt(row):
|
|
if kind == "words":
|
|
return " ".join(f"word{row}_{index}" for index in range(2048))
|
|
alphabet = "".join(chr(0x4E00 + row * 2048 + index) for index in range(2048))
|
|
return alphabet if kind == "trigrams" else "\ufb03\U0001f642".join(alphabet)
|
|
|
|
texts = ([prompt(row) for row in range(4)],
|
|
{"neutral": [prompt(row) for row in range(4, 8)]})
|
|
splits = _fold_indices(sizes, 2, 245)
|
|
characters = sum(map(len, texts[0])) + sum(map(len, texts[1]["neutral"]))
|
|
bound = lexical_metadata_bytes(8, characters)
|
|
tracemalloc.start()
|
|
try:
|
|
features = _lexical_features(texts, sizes)
|
|
rows, vocabulary = _lexical_vocabulary(features, splits[0][0])
|
|
_, peak = tracemalloc.get_traced_memory()
|
|
finally:
|
|
tracemalloc.stop()
|
|
assert len(rows) == 4 and len(vocabulary) == 1024
|
|
assert len(set().union(*(values for group in features.values() for values in group))) > 1024
|
|
assert 0 < peak <= bound
|
|
assert _lexical_workspace(texts, sizes, splits) >= bound
|
|
# The runtime still builds features when no CV folds can be formed.
|
|
assert _lexical_workspace(texts, sizes, []) >= peak
|
|
|
|
|
|
@pytest.mark.parametrize("problem", ["counts", "names", "types"])
|
|
def test_gate_admission_validates_text_alignment_without_features(problem):
|
|
direction, inputs = _inputs()
|
|
if problem == "counts":
|
|
inputs.texts[0].pop()
|
|
elif problem == "names":
|
|
inputs.texts[1]["other"] = inputs.texts[1].pop("neutral")
|
|
else:
|
|
inputs.texts[0][0] = 1
|
|
with pytest.raises(ValueError, match="Prompt texts must match"):
|
|
estimate_gate_host_bytes(direction, inputs)
|
|
|
|
|
|
@pytest.mark.parametrize("pooling", ["mean_of_means", "concat"])
|
|
def test_multiclass_leace_refit_and_auxiliary_geometry_fit_host_bound(pooling, cpu_threads):
|
|
direction, inputs = _inputs(hidden=32, layers=2, count=8, auxiliary=True)
|
|
inputs.texts = None
|
|
direction.recipe = replace(
|
|
direction.recipe, method="leace_exact", pool_controls=pooling,
|
|
layer_select={"strategy": "cv_auc", "folds": 2},
|
|
)
|
|
binary_bound = estimate_gate_host_bytes(direction, inputs, random_directions=2)
|
|
direction.recipe = replace(direction.recipe, n_directions=2)
|
|
bound = estimate_gate_host_bytes(direction, inputs, random_directions=2)
|
|
assert bound > binary_bound
|
|
tracker = _LiveTensorBytes(_resident(direction, inputs))
|
|
with cpu_memory_profile() as profile, torch.no_grad(), tracker:
|
|
report = validate_direction(direction, inputs.materialize(), random_directions=2)
|
|
assert report["cv_recipe"]["n_directions"] == 2
|
|
assert report["auxiliary_calibration"]["n_directions"] == 1
|
|
assert tracker.peak <= bound
|
|
assert 0 < cpu_peak_bytes(profile) <= bound
|
|
|
|
|
|
def test_admission_preserves_rng_and_full_gate_report(cpu_threads):
|
|
direction, inputs = _inputs(hidden=12, count=4, auxiliary=True)
|
|
direction.recipe = replace(
|
|
direction.recipe, layer_select={"strategy": "cv_auc", "folds": 2},
|
|
)
|
|
before = validate_direction(direction, inputs.materialize(), random_directions=2)
|
|
rng = torch.random.get_rng_state().clone()
|
|
estimate_gate_host_bytes(direction, inputs, random_directions=2)
|
|
assert torch.equal(torch.random.get_rng_state(), rng)
|
|
after = validate_direction(direction, inputs.materialize(), random_directions=2)
|
|
assert before == after
|
|
|
|
|
|
def test_fp64_primary_cache_does_not_charge_a_second_conversion():
|
|
fp32, inputs32 = _inputs(dtype=torch.float32)
|
|
fp64, inputs64 = _inputs(dtype=torch.float64)
|
|
# FP32 concat + FP64 conversion = 12 bytes/element; FP64 concat alone = 8.
|
|
difference = estimate_gate_host_bytes(fp32, inputs32) - estimate_gate_host_bytes(fp64, inputs64)
|
|
elements = sum(value.numel() for group in [inputs32.positive, *inputs32.controls.values()]
|
|
for chunks in group.values() for value in chunks)
|
|
vector_conversion = sum(value.numel() * 8 for value in fp32.directions.values())
|
|
assert difference == 4 * elements + vector_conversion
|
|
|
|
|
|
def test_refusal_advisory_refit_uses_shared_diff_means_override():
|
|
direction, inputs = _inputs()
|
|
direction.concept_id = "refusal"
|
|
original = estimate_gate_host_bytes(direction, inputs)
|
|
direction.recipe = replace(direction.recipe, method="fisher_lda",
|
|
denoise={"control_pc_variance": 1.0})
|
|
assert estimate_gate_host_bytes(direction, inputs) == original
|
|
|
|
|
|
@pytest.mark.parametrize("eligible", [False, True])
|
|
def test_off_and_missing_legacy_cache_omit_gate_work(tmp_path, eligible):
|
|
pipeline = AbliterationPipeline(
|
|
"local", output_dir=str(tmp_path), method="basic", gate_mode="report",
|
|
harmful_prompts=["a"], harmless_prompts=["b"],
|
|
)
|
|
if eligible:
|
|
pipeline.concept_directions = {0: torch.ones(4)}
|
|
pipeline._harmful_acts = {0: [torch.ones(1, 4)]}
|
|
pipeline._harmless_acts = {0: [torch.ones(1, 4)]}
|
|
assert pipeline._gate_admission_host_bytes() > 0
|
|
else:
|
|
assert pipeline._concept_gate_inputs() is None
|
|
assert pipeline._gate_admission_host_bytes() == 0
|
|
pipeline._gate_mode = "off"
|
|
assert pipeline._gate_admission_host_bytes() == 0
|
|
|
|
|
|
@pytest.mark.parametrize("problem", ["missing_layer", "counts", "hidden", "device"])
|
|
def test_invalid_cache_layout_rejects_explicitly(problem):
|
|
direction, inputs = _inputs()
|
|
if problem == "missing_layer":
|
|
del inputs.controls["neutral"][0]
|
|
elif problem == "counts":
|
|
inputs.controls["neutral"][0] = [torch.zeros(1, 32)]
|
|
elif problem == "hidden":
|
|
inputs.controls["neutral"][0] = [torch.zeros(16, 31)]
|
|
else:
|
|
inputs.controls["neutral"][0] = [torch.empty(16, 32, device="meta")]
|
|
with pytest.raises(ValueError, match="cache|CPU chunks"):
|
|
estimate_gate_host_bytes(direction, inputs)
|